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
| // handler_analyzer.js - Handler 深度分析
class HandlerAnalyzer {
constructor(moduleBase) {
this.moduleBase = moduleBase;
this.knownPatterns = this.initPatterns();
}
// 初始化已知 Handler 模式
initPatterns() {
return {
// ARM64 模式
arm64: {
vAdd: {
pattern: 'add x*, x*, x*',
description: '虚拟加法'
},
vSub: {
pattern: 'sub x*, x*, x*',
description: '虚拟减法'
},
vXor: {
pattern: 'eor x*, x*, x*',
description: '虚拟异或'
},
vAnd: {
pattern: 'and x*, x*, x*',
description: '虚拟与'
},
vOr: {
pattern: 'orr x*, x*, x*',
description: '虚拟或'
},
vShl: {
pattern: 'lsl x*, x*, x*',
description: '虚拟左移'
},
vShr: {
pattern: 'lsr x*, x*, x*',
description: '虚拟右移'
},
vLoad: {
pattern: 'ldr x*, [x*]',
description: '虚拟内存读取'
},
vStore: {
pattern: 'str x*, [x*]',
description: '虚拟内存写入'
},
vPush: {
pattern: 'str x*, [x*, #-8]!',
description: '虚拟压栈'
},
vPop: {
pattern: 'ldr x*, [x*], #8',
description: '虚拟出栈'
}
},
// x86-64 模式
x64: {
vAdd: {
pattern: 'add r*, r*',
description: '虚拟加法'
},
vSub: {
pattern: 'sub r*, r*',
description: '虚拟减法'
},
vXor: {
pattern: 'xor r*, r*',
description: '虚拟异或'
},
vMov: {
pattern: 'mov r*, r*',
description: '虚拟移动'
},
vLoad: {
pattern: 'mov r*, [r*]',
description: '虚拟内存读取'
},
vStore: {
pattern: 'mov [r*], r*',
description: '虚拟内存写入'
}
}
};
}
// 分析单个 Handler
analyzeHandler(address) {
const result = {
address: address,
offset: address.sub(this.moduleBase).toString(16),
type: 'Unknown',
confidence: 0,
disassembly: [],
operations: [],
dataFlow: {
reads: [],
writes: []
}
};
try {
// 反汇编 Handler
let ptr = address;
for (let i = 0; i < 50; i++) {
const inst = Instruction.parse(ptr);
if (!inst) break;
result.disassembly.push({
address: ptr.sub(this.moduleBase).toString(16),
mnemonic: inst.mnemonic,
opStr: inst.opStr,
bytes: inst.size
});
// 分析操作
this.analyzeInstruction(inst, result);
// 检测 Handler 结束
if (this.isHandlerEnd(inst)) {
break;
}
ptr = ptr.add(inst.size);
}
// 推断 Handler 类型
this.inferHandlerType(result);
} catch (e) {
result.error = e.message;
}
return result;
}
// 分析单条指令
analyzeInstruction(inst, result) {
const mnemonic = inst.mnemonic.toLowerCase();
// 记录操作类型
if (mnemonic.includes('add')) {
result.operations.push('ADD');
} else if (mnemonic.includes('sub')) {
result.operations.push('SUB');
} else if (mnemonic.includes('mul')) {
result.operations.push('MUL');
} else if (mnemonic.includes('div')) {
result.operations.push('DIV');
} else if (mnemonic.includes('xor') || mnemonic.includes('eor')) {
result.operations.push('XOR');
} else if (mnemonic.includes('and')) {
result.operations.push('AND');
} else if (mnemonic.includes('or')) {
result.operations.push('OR');
} else if (mnemonic.includes('ldr') || mnemonic === 'mov') {
result.operations.push('LOAD');
} else if (mnemonic.includes('str')) {
result.operations.push('STORE');
} else if (mnemonic.includes('cmp')) {
result.operations.push('CMP');
} else if (mnemonic.startsWith('b') || mnemonic.startsWith('j')) {
result.operations.push('BRANCH');
}
// 分析数据流
// 简化版:基于操作数分析读写
if (inst.operands) {
inst.operands.forEach((op, i) => {
if (op.type === 'reg') {
if (i === 0 && !mnemonic.includes('str') && !mnemonic.includes('cmp')) {
result.dataFlow.writes.push(op.value);
} else {
result.dataFlow.reads.push(op.value);
}
}
});
}
}
// 检测 Handler 结束
isHandlerEnd(inst) {
const mnemonic = inst.mnemonic.toLowerCase();
// 间接跳转通常标志着返回 dispatcher
if (mnemonic === 'br' || mnemonic === 'blr' || mnemonic === 'ret') {
return true;
}
// x86: jmp reg
if (mnemonic === 'jmp' && inst.opStr && !inst.opStr.includes('0x')) {
return true;
}
return false;
}
// 推断 Handler 类型
inferHandlerType(result) {
const ops = result.operations;
// 统计操作
const opCounts = {};
ops.forEach(op => {
opCounts[op] = (opCounts[op] || 0) + 1;
});
// 基于操作组合推断类型
if (opCounts['ADD'] && !opCounts['SUB'] && !opCounts['MUL']) {
result.type = 'vAdd';
result.confidence = 0.8;
} else if (opCounts['SUB'] && !opCounts['ADD'] && !opCounts['MUL']) {
result.type = 'vSub';
result.confidence = 0.8;
} else if (opCounts['MUL']) {
result.type = 'vMul';
result.confidence = 0.8;
} else if (opCounts['XOR'] && !opCounts['ADD'] && !opCounts['SUB']) {
result.type = 'vXor';
result.confidence = 0.8;
} else if (opCounts['AND'] && !opCounts['OR']) {
result.type = 'vAnd';
result.confidence = 0.7;
} else if (opCounts['OR'] && !opCounts['AND']) {
result.type = 'vOr';
result.confidence = 0.7;
} else if (opCounts['CMP'] && opCounts['BRANCH']) {
result.type = 'vCmp/vJcc';
result.confidence = 0.9;
} else if (opCounts['BRANCH'] && !opCounts['CMP']) {
result.type = 'vJmp';
result.confidence = 0.7;
} else if (opCounts['LOAD'] > opCounts['STORE']) {
result.type = 'vLoad';
result.confidence = 0.6;
} else if (opCounts['STORE'] > opCounts['LOAD']) {
result.type = 'vStore';
result.confidence = 0.6;
}
return result;
}
// 批量分析 Handler 表
analyzeHandlerTable(tableAddress, count) {
const handlers = [];
const ptrSize = Process.pointerSize;
console.log(`[*] Analyzing ${count} handlers from table at ${tableAddress}`);
for (let i = 0; i < count; i++) {
const handlerPtr = tableAddress.add(i * ptrSize).readPointer();
if (!handlerPtr.isNull()) {
const analysis = this.analyzeHandler(handlerPtr);
analysis.index = i;
handlers.push(analysis);
console.log(` [${i.toString().padStart(2, '0')}] ${analysis.type.padEnd(12)} @ 0x${analysis.offset}`);
}
}
return handlers;
}
}
// 使用示例
function analyzeHandlers() {
const module = Process.getModuleByName('libnative.so');
const analyzer = new HandlerAnalyzer(module.base);
// 假设已知 handler 表地址
const handlerTableOffset = 0x5000;
const handlerCount = 64;
const results = analyzer.analyzeHandlerTable(
module.base.add(handlerTableOffset),
handlerCount
);
// 输出统计
const typeStats = {};
results.forEach(h => {
typeStats[h.type] = (typeStats[h.type] || 0) + 1;
});
console.log('\n========== Handler Type Statistics ==========');
Object.entries(typeStats)
.sort((a, b) => b[1] - a[1])
.forEach(([type, count]) => {
console.log(` ${type}: ${count}`);
});
}
setTimeout(analyzeHandlers, 1000);
|