-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrop_vec.cpp
405 lines (312 loc) · 11.4 KB
/
rop_vec.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
#include <iostream>
#include <fstream>
#include <list>
#include <sstream>
#include "pin.H"
#define MAX_THREADS 512
#define OUT_FILE "cnt.out"
#define STAT_FILE "stat.out"
//#define CALL_FILE "call.out"
#define TARLIB "/var/services/homes/adabral/ROP/mozilla-build/mozilla-release/obj-x86_64-unknown-linux-gnu/dist/bin/libxul.so"
#define FFI_CALL 0x24a5f40
#define FFI_CALL_UNIX64 0x24a74c8
#define FF64IP 0x24a75a3
#define FFITAR 0x24a60c0
#define FF64END 0x24a7631
#define FFEND 0x24a673c
#define FF64PLT 0x981c80
#define FF64CALL 0x24a7511
#define ANONST 0x7fffbc358000
#define ANONEND 0x7fffbc500000
//ofstream OutFile[MAX_THREADS];
ofstream OutFile;
ofstream Stat;
//ofstream CallFile;
ADDRINT start = 0x0 , end = 0x0;
static UINT64 icount[MAX_THREADS] = {0};
//list<ADDRINT> RetAddrLocs;
UINT64 my_tid = 0;
list<ADDRINT> *data_ar[MAX_THREADS];
bool leaflag = false;
VOID PIN_FAST_ANALYSIS_CALL Count(THREADID tid, ADDRINT cnt)
{
if ( my_tid < tid )
my_tid = tid;
// return;
icount[tid] += cnt;
}
VOID Trace(TRACE trace, VOID *v)
{
for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
BBL_InsertCall(bbl, IPOINT_ANYWHERE, AFUNPTR(Count),
IARG_FAST_ANALYSIS_CALL,
IARG_THREAD_ID,
IARG_UINT32, BBL_NumIns(bbl),
IARG_END);
}
int depth = 10;
VOID Ret(THREADID tid, ADDRINT sp, ADDRINT target, ADDRINT eip, UINT32 push)
{
// cout << "RET " << tid << hex << " " << sp << " " << target << " " << eip << " " << push << endl;
// return;
unsigned int dep = 0;
IMG imgR, imgT;
string retName = "ANON", targetName = "ANON", rR = "unknown", tR = "unknown";
list<ADDRINT> *tdata = data_ar[tid];
list<ADDRINT>::iterator sp_iter;// = (*tdata).find(sp);
list<ADDRINT>::iterator dep_iter;// = (*tdata).find(sp);
for (sp_iter = (*tdata).begin(); sp_iter != (*tdata).end(); sp_iter++) {
++dep;
if ( *sp_iter == sp )
break;
}
--dep;
if (push) {
OutFile << "PUSH FOUND" << endl;
(*tdata).erase((*tdata).begin());
//cout << "RET FROM RET-1" << tid<<endl;
return;
}
if ( target >= (start+FFI_CALL_UNIX64) && target <= (start+FF64END) ) {
OutFile << tid << " RET-2-FF64 " << hex << eip << " " << target << " " << sp << endl;
}
if ( eip >= (start+FFI_CALL_UNIX64) && eip <= (start+FF64END) ) {
OutFile << tid << " RET-FRM-FF64 " << hex << eip << " " << target << " " << sp << endl;
}
/*
if ( eip >= (ANONST) && eip <= (ANONEND) ) {
OutFile << tid << " RET-I " << hex << eip << " " << target << " " << sp << endl;
}
if ( target >= (ANONST) && target <= (ANONEND) ) {
OutFile << tid << " RET-T " << hex << eip << " " << target << " " << sp << endl;
}
*/
if (sp_iter == (*tdata).end()) {
// cerr << hex << "ret address not found!! " << sp << " " << *(RetAddrLocs.begin())
PIN_LockClient();
imgR = IMG_FindByAddress((ADDRINT)eip);
imgT = IMG_FindByAddress((ADDRINT)target);
PIN_UnlockClient();
if ( IMG_Valid(imgR) ) {
retName = IMG_Name(imgR);
}
if ( IMG_Valid(imgT) ) {
targetName = IMG_Name(imgT);
}
rR = RTN_FindNameByAddress((ADDRINT)eip);
tR = RTN_FindNameByAddress((ADDRINT)target);
OutFile << tid << hex << "ret address not found!! " << sp << " " << *((*tdata).begin())
<< " " << target << " " << eip << " "<<targetName << " " << retName << " " << tR << " " << rR << endl;
//cout << "RET FROM RET-2" << tid <<endl;
return;
}
if (sp_iter != (*tdata).begin())
OutFile << hex <<"ret address not in the beginning!! " << target <<" "<< eip << " " << sp << " " << *((*tdata).begin()) << " " << dec << dep<< endl;
// depth -= distance((*tdata).begin(), sp_iter) + 1;
depth -= dep;
// OutFile << "Distance " << distance(RetAddrLocs.begin(), sp_iter) << endl;
// for (int i=0; i < depth; i++) cerr << " ";
// cerr << hex << tid << " ret " << sp << " " << target << endl;
// if ( sp_iter == RetAddrLocs.begin() ) {
// RetAddrLocs.erase(sp_iter);
// return;
// }
(*tdata).erase((*tdata).begin(), sp_iter);
(*tdata).erase(sp_iter);
//cout << "RET FROM RET-3" << tid<<endl;
}
VOID Call(THREADID tid, ADDRINT sp, ADDRINT target, ADDRINT eip)
{
// //cout << "CALL " << tid << hex << " " << sp << " " << target << " " << eip << endl;
// return;
// for (int i=0; i < depth; i++) cerr << " ";
// cerr << hex << tid << " call " << sp << " " << target << endl;
list <ADDRINT> *tdata = data_ar[tid];
(*tdata).push_front(sp);
depth++;
if ( target == ( start+FF64PLT ) ) {
OutFile << tid << " C-PLT64 " << hex << eip << " " << target << " " << sp << endl;
}
if ( eip >= (start+FFI_CALL_UNIX64) && eip <= (start+FF64END) ) {
OutFile << tid << " C-FF64 " << hex << eip << " " << target << " " << sp << endl;
}
/*
if ( eip >= (ANONST) && eip <= (ANONEND) ) {
OutFile << tid << " CALL-I " << hex << eip << " " << target << " " << sp << endl;
}
if ( target >= (ANONST) && target <= (ANONEND) ) {
OutFile << tid << " CALL-T " << hex << eip << " " << target << " " << sp << endl;
}
*/
}
VOID MemWrite(THREADID tid, ADDRINT ea)
{
list<ADDRINT> *tdata = data_ar[tid];
list<ADDRINT>::const_iterator sp_iter;// = (*tdata).find(sp);
for (sp_iter = (*tdata).begin(); sp_iter != (*tdata).end(); sp_iter++) {
if ( *sp_iter == ea )
break;
}
if ( sp_iter != (*tdata).end() )
OutFile << tid << hex << "return address overwrite!!! " << ea << endl;
//cout << "RET FROM MEWRITE" << tid<<endl;
}
VOID Branch( THREADID tid, ADDRINT sp, ADDRINT target, ADDRINT eip ) {
if ( target >= (start+FFI_CALL_UNIX64) && (target <= start+FF64END) ) {
OutFile << tid << " BRANCH" << hex <<" "<< eip << " " << target << " " << sp << endl;
leaflag = true;
size_t sizeread;
ADDRINT value;
PIN_LockClient();
sizeread = PIN_SafeCopy(&value, (const VOID *)sp, sizeof(uint64_t));
if ( sizeread != sizeof(uint64_t) )
OutFile << "Incorrect Size read from stack for sp: " << hex << sp << " " << sizeread << endl;
else
OutFile << tid << " Stack " << hex << sp << " Val " << value << endl;
PIN_UnlockClient();
}
if ( target == (start+FF64PLT) )
OutFile << tid << " J-PLT64 " << hex <<" "<< eip << " " << target << " " << sp << endl;
if ( eip >= (start+FFI_CALL_UNIX64) && eip <= (start+FF64END) ) {
OutFile << tid << " J-FF64 " << hex << eip << " " << target << " " << sp << endl;
}
// Reading value on threads stack
/*
if ( eip >= (ANONST) && eip <= (ANONEND) ) {
OutFile << tid << " BRANCH-I " << hex << eip << " " << target << " " << sp << endl;
}
if ( target >= (ANONST) && target <= (ANONEND) ) {
OutFile << tid << " BRANCH-T " << hex << eip << " " << target << " " << sp << endl;
}
*/
}
VOID ImageLoad(IMG img, VOID *v)
{
if ( IMG_Name(img) == TARLIB ) {
// cout << IMG_Name(img) << " " << hex << IMG_LowAddress(img) << " " << IMG_HighAddress(img) << " "
// << IMG_NumRegions(img) << endl;
start = IMG_LowAddress(img);
end = IMG_HighAddress(img);
Stat << hex << start << ":" << end << endl;
cout << hex << start << ":" << end <<" "<< (start + FFI_CALL_UNIX64) << " " << (start+FF64END) << endl;
}
Stat << hex << IMG_Name(img) << " " << IMG_LowAddress(img) << " " << IMG_HighAddress(img) << " " <<endl;// IMG_NumRegions(img) << endl;
}
VOID ThreadStart( THREADID tid, CONTEXT *ctxt, INT32 flags, VOID *v)
{
ADDRINT rsp = PIN_GetContextReg(ctxt, REG_STACK_PTR);
ADDRINT eip = PIN_GetContextReg(ctxt, REG_INST_PTR);
OutFile << dec << tid << hex<< " NTH " << eip << " " << rsp << endl;
}
VOID LeaAdd(THREADID tid, ADDRINT sp, ADDRINT eip, ADDRINT bp )
{
size_t sizeread1, sizeread2, sizeread3;
ADDRINT value, bpval, bp18 = bp + 0x18, bp18val;
PIN_LockClient();
sizeread1 = PIN_SafeCopy(&value, (const VOID *)sp, sizeof(uint64_t));
sizeread2 = PIN_SafeCopy(&bpval, (const VOID *)bp, sizeof(uint64_t));
sizeread3 = PIN_SafeCopy(&bp18val, (const VOID *)bp18, sizeof(uint64_t));
if ( sizeread1 != sizeof(uint64_t) || sizeread2 != sizeof(uint64_t) || sizeread3 != sizeof(uint64_t) )
OutFile << "Incorrect Size read from stack for sp: " << hex << sp << " " << sizeread1 << " " << sizeread2 << endl;
else
OutFile << tid << " Stack " << hex << sp << " Val " << value << " BP " << bp << " BPVAL " << bpval <<
" BP+18 " << bp18 <<" Valbp+18 " << bp18val << endl;
PIN_UnlockClient();
OutFile << dec << tid << " LEA " << hex << eip << " " << sp << " "<< bp << endl;
}
VOID Instruction(INS ins, VOID *v)
{
//if (RTN_Valid(INS_Rtn(ins)) && RTN_Name(INS_Rtn(ins)) == "__SEH_epilog4") {
// cerr << "image " << IMG_Name(SEC_Img(RTN_Sec(INS_Rtn(ins)))) << endl;
//}
if ( leaflag && INS_IsLea(ins) ) {
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(LeaAdd),
IARG_THREAD_ID,
IARG_REG_VALUE, REG_STACK_PTR,
IARG_INST_PTR,
IARG_REG_VALUE, REG_GBP,
IARG_END);
}
if ( INS_IsBranch(ins) && !(INS_IsCall(ins)) && !(INS_IsRet(ins)) ) {
INS_InsertCall(ins, IPOINT_TAKEN_BRANCH, AFUNPTR(Branch),
IARG_THREAD_ID,
IARG_REG_VALUE, REG_STACK_PTR,
IARG_BRANCH_TARGET_ADDR,
IARG_INST_PTR,
IARG_END);
}
else if (INS_IsRet(ins)) {
INS prev = INS_Prev(ins);
//cout<< "CALL TO RET" << endl;
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(Ret),
IARG_THREAD_ID,
IARG_REG_VALUE, REG_STACK_PTR,
IARG_BRANCH_TARGET_ADDR,
IARG_INST_PTR,
IARG_UINT32, (INS_Valid(prev) && INS_Opcode(prev) == XED_CATEGORY_PUSH),
IARG_END);
}
else if (INS_IsCall(ins)) {
//cout << "CALL TO CALL" << endl;
INS_InsertCall(ins, IPOINT_TAKEN_BRANCH, AFUNPTR(Call),
IARG_THREAD_ID,
IARG_REG_VALUE, REG_STACK_PTR,
IARG_BRANCH_TARGET_ADDR,
IARG_INST_PTR,
IARG_END);
}
else if (INS_IsMemoryWrite(ins)) {
//cout<< "CALL TO MEWRITE" << endl;
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(MemWrite),
IARG_THREAD_ID,
IARG_MEMORYWRITE_EA,
IARG_END);
}
}
VOID Fini(INT32 code, VOID *v)
{
// int i;
Stat << "DUMP Count DS" << endl;
for (UINT32 i = 0; i < MAX_THREADS; i++)
Stat << icount[i] << endl;
Stat << "TID = " << my_tid;
/*
for ( i = 0; i < MAX_THREADS; i++) {
OutFile[i].close();
delete(data_ar[i]);
}
*/
OutFile.close();
Stat.close();
// CallFile.close();
// cerr << "SIZE" << RetAddrLocs.size() << endl;
// for (auto ea : RetAddrLocs)
// list<ADDRINT>::const_iterator ret_iter;
// for ( ret_iter = RetAddrLocs.begin(); ret_iter != RetAddrLocs.end(); ret_iter++ )
// cerr << hex << *ret_iter<< endl;
}
int main(int argc, char * argv[])
{
int i = 0;
stringstream fn;
PIN_InitSymbols();
if (PIN_Init(argc, argv))
return 1;
for (i = 0; i<MAX_THREADS; i++) {
// fn.str("");
// fn << "cnt.out" << i;
// OutFile[i].open(fn.str().c_str());
data_ar[i] = new list<ADDRINT>();
}
OutFile.open(OUT_FILE);
Stat.open(STAT_FILE);
// CallFile.open(CALL_FILE);
// Register ThreadStart to be called when a thread starts.
// PIN_AddThreadStartFunction(ThreadStart, 0);
TRACE_AddInstrumentFunction(Trace, 0);
IMG_AddInstrumentFunction(ImageLoad, 0);
INS_AddInstrumentFunction(Instruction, 0);
PIN_AddFiniFunction(Fini, 0);
PIN_StartProgram();
return 0;
}