-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathexceptiondump.cpp
332 lines (269 loc) · 7.69 KB
/
exceptiondump.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
#include "pin.H"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <set>
#include <list>
#include <sstream>
/**
* Keeps track of legit instructions before control flow is transferred to she
* shellcode.
**/
std::list<std::string> legitInstructions;
/**
* Keeps track of disassembled instructions that were already dumped.
**/
std::set<std::string*> dumped;
/**
* Output file the shellcode information is dumped to.
**/
FILE *traceFile;
/**
* Command line option to specify the name of the output file.
* Default is shellcode.out.
**/
KNOB<string> outputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "crashdump.out", "specify trace file name");
KNOB<INT32> maxInstructions(KNOB_MODE_WRITEONCE, "pintool", "c", "100", "last instruction log count");
string dumpInstruction(INS ins);
/**
* Prints usage information.
**/
INT32 usage()
{
cerr << "This tool produces a call trace." << endl << endl;
cerr << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
class Instruction
{
public:
Instruction(INS ins);
~Instruction();
ADDRINT Address(void) { return address; }
string Disassembly(void) { return disassembly; }
private:
ADDRINT address;
char * opcodes;
char length;
string disassembly;
};
class BasicBlock
{
public:
BasicBlock();
~BasicBlock();
void addInstruction(Instruction * ins) { instructions.push_back(ins); }
list<Instruction *>::iterator begin(void) { return instructions.begin(); }
list<Instruction *>::iterator end(void) { return instructions.end(); }
private:
list<Instruction *> instructions;
};
BasicBlock * currentBBL;
Instruction::Instruction(INS ins)
{
address = INS_Address(ins);
opcodes = (char *) address;
length = INS_Size(ins);
disassembly = dumpInstruction(ins);
}
Instruction::~Instruction()
{
}
BasicBlock::BasicBlock()
{
}
BasicBlock::~BasicBlock()
{
}
/**
* Given a fully qualified path to a file, this function extracts the raw
* filename and gets rid of the path.
**/
std::string extractFilename(const std::string& filename)
{
unsigned int lastBackslash = filename.rfind("\\");
if (lastBackslash == -1)
{
return filename;
}
else
{
return filename.substr(lastBackslash + 1);
}
}
/**
* Given an address, this function determines the name of the loaded module the
* address belongs to. If the address does not belong to any module, the empty
* string is returned.
**/
std::string getModule(ADDRINT address)
{
// To find the module name of an address, iterate over all sections of all
// modules until a section is found that contains the address.
for(IMG img=APP_ImgHead(); IMG_Valid(img); img = IMG_Next(img)) {
for(SEC sec=IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec)) {
if (address >= SEC_Address(sec) && address < SEC_Address(sec) + SEC_Size(sec)) {
return extractFilename(IMG_Name(img));
}
}
}
return "";
}
/**
* Converts a PIN instruction object into a disassembled string.
**/
std::string dumpInstruction(INS ins)
{
// save only the instruction bytes??
std::stringstream ss;
ADDRINT address = INS_Address(ins);
// Generate address and module information
ss << "0x" << setfill('0') << setw(8) << uppercase << hex << address << "::" << getModule(address) << " ";
// Generate instruction byte encoding
for (int i=0;i<INS_Size(ins);i++)
{
ss << setfill('0') << setw(2) << (((unsigned int) *(unsigned char*)(address + i)) & 0xFF) << " ";
}
for (int i=INS_Size(ins);i<8;i++)
{
ss << " ";
}
// Generate diassembled string
ss << INS_Disassemble(ins);
// Look up call information for direct calls
if (INS_IsCall(ins) && INS_IsDirectBranchOrCall(ins))
{
ss << " -> " << RTN_FindNameByAddress(INS_DirectBranchOrCallTargetAddress(ins));
}
return ss.str();
}
static void
logStart(int exceptionCode)
{
fprintf(traceFile, "EXCEPTION CODE: %x\n", exceptionCode);
}
static void
logInstructions(void)
{
std::list<std::string>::iterator it;
fprintf(traceFile, "PREVIOUS INSTRUCTIONS:\n");
for (it = legitInstructions.begin(); it != legitInstructions.end(); it++) {
fprintf(traceFile, " %s\n", (*it).c_str());
}
fprintf(traceFile, "\n");
}
static void
logBBL(ADDRINT faultAddress)
{
fprintf(traceFile, "FAULTING BASIC BLOCK\n");
for (list<Instruction *>::iterator it = currentBBL->begin(); it != currentBBL->end(); it++) {
if ((*it)->Address() == faultAddress) {
fprintf(traceFile, "--->");
} else {
fprintf(traceFile, " ");
}
fprintf(traceFile, "%s\n", (*it)->Disassembly().c_str());
}
fprintf(traceFile, "\n");
}
static void
logContext(const CONTEXT *ctxt)
{
fprintf(traceFile, "CONTEXT:\n");
fprintf(traceFile, "EAX: 0x%0.8x EBX: 0x%0.8x ECX: 0x%0.8x EDX: 0x%0.8x\n",
PIN_GetContextReg( ctxt, REG_GAX ), PIN_GetContextReg( ctxt, REG_GBX ),
PIN_GetContextReg( ctxt, REG_GCX ), PIN_GetContextReg( ctxt, REG_GDX )
);
fprintf(traceFile, "ESI: 0x%0.8x EDI: 0x%0.8x EBP: 0x%0.8x ESP: 0x%0.8x\n",
PIN_GetContextReg( ctxt, REG_GSI ), PIN_GetContextReg( ctxt, REG_GDI ),
PIN_GetContextReg( ctxt, REG_GBP ), PIN_GetContextReg( ctxt, REG_ESP )
);
fprintf(traceFile, "SS : 0x%0.8x CS : 0x%0.8x DS : 0x%0.8x ES : 0x%0.8x\n",
PIN_GetContextReg( ctxt, REG_SEG_SS ), PIN_GetContextReg( ctxt, REG_SEG_SS ),
PIN_GetContextReg( ctxt, REG_SEG_DS ), PIN_GetContextReg( ctxt, REG_SEG_ES )
);
fprintf(traceFile, "FS : 0x%0.8x GS : 0x%0.8x GFLAGS: 0x%0.8x IP: 0x%0.8x\n",
PIN_GetContextReg( ctxt, REG_SEG_FS ), PIN_GetContextReg( ctxt, REG_SEG_GS ),
PIN_GetContextReg( ctxt, REG_GFLAGS ), PIN_GetContextReg( ctxt, REG_INST_PTR )
);
fprintf(traceFile, "\n");
}
static void
logEnd(void)
{
fflush(traceFile);
fclose(traceFile);
}
static void OnException(THREADID threadIndex,
CONTEXT_CHANGE_REASON reason,
const CONTEXT *ctxtFrom,
CONTEXT *ctxtTo,
INT32 info,
VOID *v)
{
if (reason != CONTEXT_CHANGE_REASON_EXCEPTION)
return;
UINT32 exceptionCode = info;
ADDRINT address = PIN_GetContextReg(ctxtFrom, REG_INST_PTR);
// Depending on the system and CRT version, C++ exceptions can be implemented
// as kernel- or user-mode- exceptions.
// This callback does not not intercept user mode exceptions, so we do not
// log C++ exceptions to avoid difference in output files.
if ((exceptionCode >= 0xc0000000) && (exceptionCode <= 0xcfffffff))
{
logStart(exceptionCode);
// logInstructions();
logBBL(address);
logContext(ctxtFrom);
logEnd();
PIN_ExitProcess(-1);
}
}
static void
basicBlockLogger(BasicBlock * bbl)
{
currentBBL = bbl;
return;
}
void Trace(TRACE trace, VOID *)
{
for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl)) {
BasicBlock * basicBlock = new BasicBlock();
for (INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins)) {
basicBlock->addInstruction(new Instruction(ins));
}
BBL_InsertCall(bbl, IPOINT_BEFORE,
(AFUNPTR) basicBlockLogger,
IARG_PTR, basicBlock,
IARG_END
);
}
}
void TraceInst(INS ins, VOID*)
{
// The address is a legit address, meaning it is probably not part of
// any shellcode. In this case we just log the instruction to dump it
// later to show when control flow was transfered from legit code to
// shellcode.
legitInstructions.push_back(dumpInstruction(ins));
if (legitInstructions.size() > maxInstructions)
{
legitInstructions.pop_front();
}
}
VOID Fini(INT32 code, VOID *v)
{
}
int main(INT32 argc, CHAR **argv)
{
PIN_InitSymbols();
PIN_Init(argc, argv);
traceFile = fopen(outputFile.Value().c_str(), "wb+");
PIN_AddContextChangeFunction(OnException, 0);
TRACE_AddInstrumentFunction(Trace, 0);
// PIN_AddFiniFunction(Fini, 0);
// Never returns
PIN_StartProgram();
return 0;
}