-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathTraceFunction.java
More file actions
273 lines (252 loc) · 11.6 KB
/
TraceFunction.java
File metadata and controls
273 lines (252 loc) · 11.6 KB
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
package com.open;
import com.github.unidbg.Emulator;
import com.github.unidbg.Module;
import com.github.unidbg.arm.context.RegisterContext;
import com.github.unidbg.debugger.Debugger;
import com.github.unidbg.debugger.FunctionCallListener;
import com.github.unidbg.pointer.UnidbgPointer;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
public class TraceFunction {
/*
outPutPath: 日志文件保存路径
arg_len: 打印入参个数
windowSize: 读取内存大小( 自己可以尝试加入偏移,可能会有惊喜
showRets: 显示函数返回值
callCounter: 函数计数器
functionTimes: 函数调用次数阈值
*/
private final Emulator<?> emulator;
private final RegisterContext registerContext;
private final Module module;
private final String outPutPath;
private final int arg_len; // 打印入参个数
private final int windowSize; // 显示内存大小(自己可以尝试加入偏移,可能会有惊喜
private final boolean showRets; // 显示函数返回值
private final Map<Long, Integer> callCounter = new HashMap<>(); // 函数计数器
private final int functionTimes; // 函数调用次数阈值
public TraceFunction(Emulator<?> emulator, Module module, String outPutPath) {
this.emulator = emulator;
this.module = module;
this.outPutPath = outPutPath;
this.windowSize = 80;
this.registerContext = emulator.getContext();
this.arg_len = 4;
this.showRets = true;
this.functionTimes = 0;
trace_function();
}
public TraceFunction(Emulator<?> emulator, Module module, String outPutPath, int functionTimes) {
this.emulator = emulator;
this.module = module;
this.outPutPath = outPutPath;
this.windowSize = 80;
this.registerContext = emulator.getContext();
this.arg_len = 4;
this.showRets = true;
this.functionTimes = functionTimes;
trace_function();
}
public TraceFunction(Emulator<?> emulator, Module module, String outPutPath, int arg_len, int functionTimes) {
this.emulator = emulator;
this.module = module;
this.outPutPath = outPutPath;
this.windowSize = 80;
this.registerContext = emulator.getContext();
this.showRets = true;
this.arg_len = arg_len;
this.functionTimes = functionTimes;
trace_function();
}
public TraceFunction(Emulator<?> emulator, Module module, String outPutPath, boolean showRets, int functionTimes) {
this.emulator = emulator;
this.module = module;
this.outPutPath = outPutPath;
this.windowSize = 80;
this.registerContext = emulator.getContext();
this.showRets = showRets;
this.arg_len = 4;
this.functionTimes = functionTimes;
trace_function();
}
public TraceFunction(Emulator<?> emulator, Module module, String outPutPath, int arg_len, boolean showRets, int functionTimes) {
this.emulator = emulator;
this.module = module;
this.outPutPath = outPutPath;
this.windowSize = 80;
this.registerContext = emulator.getContext();
this.showRets = showRets;
this.arg_len = arg_len;
this.functionTimes = functionTimes;
trace_function();
}
public TraceFunction(Emulator<?> emulator, Module module, String outPutPath, int arg_len, boolean showRets, int windowSize, int functionTimes) {
this.emulator = emulator;
this.module = module;
this.outPutPath = outPutPath;
this.windowSize = windowSize;
this.registerContext = emulator.getContext();
this.showRets = showRets;
this.arg_len = arg_len;
this.functionTimes = functionTimes;
trace_function();
}
public void trace_function() {
Debugger debugger = emulator.attach();
PrintStream traceStream = null;
try {
traceStream = new PrintStream(new FileOutputStream(outPutPath, false));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
final PrintStream finalTraceStream = traceStream;
assert finalTraceStream != null;
debugger.traceFunctionCall(null, new FunctionCallListener() {
@Override
public void onCall(Emulator<?> emulator, long callerAddress, long functionAddress) {
try {
int callCount = callCounter.getOrDefault(functionAddress, 0);
if (functionTimes != 0 && callCount >= functionTimes) return;
callCounter.put(functionAddress, ++callCount);
StringBuilder pcString = new StringBuilder(" ");
pcString.append(registerContext.getPCPointer().toString());
while (pcString.length() < 59) {
pcString.append(" ");
}
pcString.append("sub: 0x" + Long.toHexString(functionAddress - module.base));
pcString.append(" [call count: ").append(callCount).append("]"); // 添加调用次数
writeFuncToFile(pcString + "\n");
for (int i = 0; i < arg_len; i++) {
UnidbgPointer args = registerContext.getPointerArg(i);
byte[] readBytes;
try {
readBytes = emulator.getBackend().mem_read(args.peer, windowSize);
} catch (Exception e) {
readBytes = new byte[16];
}
writeArgToFile(readBytes, i, args.peer);
}
} catch (Exception e) {
}
}
@Override
public void postCall(Emulator<?> emulator, long callerAddress, long functionAddress, Number[] args) {
if (showRets) {
try {
int callCount = callCounter.getOrDefault(functionAddress, 0);
if (functionTimes != 0 && callCount >= functionTimes) return;
StringBuilder pcString = new StringBuilder(" ");
pcString.append(registerContext.getPCPointer().toString());
while (pcString.length() < 59) {
pcString.append(" ");
}
pcString.append("call_by: 0x" + Long.toHexString(functionAddress - module.base));
writeFuncToFile(pcString + "\n");
if (args != null && args.length > 0) {
for (int l = 0; l < args.length; l++) {
byte[] readBytes;
UnidbgPointer pointer = UnidbgPointer.pointer(emulator, args[l].longValue());
try {
readBytes = pointer.getByteArray(0, windowSize);
} catch (Exception e) {
readBytes = new byte[16];
}
writeRetArgToFile(readBytes, l, pointer.peer);
}
}
byte[] retBytes;
long lrPoint = registerContext.getLR();
try {
retBytes = emulator.getBackend().mem_read(lrPoint, windowSize);
} catch (Exception e) {
retBytes = new byte[16];
}
writeRetToFile(retBytes, lrPoint);
} catch (Exception e) {
}
}
}
});
}
private void writeArgToFile(byte[] data, int args, long baseAddress) {
Path outputPath = Paths.get(this.outPutPath);
try (OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputPath, StandardOpenOption.CREATE, StandardOpenOption.APPEND))) {
String separator = String.format("\n-------- ----------------------------------------------- ---------------- arg %d\n", args);
outputStream.write(separator.getBytes());
String formattedHex = formatHexOutput(data, baseAddress);
outputStream.write(formattedHex.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeRetToFile(byte[] data, long baseAddress) {
Path outputPath = Paths.get(this.outPutPath);
try (OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputPath, StandardOpenOption.CREATE, StandardOpenOption.APPEND))) {
String separator = String.format("\n-------- ----------------------------------------------- ---------------- retValue \n");
outputStream.write(separator.getBytes());
String formattedHex = formatHexOutput(data, baseAddress);
outputStream.write(formattedHex.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeRetArgToFile(byte[] data, int args, long baseAddress) {
Path outputPath = Paths.get(this.outPutPath);
try (OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputPath, StandardOpenOption.CREATE, StandardOpenOption.APPEND))) {
String separator = String.format("\n-------- ----------------------------------------------- ---------------- retArg %d\n", args);
outputStream.write(separator.getBytes());
String formattedHex = formatHexOutput(data, baseAddress);
outputStream.write(formattedHex.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeFuncToFile(String data) {
Path outputPath = Paths.get(this.outPutPath);
try (OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputPath, StandardOpenOption.CREATE, StandardOpenOption.APPEND))) {
String separator = "\n\n======== =============================================== =========== func\n";
outputStream.write(separator.getBytes());
outputStream.write(data.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private String formatHexOutput(byte[] bytes, long baseAddress) {
StringBuilder sb = new StringBuilder();
int length = bytes.length;
for (int i = 0; i < length; i += 16) {
sb.append(String.format("%08X ", baseAddress + i));
for (int j = 0; j < 16; j++) {
if (i + j < length) {
sb.append(String.format("%02X ", bytes[i + j]));
} else {
sb.append(" ");
}
}
sb.append(" ");
for (int j = 0; j < 16; j++) {
if (i + j < length) {
byte b = bytes[i + j];
if (b >= 32 && b <= 126) {
sb.append((char) b);
} else {
sb.append('.');
}
} else {
sb.append(" ");
}
}
sb.append(System.lineSeparator());
}
return sb.toString();
}
}