-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranchTargetDetector.cpp
More file actions
287 lines (253 loc) · 7.89 KB
/
Copy pathBranchTargetDetector.cpp
File metadata and controls
287 lines (253 loc) · 7.89 KB
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
/*! @file
* This is an example of the PIN tool that demonstrates some basic PIN APIs
* and could serve as the starting point for developing your first PIN tool
*/
#include "pin.H"
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <map>
#include <set>
namespace WINDOWS
{
#include <Windows.h>
#include <WinCrypt.h>
}
/* ================================================================== */
// Global variables
/* ================================================================== */
class image_data
{
public:
image_data() : hash(""), base(0x0), high(0x0) {};
string hash;
ADDRINT base;
ADDRINT high;
};
static std::ofstream out;
std::map<string, image_data> img_name_to_data;
std::map<string, set<ADDRINT>> img_name_to_valid_branch_locations;
/* ===================================================================== */
// Command line switches
/* ===================================================================== */
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "", "specify file name for MyPinTool output");
/*!
* Print out help message.
*/
INT32 Usage()
{
cerr << "This tool prints out the number of dynamically executed " << endl <<
"instructions, basic blocks and threads in the application." << endl << endl;
cerr << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
VOID ImageLoad(IMG img, VOID *v)
{
if(IMG_Valid(img))
{
string image_name = IMG_Name(img);
ifstream current_image;
current_image.open(image_name, ios::binary);
if(current_image.is_open())
{
current_image.seekg(0, ios::end);
std::streamoff size = current_image.tellg();
char* mem = new char[size];
current_image.seekg(0, ios::beg);
current_image.read(mem, size);
current_image.close();
WINDOWS::DWORD dwStatus = 0;
WINDOWS::PBYTE hash_length = NULL;
WINDOWS::DWORD buffer_size = 16;
WINDOWS::BOOL bResult = FALSE;
WINDOWS::PBYTE hash_buffer;
WINDOWS::HCRYPTPROV hProv = 0;
WINDOWS::HCRYPTHASH hHash = 0;
WINDOWS::CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
WINDOWS::BYTE* b = reinterpret_cast<WINDOWS::BYTE*>(mem);
WINDOWS::CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
WINDOWS::CryptHashData(hHash, b, size, 0);
hash_buffer = new WINDOWS::BYTE[buffer_size];
WINDOWS::CryptGetHashParam(hHash, HP_HASHVAL, hash_buffer, &buffer_size, 0);
std::stringstream ss;
for(int i=0; i<16; i++)
{
ss << std::hex << std::setfill('0') << std::setw(2) << int(hash_buffer[i]);
}
image_data id;
id.hash = ss.str();
id.base = IMG_StartAddress(img);
id.high = IMG_HighAddress(img);
img_name_to_data[image_name] = id;
delete[] hash_buffer;
delete[] mem;
set<ADDRINT> valid_targets;
ADDRINT base = id.base;
for (SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec))
{
if(SEC_Valid(sec) && SEC_IsExecutable(sec))
{
int new_rtn = 0;
for (RTN rtn = SEC_RtnHead(sec); RTN_Valid(rtn); rtn = RTN_Next(rtn))
{
// Prepare for processing of RTN, an RTN is not broken up into BBLs,
// it is merely a sequence of INSs
string rtn_name = RTN_Name(rtn);
//out << image_name << " -- " << rtn_name << std::endl;
RTN_Open(rtn);
bool first = true;
int nops=0;
for (INS ins = RTN_InsHead(rtn); INS_Valid(ins); ins = INS_Next(ins))
{
ADDRINT a = INS_Address(ins);
string d = INS_Disassemble(ins);
if(d == "nop ")
{
nops++;
}
else
{
if(nops >= 5)
{
RTN_Close(rtn);
stringstream ss;
ss << "new_rtn_" << new_rtn;
string new_rtn_name = ss.str();
new_rtn++;
rtn = RTN_CreateAt(a, new_rtn_name);
//out << name << " -- " << new_rtn_name << std::endl;
RTN_Open(rtn);
}
nops = 0;
}
//out << std::hex << a << ": " << std::dec << d << std::endl;
if(first)
{
ADDRINT c = INS_Address(ins);
valid_targets.insert(c - id.base);
first = false;
}
if(INS_IsDirectBranchOrCall(ins))
{
ADDRINT t = INS_DirectBranchOrCallTargetAddress(ins);
if(t >= id.base && t < id.high)
{
valid_targets.insert(t-base);
}
}
if(INS_IsCall(ins))
{
ADDRINT next = INS_NextAddress(ins);
valid_targets.insert(next-base);
}
}
// to preserve space, release data associated with RTN after we have processed it
RTN_Close(rtn);
}
}
}
img_name_to_valid_branch_locations[image_name] = valid_targets;
}
}
}
VOID indirect(ADDRINT target)
{
for(std::map<string, image_data>::iterator it = img_name_to_data.begin(); it != img_name_to_data.end(); it++)
{
string name = it->first;
image_data id = it->second;
if(target >= id.base && target < id.high)
{
img_name_to_valid_branch_locations[name].insert(target-id.base);
break;
}
}
}
VOID InstrumentInstructions(INS ins, VOID *v)
{
ADDRINT c = INS_Address(ins);
IMG i = IMG_FindByAddress(c);
if(IMG_Valid(i))
{
ADDRINT base = IMG_StartAddress(i);
string name = IMG_Name(i);
if(INS_IsCall(ins))
{
ADDRINT next = INS_NextAddress(ins);
img_name_to_valid_branch_locations[name].insert(next-base);
}
if(INS_IsDirectBranchOrCall(ins))
{
ADDRINT t = INS_DirectBranchOrCallTargetAddress(ins);
if(t < base)
{
out << "PROBLEMS" << std::endl;
}
img_name_to_valid_branch_locations[name].insert(t-base);
}
if(INS_IsIndirectBranchOrCall(ins))
{
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(indirect), IARG_BRANCH_TARGET_ADDR, IARG_END);
}
}
}
/*!
* Print out analysis results.
* This function is called when the application exits.
* @param[in] code exit code of the application
* @param[in] v value specified by the tool in the
* PIN_AddFiniFunction function call
*/
VOID Fini(INT32 code, VOID *v)
{
for(std::map<string, std::set<ADDRINT>>::iterator it = img_name_to_valid_branch_locations.begin(); it != img_name_to_valid_branch_locations.end(); it++)
{
set<ADDRINT> s = it->second;
out << "###########################" << std::endl;
out << it->first << std::endl;
out << img_name_to_data[it->first].hash << std::endl;
for(std::set<ADDRINT>::iterator sit = s.begin(); sit != s.end(); sit++)
{
out << *sit << std::endl;
}
out << "###########################" << std::endl;
}
}
/*!
* The main procedure of the tool.
* This function is called when the application image is loaded but not yet started.
* @param[in] argc total number of elements in the argv array
* @param[in] argv array of command line arguments,
* including pin -t <toolname> -- ...
*/
int main(int argc, char *argv[])
{
// Initialize PIN library. Print help message if -h(elp) is specified
// in the command line or the command line is invalid
PIN_InitSymbols();
if( PIN_Init(argc,argv) )
{
return Usage();
}
string fileName = KnobOutputFile.Value();
if (!fileName.empty())
{
out.open(fileName.c_str());
}
else
{
out.open("hash_dump.txt");
}
INS_AddInstrumentFunction(InstrumentInstructions, 0);
IMG_AddInstrumentFunction(ImageLoad, 0);
// Register function to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}
/* ===================================================================== */
/* eof */
/* ===================================================================== */