-
Notifications
You must be signed in to change notification settings - Fork 11
/
afl-pin.cpp
236 lines (213 loc) · 8.32 KB
/
afl-pin.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
#include "pin.H"
#include <asm/unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <sstream>
#include <sys/types.h>
#include <sys/shm.h>
#include "afl/config.h"
#if PIN_PRODUCT_VERSION_MAJOR < 3
#if PIN_PRODUCT_VERSION_MINOR < 6
#warn "WARNING: you should use pintool >= 3.6!"
#endif
#endif
using namespace std;
KNOB < BOOL > KnobAlt(KNOB_MODE_WRITEONCE, "pintool", "alternative", "0", "use alternative mode for bb reporting");
KNOB < BOOL > KnobLibs(KNOB_MODE_WRITEONCE, "pintool", "libs", "0", "also report basic bocks of dynamic libraries");
KNOB < BOOL > KnobForkserver(KNOB_MODE_WRITEONCE, "pintool", "forkserver", "0", "install a fork server into main");
KNOB < string > KnobEntrypoint(KNOB_MODE_WRITEONCE, "pintool", "entrypoint", "main", "install a fork server into this function (name or 0xaddr)");
KNOB < string > KnobExitpoint(KNOB_MODE_WRITEONCE, "pintool", "exitpoint", "", "exit the program when this function or address is reached");
typedef uint16_t map_t;
static int libs = 0, doit = 0, imageload = 0;
static ADDRINT exe_start = 0, exe_end = 0;
static map_t prev_id;
RTN entrypoint, exitpoint;
static ADDRINT forkserver = 0, exitfunc = 0;
static bool forkserver_installed = false;
#ifndef DEBUG
static uint8_t *trace_bits = NULL;
#endif
/* ===================================================================== */
/* Usage */
/* ===================================================================== */
INT32 Usage() {
cout << "afl-pin (c) 2018-2020 by Marc \"van Hauser\" Heuse <[email protected]> AGPL 3.0" << endl;
cout << "=====================================================================" << endl;
cout << " -libs also report basic bocks of dynamic libraries" << endl;
cout << " -alternative report all basic blocks, not only conditional and indirect ones" << endl;
cout << " -forkserver insert forkserver into main() (otherwise use afl-dyninst -D)" << endl;
cout << " Note: load the forkserver.so via PIN_APP_LD_PRELOAD or use afl-fuzz-pin.sh" << endl;
cout << " -entrypoint dst specify a location for the forkserver, funcname or 0xaddr" << endl;
cout << " -exitpoint dst specify a location where the program will exit" << endl;
return -1;
}
static VOID PIN_FAST_ANALYSIS_CALL bbreport(ADDRINT addr) {
if (doit == 0)
return;
map_t id = (map_t)(((uintptr_t)addr) >> 1);
#ifdef DEBUG
cerr << "BB: 0x" << hex << addr << " and id 0x" << (prev_id ^ id) << endl;;
#else
trace_bits[prev_id ^ id]++;
#endif
prev_id = id >> 1;
}
static VOID Trace_alt(TRACE trace, VOID *v) {
BBL bbl = TRACE_BblHead(trace);
if (exe_start != 0 && (BBL_Address(bbl) > exe_end || BBL_Address(bbl) < exe_start))
return;
for ( ; BBL_Valid(bbl); bbl = BBL_Next(bbl)) {
BBL_InsertCall(bbl, IPOINT_ANYWHERE, (AFUNPTR)bbreport, IARG_FAST_ANALYSIS_CALL,
IARG_ADDRINT, BBL_Address(bbl), IARG_END);
}
}
static VOID Trace(TRACE trace, VOID *v) {
BBL bbl = TRACE_BblHead(trace);
if (exe_start != 0 && (BBL_Address(bbl) > exe_end || BBL_Address(bbl) < exe_start))
return;
for ( ; BBL_Valid(bbl); bbl = BBL_Next(bbl)) {
INS ins = BBL_InsTail(bbl);
#ifdef DEBUG
cerr << "DEBUG: INS@BB at 0x" << BBL_Address(bbl) << " " << INS_Disassemble(ins) << " => isfallthrough:" << INS_HasFallThrough(ins) << " condbranch:" << (INS_Category(ins) == XED_CATEGORY_COND_BR) << " indirect:" << INS_IsIndirectBranchOrCall(ins) << " isret:" << INS_IsRet(ins) << endl;
#endif
if (INS_IsRet(ins) == false && (INS_Category(ins) == XED_CATEGORY_COND_BR || INS_IsIndirectBranchOrCall(ins) == true))
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)bbreport, IARG_BRANCH_TARGET_ADDR, IARG_END);
}
}
static void DTearly() { PIN_Detach(); }
static VOID startForkServer(CONTEXT *ctxt, THREADID tid) {
if (forkserver_installed == true)
return;
forkserver_installed = true;
#ifdef DEBUG
fprintf(stderr, "DEBUG: starting forkserver()\n");
#endif
PIN_CallApplicationFunction(ctxt, tid, CALLINGSTD_DEFAULT, AFUNPTR(forkserver), NULL, PIN_PARG_END());
}
static VOID exitFunction(CONTEXT *ctxt, THREADID tid) {
PIN_CallApplicationFunction(ctxt, tid, CALLINGSTD_DEFAULT, AFUNPTR(exitfunc), NULL, PIN_PARG_END());
}
VOID Image(IMG img, VOID * v) {
#ifdef DEBUG
cerr << "DEBUG: image load no " << imageload << " for " << IMG_Name(img) << " from " << hex << IMG_LowAddress(img) << " to " << IMG_HighAddress(img) << endl ;
#endif
if (imageload == 0) {
if (libs == 0) {
exe_start = IMG_LowAddress(img);
exe_end = IMG_HighAddress(img);
}
if (KnobForkserver.Value()) {
entrypoint = RTN_FindByName(img, KnobEntrypoint.Value().c_str());
if (entrypoint == RTN_Invalid()) {
entrypoint = RTN_FindByAddress(strtoul(KnobEntrypoint.Value().c_str(), NULL, 16));
if (entrypoint == RTN_Invalid()) {
entrypoint = RTN_FindByName(img, "__libc_start_main");
if (entrypoint == RTN_Invalid()) {
fprintf(stderr, "Error: could not find entrypoint %s\n", KnobEntrypoint.Value().c_str());
exit(-1);
}
}
}
}
if (KnobExitpoint.Value().length() > 0) {
exitpoint = RTN_FindByName(img, KnobExitpoint.Value().c_str());
if (exitpoint == RTN_Invalid()) {
exitpoint = RTN_FindByAddress(strtoul(KnobExitpoint.Value().c_str(), NULL, 16));
if (exitpoint == RTN_Invalid()) {
fprintf(stderr, "Warning: could not find exitpoint %s\n", KnobExitpoint.Value().c_str());
}
}
}
}
if (exitpoint != RTN_Invalid() && exitfunc == 0) {
RTN rtn = RTN_FindByName(img, "_exit");
if (rtn != RTN_Invalid()) {
exitfunc = RTN_Address(rtn);
if (exitfunc != 0) {
RTN_Open(exitpoint);
RTN_InsertCall(exitpoint, IPOINT_BEFORE, (AFUNPTR)exitFunction, IARG_CONTEXT, IARG_THREAD_ID, IARG_END);
RTN_Close(exitpoint);
}
}
}
if (entrypoint != RTN_Invalid() && IMG_Name(img).find("forkserver.so") != string::npos) {
RTN rtn = RTN_FindByName(img, "startForkServer");
if (rtn == RTN_Invalid()) {
fprintf(stderr, "Error: could not find startForkServer in forkserver.so\n");
exit(-1);
}
forkserver = RTN_Address(rtn);
RTN_Open(entrypoint);
RTN_InsertCall(entrypoint, IPOINT_BEFORE, (AFUNPTR)startForkServer, IARG_CONTEXT, IARG_THREAD_ID, IARG_END);
RTN_InsertCall(entrypoint, IPOINT_AFTER, (AFUNPTR)DTearly, IARG_END);
RTN_Close(entrypoint);
}
++imageload;
}
VOID fini(INT32 code, VOID * v) {
#ifdef DEBUG
cerr << "DEBUG: END OF PROGRAM" << endl;
#endif
return;
}
VOID AfterForkInChild(THREADID threadid, const CONTEXT* ctxt, VOID * arg) {
#ifdef DEBUG
cerr << "DEBUG: After fork in child " << threadid << endl;
#endif
doit = 1;
prev_id = 0;
}
/* ===================================================================== */
/* MAIN function */
/* ===================================================================== */
int main(int argc, char *argv[]) {
#ifndef DEBUG
char *shmenv;
int shm_id;
#endif
PIN_InitSymbols();
if (PIN_Init(argc, argv))
return Usage();
PIN_SetSyntaxIntel();
if (KnobLibs.Value())
libs = 1;
#ifndef DEBUG
if ((shmenv = getenv(SHM_ENV_VAR)) == NULL) {
fprintf(stderr, "Error: AFL environment variable " SHM_ENV_VAR " not set\n");
exit(-1);
}
if ((shm_id = atoi(shmenv)) < 0) {
fprintf(stderr, "Error: invalid " SHM_ENV_VAR " contents\n");
exit(-1);
}
if ((trace_bits = (u8 *) shmat(shm_id, NULL, 0)) == (void*) -1 || trace_bits == NULL) {
fprintf(stderr, "Error: " SHM_ENV_VAR " attach failed\n");
exit(-1);
}
if (fcntl(FORKSRV_FD, F_GETFL) == -1 || fcntl(FORKSRV_FD + 1, F_GETFL) == -1) {
fprintf(stderr, "Error: AFL fork server file descriptors are not open\n");
exit(-1);
}
#endif
entrypoint = RTN_Invalid();
exitpoint = RTN_Invalid();
if (libs == 0 || KnobForkserver.Value() || KnobExitpoint.Value().length() > 0)
IMG_AddInstrumentFunction(Image, 0);
#ifdef DEBUG
doit = 1;
#endif
PIN_AddForkFunction(FPOINT_AFTER_IN_CHILD, AfterForkInChild, 0);
PIN_AddFiniFunction(fini, 0);
if (KnobAlt.Value())
TRACE_AddInstrumentFunction(Trace_alt, NULL);
else
TRACE_AddInstrumentFunction(Trace, NULL);
PIN_StartProgram();
return 0;
}