forked from androguard/androguard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.py
executable file
·413 lines (355 loc) · 11 KB
/
cli.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Androguard is a full Python tool to play with Android files."""
# core modules
import sys, json
from loguru import logger
# 3rd party modules
import click
import androguard.core.apk
# local modules
import androguard
from androguard.core.androconf import show_logging
from main import (androarsc_main,
androaxml_main,
export_apps_to_format,
androsign_main,
androlyze_main,
androgui_main,
androdis_main
)
#logger.add(sys.stderr, format="{time} {level} {message}", filter="cli", level="INFO")
@click.group(help=__doc__)
@click.version_option(version=androguard.__version__)
@click.option("--verbose", "--debug", 'verbosity', flag_value='verbose', help="Print more")
@click.option("--quiet", 'verbosity', flag_value='quiet', help="Print less (only warnings and above)")
@click.option("--silent", 'verbosity', flag_value='silent', help="Print no log messages")
def entry_point(verbosity):
pass
@entry_point.command()
@click.option(
'--input', '-i', 'input_',
type=click.Path(exists=True, file_okay=True, dir_okay=False),
help='AndroidManifest.xml or APK to parse (legacy option)',
)
@click.option(
'--output', '-o',
help='filename to save the decoded AndroidManifest.xml to, default stdout',
)
@click.option("--resource", "-r",
help="Resource (any binary XML file) inside the APK to parse instead of AndroidManifest.xml"
)
@click.argument(
'file_',
required=False,
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
def axml(input_, output, file_, resource):
"""
Parse the AndroidManifest.xml.
Parsing is either direct or from a given APK and prints in XML format or
saves to file.
This tool can also be used to process any AXML encoded file, for example
from the layout directory.
Example:
\b
$ androguard axml AndroidManifest.xml
"""
if file_ is not None and input_ is not None:
print("Can not give --input and positional argument! "
"Please use only one of them!")
sys.exit(1)
if file_ is None and input_ is None:
print("Give one file to decode!")
sys.exit(1)
if file_ is not None:
androaxml_main(file_, output, resource)
elif input_ is not None:
androaxml_main(input_, output, resource)
@entry_point.command()
@click.option(
'--input', '-i', 'input_',
type=click.Path(exists=True),
help='resources.arsc or APK to parse (legacy option)',
)
@click.argument(
'file_',
required=False,
)
@click.option(
'--output', '-o',
# required=True, # not required due to --list-types
help='filename to save the decoded resources to',
)
@click.option(
'--package', '-p',
help='Show only resources for the given package name '
'(default: the first package name found)',
)
@click.option(
'--locale', '-l',
help='Show only resources for the given locale (default: \'\\x00\\x00\')',
)
@click.option(
'--type', '-t', 'type_',
help='Show only resources of the given type (default: public)',
)
@click.option(
'--id', 'id_',
help="Resolve the given ID for the given locale and package. Provide the hex ID!"
)
@click.option(
'--list-packages', is_flag=True,
default=False,
help='List all package names and exit',
)
@click.option(
'--list-locales', is_flag=True,
default=False,
help='List all package names and exit',
)
@click.option(
'--list-types', is_flag=True,
default=False,
help='List all types and exit',
)
def arsc(input_,
file_,
output,
package,
locale,
type_,
id_,
list_packages,
list_locales,
list_types):
"""
Decode resources.arsc either directly from a given file or from an APK.
Example:
\b
$ androguard arsc app.apk
"""
from androguard.core import androconf
from androguard.core import axml, apk
if file_ and input_:
logger.info("Can not give --input and positional argument! Please use only one of them!")
sys.exit(1)
if not input_ and not file_:
logger.info("Give one file to decode!")
sys.exit(1)
if input_:
fname = input_
else:
fname = file_
ret_type = androconf.is_android(fname)
if ret_type == "APK":
a = apk.APK(fname)
arscobj = a.get_android_resources()
if not arscobj:
logger.error("The APK does not contain a resources file!")
sys.exit(0)
elif ret_type == "ARSC":
with open(fname, 'rb') as fp:
arscobj = axml.ARSCParser(fp.read())
if not arscobj:
logger.error("The resources file seems to be invalid!")
sys.exit(1)
else:
logger.error("Unknown file type!")
sys.exit(1)
if id_:
# Strip the @, if any
if id_[0] == "@":
id_ = id_[1:]
try:
i_id = int(id_, 16)
except ValueError:
print("ID '{}' could not be parsed! have you supplied the correct hex ID?".format(id_))
sys.exit(1)
name = arscobj.get_resource_xml_name(i_id)
if not name:
print("Specified resource was not found!")
sys.exit(1)
print("@{:08x} resolves to '{}'".format(i_id, name))
print()
# All the information is in the config.
# we simply need to get the actual value of the entry
for config, entry in arscobj.get_resolved_res_configs(i_id):
print("{} = '{}'".format(config.get_qualifier() if not config.is_default() else "<default>", entry))
sys.exit(0)
if list_packages:
print("\n".join(arscobj.get_packages_names()))
sys.exit(0)
if list_locales:
for p in arscobj.get_packages_names():
print("In Package:", p)
print("\n".join(map(lambda x: " \\x00\\x00"
if x == "\x00\x00"
else " {}".format(x),
sorted(arscobj.get_locales(p)))))
sys.exit(0)
if list_types:
for p in arscobj.get_packages_names():
print("In Package:", p)
for locale in sorted(arscobj.get_locales(p)):
print(" In Locale: {}".format("\\x00\\x00"
if locale == "\x00\x00" else locale))
print("\n".join(map(" {}".format,
sorted(arscobj.get_types(p, locale)))))
sys.exit(0)
androarsc_main(arscobj,
outp=output,
package=package,
typ=type_,
locale=locale)
@entry_point.command()
@click.option(
'--input', '-i', 'input_',
type=click.Path(exists=True, dir_okay=False, file_okay=True),
help='APK to parse (legacy option)',
)
@click.argument(
'file_',
type=click.Path(exists=True, dir_okay=False, file_okay=True),
required=False,
)
@click.option(
'--output', '-o',
required=True,
help='output directory. If the output folder already exsist, '
'it will be overwritten!',
)
@click.option(
'--format', '-f', 'format_',
help='Additionally write control flow graphs for each method, specify '
'the format for example png, jpg, raw (write dot file), ...',
)
@click.option(
'--jar', '-j',
is_flag=True,
default=False,
help='Use DEX2JAR to create a JAR file',
)
@click.option(
'--limit', '-l',
help='Limit to certain methods only by regex (default: \'.*\')',
)
@click.option(
'--decompiler', '-d',
help='Use a different decompiler (default: DAD)',
)
def decompile(input_, file_, output, format_, jar, limit, decompiler):
"""
Decompile an APK and create Control Flow Graphs.
Example:
\b
$ androguard resources.arsc
"""
from androguard import session
if file_ and input_:
print("Can not give --input and positional argument! "
"Please use only one of them!", file=sys.stderr)
sys.exit(1)
if not input_ and not file_:
print("Give one file to decode!", file=sys.stderr)
sys.exit(1)
if input_:
fname = input_
else:
fname = file_
s = session.Session()
with open(fname, "rb") as fd:
s.add(fname, fd.read())
export_apps_to_format(fname, s, output, limit,
jar, decompiler, format_)
@entry_point.command()
@click.option(
'--hash', 'hash_',
type=click.Choice(['md5', 'sha1', 'sha256', 'sha512']),
default='sha1', show_default=True,
help='Fingerprint Hash algorithm',
)
@click.option(
'--all', '-a', 'print_all_hashes',
is_flag=True,
default=False, show_default=True,
help='Print all supported hashes',
)
@click.option(
'--show', '-s',
is_flag=True,
default=False, show_default=True,
help='Additionally of printing the fingerprints, show more '
'certificate information',
)
@click.argument(
'apk',
nargs=-1,
type=click.Path(exists=True, dir_okay=False, file_okay=True),
)
def sign(hash_, print_all_hashes, show, apk):
"""Return the fingerprint(s) of all certificates inside an APK."""
androsign_main(apk, hash_, print_all_hashes, show)
@entry_point.command()
@click.argument(
'apks',
nargs=-1,
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
def apkid(apks):
"""Return the packageName/versionCode/versionName per APK as JSON."""
from androguard.core.apk import get_apkid
logger.debug("APKID")
results = dict()
for apk in apks:
results[apk] = get_apkid(apk)
print(json.dumps(results, indent=2))
@entry_point.command()
@click.option(
'--input_file', '-i',
help="Specify the inital file to load in the GUI",
type=click.Path(exists=True, dir_okay=False, file_okay=True),
)
@click.option(
'--input_plugin', '-p',
help="Additional Plugin (currently unused)",
type=click.Path(exists=True),
)
def gui(input_file, input_plugin):
"""Androguard GUI"""
androgui_main(input_file, input_plugin)
@entry_point.command()
@click.option(
'--session',
help='Previously saved session to load instead of a file',
type=click.Path(exists=True),
)
@click.argument(
'apk',
default=None,
required=False,
type=click.Path(exists=True, dir_okay=False, file_okay=True),
)
def analyze(session, apk):
"""Open a IPython Shell and start reverse engineering."""
androlyze_main(session, apk)
@entry_point.command()
@click.option("-o", "--offset",
default=0,
type=int,
help="Offset to start dissassembly inside the file")
@click.option("-s", "--size",
default=0,
type=int,
help="Number of bytes from offset to disassemble, 0 for whole file")
@click.argument(
"DEX",
type=click.Path(exists=True, dir_okay=False, file_okay=True),
)
def disassemble(offset, size, dex):
"""
Disassemble Dalvik Code with size SIZE starting from an offset
"""
androdis_main(offset, size, dex)
if __name__ == '__main__':
entry_point()