-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
368 lines (298 loc) · 11.8 KB
/
agent.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
# !/usr/bin/python3
# -*- coding: utf8 -*-
# author: qiyichen
# date: 2021/12/30
import atexit
import datetime
import json
import os.path
import re
import sys
from dataclasses import dataclass
from pathlib import Path
from pprint import pprint, pformat
import frida
from Addr2line import Addr2line
from AndroidDevice import AndroidDevice
from CmdHelper import GetCmdOutput
from utils import log_info, log_error, log_warning, log_debug
my_source = """
var libGFrame_so = [
{
"user_name": "GNS_FRAME::CGWorkStation::onVsync(unsigned int, long long, int, bool)",
"short_name": "GNS_FRAME::CGWorkStation::onVsync",
"low_name": "_ZN9GNS_FRAME13CGWorkStation7onVsyncEjxib",
// "parameters": { "in": [], "out": [], "return": '' },
"rva": 1972485
},
];
hookMethods("libGFrame.so", libGFrame_so, false);
hook_libraries["libGFrame.so"] = libGFrame_so;
"""
log_reg = "(?P<ts>.{21}) (?P<tid>\\d+) (?P<f_name>[\\w:~\\(\\*,\\s\\[\\]<>&\\)]+) (?P<tag>begin|end)(?P<tail>.*)"
begin_log_reg = "(?P<ts>.{21}) (?P<tid>\\d+) (?P<f_name>[\\w:~\\(\\*,\\s\\[\\]<>&\\)]+) (?P<tag>begin)(?P<tail>.*)"
@dataclass
class AgentConfig(object):
name: str
host: str = None
port: int = None
device_type: str = 'usb'
device_id: str = None
foremost: bool = False
spawn: bool = False
pause: bool = True
debugger: bool = False
symbol_dir: str = None
def convert_timestamp(log):
ts_reg = re.compile('^([\\d\\.]+) (.*)', re.DOTALL)
t = ts_reg.match(log)
if not t:
return log
timestamp = float(t.group(1))
msg = t.group(2)
# 精确到毫秒
d = datetime.datetime.fromtimestamp(timestamp)
line_header = "{}".format(d.strftime("%m-%d %H:%M:%S.%f"))
return "{} {}".format(line_header, msg)
def extract_tid(log):
m = re.match(begin_log_reg, log, re.DOTALL)
if not m:
return None
return int(m.group('tid'))
def add_thread_name(log, thread_name):
m = re.match(begin_log_reg, log, re.DOTALL)
if not m:
return log
ts = m.group('ts')
tid = int(m.group('tid'))
f_name = m.group('f_name')
tag = m.group('tag')
tail = m.group('tail')
msg = "{} {} {} {} [{}]{}".format(ts, tid, f_name, tag, thread_name, tail)
return msg
def get_thread_id_name_dict(pid):
threads = {}
device = AndroidDevice()
app_thread_list = device.get_thread_list(pid)
if len(app_thread_list) > 0:
for t in app_thread_list:
threads[t['TID']] = t['T_NAME']
else:
log_error('cannot retrieve the thread list of {}.'.format(pid))
return threads
class OutputHandlers(object):
logfile: Path = None
pid: int = None
symbol_dir: Path = None
addr2line: Addr2line
def __init__(self, filename: str, symbol_dir: str = None):
OutputHandlers.logfile = filename
with open(OutputHandlers.logfile, 'w+') as f:
pass
OutputHandlers.symbol_dir = symbol_dir
def device_output(self):
pass
def device_lost(self):
pass
@staticmethod
def session_on_detached(message: dict, crash):
try:
if message:
log_info("(session detach message) {}".format(message))
if crash:
log_info("(process crash report)")
log_info("\t{}".format(crash.report))
except Exception as e:
log_error("Failed to process an incoming message for a session detach signal: {}.".format(e))
raise e
@staticmethod
def script_on_message(message: dict, data):
try:
if message and 'payload' in message:
payload = message['payload']
content = None
if len(payload) > 0:
if isinstance(payload, dict):
content = json.dumps(payload)
elif isinstance(payload, str):
content = convert_timestamp(payload)
content = OutputHandlers.convert_backtrace(content)
elif isinstance(payload, list):
content = pformat(payload)
else:
log_info("Dumping unknown agent message")
pprint(payload)
if content:
log_info("(agent) {}".format(content))
with open(OutputHandlers.logfile, 'a+') as f:
f.write(content + "\n")
except Exception as e:
log_error("Failed to process an incoming message from agent: {}.".format(e))
raise e
@staticmethod
def convert_backtrace(log):
tag = log.find('Called from:')
if tag == -1:
return log
symbol_dir = OutputHandlers.symbol_dir
# " #00: 0x91899197 libhsl.so!0x3a4197"
bt_reg1 = re.compile('^( #\\d{2}: )(0x[0-9a-f]+ )(.*)(!)(0x[0-9a-f]+)')
# " #04: 0x95aa8ddd libbase_utils.so!_ZN3asl17BaseMessageLooper13onProcMessageEPNS_7MessageE+0xe8"
bt_reg2 = re.compile('^( #\\d{2}: )(0x[0-9a-f]+ )(.*)(!)([\\d\\w]+)(\\+0x[0-9a-f]+)')
result = []
lines = log.split('\n')
for l in lines:
r = bt_reg1.match(l)
if r:
so = os.path.join(symbol_dir, r.group(3))
rva = hex(int(r.group(5), 16))
o = GetCmdOutput(['arm-linux-androideabi-addr2line', '-pfCs', '-e', so, rva])
l = r.group(1) + r.group(2) + r.group(3) + r.group(4) + r.group(5) + ' => ' + o
else:
r = bt_reg2.match(l)
if r:
so = os.path.join(symbol_dir, r.group(3))
mangled_name = r.group(5)
OutputHandlers.addr2line.get_symbol_address(so, mangled_name)
o = GetCmdOutput(['arm-linux-androideabi-c++filt', mangled_name])
l = r.group(1) + r.group(2) + r.group(3) + r.group(4) + o + r.group(6)
result.append(l)
return "\n".join(result)
class Agent(object):
config: AgentConfig = None
device: frida.core.Device = None
session: frida.core.Session = None
script: frida.core.Script = None
handlers: OutputHandlers = None
pid: int = None
resumed: bool = True
agent_path: Path = None
user_scripts: list = []
def __init__(self, config: AgentConfig):
self.config = config
log_info("agent config: {}".format(self.config))
self.handlers = OutputHandlers(Path(__file__).parent / 'message.log', config.symbol_dir)
self.agent_path = Path(__file__).parent / 'agent.js'
atexit.register(self.teardown)
def set_device(self):
if self.config.device_id:
self.device = frida.get_device(self.config.device_id)
elif self.config.device_type:
for dev in frida.enumerate_devices():
if dev.type == self.config.device_type:
self.device = dev
break
else:
self.device = frida.get_local_device()
if self.device is None:
raise Exception("Unable to find device.")
# Device(id="s24.btos.cn:7482", name="SX11M", type='usb')
log_info("Device determined as: {}".format(self.device))
params = self.device.query_system_parameters()
log_info(pformat(params))
def set_target(self):
if self.config.foremost:
try:
log_info("Get the foremost application...")
app = self.device.get_frontmost_application()
if app is None:
raise Exception("No foremost application on {}.".format(self.device))
# Application(identifier="com.autonavi.amapauto", name="高德地图", pid=18510, parameters={})
log_info("The foremost application on {} is {}.".format(self.device, app))
self.pid = app.pid
except Exception as e:
log_error("Could not get the foremost application on {}: {}.".format(self.device, e))
elif self.config.spawn:
try:
log_info("Spawning `{}`...".format(self.config.name))
self.pid = self.device.spawn(self.config.name)
self.resumed = False
log_info("Spawned `{}`.".format(self.config.name))
except Exception as e:
log_error("Could not spawn application {} on {}: {}.".format(self.config.name, self.device, e))
else:
try:
self.pid = int(self.config.name)
except ValueError:
pass
if self.pid is None:
try:
self.pid = self.device.get_process(self.config.name).pid
except Exception as e:
log_error("Could not get process {} on {}: {}.".format(self.config.name, self.device, e))
OutputHandlers.pid = self.pid
log_info("process PID determined as {}.".format(self.pid))
def attach(self):
if self.pid is None:
raise Exception("A PID needs to be set before attach().")
self.session = self.device.attach(self.pid)
self.session.on('detached', self.handlers.session_on_detached)
self.script = self.session.create_script(source=self._get_agent_source())
self.script.on('message', self.handlers.script_on_message)
self.script.load()
def resume(self):
if self.resumed:
return
if self.pid is None:
raise Exception("Cannot resume without PID.")
log_info("Resuming `{}`...".format(self.config.name))
self.device.resume(self.pid)
self.resumed = True
def run(self):
self.set_device()
self.set_target()
self.attach()
if not self.config.pause:
log_info("Asked to run without pausing, so resuming in run().")
self.resume()
log_info("{}".format(self.exports().env_frida()))
def attach_script(self, source):
session = self.device.attach(self.pid)
script = session.create_script(source=source)
script.on('message', self.handlers.script_on_message)
script.load()
self.user_scripts.append(script)
def attach_script_file(self, filename):
if not filename:
filename = './frida.js'
with open(filename, 'r') as f:
source = "\n".join(f.readlines())
self.attach_script(source)
def exports(self) -> frida.core.ScriptExports:
if not self.script:
raise Exception("Needs a script created before reading exports().")
return self.script.exports
def _get_agent_source(self) -> str:
"""
Loads the frida-compiled agent from disk.
:return:
"""
with open(self.agent_path, 'r', encoding='utf-8') as f:
src = f.readlines()
return ''.join([str(x) for x in src])
def teardown(self):
log_info("Cleanup...")
try:
if self.script:
log_info("Unloading agent script...")
self.script.unload()
log_info("Unloading user scripts...")
for s in self.user_scripts:
s.unload()
except frida.InvalidOperationError as e:
log_error("Unable to run cleanups: {}.".format(e))
if __name__ == '__main__':
bt_reg = re.compile('^( #\\d{2}: )(0x[0-9a-f]+ )(.*)!(0x[0-9a-f]+)')
r = bt_reg.match(" #00: 0x91899197 libhsl.so!0x3a4197")
# c = AgentConfig(name="高德地图", spawn=False)
c = AgentConfig(name="com.autonavi.amapauto", spawn=True)
a = Agent(c)
a.run()
a.attach_script(my_source)
a.resume()
log_info("start tracing. press any key to stop.")
sys.stdin.read(1)
api = a.exports()
exports = api.android_file_ls("/data/local/tmp")
pprint(exports)
sys.stdin.read(1)