-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLibID.py
executable file
·415 lines (338 loc) · 13.8 KB
/
LibID.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python2
# @CreateTime: Jun 7, 2017 9:07 AM
# @Author: Stan Zhang
# @Contact: [email protected]
# @Last Modified By: Stan Zhang
# @Last Modified Time: Feb 12, 2019 3:32 PM
# @Description: LibID
import argparse
import datetime
import subprocess
import time
from itertools import izip, repeat
from multiprocessing import Pool
from os import path
import glob2
from datasketch import MinHashLSHEnsemble
from module import profiler
from module.analyzer import LibAnalyzer
from module.config import (DEX2JAR_PATH, LOGGER, LSH_PERM_NUM, LSH_THRESHOLD,
MODE)
LSH = None
LIB_RELATIONSHIP_GRAPHS = dict()
# Helper methods
# ----------------------------------------------
def _get_output_path(file_path, output_folder):
file_name = path.splitext(path.basename(file_path))[0]
output_path = path.join(output_folder, file_name + ".json")
return output_path
def _export_result_to_json(analyzer, output_path, start_time):
json_info = analyzer.get_matched_libs_json_info()
end_time = time.time()
json_info["time"] = end_time - start_time
profiler.write_to_json(output_path, json_info)
LOGGER.info("The result of %s is stored at %s", path.basename(output_path),
output_path)
# Loading related methods
# ----------------------------------------------
def load_LSH(lib_profiles, mode=MODE.SCALABLE, repackage=False,
processes=None):
"""Load library profiles to an LSH object.
Args:
lib_profiles (list): The list of library profiles.
mode (<enum 'MODE'>, optional): Defaults to MODE.SCALABLE. The detection mode. Either MODE.ACCURATE or MODE.SCALABLE. See the paper for more details.
repackage (bool, optional): Defaults to False. Should LibID consider classes repackaging? This should only be enabled if already know classes repackaging is applied.
processes (int, optional): Defaults to None. The number of processes to use. If processes is None then the number returned by cpu_count() is used.
"""
global LSH, LIB_RELATIONSHIP_GRAPHS
weights = (0.5, 0.5) if repackage else (0.1, 0.9)
LSH = MinHashLSHEnsemble(
threshold=LSH_THRESHOLD,
num_perm=LSH_PERM_NUM,
num_part=32,
weights=weights)
(minhash_list,
LIB_RELATIONSHIP_GRAPHS) = profiler.parallel_load_libs_profile(
lib_profiles=lib_profiles,
mode=mode,
repackage=repackage,
processes=processes)
LOGGER.info("Start indexing LSH (this could take a while) ...")
start_time = time.time()
LSH.index(minhash_list)
end_time = time.time()
LOGGER.info("LSH indexed. Duration: %fs", end_time - start_time)
# Profiling related methods
# ----------------------------------------------
def _profile_apps(apk_files,
output_folder=None,
processes=None,
overwrite=False):
if apk_files:
profiler.parallel_profiling_binaries(
apk_files,
output_folder,
"app",
processes=processes,
overwrite=overwrite)
def _profile_libs(dex_files,
jar_files,
output_folder="profiles",
processes=None,
overwrite=False):
# Convert jar file to dex file
for f in jar_files:
dex_file_path = path.join(
path.dirname(f),
path.basename(f)[:-4] + ".dex")
if not path.exists(dex_file_path):
LOGGER.info("Converting %s to %s ...", path.basename(f),
path.basename(dex_file_path))
cmd = "{} -o {} {}".format(DEX2JAR_PATH, dex_file_path, f)
try:
subprocess.check_output(cmd, shell=True)
LOGGER.info("Converted")
dex_files.append(dex_file_path)
except:
LOGGER.error("Conversion failed")
continue
if dex_files:
profiler.parallel_profiling_binaries(
dex_files,
output_folder,
"lib",
processes=processes,
overwrite=overwrite)
def profile_binaries(base_path=None,
file_paths=None,
output_folder='profiles',
processes=None,
overwrite=False):
"""Profile app/library binaries to JSON files.
Must provide either `base_path` or `file_paths`.
Args:
base_path (str, optional): Defaults to None. The folder that contains app/library binaries.
file_paths (list, optional): Defaults to None. The list of app/library binaries.
output_folder (str, optional): Defaults to 'profiles'. The folder to store profiles.
processes (int, optional): Defaults to None. The number of processes to use. If processes is None then the number returned by cpu_count() is used.
overwrite (bool, optional): Defaults to False. Should LibID overwrite the output file if it exists?
"""
if not file_paths:
if base_path:
apk_files = glob2.glob(path.join(base_path, "**/*.apk"))
dex_files = glob2.glob(path.join(base_path, "**/*.dex"))
jar_files = glob2.glob(path.join(base_path, "**/*.jar"))
else:
LOGGER.error("No valid folder or file path provided.")
else:
apk_files = [f for f in file_paths if f[-4:] == '.apk']
dex_files = [f for f in file_paths if f[-4:] == '.dex']
jar_files = [f for f in file_paths if f[-4:] == '.jar']
_profile_apps(
apk_files,
output_folder=output_folder,
processes=processes,
overwrite=overwrite)
_profile_libs(
dex_files,
jar_files,
output_folder=output_folder,
processes=processes,
overwrite=overwrite)
# Searching related methods
# ----------------------------------------------
def _search_libs_in_app(profile_n_mode_n_output_n_repackage_n_exclude):
global LSH
(app_profile, mode, output_folder, repackage,
exclude_builtin) = profile_n_mode_n_output_n_repackage_n_exclude
output_path = _get_output_path(app_profile, output_folder)
try:
start_time = time.time()
analyzer = LibAnalyzer(app_profile)
analyzer.get_libraries(
LSH,
mode=mode,
repackage=repackage,
LIB_RELATIONSHIP_GRAPHS=LIB_RELATIONSHIP_GRAPHS,
exclude_builtin=exclude_builtin)
_export_result_to_json(analyzer, output_path, start_time)
except Exception:
LOGGER.exception("%s failed", app_profile)
def search_libs_in_apps(lib_folder=None,
lib_profiles=None,
app_folder=None,
app_profiles=None,
mode=MODE.SCALABLE,
overwrite=False,
output_folder='outputs',
repackage=False,
processes=None,
exclude_builtin=True):
"""Find if specified libraries are used in specified apps. Results will be stored in the `output_folder` as JSON files.
Must provide either `lib_folder` or `lib_profiles`.
Must provide either `app_folder` or `app_profiles`.
Args:
lib_folder (str, optional): Defaults to None. The folder that contains library binaries.
lib_profiles (list, optional): Defaults to None. The list of library profiles.
app_folder (str, optional): Defaults to None. The folder that contains app binaries.
app_profiles (list, optional): Defaults to None. The list of app profiles.
mode (<enum 'MODE'>, optional): Defaults to MODE.SCALABLE. The detection mode. Either MODE.ACCURATE or MODE.SCALABLE. See the paper for more details.
overwrite (bool, optional): Defaults to False. Should LibID overwrite the output file if it exists?
output_folder (str, optional): Defaults to 'outputs'. The folder to store results.
repackage (bool, optional): Defaults to False. Should LibID consider classes repackaging? This should only be enabled if already know classes repackaging is applied.
processes (int, optional): Defaults to None. The number of processes to use. If processes is None then the number returned by cpu_count() is used.
exclude_builtin (bool, optional): Defaults to True. Should LibID exclude builtin Android libraries (e.g., Android Support V14)? Enable this option can speed up the detection process.
"""
if not app_profiles:
if app_folder:
app_profiles = glob2.glob(path.join(app_folder, "**/*.json"))
if not lib_profiles:
if lib_folder:
lib_profiles = glob2.glob(path.join(lib_folder, "**/*.json"))
if not overwrite:
original_profile_num = len(app_profiles)
app_profiles = [
fp for fp in app_profiles
if not path.exists(_get_output_path(fp, output_folder))
]
ignored_profile_num = original_profile_num - len(app_profiles)
if ignored_profile_num:
LOGGER.warning(
"Ignored %i app profiles because the output files already exist. Use -w to overwrite",
ignored_profile_num)
if app_profiles and lib_profiles:
start_time = time.time()
load_LSH(
lib_profiles, mode=mode, repackage=repackage, processes=processes)
if processes == 1:
map(
_search_libs_in_app,
izip(app_profiles, repeat(mode), repeat(output_folder),
repeat(repackage), repeat(exclude_builtin)))
else:
pool = Pool(processes=None)
pool.map(
_search_libs_in_app,
izip(app_profiles, repeat(mode), repeat(output_folder),
repeat(repackage), repeat(exclude_builtin)))
end_time = time.time()
LOGGER.info("Finished. Numer of apps: %d, date: %s, duration: %fs",
len(app_profiles),
datetime.datetime.now().ctime(), end_time - start_time)
# Command line arguments parser
# ----------------------------------------------
def parse_arguments():
parser = argparse.ArgumentParser(description='Process some integers')
subparsers = parser.add_subparsers(
help='sub-command help', dest='subparser_name')
parser_profiling = subparsers.add_parser(
'profile', help='profiling the app/library binaries')
parser_profiling.add_argument(
'-o',
metavar='FOLDER',
type=str,
default='profiles',
help='specify output folder')
parser_profiling.add_argument(
'-w',
help='overwrite the output file if it exists',
action='store_true')
parser_profiling.add_argument(
'-p',
metavar='N',
type=int,
default=None,
help='the number of processes to use [default: the number of CPUs in the system]')
parser_profiling.add_argument(
'-v', help='show debug information', action='store_true')
group = parser_profiling.add_mutually_exclusive_group(required=True)
group.add_argument(
'-f',
metavar='FILE',
type=str,
nargs='+',
help='the app/library binaries')
group.add_argument(
'-d',
metavar='FOLDER',
type=str,
help='the folder that contains app/library binaries')
parser_detection = subparsers.add_parser(
'detect', help='detect whether the library is used in the app')
parser_detection.add_argument(
'-o',
metavar='FOLDER',
type=str,
default='outputs',
help='specify output folder')
parser_detection.add_argument(
'-w',
help='overwrite the output file if it exists',
action='store_true')
parser_detection.add_argument(
'-b',
help='considering build-in Android libraries',
action='store_true')
parser_detection.add_argument(
'-p',
metavar='N',
type=int,
default=None,
help=
'the number of processes to use [default: the number of CPUs in the system]'
)
parser_detection.add_argument(
'-A',
help='run program in Lib-A mode [default: LibID-S mode]',
action='store_true')
parser_detection.add_argument(
'-r', help='consider classes repackaging', action='store_true')
parser_detection.add_argument(
'-v', help='show debug information', action='store_true')
group = parser_detection.add_mutually_exclusive_group(required=True)
group.add_argument(
'-af', metavar='FILE', type=str, nargs='+', help='the app profiles')
group.add_argument(
'-ad',
metavar='FOLDER',
type=str,
help='the folder that contains app profiles')
group = parser_detection.add_mutually_exclusive_group(required=True)
group.add_argument(
'-lf',
metavar='FILE',
type=str,
nargs='+',
help='the library profiles')
group.add_argument(
'-ld',
metavar='FOLDER',
type=str,
help='the folder that contains library profiles')
return parser.parse_args()
if __name__ == '__main__':
args = parse_arguments()
if args.v:
LOGGER.setLevel('DEBUG')
else:
LOGGER.setLevel('INFO')
LOGGER.debug("args: %s", args)
if args.subparser_name == 'profile':
profile_binaries(
base_path=args.d,
file_paths=args.f,
output_folder=args.o,
processes=args.p,
overwrite=args.w)
else:
search_libs_in_apps(
lib_folder=args.ld,
lib_profiles=args.lf,
app_folder=args.ad,
app_profiles=args.af,
mode=MODE.ACCURATE if args.A else MODE.SCALABLE,
overwrite=args.w,
output_folder=args.o,
repackage=args.r,
processes=args.p,
exclude_builtin=not args.b)