-
Notifications
You must be signed in to change notification settings - Fork 1
/
HookScriptGenerator.py
378 lines (331 loc) · 12.5 KB
/
HookScriptGenerator.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
# !/usr/bin/python3
# -*- coding: utf8 -*-
# author: qiyichen
# date: 2021/12/30
import os
import click
from Hooker import LibrarySymbol, SymbolLoader
from utils import log_info, log_warning, log_error
libjs = '''
/**
* author: moyichen
* https://www.runoob.com/js/js-tutorial.html
* https://frida.re/docs/javascript-api/
*
* onEnter: function(args)
* onLeave: function(retval)
* @this {object} - Object allowing you to store state for use in onLeave.
* @param {array} args - Function arguments represented as an array of NativePointer objects.
* @param {NativePointer} retval - Return value represented as a NativePointer object.
*
* 通过指针生成object
var msg = {
what: args[1].add(0x00).readS32(),
result: args[1].add(0x04).readS32(),
arg1: args[1].add(0x08).readS32(),
arg2: args[1].add(0x0c).readS32(),
arg3: args[1].add(0x10).readS32(),
arg4: args[1].add(0x14).readS32(),
ptr: args[1].add(0x18).readPointer(),
callback: args[1].add(0x1c).readPointer(),
target: args[1].add(0x20).readPointer(),
when: args[1].add(0x24).readPointer()};
* 读取字符串 setTextInner(CGString const&)
var pcData = args[1].add(4).readPointer();
var = pcData.readUtf8String();
var = pcData.readUtf16String();
* CGView控件id,因为控件是多重继承,args[0]需指向CGView,才能使用下面代码获取控件id
var id = args[0].add(288).readS32();
* json dump对象:JSON.stringify(o)
*/
// ==============================================================================
String.prototype.times = function(n) {
if (n < 0) return "";
return (new Array(n+1)).join(this);
};
function enumerateModules() {
var modules = Process.enumerateModules();
// console.log(JSON.stringify(modules));
send(modules);
}
//enumerateModules();
var hook_libraries = [];
function hook_dlopen(_dlopen_) {
var f = Module.findExportByName(null, _dlopen_);
if (f) {
console.log("dlopen: " + _dlopen_);
Interceptor.attach(f, {
onEnter: function(args) {
var pathptr = args[0];
if (pathptr != undefined && pathptr != null) {
var fn = ptr(pathptr).readCString();
var idx = fn.lastIndexOf("/");
if (idx >= 0) {
fn = fn.slice(idx+1);
}
this.params = fn;
}
},
onLeave: function(retval) {
console.log("Library " + this.params + " has been loaded at " + Module.getBaseAddress(this.params) + ".");
if (hook_libraries[this.params]) {
hookMethods(this.params, hook_libraries[this.params]['functions'], hook_libraries[this.params]['backtrace']);
}
}
});
}
}
hook_dlopen("dlopen");
//hook_dlopen("android_dlopen_ext");
// 获取时间戳,返回值秒为单位,精确到小数点后6位:123456789.123456s
// js只能精确到毫秒:var timestamp = new Date().getTime();
var clock_gettime = new NativeFunction(Module.findExportByName(null, "clock_gettime"), "int32", ['int32', 'pointer']);
function getTickCountUS() {
var timespec = Memory.alloc(8);
var ret = clock_gettime(0, timespec);
var sec = timespec.readS32();
var nsec = timespec.add(4).readS32();
var ts = sec + nsec/1000000000;
return ts.toFixed(6);
}
function getCurrentThreadName() {
var tn = '';
if (Java.available) {
Java.perform(() => {
const Thread = Java.use('java.lang.Thread');
tn = `[${Thread.currentThread().getName()}]`;
});
}
return tn;
}
function getMsgHeader(threadId) {
var ts = getTickCountUS();
var aa = ts + " " + threadId + " ";
return aa;
}
function findFunction(moduleName, functionName, offset) {
console.log("findFunction: " + functionName + " : " + moduleName + " at " + offset);
var f = Module.findExportByName(moduleName, functionName);
if (f == null) {
var rva = new NativePointer(offset);
if (!rva.equals(0)) {
var base = Module.findBaseAddress(moduleName);
if (base != null)
{
f = rva.add(base);
// console.log("hook: " + functionName + " " + "-".times(60-functionName.length) + " : " + moduleName + " at " + f + " <= " + base + " + " + rva);
}
}
}
if (f==null) {
console.log("Cann't find function address with: " + functionName + " : " + moduleName + " : " + offset);
}
return f;
}
function hex2float(v) {
var buf = new ArrayBuffer(4);
var i = new Int32Array(buf);
i[0] = v.toInt32();
var f = new Float32Array(buf);
return f[0];
}
function dumpArg(arg_type, arg_value) {
switch (arg_type) {
case 'this':
case 'pointer':
return arg_value;
case 'uint32_t':
return arg_value.toInt32();
case '*uint32_t':
return Memory.readU32(arg_value);
case 'int32_t':
return arg_value.toInt32();
case '*int32_t':
return Memory.readS32(arg_value);
case 'bool':
return Boolean(arg_value.toInt32());
case 'utf-8':
return Memory.readUtf8String(arg_value);
case 'utf-16':
return Memory.readUtf16String(arg_value);
case 'float':
return hex2float(arg_value);
case 'CGString':
var pcData = arg_value.add(4).readPointer();
return Memory.readUtf16String(pcData);
default:
return 'NA';
}
}
function dumpArgs(arg_types, arg_values) {
var args = [];
for (var i = 0; i < arg_types.length; i++) {
args.push(dumpArg(arg_types[i], arg_values[i]));
}
return args.join(", ");
}
function getBacktrace(context) {
var result = [];
var backtraces = Thread.backtrace(context, Backtracer.ACCURATE).map(DebugSymbol.fromAddress);
for (var i = 0; i < backtraces.length; i++) {
var index = "" + i;
// 占2位
index = "0".repeat(2-index.length) + index;
result[i] = " #" + index + ": " + backtraces[i];
}
return result.join('\\n');
}
function hookMethod(so_name, user_name, low_name, rva, arg_types, backtrace) {
var f = findFunction(so_name, low_name, rva);
if (f) {
console.log("hook: " + user_name + " " + "-".times(60-user_name.length) + " : " + so_name + " at " + f);
Interceptor.attach(f, {
onEnter: function(args) {
var msgHdr = getMsgHeader(this.threadId);
// backtrace
var strBacktrace = "";
if (backtrace) {
strBacktrace = "\\n Called from:\\n" + getBacktrace(this.context);
}
// input parameters
var strInArgs = "";
if (arg_types.hasOwnProperty("in")) {
strInArgs = "\\n param[in]: " + dumpArgs(arg_types["in"], args);
if (arg_types.hasOwnProperty("out")) {
this.args = [];
for (var i = 0; i < arg_types['out'].length; i++) {
this.args[i] = args[i];
}
}
}
send(msgHdr + user_name + " begin " + getCurrentThreadName() + strInArgs + strBacktrace);
},
onLeave: function(retval) {
var msgHdr = getMsgHeader(this.threadId);
// output paramters
var strOutArgs = "";
if (arg_types.hasOwnProperty("out")) {
strOutArgs = "\\n param[out]: " + dumpArgs(arg_types["out"], this.args);
}
// return value
var strReturn = "";
if (arg_types.hasOwnProperty("return")) {
strReturn = "\\n return: " + dumpArg(arg_types["return"], retval);
}
send(msgHdr + user_name + " end" + strOutArgs + strReturn);
}
});
}
}
function hookMethods(so_name, symbols, backtrace) {
var base = Module.findBaseAddress(so_name);
if (base == null) {
console.log("Library " + so_name + " hasn't been loaded. All the methods of it will be hooked when it's loaded.");
return;
}
for (var idx in symbols) {
var sym = symbols[idx];
var arg_types = {};
if (sym.hasOwnProperty("parameters")) {
arg_types = sym["parameters"];
}
hookMethod(so_name, sym["short_name"], sym["low_name"], sym["rva"], arg_types, backtrace);
}
}
/*
如果打印中间的参数,需要有空的占位;支持的类型参见dumpArg方法;
"parameters":
{
"in": ["this", "int32_t", "*uint32_t", ...],
"out": ['', '', '', '*uint32_t'],
"return": 'bool'
}
*/
// ==============================================================================
'''
def get_so_files(syms, symbol_dir):
# 指定so的情况下,只加载指定的so
so_files = []
for sym in syms:
sym_and_so = sym.split('#')
if len(sym_and_so) > 1:
if sym_and_so[1] not in so_files:
so_files.append(sym_and_so[1])
else:
# 有一个未指定就需要遍历所有的so
so_files = os.listdir(symbol_dir)
break
return so_files
def get_full_symbols_dict(sym_list, symbol_dir, type_filters=[]):
'''
:param sym_list: ["abc#libXXX.so", "efg", ...]
:param type_filters:
:return: {'so_name': xxx, [{'user_name': xxx, 'low_name': xxx, 'rva': xxx}, {}, ...]}
'''
result = LibrarySymbol()
unfounded_syms = []
symLoader = SymbolLoader(symbol_dir)
so_files = get_so_files(sym_list, symbol_dir)
symLoader.load(so_files)
for sym in sym_list:
sym_and_so = sym.split('#')
filter_sym = sym_and_so[0]
filter_so = None
if len(sym_and_so) > 1:
filter_so = sym_and_so[1]
matched_syms = symLoader.findSymbol(filter_so, filter_sym, type_filters)
if len(matched_syms) == 0:
unfounded_syms.append(sym)
else:
result.addSymbols(matched_syms)
if result.hasSymbols():
log_info('the following symbols will be hooked.')
s = result.toJson()
log_info(s)
if len(unfounded_syms) > 0:
if len(sym_list) == len(unfounded_syms):
log_error('the following symbols have not been founded in any dynamic libraries:')
log_error("\n".join(unfounded_syms))
result = None
else:
log_warning('the following symbols have not been founded in any dynamic libraries:')
log_warning("\n".join(unfounded_syms))
return result, unfounded_syms
def genFridaAgentScript(sym_list, symbol_dir, backtrace=False) -> str:
log_info("generate hook config: {} with option {}.".format(sym_list, backtrace))
config, unfounded_syms = get_full_symbols_dict(sym_list, symbol_dir)
syms = config.library
# 将bool型backtrace转换成js bool型
backtrace = "true" if backtrace else "false"
# 先加上基础函数
hookscript = libjs
# 对每一个so循环
for so_name in syms:
so_function_scripts = []
# 对当前so内的符号循环
for low_name in syms[so_name]:
size = syms[so_name][low_name]["size"]
user_name = syms[so_name][low_name]["user_name"]
short_name = syms[so_name][low_name]["short_name"]
rva = syms[so_name][low_name]["rva"] + 1
if size <= 4:
log_warning('{}:{} is too small to hook well. Skip it.'.format(user_name, so_name))
continue
f_script = [
''' "user_name": "{}"'''.format(user_name),
''' "short_name": "{}"'''.format(short_name),
''' "low_name": "{}"'''.format(low_name),
''' // "parameters": { "in": [], "out": [], "return": '' }''',
''' "rva": "{}"'''.format(rva)
]
so_function_scripts.append("\n".join(['{', ',\n'.join(f_script), '}']))
c_so_name = so_name.replace(".", "_")
so_script = [
'''var {} = ['''.format(c_so_name),
''',\n'''.join(so_function_scripts),
'''];''',
'''hookMethods("{}", {}, {});'''.format(so_name, c_so_name, backtrace),
'''hook_libraries["{}"] = {{ 'functions': {}, 'backtrace': {} }}; '''.format(so_name, c_so_name, backtrace)
]
hookscript += "\n".join(so_script)
return hookscript