This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
forked from sfahl/mallodroid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmallodroid.py
executable file
·644 lines (557 loc) · 22.3 KB
/
mallodroid.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#!/usr/bin/env python3
# encoding: utf-8
"""
# This file is part of MalloDroid which is built up-on Androguard.
#
# Copyright (C) 2013, Sascha Fahl <fahl at dcsec.uni-hannover.de>
# All rights reserved.
#
# MalloDroid is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MalloDroid is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with MalloDroid. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
from androguard.decompiler.dad import decompile
from androguard.core.analysis.analysis import ClassAnalysis
import os
import base64
import argparse
from androguard.misc import AnalyzeAPK
def _get_java_code(_class, _vmx):
try:
_ms = decompile.DvClass(_class, _vmx)
_ms.process()
return _ms.get_source().encode("utf-8")
except Exception as e:
print("Error getting Java source code for: {:s}".format(_class.get_name()))
return None
def _has_signature(_method, _signatures):
_name = _method.get_name()
_return = _method.get_information().get("return", None)
_params = [_p[1] for _p in _method.get_information().get("params", [])]
_access_flags = _method.get_access_flags_string()
for _signature in _signatures:
if (
(_access_flags == _signature["access_flags"])
and (_name == _signature["name"])
and (_return == _signature["return"])
and (_params == _signature["params"])
):
return True
return False
def _class_implements_interface(_class, _interfaces):
return _class.get_interfaces() and any(
[True for i in _interfaces if i in _class.get_interfaces()]
)
def _class_extends_class(_class, _classes):
return any([True for i in _classes if i == _class.get_superclassname()])
def _get_method_instructions(_method):
_code = _method.get_code()
_instructions = []
if _code:
_bc = _code.get_bc()
for _instr in _bc.get_instructions():
_instructions.append(_instr)
return _instructions
def _returns_true(_method):
_instructions = _get_method_instructions(_method)
if len(_instructions) == 2:
_i = "->".join(
[
_instructions[0].get_output(),
_instructions[1].get_name() + "," + _instructions[1].get_output(),
]
)
_i = _i.replace(" ", "")
_v = _instructions[0].get_output().split(",")[0]
_x = "{:s},1->return,{:s}".format(_v, _v)
return _i == _x
return False
def _returns_void(_method):
_instructions = _get_method_instructions(_method)
if len(_instructions) == 1:
return _instructions[0].get_name() == "return-void"
return False
def _instantiates_allow_all_hostname_verifier(_method):
if not _method.get_class_name() == "Lorg/apache/http/conn/ssl/SSLSocketFactory;":
_instructions = _get_method_instructions(_method)
for _i in _instructions:
if _i.get_name() == "new-instance" and _i.get_output().endswith(
"Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier;"
):
return True
elif (
_i.get_name() == "sget-object"
and "Lorg/apache/http/conn/ssl/SSLSocketFactory;->ALLOW_ALL_HOSTNAME_VERIFIER"
in _i.get_output()
):
return True
return False
def _instantiates_get_insecure_socket_factory(_method):
_instructions = _get_method_instructions(_method)
for _i in _instructions:
if _i.get_name() == "invoke-static" and _i.get_output().endswith(
"Landroid/net/SSLCertificateSocketFactory;->getInsecure(I Landroid/net/SSLSessionCache;)Ljavax/net/ssl/SSLSocketFactory;"
):
return True
return False
def _get_javab64_xref(_class, _vmx):
_java_b64 = base64.b64encode(_get_java_code(_class, _vmx))
_xref = None
try:
_xref = ClassAnalysis(_class).get_xref_from()
if _xref:
_xref = [_m[0] for _m in _xref.items]
except AttributeError as e:
print("Error {} occured".format(e))
return _java_b64, _xref
def _check_trust_manager(_method, _vm, _vmx):
_check_server_trusted = {
"access_flags": "public",
"return": "void",
"name": "checkServerTrusted",
"params": ["java.security.cert.X509Certificate[]", "java.lang.String"],
}
_trustmanager_interfaces = [
"Ljavax/net/ssl/TrustManager;",
"Ljavax/net/ssl/X509TrustManager;",
]
_custom_trust_manager = []
_insecure_socket_factory = []
if _has_signature(_method, [_check_server_trusted]):
_class = _vm.get_class(_method.get_class_name())
if _class_implements_interface(_class, _trustmanager_interfaces):
_java_b64, _xref = _get_javab64_xref(_class, _vmx)
_empty = _returns_true(_method) or _returns_void(_method)
_custom_trust_manager.append(
{"class": _class, "xref": _xref, "java_b64": _java_b64, "empty": _empty}
)
if _instantiates_get_insecure_socket_factory(_method):
_class = _vm.get_class(_method.get_class_name())
_java_b64, _xref = _get_javab64_xref(_class, _vmx)
_insecure_socket_factory.append(
{"class": _class, "method": _method, "java_b64": _java_b64}
)
return _custom_trust_manager, _insecure_socket_factory
def _check_hostname_verifier(_method, _vm, _vmx):
_verify_string_sslsession = {
"access_flags": "public",
"return": "boolean",
"name": "verify",
"params": ["java.lang.String", "javax.net.ssl.SSLSession"],
}
_verify_string_x509cert = {
"access_flags": "public",
"return": "void",
"name": "verify",
"params": ["java.lang.String", "java.security.cert.X509Certificate"],
}
_verify_string_sslsocket = {
"access_flags": "public",
"return": "void",
"name": "verify",
"params": ["java.lang.String", "javax.net.ssl.SSLSocket"],
}
_verify_string_subj_alt = {
"access_flags": "public",
"return": "void",
"name": "verify",
"params": ["java.lang.String", "java.lang.String[]", "java.lang.String[]"],
}
_verifier_interfaces = [
"Ljavax/net/ssl/HostnameVerifier;",
"Lorg/apache/http/conn/ssl/X509HostnameVerifier;",
]
_verifier_classes = [
"L/org/apache/http/conn/ssl/AbstractVerifier;",
"L/org/apache/http/conn/ssl/AllowAllHostnameVerifier;",
"L/org/apache/http/conn/ssl/BrowserCompatHostnameVerifier;",
"L/org/apache/http/conn/ssl/StrictHostnameVerifier;",
]
_custom_hostname_verifier = []
_allow_all_hostname_verifier = []
if _has_signature(
_method,
[
_verify_string_sslsession,
_verify_string_x509cert,
_verify_string_sslsocket,
_verify_string_subj_alt,
],
):
_class = _vm.get_class(_method.get_class_name())
if _class_implements_interface(
_class, _verifier_interfaces
) or _class_extends_class(_class, _verifier_classes):
_java_b64, _xref = _get_javab64_xref(_class, _vmx)
_empty = _returns_true(_method) or _returns_void(_method)
_custom_hostname_verifier.append(
{"class": _class, "xref": _xref, "java_b64": _java_b64, "empty": _empty}
)
if _instantiates_allow_all_hostname_verifier(_method):
_class = _vm.get_class(_method.get_class_name())
_java_b64, _xref = _get_javab64_xref(_class, _vmx)
_allow_all_hostname_verifier.append(
{"class": _class, "method": _method, "java_b64": _java_b64}
)
return _custom_hostname_verifier, _allow_all_hostname_verifier
def _check_ssl_error(_method, _vm, _vmx):
_on_received_ssl_error = {
"access_flags": "public",
"return": "void",
"name": "onReceivedSslError",
"params": [
"android.webkit.WebView",
"android.webkit.SslErrorHandler",
"android.net.http.SslError",
],
}
_webviewclient_classes = ["Landroid/webkit/WebViewClient;"]
_custom_on_received_ssl_error = []
if _has_signature(_method, [_on_received_ssl_error]):
_class = _vm.get_class(_method.get_class_name())
if _class_extends_class(_class, _webviewclient_classes) or True:
_java_b64, _xref = _get_javab64_xref(_class, _vmx)
_empty = _returns_true(_method) or _returns_void(_method)
_custom_on_received_ssl_error.append(
{"class": _class, "xref": _xref, "java_b64": _java_b64, "empty": _empty}
)
return _custom_on_received_ssl_error
def _check_all(_vms, _vmx):
_custom_trust_manager = []
_insecure_socket_factory = []
_custom_hostname_verifier = []
_allow_all_hostname_verifier = []
_custom_on_received_ssl_error = []
for _vm in _vms:
for _method in _vm.get_methods():
_hv, _a = _check_hostname_verifier(_method, _vm, _vmx)
if len(_hv) > 0:
_custom_hostname_verifier += _hv
if len(_a) > 0:
_allow_all_hostname_verifier += _a
_tm, _i = _check_trust_manager(_method, _vm, _vmx)
if len(_tm) > 0:
_custom_trust_manager += _tm
if len(_i) > 0:
_insecure_socket_factory += _i
_ssl = _check_ssl_error(_method, _vm, _vmx)
if len(_ssl) > 0:
_custom_on_received_ssl_error += _ssl
return {
"trustmanager": _custom_trust_manager,
"insecuresocketfactory": _insecure_socket_factory,
"customhostnameverifier": _custom_hostname_verifier,
"allowallhostnameverifier": _allow_all_hostname_verifier,
"onreceivedsslerror": _custom_on_received_ssl_error,
}
def _print_result(_result, _java=True):
print("Analysis result:")
if len(_result["trustmanager"]) > 0:
if len(_result["trustmanager"]) == 1:
print("App implements custom TrustManager:")
elif len(_result["trustmanager"]) > 1:
print(
"App implements {:d} custom TrustManagers".format(
len(_result["trustmanager"])
)
)
for _tm in _result["trustmanager"]:
_class_name = _tm["class"].get_name()
print(
"\tCustom TrustManager is implemented in class {:s}".format(
_translate_class_name(_class_name)
)
)
if _tm["empty"]:
print(
"\tImplements naive certificate check. This TrustManager breaks certificate validation!"
)
for _ref in _tm["xref"]:
print(
"\t\tReferenced in method {:s}->{:s}".format(
_translate_class_name(_ref.get_class_name()), _ref.get_name()
)
)
if _java:
print("\t\tJavaSource code:")
print("{:s}".format(base64.b64decode(_tm["java_b64"])))
if len(_result["insecuresocketfactory"]) > 0:
if len(_result["insecuresocketfactory"]) == 1:
print("App instantiates insecure SSLSocketFactory:")
elif len(_result["insecuresocketfactory"]) > 1:
print(
"App instantiates {:d} insecure SSLSocketFactorys".format(
len(_result["insecuresocketfactory"])
)
)
for _is in _result["insecuresocketfactory"]:
_class_name = _translate_class_name(_is["class"].get_name())
print(
"\tInsecure SSLSocketFactory is instantiated in {:s}->{:s}".format(
_class_name, _is["method"].get_name()
)
)
if _java:
print("\t\tJavaSource code:")
print("{:s}".format(base64.b64decode(_is["java_b64"])))
if len(_result["customhostnameverifier"]) > 0:
if len(_result["customhostnameverifier"]) == 1:
print("App implements custom HostnameVerifier:")
elif len(_result["customhostnameverifier"]) > 1:
print(
"App implements {:d} custom HostnameVerifiers".format(
len(_result["customhostnameverifier"])
)
)
for _hv in _result["customhostnameverifier"]:
_class_name = _hv["class"].get_name()
print(
"\tCustom HostnameVerifiers is implemented in class {:s}".format(
_translate_class_name(_class_name)
)
)
if _hv["empty"]:
print(
"\tImplements naive hostname verification. This HostnameVerifier breaks certificate validation!"
)
# for _ref in _tm['xref']:
for _ref in _hv["xref"]:
print(
"\t\tReferenced in method {:s}->{:s}".format(
_translate_class_name(_ref.get_class_name()), _ref.get_name()
)
)
if _java:
print("\t\tJavaSource code:")
print("{:s}".format(base64.b64decode(_hv["java_b64"])))
if len(_result["allowallhostnameverifier"]) > 0:
if len(_result["allowallhostnameverifier"]) == 1:
print("App instantiates AllowAllHostnameVerifier:")
elif len(_result["allowallhostnameverifier"]) > 1:
print(
"App instantiates {:d} AllowAllHostnameVerifiers".format(
len(_result["allowallhostnameverifier"])
)
)
for _aa in _result["allowallhostnameverifier"]:
_class_name = _translate_class_name(_aa["class"].get_name())
print(
"\tAllowAllHostnameVerifier is instantiated in {:s}->{:s}".format(
_class_name, _aa["method"].get_name()
)
)
if _java:
print("\t\tJavaSource code:")
print("{:s}".format(base64.b64decode(_aa["java_b64"])))
if len(_result["onreceivedsslerror"]) > 0:
if len(_result["onreceivedsslerror"]) == 1:
print("App extends WebViewClient:")
elif len(_result["onreceivedsslerror"]) > 1:
print(
"App extends {:d} WebViewClients".format(
len(_result["onreceivedsslerror"])
)
)
for _se in _result["onreceivedsslerror"]:
_class_name = _se["class"].get_name()
print(
"\tWebViewClient is extended in class {:s}".format(
_translate_class_name(_class_name)
)
)
if _se["empty"]:
print(
"\tImplements naive ssl-error handling. This WebViewClient breaks certificate validation!"
)
for _ref in _se["xref"]:
print(
"\t\tReferenced in method {:s}->{:s}".format(
_translate_class_name(_ref.get_class_name()), _ref.get_name()
)
)
if _java:
print("\t\tJavaSource code:")
print("{:s}".format(base64.b64decode(_se["java_b64"])))
def _xml_result(_a, _result, printed=True, file=None):
from xml.etree.ElementTree import Element, SubElement, tostring
import xml.dom.minidom
_result_xml = Element("result")
_result_xml.set("package", _a.get_package())
_tms = SubElement(_result_xml, "trustmanagers")
_hvs = SubElement(_result_xml, "hostnameverifiers")
_orse = SubElement(_result_xml, "onreceivedsslerrors")
if printed:
print("\nXML output:\n")
for _tm in _result["trustmanager"]:
_class_name = _translate_class_name(_tm["class"].get_name())
_t = SubElement(_tms, "trustmanager")
_t.set("class", _class_name)
if _tm["empty"]:
_t.set("broken", "True")
else:
_t.set("broken", "Maybe")
for _r in _tm["xref"]:
_rs = SubElement(_t, "xref")
_rs.set("class", _translate_class_name(_r.get_class_name()))
_rs.set("method", _r.get_name())
if len(_result["insecuresocketfactory"]):
for _is in _result["insecuresocketfactory"]:
_class_name = _translate_class_name(_is["class"].get_name())
_i = SubElement(_tms, "insecuresslsocket")
_i.set("class", _class_name)
_i.set("method", _is["method"].get_name())
else:
_i = SubElement(_tms, "insecuresslsocket")
for _hv in _result["customhostnameverifier"]:
_class_name = _translate_class_name(_hv["class"].get_name())
_h = SubElement(_hvs, "hostnameverifier")
_h.set("class", _class_name)
if _hv["empty"]:
_h.set("broken", "True")
else:
_h.set("broken", "Maybe")
for _ref in _hv["xref"]:
_hs = SubElement(_h, "xref")
_hs.set("class", _translate_class_name(_ref.get_class_name()))
_hs.set("method", _ref.get_name())
if len(_result["allowallhostnameverifier"]):
for _aa in _result["allowallhostnameverifier"]:
_class_name = _translate_class_name(_aa["class"].get_name())
_a = SubElement(_hvs, "allowhostnames")
_a.set("class", _class_name)
_a.set("method", _aa["method"].get_name())
else:
_a = SubElement(_hvs, "allowhostnames")
for _se in _result["onreceivedsslerror"]:
_class_name = _translate_class_name(_se["class"].get_name())
_s = SubElement(_orse, "sslerror")
_s.set("class", _class_name)
if _se["empty"]:
_s.set("broken", "True")
else:
_s.set("broken", "Maybe")
for _ref in _se["xref"]:
_ss = SubElement(_s, "xref")
_ss.set("class", _translate_class_name(_ref.get_class_name()))
_ss.set("method", _ref.get_name())
_xml = xml.dom.minidom.parseString(tostring(_result_xml, method="xml"))
if file:
with open(file, "w") as out:
out.write(_xml.toprettyxml())
if printed:
print(_xml.toprettyxml())
def _translate_class_name(_class_name):
_class_name = _class_name[1:-1]
_class_name = _class_name.replace("/", ".")
return _class_name
def _file_name(_class_name, _base_dir):
_class_name = _class_name[1:-1]
_f = os.path.join(_base_dir, _class_name + ".java")
return _f
def _ensure_dir(_d):
d = os.path.dirname(_d)
if not os.path.exists(d):
os.makedirs(d)
def _store_java(_vmx, _vms, _args):
for _vm in _vms:
for _class in _vm.get_classes():
try:
_ms = decompile.DvClass(_class, _vmx)
_ms.process()
_f = _file_name(_class.get_name(), _args.dir)
_ensure_dir(_f)
with open(_f, "w") as f:
_java = str(_ms.get_source())
f.write(_java)
except Exception as e:
print(
("Could not process {:s}: {:s}".format(_class.get_name(), str(e)))
)
def _parseargs(args=None):
parser = argparse.ArgumentParser(
description="Analyse Android Apps for broken SSL certificate validation."
)
parser.add_argument(
"-f", "--file", help="APK File to check", type=str, required=True
)
parser.add_argument(
"-j",
"--java",
help="Show Java code for results for non-XML output",
action="store_true",
required=False,
)
parser.add_argument(
"-x", "--xml", help="Print XML output", action="store_true", required=False
)
parser.add_argument(
"-o", "--output", help="Output file (XML FORMAT)", type=str, required=False
)
parser.add_argument(
"-d",
"--dir",
help="Store decompiled App's Java code for further analysis in dir",
type=str,
required=False,
)
args = parser.parse_args(args)
return args
def main(args=None, stdout_suppress=False, stderr_suppress=False):
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = devnull if stdout_suppress else sys.stdout
sys.stderr = devnull if stderr_suppress else sys.stderr
results = None
try:
if args and not isinstance(args, list):
raise TypeError(f"Args are {type(args)}, which is not a list.")
_args = _parseargs(args)
_a, _vm, _vmx = AnalyzeAPK(_args.file)
print(("Analyse file: {:s}".format(_args.file)))
print(("Package name: {:s}".format(_a.get_package())))
if "android.permission.INTERNET" in _a.get_permissions():
print("App requires INTERNET permission. Continue analysis...")
_result = {
"trustmanager": [],
"hostnameverifier": [],
"onreceivedsslerror": [],
}
_result = _check_all(_vm, _vmx)
results = _result
if not _args.xml and not _args.output:
_print_result(_result, _java=_args.java)
else:
_xml_result(
_a,
_result,
printed=True if _args.xml else False,
file=_args.output if _args.output else None,
)
if _args.dir:
print("Store decompiled Java code in {:s}".format(_args.dir))
_store_java(_vmx, _vm, _args)
else:
print(
"App does not require INTERNET permission. No need to worry about SSL misuse... Abort!"
)
except:
import traceback
# printing stack trace
traceback.print_exc()
finally:
sys.stdout = old_stdout
sys.stderr = old_stderr
return results
if __name__ == "__main__":
main()