-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.js
228 lines (228 loc) · 9.67 KB
/
debug.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
// Utility Functions
function logJavaLog(level, a, b, c) {
console.log(`Log.${level}(${a}, ${b}, ${c})`);
return this[level](a, b, c);
}
function formatHex(array) {
return array.map(byte => byte.toString(16).padStart(2, '0')).join('');
}
function formatBase64(array) {
Java.perform(() => {
const byteBuffer = Java.array('byte', array);
const base64Data = Java.use('java.util.Base64').getEncoder().encodeToString(byteBuffer);
console.log(`Base64: ${base64Data}`);
});
}
function printMemory(address, index, color) {
if (CONFIG.DEBUG) print(JSON.stringify({...Process.findRangeByAddress(address), address}, null, 2));
try {
const stringData = Memory.readCString(address);
if (stringData) {
console.log(` --> [${index}] String: ${stringData}`);
const byteArray = new Uint8Array(Memory.readByteArray(address, stringData.length));
const intValue = byteArray.reduce((sum, byte) => sum * 10 + (byte >= 48 && byte <= 57 ? byte - 48 : NaN), 0);
if (!isNaN(intValue)) console.log(` --> [${index}] Integer: ${intValue}`);
const hexData = Array.from(byteArray, byte => byte.toString(16).padStart(2, "0")).join("");
if (hexData.length === 10) console.log(` --> [${index}] Pointer: 0x${hexData}`);
if (Java.available) {
formatBase64(byteArray);
console.log(` --> [${index}] Hex: ${hexData}`);
} else {
console.log(` --> [${index}] Hex: ${hexData}`);
}
} else {
console.log(` --> [${index}] Integer: ${parseInt(address, 16)}`);
}
} catch (e) {
console.log(`Error reading memory: ${e.message}`, "red");
}
}
function print(data, color) {
const COLORS = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
};
console.log(color && CONFIG.COLOR ? `${COLORS[color]}${data}${COLORS.reset}` : data);
}
function attachFunction(module) {
const color = Color();
const address = module.address;
const params = {};
Interceptor.attach(address, {
onEnter: function(args) {
print(`[+] onEnter: ${module.name}`, color);
params[address] = [];
for (let i = 0; i < args.length; i++) {
printMemory(args[i], i, color);
params[address].push(args[i]);
}
},
onLeave: function(retval) {
print(`[-] onLeave: ${module.name}`, color);
if (CONFIG.RECURSIVE) params[address].forEach((arg, i) => printMemory(arg, i, color));
printMemory(retval, CONFIG.RECURSIVE ? params[address].length : 0, color);
delete params[address];
}
});
}
function attachVariable(module) {
const color = Color();
print(`[+] VarEnter: ${module.name}`, color);
printMemory(module.address, 0, color);
print(`[-] VarLeave: ${module.name}`, color);
}
function searchLibraries() {
return Process.enumerateModules().filter(l =>
(!CONFIG.PACKAGE || l.path.toLowerCase().includes(CONFIG.PACKAGE.toLowerCase())) &&
CONFIG.EXTENSIONS.some(e => e instanceof RegExp ? l.path.match(e) : l.path.toLowerCase().endsWith(e.toLowerCase())) &&
LIBRARIES.some(L => L instanceof Object ? l.name.toLowerCase().includes(L.name.toLowerCase()) : l.name.toLowerCase().includes(L.toLowerCase()))
);
}
function filterModules(modules, filters) {
return modules.filter(m =>
filters.some(f => f instanceof RegExp ? m.name.match(f) : m.name.toLowerCase().includes(f.toLowerCase()))
);
}
function searchModules(library) {
let modules = library.enumerateExports();
LIBRARIES.forEach(L => {
if (L instanceof Object && library.name.toLowerCase().includes(L.name.toLowerCase())) {
L.modules.forEach(m => {
if (!modules.some(obj => JSON.stringify(obj) === JSON.stringify(m))) modules.push(m);
});
}
});
modules = modules.map(m => ({...m, address: ptr(m.address)}));
if (CONFIG.INCLUDES.length) modules = filterModules(modules, CONFIG.INCLUDES);
if (CONFIG.EXCLUDES.length) {
const excludes = filterModules(modules, CONFIG.EXCLUDES);
modules = modules.filter(m => !excludes.some(e => e.name === m.name));
}
return modules;
}
// Enhanced Logs
Java.perform(function() {
// Android Log Override
const Log = Java.use("android.util.Log");
['d', 'v', 'i', 'e', 'w'].forEach(level => {
Log[level].overload('java.lang.String', 'java.lang.String', 'java.lang.Throwable').implementation = function(a, b, c) {
logJavaLog.call(this, level, a, b, c);
};
Log[level].overload('java.lang.String', 'java.lang.String').implementation = function(a, b) {
console.log(`Log.${level}(${a}, ${b})`);
return this[level](a, b);
};
});
// AES Cryptography Logs
const bin2ascii = array => array.map(byte => String.fromCharCode(byte)).join('');
const bin2hex = (array, length = array.length) => array.slice(0, length).map(byte => byte.toString(16).padStart(2, '0')).join('');
Java.use('javax.crypto.spec.SecretKeySpec').$init.overload('[B', 'java.lang.String').implementation = function(key, spec) {
console.log(`KEY: ${bin2hex(key)} | ${bin2ascii(key)}`);
return this.$init(key, spec);
};
Java.use('javax.crypto.Cipher').getInstance.overload('java.lang.String').implementation = function(spec) {
console.log(`CIPHER: ${spec}`);
return this.getInstance(spec);
};
Java.use('javax.crypto.Cipher').doFinal.overload('[B').implementation = function(data) {
console.log("Cipher Operation:");
console.log(bin2ascii(data));
return this.doFinal(data);
};
// Process native libraries and functions
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"}
]
}
];
const CONFIG = {
PACKAGE: "PACKAGE",
INCLUDES: ["selectedFunction", /^md5$/, "anotherNativeFunction"],
EXCLUDES: [/create.*token$/],
EXTENSIONS: [".so", ".dll", /\.exe$/],
COLOR: true,
TIMEOUT: 0,
VARIABLE: true,
FUNCTION: true,
RECURSIVE: false,
DEBUG: false
};
const COLORS = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
};
let current = 0;
function Color() {
const keys = Object.keys(COLORS).filter(key => key !== "reset" && key !== "red");
return keys[(current++) % keys.length];
}
function searchLibraries() {
return Process.enumerateModules().filter(l =>
(!CONFIG.PACKAGE || l.path.toLowerCase().includes(CONFIG.PACKAGE.toLowerCase())) &&
CONFIG.EXTENSIONS.some(e => e instanceof RegExp ? l.path.match(e) : l.path.toLowerCase().endsWith(e.toLowerCase())) &&
LIBRARIES.some(L => L instanceof Object ? l.name.toLowerCase().includes(L.name.toLowerCase()) : l.name.toLowerCase().includes(L.toLowerCase()))
);
}
function searchModules(library) {
let modules = library.enumerateExports();
LIBRARIES.forEach(L => {
if (L instanceof Object && library.name.toLowerCase().includes(L.name.toLowerCase())) {
L.modules.forEach(m => {
if (!modules.some(obj => JSON.stringify(obj) === JSON.stringify(m))) modules.push(m);
});
}
});
modules = modules.map(m => ({...m, address: ptr(m.address)}));
if (CONFIG.INCLUDES.length) modules = filterModules(modules, CONFIG.INCLUDES);
if (CONFIG.EXCLUDES.length) {
const excludes = filterModules(modules, CONFIG.EXCLUDES);
modules = modules.filter(m => !excludes.some(e => e.name === m.name));
}
return modules;
}
function filterModules(modules, filters) {
return modules.filter(m =>
filters.some(f => f instanceof RegExp ? m.name.match(f) : m.name.toLowerCase().includes(f.toLowerCase()))
);
}
// Start capturing native process
setTimeout(() => {
print("Capturing Native process...\n---");
const libraries = searchLibraries();
if (libraries.length) {
print(`[*] Native libraries found (${libraries.length})`);
let variableCount = 0, functionCount = 0;
libraries.forEach(library => {
if (CONFIG.DEBUG) print(JSON.stringify(library, null, 2));
const modules = searchModules(library);
if (modules.length) {
print(`[*] Modules found in ${library.path} (${modules.length})`);
modules.forEach(module => {
if (module.type === "variable" && CONFIG.VARIABLE) attachVariable(module), variableCount++;
else if (module.type === "function" && CONFIG.FUNCTION) attachFunction(module), functionCount++;
});
}
});
print(`\n---\nNative process successfully captured:\n - Variables (${variableCount})\n - Functions (${functionCount})`);
} else {
print("[*] No native libraries found");
}
}, CONFIG.TIMEOUT);
});