-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
349 lines (332 loc) · 10.9 KB
/
main.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
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
Java.perform(function () {
const DexClassLoader = Java.use("dalvik.system.DexClassLoader");
const AGENT_PATH = "/data/local/tmp/agent.dex";
const AGENT_CLASS = "Agent";
const AGENT_FUNCTION = "log_called_function";
const STRING_CLASS = Java.use("java.lang.String").class;
const AGENT_FUNCTION_ARGUMENTS = [STRING_CLASS, STRING_CLASS, STRING_CLASS];
const AGENT_LIBRARY = "libagent.so";
const AGENT_FUNCTION_NATIVE = "_Z6reportPv"; // mangled name of void report(void *address_of_crash) function
const System = Java.use("java.lang.System");
const Runtime = Java.use("java.lang.Runtime");
const SystemLoadLibrary = System.loadLibrary.overload("java.lang.String");
const VMStack = Java.use("dalvik.system.VMStack");
var LOADED_AGENT_CLASS = null;
let DICTIONARY_CLASS_OF_METHODS = {};
let all_methods = [];
let agentLoaded = false;
let ExecutableMemoryBlacklist = [];
const PROT_EXEC = 0x4;
// We hook system load library to intercept loading any native library
// After loading we intercept AGENT_NATIVE_FUNCTION
// We do this because we have to normally wait until our AGENT_LIBRARY to be loaded
// But in our "run.py" we add our AGENT_LIBRARY as dependency to all existing libraries
// So, when a library loaded eour AGENT_LIBRARY will be loaded as well, then now we can replace our AGENT_NATIVE_FUNCTION function
SystemLoadLibrary.implementation = function (library) {
console.log("Loading dynamic library => " + library);
try {
const loaded = Runtime.getRuntime().loadLibrary0(
VMStack.getCallingClassLoader(),
library
);
const AGENT_NATIVE_FUNCTION_HANDLE = Module.findExportByName(
AGENT_LIBRARY,
AGENT_FUNCTION_NATIVE
);
if (AGENT_NATIVE_FUNCTION_HANDLE == null) {
return loaded;
}
console.log("Agent function handle -> ", AGENT_NATIVE_FUNCTION_HANDLE);
Interceptor.replace(
AGENT_NATIVE_FUNCTION_HANDLE,
new NativeCallback(
function (crash_address) {
console.log("Crash address -> ", crash_address);
for (var i = 0; i < ExecutableMemoryBlacklist.length; i++) {
console.log(
"comparing with -> ",
ExecutableMemoryBlacklist[i].name,
"addr",
ExecutableMemoryBlacklist[i].addr.toString(16),
"length",
ExecutableMemoryBlacklist[i].length
);
if (
crash_address.compare(
ExecutableMemoryBlacklist[i].addr,
ExecutableMemoryBlacklist[i] +
ExecutableMemoryBlacklist[i].length
) == 0
) {
console.log(
"Crash address allocation with -> ",
ExecutableMemoryBlacklist[i].name,
"addr",
ExecutableMemoryBlacklist[i].addr.toString(16),
"length",
ExecutableMemoryBlacklist[i].length
);
// dump memory at max 100 byte
var buf = Memory.readByteArray(
ExecutableMemoryBlacklist[i].addr,
Math.min(100, ExecutableMemoryBlacklist[i].length)
);
console.log(
hexdump(buf, {
offset: 0,
length: 100,
header: true,
ansi: true,
})
);
// Sleep for 5 seconds
Java.use("java.lang.Thread").sleep(5000);
break;
}
}
},
"void",
["pointer"]
)
);
return loaded;
} catch (ex) {
console.log(ex);
}
};
// -------------------------------------- JAVA PART --------------------------------------
// get caller from stacktrace
function get_caller() {
var straces = Java.use("java.lang.Exception")
.$new("Exception")
.getStackTrace();
var caller = straces[3].getClassName() + "." + straces[3].getMethodName();
return caller;
}
// make toast function
function makeToast(message) {
Java.scheduleOnMainThread(function () {
const Toast = Java.use("android.widget.Toast");
var currentApplication = Java.use(
"android.app.ActivityThread"
).currentApplication();
var context = currentApplication.getApplicationContext();
Toast.makeText(
context,
Java.use("java.lang.String").$new(message),
Toast.LENGTH_SHORT.value
).show();
});
}
// call agent function from our agent dex file
function callAgentFunction(function_name, class_name, caller) {
const method = LOADED_AGENT_CLASS.getMethod(
AGENT_FUNCTION,
AGENT_FUNCTION_ARGUMENTS
);
return method.invoke(null, [
Java.use("java.lang.String").$new(function_name),
Java.use("java.lang.String").$new(class_name),
Java.use("java.lang.String").$new(caller),
]);
}
// load agent dex file
function loadAgent() {
Java.perform(function () {
var context = Java.use("android.app.ActivityThread")
.currentApplication()
.getApplicationContext();
var dexOptimizedPath = "/data/local/tmp";
var DexClassLoader = Java.use("dalvik.system.DexClassLoader");
var classLoader = DexClassLoader.$new(
AGENT_PATH,
dexOptimizedPath,
null,
context.getClassLoader()
);
// load agent class
LOADED_AGENT_CLASS = Java.cast(
classLoader.loadClass(AGENT_CLASS),
Java.use("java.lang.Class")
);
});
}
// hook DexClassLoader constructor
DexClassLoader.$init.overload(
"java.lang.String",
"java.lang.String",
"java.lang.String",
"java.lang.ClassLoader"
).implementation = function (
dexPath,
optimizedDirectory,
librarySearchPath,
parent
) {
console.log("DexClassLoader -> ", dexPath, "caller -> ", get_caller());
this.$init(dexPath, optimizedDirectory, librarySearchPath, parent);
if (!agentLoaded) {
agentLoaded = true;
loadAgent();
console.log("Agent loaded");
}
var loadedDex = Java.use("dalvik.system.DexFile").$new(dexPath);
var dexEntries = loadedDex.entries();
var BaseDexClassLoader = Java.use("dalvik.system.BaseDexClassLoader");
var File = Java.use("java.io.File");
var systemClassLoader = Java.classFactory.loader;
var loader = BaseDexClassLoader.$new(
dexPath,
File.$new("/data/local/tmp"),
null,
systemClassLoader
);
while (dexEntries.hasMoreElements()) {
var className = dexEntries.nextElement().toString();
className = className.replace(/\//g, "."); // replace slashes with dots
try {
var classInstance = Java.cast(
loader.findClass(className),
Java.use("java.lang.Class")
);
var methods = classInstance.getDeclaredMethods();
console.log("[+] Class: " + className);
DICTIONARY_CLASS_OF_METHODS[className] = [];
methods.forEach(function (method) {
console.log(" " + method.getName());
DICTIONARY_CLASS_OF_METHODS[className].push(method.getName());
all_methods.push(method.getName());
});
} catch (err) {
console.log(
"[-] An exception occurred when trying to find class " +
className +
" : " +
err.message
);
}
}
};
// hook reflected invoke method
const Method = Java.use("java.lang.reflect.Method");
Method.invoke.overload(
"java.lang.Object",
"[Ljava.lang.Object;"
).implementation = function (obj, args) {
const name = this.getName();
const class_name = this.getDeclaringClass().getName();
if (
DICTIONARY_CLASS_OF_METHODS[class_name] != undefined &&
DICTIONARY_CLASS_OF_METHODS[class_name].indexOf(name) != -1
) {
const message =
"Malicious call -> " +
class_name +
"->" +
name +
" caller -> " +
get_caller();
console.log(message);
makeToast(message);
const should_continue = callAgentFunction(name, class_name, get_caller());
if (should_continue.toString() === "false") {
console.log("[-] Stopping execution");
return;
}
}
return this.invoke(obj, args);
};
// -------------------------------------- NATIVE PART --------------------------------------
function isExecutable(protection) {
return (protection & PROT_EXEC) === PROT_EXEC;
}
// hook mmap, mmap64 and mprotect to keep track of executable memory blocks
Interceptor.attach(Module.findExportByName(null, "mmap"), {
onEnter: function (args) {
this.addr = ptr(args[0]);
this.length = ptr(args[1]);
this.protection = ptr(args[2]);
this.executable = isExecutable(this.protection);
// remove PROT_EXEC from protection
args[2] = ptr(this.protection & ~PROT_EXEC);
},
onLeave: function (retval) {
// if executable push to memoryBlocks
if (retval.toInt32() != -1 && this.executable) {
ExecutableMemoryBlacklist.push({
addr: ptr(retval),
length: this.length,
protection: this.protection,
name: "mmap",
});
console.log(
"mmap(",
retval,
",",
this.length,
",",
this.protection,
") = ",
retval
);
}
},
});
Interceptor.attach(Module.findExportByName(null, "mmap64"), {
onEnter: function (args) {
this.addr = ptr(args[0]);
this.length = ptr(args[1]);
this.protection = ptr(args[2]);
this.executable = isExecutable(this.protection);
// remove PROT_EXEC from protection
args[2] = ptr(this.protection & ~PROT_EXEC);
},
onLeave: function (retval) {
// if executable push to memoryBlocks
if (retval.toInt32() != -1 && this.executable) {
ExecutableMemoryBlacklist.push({
addr: retval,
length: this.length,
protection: this.protection,
name: "mmap64",
});
console.log(
"mmap64(",
retval,
",",
this.length,
",",
this.protection,
") = ",
retval
);
}
},
});
Interceptor.attach(Module.findExportByName(null, "mprotect"), {
onEnter: function (args) {
this.addr = ptr(args[0]);
this.length = ptr(args[1]);
this.protection = ptr(args[2]);
this.executable = isExecutable(this.protection);
if (this.executable) {
args[2] = ptr(this.protection & ~PROT_EXEC);
ExecutableMemoryBlacklist.push({
addr: this.addr,
length: this.length,
protection: this.protection,
name: "mprotect",
});
console.log(
"mprotect(",
this.addr,
",",
this.length,
",",
this.protection,
")"
);
}
},
});
});