-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathropth.cpp
287 lines (226 loc) · 7.56 KB
/
ropth.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
#include <iostream>
#include <fstream>
#include <list>
#include <sstream>
#include "pin.H"
#define MAX_THREADS 512
#define OUT_FILE "ropth.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"
ofstream OutFile;
ofstream Stat;
//ofstream CallFile;
ADDRINT start = 0x0 , end = 0x0;
PIN_LOCK lock;
static UINT64 icount[MAX_THREADS] = {0};
THREADID ids[50000];
//list<ADDRINT> RetAddrLocs;
UINT64 my_tid = 0;
INT32 numThreads = 0;
list<ADDRINT> *data_ar[MAX_THREADS];
std::list<ADDRINT>::iterator sp_iter;
struct thread_data_t {
public:
std::list<ADDRINT> th_list; // To get the rsp per thread
int depth;
public:
thread_data_t(int i) : th_list(), depth(10) {}
};
static TLS_KEY tls_key;
thread_data_t* get_tls(THREADID threadid)
{
thread_data_t* tdata = static_cast<thread_data_t*>(PIN_GetThreadData(tls_key, threadid));
return tdata;
}
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);
}
VOID Ret(THREADID tid, ADDRINT sp, ADDRINT target, ADDRINT eip, UINT32 push)
{
unsigned int dep = 0;
IMG imgR, imgT;
string retName = "ANON", targetName = "ANON", rR = "unknown", tR = "unknown";
thread_data_t* tdata = get_tls(tid);
for (sp_iter = tdata->th_list.begin(); sp_iter != tdata->th_list.end(); sp_iter++) {
++dep;
if ( *sp_iter == sp )
break;
}
if (push) {
OutFile << "PUSH FOUND" << endl;
tdata->th_list.erase(tdata->th_list.begin());
//cout << "RET FROM RET-1" << tid<<endl;
return;
}
if (sp_iter == tdata->th_list.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->th_list.begin())
<< " " << target << " " << eip << " "<<targetName << " " << retName << " " << tR << " " << rR << endl;
//cout << "RET FROM RET-2" << tid <<endl;
return;
}
if (sp_iter != tdata->th_list.begin())
OutFile << tid << hex <<"ret address not in the beginning!! " << target <<" "<< eip << " " << sp <<endl;
// depth -= distance((*tdata).begin(), sp_iter) + 1;
tdata->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->th_list.erase(tdata->th_list.begin(), sp_iter);
tdata->th_list.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;
thread_data_t* tdata = get_tls(tid);
tdata->th_list.push_front(sp);
tdata->depth++;
}
VOID MemWrite(THREADID tid, ADDRINT ea)
{
thread_data_t* tdata = get_tls(tid);
for (sp_iter = tdata->th_list.begin(); sp_iter != tdata->th_list.end(); sp_iter++) {
if ( *sp_iter == ea )
break;
}
if ( sp_iter != tdata->th_list.end() )
OutFile << "return address overwrite!!!" << endl;
//cout << "RET FROM MEWRITE" << tid<<endl;
}
VOID Branch( THREADID tid, ADDRINT sp, ADDRINT target, ADDRINT eip ) {
// if ( target >= start && target <= end) {
// OutFile << "BRANCH" << 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 << IMG_Name(img) << " " << IMG_LowAddress(img) << " " << IMG_HighAddress(img) << " " <<endl;// IMG_NumRegions(img) << endl;
}
VOID Instruction(INS ins, VOID *v)
{
/*
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 ThreadStart(THREADID threadid, CONTEXT *ctxt, INT32 flags, VOID *v)
{
PIN_GetLock(&lock, threadid+1);
cout << "TID: " << threadid << endl;
ids[numThreads] = threadid;
numThreads++;
PIN_ReleaseLock(&lock);
thread_data_t* tdata = new thread_data_t(numThreads-1);
tdata->depth = 10;
PIN_SetThreadData(tls_key, tdata, threadid);
}
VOID Fini(INT32 code, VOID *v)
{
Stat << "DUMP Count DS" << endl;
for (UINT32 i = 0; i < MAX_THREADS; i++)
Stat << icount[i] << endl;
Stat << "TID = " << my_tid;
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[])
{
PIN_InitSymbols();
if (PIN_Init(argc, argv))
return 1;
OutFile.open(OUT_FILE);
Stat.open(STAT_FILE);
// CallFile.open(CALL_FILE);
// Initialize the lock
PIN_InitLock(&lock);
// Obtain a key for TLS storage
tls_key = PIN_CreateThreadDataKey(0);
// Register ThreadStart to be called when a thread starts.
PIN_AddThreadStartFunction(ThreadStart, 0);
// 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;
}