-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
159 lines (138 loc) · 4.98 KB
/
main.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
import frida
import threading
import time
import sys
import toml
from define import OS, MODE
import tsubame
import os
import re
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/memoryview")
from hexview import memory_view_mode
with open("config.toml") as f:
config = toml.loads(f.read())
def parse_int(s):
if re.match(r"^0x[0-9a-fA-F]+$", s):
return int(s, 16)
else:
return int(s)
def get_device():
mgr = frida.get_device_manager()
changed = threading.Event()
def on_changed():
changed.set()
mgr.on("changed", on_changed)
device = None
while device is None:
devices = [dev for dev in mgr.enumerate_devices() if dev.type == "usb"]
if len(devices) == 0:
print("Waiting for usb device...")
changed.wait()
else:
device = devices[0]
mgr.off("changed", on_changed)
return device
def main(package, pid=None, run_mode="medit", memoryview_address=None):
target_os = config["general"]["target_os"]
mode = config["general"]["mode"]
frida_server_ip = config["ipconfig"]["frida_server_ip"]
binary_path = config["general"]["binary_path"]
if target_os in [OS.ANDROID.value, OS.IOS.value]:
if frida_server_ip != "":
device = frida.get_device_manager().add_remote_device(frida_server_ip)
else:
device = get_device()
if pid is None:
apps = device.enumerate_applications()
target = package
for app in apps:
if target == app.identifier or target == app.name:
app_identifier = app.identifier
app_name = app.name
break
if mode == MODE.SPAWN.value:
process_id = device.spawn([app_identifier])
session = device.attach(process_id)
device.resume(process_id)
time.sleep(1)
else:
session = device.attach(app_name)
else:
session = device.attach(pid)
else:
if frida_server_ip != "":
device = frida.get_device_manager().add_remote_device(frida_server_ip)
else:
device = frida.get_remote_device()
if pid is None:
processes = device.enumerate_processes()
target = package
for process in processes:
if target == str(process.pid) or target == process.name:
_process_name = process.name
process_id = process.pid
break
if mode == MODE.SPAWN.value:
process_id = device.spawn([binary_path])
session = device.attach(process_id)
device.resume(process_id)
time.sleep(1)
else:
session = device.attach(process_id)
else:
session = device.attach(pid)
def on_message(message, data):
print(message)
with open("javascript/core.js", "r") as f:
jscode = f.read()
script = session.create_script(jscode)
script.on("message", on_message)
script.load()
frida_api = script.exports_sync
frida_api.SetConfig(config)
info = frida_api.GetInfo()
if mode == MODE.ATTACH.value:
process_id = info["pid"]
if run_mode == "medit":
tsubame.exec(process_id, config, frida_api, info)
else:
memory_view_mode(frida_api, memoryview_address)
if __name__ == "__main__":
args = sys.argv
target = config["general"]["target"]
target_os = config["general"]["target_os"]
binary_path = config["general"]["binary_path"]
run_mode = "medit"
memoryview_address = None
if "--memoryview" in args:
memoryview_address = int(args[args.index("--memoryview") + 1], 16)
run_mode = "memoryview"
if target_os in [OS.ANDROID.value, OS.IOS.value]:
if target == "":
if args[1] == "-p" or args[1] == "--pid":
pid = parse_int(args[2])
main(None, pid, run_mode, memoryview_address)
else:
main(args[1], None, run_mode, memoryview_address)
else:
if run_mode == "memoryview":
pid = parse_int(args[2])
main(None, pid, run_mode, memoryview_address)
else:
main(target, None, run_mode, memoryview_address)
else:
if target == "":
if binary_path == "":
if args[1] == "-p" or args[1] == "--pid":
pid = parse_int(args[2])
main(None, pid, run_mode, memoryview_address)
else:
main(args[1], None, run_mode, memoryview_address)
else:
main("", None, run_mode, memoryview_address)
else:
if run_mode == "memoryview":
pid = parse_int(args[2])
main(None, pid, run_mode, memoryview_address)
else:
main(target, None, run_mode, memoryview_address)