-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
179 lines (139 loc) · 5.22 KB
/
script.py
File metadata and controls
179 lines (139 loc) · 5.22 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import logging
import os
import sys
import time
from timeit import default_timer
import frida
from droidsf.droidstatx import DroidStatX
from droidsf.subprocess import Subprocess, SubprocessShell
import droidsf.config
import droidsf.utils
import droidsf.apk
import droidsf.adb
log = logging.getLogger('droidsf')
app_class_list = []
def parse_class_list(message, data):
# {'type': 'send', 'payload': '[Ljava.io.FileDescriptor;'}
# None
if message['type'] == 'send':
app_class_list.append(message['payload'])
def export_class_list(apk):
filename = apk.output_name + "-class_list.txt"
droidsf.utils.export_file(apk.output_path, filename, app_class_list)
log.info("Exported class list: %s", filename)
def on_message(message, data):
if message['type'] == 'error':
log.error(message['stack'])
elif message['type'] == 'send':
log.info(message['payload'])
else:
log.warning(message)
on_message_handlers = {
"class_list.js": parse_class_list,
}
on_resume_handlers = {
"class_list.js": export_class_list,
}
if __name__ == '__main__':
print(droidsf.utils.HEADER)
args = droidsf.utils.get_args()
try:
droidsf.utils.setup_workspace(args)
droidsf.utils.setup_logging(args, log)
except Exception as e:
log.exception("Unable to setup workspace environment: %s", e)
sys.exit(1)
if not os.path.isfile(args.apk_file):
log.critical("Unable to open APK: %s", args.apk_file)
sys.exit(1)
droidsf.config.init(args)
apk = droidsf.apk.APK(args)
if not apk.process():
log.critical("Failed to process APK: %s", args.apk_file)
sys.exit(1)
# DroidSF framework initialized -------------------------------------------
app_package = apk.apk.get_package()
if not args.no_static_analysis:
droidstatx = DroidStatX(args, apk)
log.info("Analysed %s with DroidStat-X.", app_package)
if args.no_dynamic_analysis:
log.info("Skipped Frida dynamic analysis.")
sys.exit(0)
adb = droidsf.adb.ADB(args)
adb.select_device()
adb.launch_adb_server()
adb.install_apk(app_package)
adb.launch_frida_server()
try:
found = False
for device in frida.enumerate_devices():
if device.id == adb.device_id:
found = True
log.debug("Frida found device: %s", device)
break
if not found:
log.error("Frida could not find device: %s", adb.device_id)
device = frida.get_device(id=adb.device_id)
# device = frida.get_usb_device(timeout=5)
found = False
for app in device.enumerate_applications():
if app.identifier == app_package:
found = True
log.debug("Frida found application: %s", app)
break
if not found:
log.error("Frida could not find application: %s", app_package)
pid = device.spawn([app_package])
log.info("Frida spawned application: %s (PID: %s).", app_package, pid)
session = device.attach(pid)
log.info("Frida attached to %s (PID: %s).", app_package, pid)
script_header = os.path.join(args.cwd, "frida-scripts", "_header.js")
code = droidsf.utils.load_file(script_header)
script_file = os.path.join(args.cwd, "frida-scripts", args.script)
code += droidsf.utils.load_file(script_file)
code = code.replace("%app%", app_package)
code = code.replace("%script%", args.script)
log.info("Loaded script: %s.", args.script)
script = session.create_script(code)
if args.script in on_message_handlers:
script.on('message', on_message_handlers[args.script])
else:
script.on('message', on_message)
script.load()
time.sleep(1)
device.resume(pid)
time.sleep(1) # Without it Java.perform silently fails
# Prevent the python script from terminating
# sys.stdin.read()
try:
timeout = 0
if args.instrumentation_timeout > 0:
timeout = default_timer() + args.instrumentation_timeout
log.info("Instrumenting for %d seconds.", args.instrumentation_timeout)
while True:
if timeout > 0:
if timeout > default_timer():
time.sleep(0.5)
else:
break
else:
keyword = input("Type 'x' to terminate instrumentation.\n")
if keyword.lower() == "x":
break
except KeyboardInterrupt:
log.warning("User forced instrumentation to stop.")
if args.script in on_resume_handlers:
on_resume_handlers[args.script](apk)
# api = script.exports
# print("api.hello() =>", api.hello())
# api.fail_please()
device.kill(pid)
log.info("Killed application: %s (PID: %s).", app_package, pid)
session.detach()
log.debug("Detached from application.")
except Exception as e:
log.exception("Frida failed to execute: %s", e)
adb.kill_frida_server()
adb.kill_adb_server()