-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathandroid_native_hook.js
338 lines (293 loc) · 9.66 KB
/
android_native_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
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
/**@@@+++@@@@******************************************************************
**
** Native Interceptor frida script v2.1 hyugogirubato
**
** frida -D "DEVICE" -l "native.js" -f "PACKAGE"
** frida -p "PID" -l "native.js"
** frida "C:\Program Files\Producer\Software.exe" -l native.js
**
** Update: https://github.com/hyugogirubato/Frida-CodeShare/releases/tag/v1.2.2
**
***@@@---@@@@******************************************************************
*/
/**
* Use only the name for a classic interception.
* Using an object when manually adding modules.
*/
const LIBRARIES = [
"libnative.so",
"libcrypto.so",
{
"name": "Software.exe",
"modules": [
{"type": "function", "name": "sub_14000BC30", "address": "0x14000BC30"},
{"type": "function", "name": "sub_14000BCA0", "address": "0x14000BCA0"},
{"type": "function", "name": "sub_14000DF50", "address": "0x14000DF50"}
]
}
];
/**
* Use the application package name to intercept only application-related processes.
* Using the Binary Path to only intercept binary-related processes.
* Use "undefined" to intercept all running processes (system included).
*/
const PACKAGE = "PACKAGE";
/**
* Filter processes attached to a string or regex value.
* Using an empty field to catch everything.
*/
const INCLUDES = ["selectedFunction", /^md5$/, "anotherNativeFunction"];
const EXCLUDES = [/create.*token$/];
const EXTENSIONS = [".so", ".dll", /\.exe$/];
/**
* Customize output display:
* - COLOR: Colorize the output.
* - TIMEOUT: Waiting time before attaching processes.
* - VARIABLE: Attach variables.
* - FUNCTION: Attach functions.
* - RECURSIVE: Arguments of the function in output.
* - DEBUG: Additional information on the current process.
*/
const COLOR = true;
const TIMEOUT = 0;
const VARIABLE = true;
const FUNCTION = true;
const RECURSIVE = false;
const DEBUG = false;
// Constants
let current = 0;
const COLORS = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
// white: '\x1b[37m'
};
const Color = () => {
const keys = Object.keys(COLORS).filter(key => key !== "reset" && key !== "red");
current = (current + 1) % keys.length;
return keys[current];
}
const print = (data, color) => {
console.log(color && COLOR ? `${COLORS[color]}${data}${COLORS.reset}` : data);
}
const searchLibraries = () => {
// Package
let libraries = Process.enumerateModules().filter((l) =>
PACKAGE ? l["path"].toLowerCase().includes(PACKAGE.toLowerCase()) : true
);
// Extensions
if (EXTENSIONS.length > 0) {
libraries = libraries.filter((l) =>
EXTENSIONS.some((e) =>
e instanceof RegExp
? l["path"].match(e)
: l["path"].toLowerCase().endsWith(e.toLowerCase())
)
);
}
// Libraries
if (LIBRARIES.length > 0) {
libraries = libraries.filter((l) =>
LIBRARIES.some((L) =>
L instanceof Object
? l["name"].toLowerCase().includes(L["name"].toLowerCase())
: l["name"].toLowerCase().includes(L.toLowerCase())
)
);
}
return libraries;
}
const filterModules = (modules, filters) => {
return modules.filter((m) => {
return filters.some((f) => {
if (f instanceof RegExp) {
return m["name"].match(f);
} else {
return m["name"].toLowerCase().includes(f.toLowerCase());
}
})
});
}
const searchModules = (library) => {
let modules = library.enumerateExports();
// Libraries
if (LIBRARIES.length > 0) {
for (const l of LIBRARIES) {
if (l instanceof Object) {
if (library["name"].toLowerCase().includes(l["name"].toLowerCase())) {
l["modules"].forEach((m) => {
if (!modules.some((obj) => JSON.stringify(obj) === JSON.stringify(m))) {
modules.push(m);
}
});
}
}
}
}
// Address
modules = modules.map(m => ({...m, address: ptr(m["address"])}));
// Includes
if (INCLUDES.length > 0) {
modules = filterModules(modules, INCLUDES);
}
// Excludes
if (EXCLUDES.length > 0) {
const excludes = filterModules(modules, EXCLUDES);
modules = modules.filter(m => !excludes.some(e => e["name"] === m["name"]));
}
return modules;
}
const printMemory = (address, index, color) => {
if (DEBUG) {
print(JSON.stringify({
address: address,
...Process.findRangeByAddress(address)
}, null, 2));
}
// Fix Access violation
let stringData;
try {
stringData = Memory.readCString(address);
} catch (e) {
print(e, "red");
}
if (stringData) {
// String
print(` --> [${index}] String: ${stringData}`, color);
// Bytes
let ptr = new NativePointer(address);
let size = 0;
while (ptr.add(size).readU8() !== 0) {
size++;
}
const byteArray = ptr.readByteArray(size);
const byteArrayView = new Uint8Array(byteArray);
// Integer
let intValue = 0;
for (let i = 0; i < byteArrayView.length; i++) {
const byte = byteArrayView[i];
if (!(byte >= 48 && byte <= 57)) {
intValue = NaN;
break;
}
const digitValue = byte - 48;
intValue = intValue * 10 + digitValue;
}
if (!isNaN(intValue)) {
print(` --> [${index}] Integer: ${intValue}`, color);
}
// Pointer
const hexData = Array.from(byteArrayView, byte => byte.toString(16).padStart(2, "0")).join("");
if (hexData.length === 10) {
print(` --> [${index}] Pointer: 0x${hexData}`, color);
}
// Base64
let hexValue = false;
if (Java.available) {
Java.perform(function () {
const byteBuffer = Java.array("byte", byteArrayView);
const base64Data = Java.use("java.util.Base64").getEncoder().encodeToString(byteBuffer);
print(` --> [${index}] Base64: ${base64Data}`, color);
});
} else {
hexValue = true;
}
// Hex
if ((!hexData.includes("-") && [32, 40, 48, 64].includes(hexData.length)) || hexValue) {
print(` --> [${index}] Hex: ${hexData}`, color);
}
} else {
print(` --> [${index}] Integer: ${parseInt(address, 16)}`, color);
}
}
const attachVariable = (module) => {
const color = Color();
print(`[+] VarEnter: ${module["name"]}`, color);
printMemory(module["address"], 0, color);
print(`[-] VarLeave: ${module["name"]}`, color);
}
const paramsCount = (args) => {
let count = 0;
while (true) {
try {
const tmp = new NativePointer(args[count]);
tmp.readPointer();
count += 1;
} catch (e) {
break;
}
}
return count;
}
const attachFunction = (module) => {
print(`[*] Module attached: ${module["name"]}`);
const color = Color();
const address = module["address"];
const params = {};
Interceptor.attach(address, {
onEnter: function (args) {
print(`[+] onEnter: ${module["name"]}`, color);
// Fix RangeError
params[address] = [];
for (let i = 0; i < paramsCount(args); i++) {
printMemory(args[i], i, color);
params[address].push(args[i]);
}
},
onLeave: function (retval) {
print(`[-] onLeave: ${module["name"]}`, color);
if (RECURSIVE) {
for (let i = 0; i < params[address].length; i++) {
printMemory(params[address][i], i, color);
}
}
printMemory(retval, RECURSIVE ? params[address].length : 0, color);
delete params[address];
}
});
}
setTimeout(function () {
print("Capturing Native process...\n---");
const libraries = searchLibraries();
if (libraries.length > 0) {
print(`[*] Native libraries found (${libraries.length})`);
let variableCount = 0;
let functionCount = 0;
for (const library of libraries) {
if (DEBUG) {
print(JSON.stringify(library, null, 2));
}
const modules = searchModules(library);
print(`[>] Attach: ${library["name"]} (${modules.length})`);
for (const module of modules) {
if (DEBUG) {
print(JSON.stringify({
library: library["name"],
...module,
...Process.findRangeByAddress(module["address"])
}, null, 2));
}
try {
if (module["type"] === "variable" && (VARIABLE || FUNCTION)) {
variableCount++;
attachVariable(module);
} else if (FUNCTION) {
functionCount++;
attachFunction(module);
}
} catch (e) {
print(e, "red");
}
}
}
print(`[>] Variables count: ${variableCount}`);
print(`[>] Functions count: ${functionCount}`);
} else {
print("[!] No native library found", "red");
}
print("Capturing setup completed\n---");
}, TIMEOUT);