-
Notifications
You must be signed in to change notification settings - Fork 3
/
afl.js
342 lines (302 loc) · 11.1 KB
/
afl.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
const logfile = new File("afl.log", "a+");
function debug(msg) {
var args = Array.prototype.slice.call(arguments);
console.log.apply(console, args);
logfile.write(args.join(" "));
logfile.write("\n");
logfile.flush();
}
var threads = {};
var EV_TYPE_BLOCK = 8;
var EV_TYPE_COMPILE = 16;
var intSize = Process.pointerSize;
var EV_STRUCT_SIZE = 2 * Process.pointerSize + 2 * intSize;
function parseEvents(blob, callback) {
var len = getLen(blob);
for (var i = 0; i !== len; i++) {
var type = getType(blob, i);
switch (type) {
case EV_TYPE_BLOCK:
callback(parseBlockEvent(blob, i));
break;
case EV_TYPE_COMPILE:
callback(parseCompileEvent(blob, i));
break;
default:
debug('Unsupported type ' + type);
break;
}
}
}
function getType(blob, idx) {
return parseInteger(blob, idx, 0);
}
function getLen(blob) {
return blob.byteLength / EV_STRUCT_SIZE;
}
function parseBlockEvent(blob, idx) {
var begin = parsePointer(blob, idx, intSize);
var end = parsePointer(blob, idx, intSize + Process.pointerSize);
var i = begin.add(0);
var code = [];
while (i.compare(end) < 0) {
var instr = Instruction.parse(i);
code.push(i.toString() + ' ' + instr.toString());
i = instr.next;
}
return {
type: 'block',
begin: begin,
end: end,
code: code.join('\n')
};
}
function parseCompileEvent(blob, idx) {
var parsed = parseBlockEvent(blob, idx);
parsed.type = 'compile';
return parsed;
}
function parseInteger(blob, idx, offset) {
return new Int32Array(blob, idx * EV_STRUCT_SIZE + offset, 1)[0];
}
function parsePointer(blob, idx, offset) {
var view = new Uint8Array(blob, idx * EV_STRUCT_SIZE + offset, Process.pointerSize);
var stringed = [];
for (var i = 0; i < Process.pointerSize; i++) {
var x = view[i];
var conv = x.toString(16);
if (conv.length === 1) {
conv = '0' + conv;
}
stringed.push(conv);
}
return ptr('0x' + stringed.reverse().join(''));
}
function reverse(arr) {
var result = [];
for (var i = arr.length - 1; i >= 0; i--) {
result.push(arr[i]);
}
return result;
}
// Assignments
Stalker.trustThreshold = 0;
// WATCH-OUT: seems like the first module is always the binary main module
var instrument_all = false;
var trace_bits = ptr(0);
var shmat = ptr(0);
var shmdt = ptr(0);
var getpid = ptr(0);
// keep track of previous block id
var prev_id = ptr(0);
var block_monitored = 0;
var entrypoint = ptr(0);
// forkserver
var have_forkserver = 0;
rpc.exports = {
init: function (entrypoint_address) {
entrypoint = ptr(entrypoint_address);
debug("[*] Entrypoint address set to", entrypoint);
debug("[+] Instrumentation initialized from python launcher!");
return;
}
};
const forkstarted = Memory.alloc(4);
const forkserver_module = new CModule(`
#include <stdio.h>
#define FORKSRV_FD 198
#define F_GETFL 3 /* get file status flags */
extern int fork(void);
extern int read(int fildes, void *buf, unsigned int nbyte);
extern int write(int fildes, const void *buf, unsigned int nbyte);
extern int waitpid(int pid, int *stat_loc, int options);
extern int close(int fildes);
extern int fcntl(int fildes, int cmd, ...);
int getpid(void);
// logging
FILE *fopen(const char * restrict path, const char * restrict mode);
int fclose(FILE *stream);
int fflush(FILE *stream);
extern int errno;
extern volatile int forkstarted;
void
start (void)
{
FILE *log = fopen("fork.log", "a+");
unsigned char tmp[4] = {};
int child_pid = 0;
fprintf(log, "[+] Init forkserver PID %d!\\n", getpid());
if (fcntl(FORKSRV_FD, F_GETFL) == -1 || fcntl(FORKSRV_FD + 1, F_GETFL) == -1){
fprintf(log, "[!] AFL fork server file descriptors are not open, errno %d\\n", errno);
goto getout;
} else {
fprintf(log, "[*] FDs ready!\\n");
}
if (write(FORKSRV_FD + 1, tmp, 4) != 4) {
fprintf(log, "[!] Error writing fork server to FD %d, errno %d\\n", FORKSRV_FD + 1, errno);
goto getout;
} else {
fprintf(log, "[*] write (1)!\\n");
}
while (1) {
unsigned int was_killed;
int status;
fprintf(log, "[+] Waiting for mother!\\n");
if (read(FORKSRV_FD, &was_killed, 4) != 4) {
fprintf(log, "[!] Error reading fork server\\n");
goto getout;
} else {
fprintf(log, "[*] Read!\\n");
}
child_pid = fork();
if (child_pid < 0) {
fprintf(log, "[!] Error fork\\n");
goto getout;
} else {
fprintf(log, "[*] Forked PID %d!\\n", child_pid);
}
if (child_pid == 0) { // child
fprintf(log, "[+] forkserver(): this is the child\\n");
close(FORKSRV_FD);
close(FORKSRV_FD + 1);
forkstarted = 1;
goto getout;
}
if (write(FORKSRV_FD + 1, &child_pid, 4) != 4) {
fprintf(log, "[!] Error writing child pid to fork server (2)\\n");
goto getout;
} else {
fprintf(log, "[*] Wrote child pid %d!\\n", child_pid);
}
fprintf(log, "[+] this is the forkserver(main) waiting for child %d\\n", child_pid);
if (waitpid(child_pid, &status, 0) < 0) {
fprintf(log, "[!] Error waiting for child\\n");
goto getout;
} else {
fprintf(log, "[*] Waitpid!\\n");
}
if (write(FORKSRV_FD + 1, &status, 4) != 4) {
fprintf(log, "[!] Fork server is gone before status submission, terminating\\n");
goto getout;
} else {
fprintf(log, "[*] wrote status %d!\\n", status);
}
fprintf(log, "[+] forkserver(): child is done\\n");
fflush(log);
}
getout:
fflush(log);
return;
}`, { forkstarted });
/*
// Check if one AFL FD is accessible
var fcntl_export = Module.getExportByName(null, 'fcntl');
if (fcntl_export) {
const fcntl = new NativeFunction(fcntl_export, 'int', ['int', 'int', '...']);
var res = fcntl(198, 3);
if (res > -1) {
debug('FD 198 is readable');
} else {
debug('FD 198 is NOT readable');
}
} else {
debug("Unable to resolve fcntl!");
}
*/
var getenv_export = Module.getExportByName(null, 'getenv');
if (getenv_export) {
debug("[*] Found export for getenv!");
const get_env = new NativeFunction(getenv_export, 'pointer', ['pointer']);
debug("[*] Prepared native function @", get_env);
var shm_id = parseInt(Memory.readCString(get_env(Memory.allocUtf8String("__AFL_SHM_ID"))));
debug("[*] Shared memory ID:", shm_id);
var forkserver_enabled = parseInt(Memory.readCString(get_env(Memory.allocUtf8String("AFL_NO_FORKSRV")))) != 1;
debug("[*] Forkserver enabled:", forkserver_enabled);
var whitelist_raw = Memory.readCString(get_env(Memory.allocUtf8String("WHITELIST")));
if (whitelist_raw) {
var whitelist = whitelist_raw.split(",").map(function (item) {
return item.trim();
});
debug("[*] Whitelist: ", whitelist);
if (whitelist.indexOf("all") > -1) {
debug("[*] Covering all modules!");
instrument_all = true
}
} else {
var whitelist = [Process.enumerateModules()[0].name];
debug("[!] WHITELIST env not available! tracking only main module", whitelist[0]);
}
var getpid_export = Module.getExportByName(null, 'getpid');
if (getpid_export) {
getpid = new NativeFunction(getpid_export, 'int', []);
debug("[*] getpid() found");
}
var shmat_export = Module.getExportByName(null, 'shmat');
var shmdt_export = Module.getExportByName(null, 'shmdt');
if (shmat_export && shmdt_export) {
shmat = new NativeFunction(shmat_export, 'pointer', ['int', 'pointer', 'int']);
shmdt = new NativeFunction(shmdt_export, 'int', ['pointer']);
if (shm_id > 0) {
trace_bits = shmat(shm_id, ptr(0), 0);
}
debug("[*] trace_bits mapped at", trace_bits);
} else {
debug("[!] Unable to resolve shared memory exports!\n");
}
} else {
debug("[!] Unable to find export for getenv!");
}
Process.enumerateThreads({
onMatch: function (thread) {
Stalker.follow(thread.id, {
events: {
compile: true, // block compiled
block: false //block executed
},
onReceive: function (events) {
parseEvents(events, function (event) {
if (event.type === 'compile' || event.type === 'block') {
var block = event;
var module = Process.findModuleByAddress(block.begin);
if (forkserver_enabled && forkstarted.readInt() == 0) {
if (block.begin.equals(entrypoint) && have_forkserver == 0) {
debug("[+] Entrypoint reached!");
var forkserver_start = new NativeFunction(forkserver_module.start, 'void', []);
debug("[+] Forkserver about to start... PID", getpid(), ", flag is ", forkstarted.readInt());
have_forkserver = 1;
forkserver_start();
debug("[+] Forkserver started! PID", getpid(), ", flag is ", forkstarted.readInt());
}
} else {
if (module && (instrument_all || whitelist.indexOf(module.name) > -1)) {
//debug(event.type + ":" + module.name, block.begin);
var base = ptr(module.base);
//debug(block.begin + ' -> ' + block.end, "(", module.name, ":", module.base, "-", base.add(module.size), ")");
block_monitored += 1;
if (!trace_bits.isNull()) {
var id = block.begin >> 1;
var offset = (prev_id ^ id) & 0xFFFF;
var target = trace_bits.add(offset)
const current_value = target.readU16()
target.writeU16(current_value + 1);
prev_id = id >> 1;
//debug("[*] map_offset:", offset, "id:", id, "prev_id:", prev_id, ", target:", target, ", current:", current_value);
}
}
}
}
});
}
});
},
onComplete: function () {
debug("[*] Done stalking threads");
}
});
Interceptor.attach(Module.getExportByName(null, 'exit'), {
onEnter: function (args) {
debug("[*] Monitored", block_monitored, "blocks!");
shmdt(trace_bits);
debug("[*] Shared memory unmapped!");
}
});