forked from FoRTE-Research/UnTracer-AFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnTracerDyninst.cpp
557 lines (460 loc) · 18.1 KB
/
UnTracerDyninst.cpp
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <sstream>
#include <climits>
#include <cstring>
#include <fstream>
using namespace std;
/* Command line parsing */
#include <getopt.h>
/* DyninstAPI includes */
#include "BPatch.h"
#include "BPatch_binaryEdit.h"
#include "BPatch_flowGraph.h"
#include "BPatch_function.h"
#include "BPatch_point.h"
using namespace Dyninst;
char *originalBinary;
char *outputBinary;
char *tracePath = NULL;
char *analyzedBBListPath = NULL;
char *instrumentedBBListPath = NULL;
char *skipListPath = NULL;
bool useMainExit = false;
bool skipMainModule = false;
bool includeSharedLib = false;
bool verbose = false;
bool initInstrumented = false;
int numBlksToSkip = 0;
int dynfix = 0;
unsigned int minBlkSize = 1;
Dyninst::Address entryPoint;
set < string > instrumentLibraries;
set < string > runtimeLibraries;
set < string > skipLibraries;
set < unsigned long > skipAddresses;
BPatch_function *mainExit;
BPatch_function *forkServer;
BPatch_function *saveRdi;
BPatch_function *restoreRdi;
BPatch_function *traceSHM;
BPatch_function *traceFile;
void initSkipLibraries ()
{
/* List of shared libraries to skip instrumenting. */
skipLibraries.insert ("libUnTracerDyninst.so");
skipLibraries.insert ("libc.so.6");
skipLibraries.insert ("libc.so.7");
skipLibraries.insert ("ld-2.5.so");
skipLibraries.insert ("ld-linux.so.2");
skipLibraries.insert ("ld-lsb.so.3");
skipLibraries.insert ("ld-linux-x86-64.so.2");
skipLibraries.insert ("ld-lsb-x86-64.so");
skipLibraries.insert ("ld-elf.so.1");
skipLibraries.insert ("ld-elf32.so.1");
skipLibraries.insert ("libstdc++.so.6");
return;
}
const char *instLibrary = "libUnTracerDyninst.so";
static const char *OPT_STR = "M:N:X:Ll:r:A:O:F:T:DEI:V";
static const char *USAGE = " [input_binary] [analysis_options] [inst_options]\n \
Analysis options:\n \
-M: minimum block size (default: 1)\n \
-N: number of initial blocks to skip (default: 0)\n \
-X: input list of block addresses to skip\n \
-L: don't analyze binary, only libraries\n \
-l: linked libraries to analyze\n \
-r: runtime libraries to analyze\n \
-A: output list for all blocks analyzed\n \
Instrumentation options:\n \
-O: output instrumented binary\n \
-F: forkserver address (required for stripped binaries)\n\
-T: trace to file path\n \
-D: attempt fixing Dyninst register bug\n \
-E: call exit() after hitting <main>\n \
-I: output list for all blocks instrumented\n \
Additional options:\n \
-V: verbose mode\n";
bool parseOptions(int argc, char **argv){
originalBinary = argv[1];
int c;
while ((c = getopt(argc, argv, OPT_STR)) != -1){
switch ((char) c) {
case 'M':
minBlkSize = atoi(optarg);
break;
case 'N':
numBlksToSkip = atoi(optarg);
break;
case 'X':
skipListPath = optarg;
break;
case 'L':
skipMainModule = true;
break;
case 'l':
instrumentLibraries.insert(optarg);
break;
case 'r':
runtimeLibraries.insert(optarg);
break;
case 'A':
analyzedBBListPath = optarg;
break;
case 'O':
outputBinary = optarg;
break;
case 'F':
entryPoint = strtoul(optarg, NULL, 16);;
break;
case 'T':
tracePath = optarg;
break;
case 'D':
dynfix = 1;
break;
case 'E':
useMainExit = true;
break;
case 'I':
instrumentedBBListPath = optarg;
break;
case 'V':
verbose = true;
break;
default:
cerr << "Usage: " << argv[0] << USAGE;
return false;
}
}
if (originalBinary == NULL) {
cerr << "Input binary is required!\n" << endl;
cerr << "Usage: " << argv[0] << USAGE;
return false;
}
if (skipMainModule && instrumentLibraries.empty()) {
cerr << "If using option -L , option -l is required.\n" << endl;
cerr << "Usage: " << argv[0] << USAGE;
return false;
}
if (analyzedBBListPath)
remove(analyzedBBListPath);
if (instrumentedBBListPath)
remove(instrumentedBBListPath);
return true;
}
/* Extracts function based on input name. Useful for getting instrumentation library callbacks. */
BPatch_function *findFuncByName(BPatch_image * appImage, char *curFuncName) {
BPatch_Vector < BPatch_function * >funcs;
if (NULL == appImage->findFunction(curFuncName, funcs) || !funcs.size()
|| NULL == funcs[0]) {
cerr << "Failed to find " << curFuncName << " function." << endl;
return NULL;
}
return funcs[0];
}
/* Insert callback to initialization function in the instrumentation library. */
int insertCallToInit(BPatch_binaryEdit *appBin, char *curFuncName, BPatch_function *instIncFunc, BPatch_point *curBlk, unsigned long curBlkAddr, unsigned int curBlkSize){
/* init has no args */
BPatch_Vector < BPatch_snippet * >instArgs;
BPatch_funcCallExpr instExprTrace(*instIncFunc, instArgs);
/* Insert the snippet at function entry */
BPatchSnippetHandle *handle = appBin->insertSnippet(instExprTrace, *curBlk, BPatch_callBefore, BPatch_lastSnippet);
if (!handle) {
cerr << "Failed to insert fork server callback." << endl;
return EXIT_FAILURE;
}
/* Print some useful info, if requested. */
if (verbose)
cout << "Inserted fork server callback at 0x" << hex << curBlkAddr << " of " << curFuncName << " of size " << dec << curBlkSize << endl;
return 0;
}
int insertTraceCallback_File(BPatch_binaryEdit *appBin, char *curFuncName, BPatch_function *instBlksIncFunc, BPatch_point *curBlk, unsigned long curBlkAddr, unsigned int curBlkSize, unsigned int curBlkID){
/* Verify curBlk is instrumentable. */
if (curBlk == NULL) {
cerr << "Failed to find entry at 0x" << hex << curBlkAddr << endl;
return EXIT_FAILURE;
}
BPatch_Vector < BPatch_snippet * >instArgsDynfix;
BPatch_Vector < BPatch_snippet * >instArgs;
char curBlkAddrChr[256];
snprintf(curBlkAddrChr, 256, "%lu", curBlkAddr);
BPatch_constExpr argCurBlkAddr(curBlkAddrChr);
BPatch_constExpr argTracePath(tracePath);
instArgs.push_back(&argCurBlkAddr);
instArgs.push_back(&argTracePath);
BPatch_constExpr argCurBlkID(curBlkID);
instArgs.push_back(&argCurBlkID);
BPatch_funcCallExpr instExprSaveRdi(*saveRdi, instArgsDynfix);
BPatch_funcCallExpr instExprRestRdi(*restoreRdi, instArgsDynfix);
BPatch_funcCallExpr instExprTrace(*instBlksIncFunc, instArgs);
BPatchSnippetHandle *handle;
/* RDI fix handling. */
if (dynfix)
handle = appBin->insertSnippet(instExprSaveRdi, *curBlk, BPatch_callBefore, BPatch_lastSnippet);
/* Instruments the basic block. */
handle = appBin->insertSnippet(instExprTrace, *curBlk, BPatch_callBefore, BPatch_lastSnippet);
/* Wrap up RDI fix handling. */
if (dynfix)
handle = appBin->insertSnippet(instExprRestRdi, *curBlk, BPatch_callBefore, BPatch_lastSnippet);
/* Verify instrumenting worked. If all good, advance blkIndex and return. */
if (!handle) {
cerr << "Failed to insert trace callback at 0x" << hex << curBlkAddr << endl;
}
if (handle){
/* If path to output instrumented bb addrs list set, save the addresses of each basic block instrumented to that file. */
if (instrumentedBBListPath){
FILE *blksListFile = fopen(instrumentedBBListPath, "a");
fprintf(blksListFile, "%lu\n", curBlkAddr);
fclose(blksListFile);
}
}
/* Print some useful info, if requested. */
if (verbose)
cout << "Inserted trace callback at 0x" << hex << curBlkAddr << " of " << curFuncName << " of size " << dec << curBlkSize << endl;
return 0;
}
int insertTraceCallback_SHM(BPatch_binaryEdit *appBin, char *curFuncName, BPatch_function *instBlksIncFunc, BPatch_point *curBlk, unsigned long curBlkAddr, unsigned int curBlkSize, unsigned int curBlkID){
/* Verify curBlk is instrumentable. */
if (curBlk == NULL) {
cerr << "Failed to find entry at 0x" << hex << curBlkAddr << endl;
return EXIT_FAILURE;
}
BPatch_Vector < BPatch_snippet * >instArgsDynfix;
BPatch_Vector < BPatch_snippet * >instArgs;
BPatch_constExpr argCurBlkID(curBlkID);
instArgs.push_back(&argCurBlkID);
BPatch_funcCallExpr instExprSaveRdi(*saveRdi, instArgsDynfix);
BPatch_funcCallExpr instExprRestRdi(*restoreRdi, instArgsDynfix);
BPatch_funcCallExpr instExprTrace(*instBlksIncFunc, instArgs);
BPatchSnippetHandle *handle;
/* RDI fix handling. */
if (dynfix)
handle = appBin->insertSnippet(instExprSaveRdi, *curBlk, BPatch_callBefore, BPatch_lastSnippet);
/* Instruments the basic block. */
handle = appBin->insertSnippet(instExprTrace, *curBlk, BPatch_callBefore, BPatch_lastSnippet);
/* Wrap up RDI fix handling. */
if (dynfix)
handle = appBin->insertSnippet(instExprRestRdi, *curBlk, BPatch_callBefore, BPatch_lastSnippet);
/* Verify instrumenting worked. If all good, advance blkIndex and return. */
if (!handle) {
cerr << "Failed to insert trace callback at 0x" << hex << curBlkAddr << endl;
}
if (handle){
/* If path to output instrumented bb addrs list set, save the addresses of each basic block instrumented to that file. */
if (instrumentedBBListPath){
FILE *blksListFile = fopen(instrumentedBBListPath, "a");
fprintf(blksListFile, "%lu,%u\n", curBlkAddr, curBlkID);
fclose(blksListFile);
}
}
/* Print some useful info, if requested. */
if (verbose)
cout << "Inserted trace callback at 0x" << hex << curBlkAddr << " of " << curFuncName << " of size " << dec << curBlkSize << endl;
return 0;
}
void iterateBlocks(BPatch_binaryEdit *appBin, vector < BPatch_function * >::iterator funcIter, int *blkIndex) {
/* Extract the function's name, and its pointer from the parent function vector. */
BPatch_function *curFunc = *funcIter;
char curFuncName[1024];
curFunc->getName(curFuncName, 1024);
/* Extract the function's CFG. */
BPatch_flowGraph *curFuncCFG = curFunc->getCFG();
if (!curFuncCFG) {
cerr << "Failed to find CFG for function " << curFuncName << endl;
return;
}
/* Extract the CFG's basic blocks and verify the number of blocks isn't 0. */
BPatch_Set < BPatch_basicBlock * >curFuncBlks;
if (!curFuncCFG->getAllBasicBlocks(curFuncBlks)) {
cerr << "Failed to find basic blocks for function " << curFuncName << endl;
return;
}
if (curFuncBlks.size() == 0) {
cerr << "No basic blocks for function " << curFuncName << endl;
return;
}
/* Set up this function's basic block iterator and start iterating. */
BPatch_Set < BPatch_basicBlock * >::iterator blksIter;
for (blksIter = curFuncBlks.begin(); blksIter != curFuncBlks.end(); blksIter++) {
/* Get the current basic block, and its size and address. */
BPatch_point *curBlk = (*blksIter)->findEntryPoint();
unsigned int curBlkSize = (*blksIter)->size();
/* Compute the basic block's adjusted address. */
unsigned long curBlkAddr = (*blksIter)->getStartAddress();
/* Non-PIE binary address correction. */
curBlkAddr = curBlkAddr - (long) 0x400000;
unsigned int curBlkID = *blkIndex;
/* If using forkserver, instrument the first block in function <main> with the forkserver callback.
* Use the correct forkserver based on the existence of tracePath. */
if (!initInstrumented && string(curFuncName) == string("main")){
if (useMainExit)
insertCallToInit(appBin, curFuncName, mainExit, curBlk, curBlkAddr, curBlkSize);
else
insertCallToInit(appBin, curFuncName, forkServer, curBlk, curBlkAddr, curBlkSize);
initInstrumented = true;
}
/* We skip <main> if instrumenting forkserver. */
if (!useMainExit && string(curFuncName) == string("main"))
continue;
/* Other basic blocks to ignore. */
if (string(curFuncName) == string("init") ||
string(curFuncName) == string("_init") ||
string(curFuncName) == string("fini") ||
string(curFuncName) == string("_fini") ||
string(curFuncName) == string("register_tm_clones") ||
string(curFuncName) == string("deregister_tm_clones") ||
string(curFuncName) == string("frame_dummy") ||
string(curFuncName) == string("__do_global_ctors_aux") ||
string(curFuncName) == string("__do_global_dtors_aux") ||
string(curFuncName) == string("__libc_csu_init") ||
string(curFuncName) == string("__libc_csu_fini") ||
string(curFuncName) == string("start") ||
string(curFuncName) == string("_start") ||
string(curFuncName) == string("__libc_start_main") ||
string(curFuncName) == string("__gmon_start__") ||
string(curFuncName) == string("__cxa_atexit") ||
string(curFuncName) == string("__cxa_finalize") ||
string(curFuncName) == string("__assert_fail") ||
string(curFuncName) == string("free") ||
string(curFuncName) == string("fnmatch") ||
string(curFuncName) == string("readlinkat") ||
string(curFuncName) == string("malloc") ||
string(curFuncName) == string("calloc") ||
string(curFuncName) == string("realloc") ||
string(curFuncName) == string("argp_failure") ||
string(curFuncName) == string("argp_help") ||
string(curFuncName) == string("argp_state_help") ||
string(curFuncName) == string("argp_error") ||
string(curFuncName) == string("argp_parse") ||
string(curFuncName) == string("__afl_maybe_log") ||
string(curFuncName) == string("__fsrvonly_store") ||
string(curFuncName) == string("__fsrvonly_return") ||
string(curFuncName) == string("__fsrvonly_setup") ||
string(curFuncName) == string("__fsrvonly_setup_first") ||
string(curFuncName) == string("__fsrvonly_forkserver") ||
string(curFuncName) == string("__fsrvonly_fork_wait_loop") ||
string(curFuncName) == string("__fsrvonly_fork_resume") ||
string(curFuncName) == string("__fsrvonly_die") ||
string(curFuncName) == string("__fsrvonly_setup_abort") ||
string(curFuncName) == string(".AFL_SHM_ENV") ||
(string(curFuncName).substr(0,4) == string("targ") \
&& isdigit(string(curFuncName)[5]))) {
continue;
}
/* If the address is in the list of addresses to skip, skip it. */
if (skipAddresses.find(curBlkAddr) != skipAddresses.end()) {
(*blkIndex)++;
continue;
}
/* If we're not in forkserver-only mode, check the block's indx/size and skip if necessary. */
if (*blkIndex < numBlksToSkip || curBlkSize < minBlkSize) {
(*blkIndex)++;
continue;
}
/* If path to output analyzed bb addrs list set, save the addresses of each basic block visited. */
if (analyzedBBListPath){
FILE *blksListFile = fopen(analyzedBBListPath, "a");
fprintf(blksListFile, "%lu\n", curBlkAddr);
fclose(blksListFile);
}
if (tracePath)
insertTraceCallback_File(appBin, curFuncName, traceFile, curBlk, curBlkAddr, curBlkSize, curBlkID);
else
insertTraceCallback_SHM(appBin, curFuncName, traceSHM, curBlk, curBlkAddr, curBlkSize, curBlkID);
(*blkIndex)++;
continue;
}
return;
}
void initSkipAddresses(){
if (skipListPath != NULL && access(skipListPath, R_OK) == 0){
char line[256];
FILE *skipListFile = fopen(skipListPath, "r");
while (fgets(line, sizeof(line), skipListFile)){
unsigned long addr = atoi(line);
skipAddresses.insert(addr);
}
fclose(skipListFile);
}
return;
}
int main(int argc, char **argv) {
/* Parse arguments. */
if (!parseOptions(argc, argv)) {
return EXIT_FAILURE;
}
/* Initialize libraries and addresses to skip. */
initSkipLibraries();
initSkipAddresses();
/* Set up Dyninst BPatch object. */
BPatch bpatch;
/* The three options afterwards prevent Segfaults on some systems. No idea why. */
bpatch.setDelayedParsing(true);
bpatch.setLivenessAnalysis(false);
bpatch.setMergeTramp(false);
/* Verify existence of original binary. */
BPatch_binaryEdit *appBin = bpatch.openBinary(originalBinary, instrumentLibraries.size() != 1);
if (appBin == NULL) {
cerr << "Failed to open binary" << endl;
return EXIT_FAILURE;
}
/* Extract BPatch image of original binary. */
BPatch_image *appImage = appBin->getImage();
if (!appBin->loadLibrary(instLibrary)) {
cerr << "Failed to open instrumentation library " << instLibrary << endl;
cerr << "It needs to be located in the curent working directory." << endl;
return EXIT_FAILURE;
}
/* Verify code coverage functions in the instrumentation library. */
forkServer = findFuncByName(appImage, (char *) "forkServer");
mainExit = findFuncByName(appImage, (char *) "mainExit");
traceSHM = findFuncByName(appImage, (char *) "traceSHM");
traceFile = findFuncByName(appImage, (char *) "traceFile");
saveRdi = findFuncByName(appImage, (char *) "saveRdi");
restoreRdi = findFuncByName(appImage, (char *) "restoreRdi");
if (!forkServer || !saveRdi || !restoreRdi || !traceSHM || !traceFile) {
cerr << "Instrumentation library lacks callbacks!" << endl;
return EXIT_FAILURE;
}
/* Set up modules iterator and other necessary variables. */
vector < BPatch_module * >*modules = appImage->getModules();
vector < BPatch_module * >::iterator moduleIter;
int blkIndex = 0;
/* For each module, iterate through its functions. */
for (moduleIter = modules->begin(); moduleIter != modules->end(); ++moduleIter) {
/* Extract module name and verify whether it should be skipped based. */
char curModuleName[1024];
(*moduleIter)->getName(curModuleName, 1024);
if ((*moduleIter)->isSharedLib ()) {
if (!includeSharedLib || skipLibraries.find (curModuleName) != skipLibraries.end ()) {
if (verbose) {
cout << "Skipping library: " << curModuleName << endl;
}
continue;
}
}
/* Extract the module's functions and iterate through its basic blocks. */
vector < BPatch_function * >*funcsInModule = (*moduleIter)->getProcedures();
vector < BPatch_function * >::iterator funcIter;
for (funcIter = funcsInModule->begin(); funcIter != funcsInModule->end(); ++funcIter) {
/* Go through each function's basic blocks and insert callbacks accordingly. */
iterateBlocks(appBin, funcIter, &blkIndex);
}
}
/* If specified, save the instrumented binary and verify success. */
if (outputBinary){
if (verbose)
cout << "Saving the instrumented binary to " << outputBinary << " ..." << endl;
if (!appBin->writeFile(outputBinary)) {
cerr << "Failed to write output file: " << outputBinary << endl;
return EXIT_FAILURE;
}
}
if (verbose)
cout << "All done!" << endl;
return EXIT_SUCCESS;
}