-
Notifications
You must be signed in to change notification settings - Fork 55
/
frida_fart_hook.js
293 lines (258 loc) · 12.1 KB
/
frida_fart_hook.js
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
//typed by hanbingle,just for fun!!
//email:edunwu@gmail.com
// 本版本可在Frida-16.1.0 + Android12环境下使用
/*使用说明
首先拷贝fart.so和fart64.so到/data/app目录下,并使用chmod 777 设置好权限,然后就可以使用了。
该frida版fart是使用hook的方式实现的函数粒度的脱壳,仅仅是对类中的所有函数进行了加载,但依然可以解决绝大多数的抽取保护
需要以spawn方式启动app,等待app进入Activity界面后,执行fart()函数即可。如app包名为com.example.test,则
frida -U -f com.example.test -l frida_fart_hook.js --no-pause,然后等待app进入主界面,执行fart()
高级用法:如果发现某个类中的函数的CodeItem没有dump下来,可以调用dump(classname),传入要处理的类名,完成对该类下的所有函数体的dump,dump下来的函数体会追加到bin文件当中。
*/
var ishook_libart = false;
var addrLoadMethod = null;
var addrGetCodeItemLength = null;
var funcGetCodeItemLength = null;
var addrBase64_encode = null;
var funcBase64_encode = null;
var addrFreeptr = null;
var funcFreeptr = null;
// var savepath = "/sdcard";
var savepath = "/data/data/com.kanxue.test/hook"
var dex_maps = {};
var artmethod_maps = {};
function DexFile(start, size) {
this.start = start;
this.size = size;
}
function ArtMethod(dexfile, artmethodptr) {
this.dexfile = dexfile;
this.artmethodptr = artmethodptr;
}
function hookart() {
if (ishook_libart === true) {
return;
}
var module_libext = null;
if (Process.arch === "arm64") {
module_libext = Module.load("/data/app/fart64.so");
} else if (Process.arch === "arm") {
module_libext = Module.load("/data/app/fart.so");
}
if (module_libext != null) {
addrGetCodeItemLength = module_libext.findExportByName("GetCodeItemLength");
funcGetCodeItemLength = new NativeFunction(addrGetCodeItemLength, "int", ["pointer"]);
addrBase64_encode = module_libext.findExportByName("Base64_encode");
funcBase64_encode = new NativeFunction(addrBase64_encode, "pointer", ["pointer", "int", "pointer"]);
addrFreeptr = module_libext.findExportByName("Freeptr");
funcFreeptr = new NativeFunction(addrFreeptr, "void", ["pointer"]);
}
var symbols = Module.enumerateSymbolsSync("libart.so");
for (var i = 0; i < symbols.length; i++) {
var symbol = symbols[i];
if(symbol.name.indexOf("LoadMethod") >= 0 && symbol.name.indexOf("ArtMethod") >= 0){
console.log(symbol.name);
}
//Android8 _ZN3art11ClassLinker10LoadMethodERKNS_7DexFileERKNS_21ClassDataItemIteratorENS_6HandleINS_6mirror5ClassEEEPNS_9ArtMethodE
// if (symbol.name.indexOf("ClassLinker") >= 0
// && symbol.name.indexOf("LoadMethod") >= 0
// && symbol.name.indexOf("DexFile") >= 0
// && symbol.name.indexOf("ClassDataItemIterator") >= 0
// && symbol.name.indexOf("ArtMethod") >= 0) {
// addrLoadMethod = symbol.address;
// break;
// }
// Android12 _ZN3art11ClassLinker10LoadMethodERKNS_7DexFileERKNS_13ClassAccessor6MethodENS_6HandleINS_6mirror5ClassEEEPNS_9ArtMethodE
if (symbol.name.indexOf("ClassLinker") >= 0
&& symbol.name.indexOf("LoadMethod") >= 0
&& symbol.name.indexOf("DexFile") >= 0
&& symbol.name.indexOf("ArtMethod") >= 0) {
addrLoadMethod = symbol.address;
break;
}
}
if (addrLoadMethod != null) {
Interceptor.attach(addrLoadMethod, {
onEnter: function (args) {
this.dexfileptr = args[1];
this.artmethodptr = args[4];
},
onLeave: function (retval) {
var dexfilebegin = null;
var dexfilesize = null;
if (this.dexfileptr != null) {
dexfilebegin = Memory.readPointer(ptr(this.dexfileptr).add(Process.pointerSize * 1));
dexfilesize = Memory.readU32(ptr(this.dexfileptr).add(Process.pointerSize * 2));
var dexfile_path = savepath + "/" + dexfilesize + "_loadMethod.dex";
var dexfile_handle = null;
try {
dexfile_handle = new File(dexfile_path, "r");
if (dexfile_handle && dexfile_handle != null) {
dexfile_handle.close()
}
} catch (e) {
dexfile_handle = new File(dexfile_path, "a+");
if (dexfile_handle && dexfile_handle != null) {
var dex_buffer = ptr(dexfilebegin).readByteArray(dexfilesize);
dexfile_handle.write(dex_buffer);
dexfile_handle.flush();
dexfile_handle.close();
console.log("[dumpdex]:", dexfile_path);
}
}
}
var dexfileobj = new DexFile(dexfilebegin, dexfilesize);
if (dex_maps[dexfilebegin] == undefined) {
dex_maps[dexfilebegin] = dexfilesize;
console.log("got a dex:", dexfilebegin, dexfilesize);
}
if (this.artmethodptr != null) {
var artmethodobj = new ArtMethod(dexfileobj, this.artmethodptr);
if (artmethod_maps[this.artmethodptr] == undefined) {
artmethod_maps[this.artmethodptr] = artmethodobj;
}
}
}
});
}
ishook_libart = true;
}
function dumpcodeitem(artmethodobj) {
if (artmethodobj != null) {
var dexfileobj = artmethodobj.dexfile;
var dexfilebegin = dexfileobj.start;
var dexfilesize = dexfileobj.size;
var dexfile_path = savepath + "/" + dexfilesize + "_" + Process.getCurrentThreadId() + ".dex";
var dexfile_handle = null;
try {
dexfile_handle = new File(dexfile_path, "r");
if (dexfile_handle && dexfile_handle != null) {
dexfile_handle.close()
}
} catch (e) {
dexfile_handle = new File(dexfile_path, "a+");
if (dexfile_handle && dexfile_handle != null) {
var dex_buffer = ptr(dexfilebegin).readByteArray(dexfilesize);
dexfile_handle.write(dex_buffer);
dexfile_handle.flush();
dexfile_handle.close();
console.log("[dumpdex]:", dexfile_path);
}
}
var artmethodptr = artmethodobj.artmethodptr;
var dex_code_item_offset_ = Memory.readU32(ptr(artmethodptr).add(8));
var dex_method_index_ = Memory.readU32(ptr(artmethodptr).add(12));
if (dex_code_item_offset_ != null && dex_code_item_offset_ > 0) {
var dir = savepath;
var file_path = dir + "/" + dexfilesize + "_" + Process.getCurrentThreadId() + ".bin";
var file_handle = new File(file_path, "a+");
if (file_handle && file_handle != null) {
var codeitemstartaddr = ptr(dexfilebegin).add(dex_code_item_offset_);
var codeitemlength = funcGetCodeItemLength(ptr(codeitemstartaddr));
if (codeitemlength != null & codeitemlength > 0) {
Memory.protect(ptr(codeitemstartaddr), codeitemlength, 'rwx');
var base64lengthptr = Memory.alloc(8);
var arr = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
Memory.writeByteArray(base64lengthptr, arr);
var base64ptr = funcBase64_encode(ptr(codeitemstartaddr), codeitemlength, ptr(base64lengthptr));
var b64content = ptr(base64ptr).readCString(base64lengthptr.readInt());
funcFreeptr(ptr(base64ptr));
var content = "{name:ooxx,method_idx:" + dex_method_index_ + ",offset:" + dex_code_item_offset_ + ",code_item_len:" + codeitemlength + ",ins:" + b64content + "};";
file_handle.write(content);
file_handle.flush();
file_handle.close();
}
} else {
console.log("openfile failed,filepath:", file_path);
}
}
}
}
function dumpall() {
console.log("start dump all CodeItem.......");
for (var artmethodptr in artmethod_maps) {
var artmethodobj = artmethod_maps[artmethodptr];
try {
dumpcodeitem(artmethodobj);
} catch (e) {
console.log("error", e);
}
}
console.log("end dump all CodeItem.......");
}
function dealwithClassLoader(classloaderobj) {
if (classloaderobj.$className.toString().indexOf("java.lang.BootClassLoader") >= 0) {
return
}
if (Java.available) {
Java.perform(function () {
try {
var dexfileclass = Java.use("dalvik.system.DexFile");
var BaseDexClassLoaderclass = Java.use("dalvik.system.BaseDexClassLoader");
var DexPathListclass = Java.use("dalvik.system.DexPathList");
var Elementclass = Java.use("dalvik.system.DexPathList$Element");
var basedexclassloaderobj = Java.cast(classloaderobj, BaseDexClassLoaderclass);
var pathlistobj = Java.cast(basedexclassloaderobj.pathList.value, DexPathListclass);
for(var i = 0; i < pathlistobj.dexElements.value.length; i++){
var dexElementobj = Java.cast(pathlistobj.dexElements.value[i], Elementclass);
if (dexElementobj.dexFile.value) {
var dexfileobj = Java.cast(dexElementobj.dexFile.value, dexfileclass);
const enumeratorClassNames = dexfileobj.entries();
while (enumeratorClassNames.hasMoreElements()) {
var className = enumeratorClassNames.nextElement().toString();
console.log("start loadclass->", className);
var loadclass = classloaderobj.loadClass(className);
console.log("after loadclass->", loadclass);
}
}
}
} catch (e) {
console.log(e)
}
});
}
}
function dumpclass(classname) {
if (Java.available) {
Java.perform(function () {
Java.enumerateClassLoaders({
onMatch: function (loader) {
try {
console.log("start loadclass->", className);
var loadclass = loader.loadClass(className);
console.log("after loadclass->", loadclass);
} catch (e) {
//console.log("error", e);
}
},
onComplete: function () {
//console.log("find Classloader instance over");
}
});
dumpall();
});
}
}
function fart() {
if (Java.available) {
Java.perform(function () {
dumpall();
//上面是利用被动调用进行函数粒度的dump,对app正常运行过程中自己加载的所有类函数进行dump
Java.enumerateClassLoaders({
onMatch: function (loader) {
try {
console.log("startdealwithclassloader:", loader, '\n');
dealwithClassLoader(loader);
} catch (e) {
console.log("error", e);
}
},
onComplete: function () {
//console.log("find Classloader instance over");
}
});
dumpall();
//上面为对当前ClassLoader中的所有类进行主动加载,从而完成ArtMethod中的CodeItem的dump
});
}
}
setImmediate(hookart);