-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddrIntercept.cpp
More file actions
314 lines (258 loc) · 9.74 KB
/
addrIntercept.cpp
File metadata and controls
314 lines (258 loc) · 9.74 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
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
/*
* Covered by Apache License 2.0
* Copyright (C) 2018 Evgeny Chormonov (en.chormonov@gmail.com)
* For more information https://github.com/ser-mk/AddressIntercept
*/
#include "addrIntercept.h"
#include "logger.h"
#include <fstream>
#include <iostream>
#include <pin.H>
// todo: add magic field
#if defined(TARGET_IA32)
#define FORMAT_PIPE_STRING \
"id:%lld | %s | addr: %p | size: %d | value: 0x%x | st: %s"
#else
#define FORMAT_PIPE_STRING \
"id:%ld | %s | addr: %p | size: %d | value: 0x%lx | st: %s"
#endif
static const size_t NUMBER_ARGS_FROM_PIPE_STRING = 6;
static const std::string COMMAND_LOAD("LOAD");
static const std::string COMMAND_STORE("STORE");
LevelDebug Log::gLevel = _ERROR;
using namespace std;
KNOB<string> KnobInputFifo(KNOB_MODE_WRITEONCE, "pintool", "in", "in.fifo",
"specify file name");
KNOB<string> KnobOutputFifo(KNOB_MODE_WRITEONCE, "pintool", "out", "out.fifo",
"specify file name");
KNOB<int> KnobLevelDebug(KNOB_MODE_WRITEONCE, "pintool", "v", "0",
"specify the current scenario to be checked (0-4)");
static ifstream inFifo;
static ofstream outFifo;
static memoryTranslate *addrMap = NULL;
static sizeMemoryTranslate_t sizeMap = 0;
static uint64_t id = 0;
const size_t BUFFER_SIZE = 100;
char pipeBuffer[BUFFER_SIZE] = {0};
static bool isEntryInMap(const ADDRINT *addr) {
if (addrMap == NULL || sizeMap == 0) {
return false;
}
for (sizeMemoryTranslate_t i = 0; i < sizeMap; i++) {
bool isEntry = addrMap[i].start_addr <= addr;
isEntry &= addrMap[i].end_addr > addr;
if (isEntry) {
return true;
}
}
return false;
}
static ADDRINT *local2remoteAddr(const ADDRINT *addr) {
ADDRINT *remoteAddr = NULL;
for (sizeMemoryTranslate_t i = 0; i < sizeMap; i++) {
bool isEntry = addrMap[i].start_addr <= addr;
isEntry &= addrMap[i].end_addr > addr;
if (isEntry) {
remoteAddr = addr - addrMap[i].start_addr + addrMap[i].reference_addr;
break;
}
}
return remoteAddr;
}
static void sendCommand(const string &command, const ADDRINT *addr,
const UINT32 size, const ADDRINT value) {
const ADDRINT *remAddr = local2remoteAddr(addr);
const int write_size = snprintf(
pipeBuffer, BUFFER_SIZE,
FORMAT_PIPE_STRING,
id, command.c_str(), remAddr, size, value, "");
if (write_size <= 0) {
MAGIC_LOG(_ERROR) << "small pipe buffer: " << BUFFER_SIZE;
// todo: throw error
}
outFifo << pipeBuffer << endl;
}
static bool parseValue(const string &line, ADDRINT &value) {
char command_str[20];
char status_str[20];
static uint64_t id_in = 0;
ADDRINT *addr = NULL;
UINT32 size = 0;
// todo: check size of the line
const int scan_size = sscanf(line.c_str(), FORMAT_PIPE_STRING, &id_in,
command_str, &addr, &size, &value, status_str);
if (scan_size != NUMBER_ARGS_FROM_PIPE_STRING) {
MAGIC_LOG(_ERROR) << "Can't parse value from pipe, success args: "
<< scan_size;
return false;
}
MAGIC_LOG(_DEBUG) << " " << id_in << " " << command_str << " " << addr << " "
<< size << " " << value << " status_str " << status_str;
if (id != id_in) {
MAGIC_LOG(_ERROR) << "wrong id message: " << id_in << " expected: " << id;
return false;
}
// todo: check magic number
return true;
}
static ADDRINT queryValue(const ADDRINT *addr, const UINT32 size) {
std::string line;
sendCommand(COMMAND_LOAD, addr, size, 0);
std::getline(inFifo, line);
ADDRINT value = 0;
if (parseValue(line, value) == false) {
value = 0;
}
return value;
}
// Move a register or literal to memory
static VOID storeReg2Addr(const ADDRINT *addr, const ADDRINT value,
const UINT32 size) {
if (isEntryInMap(addr) == false)
return;
MAGIC_LOG(_DEBUG) << "!s " << addr << " size : " << size
<< " value: " << value;
std::string line;
sendCommand(COMMAND_STORE, addr, size, value);
std::getline(inFifo, line);
ADDRINT remoteValue = 0;
if (parseValue(line, remoteValue) == false) {
MAGIC_LOG(_ERROR) << "error store addr: " << addr;
}
if (remoteValue != value) {
MAGIC_LOG(_ERROR) << "error store addr: " << addr
<< " expected value: " << value
<< " recieve: " << remoteValue;
}
}
// Move from memory to register
static ADDRINT loadAddr2Reg(ADDRINT *addr, UINT32 size) {
ADDRINT value = 0;
if (isEntryInMap(addr)) {
MAGIC_LOG(_DEBUG) << "addr : " << addr << " size : " << size;
value = queryValue(addr, size);
} else {
PIN_SafeCopy(&value, addr, size);
}
return value;
}
static VOID
multiMemAccessStore(const PIN_MULTI_MEM_ACCESS_INFO *multiMemAccessInfo,
const ADDRINT value) {
if (multiMemAccessInfo->numberOfMemops != 1)
return;
const PIN_MEM_ACCESS_INFO *pinMemAccessInfo = &(multiMemAccessInfo->memop[0]);
const ADDRINT *addr = (ADDRINT *)pinMemAccessInfo->memoryAddress;
const UINT32 size = pinMemAccessInfo->bytesAccessed;
if (isEntryInMap(addr) == false)
return;
MAGIC_LOG(_DEBUG) << "addr : " << addr << " value: " << hex << value
<< " size: " << size;
storeReg2Addr(addr, value, size);
}
static memoryTranslate *replaceMemoryMapFun(CONTEXT *context,
AFUNPTR orgFuncptr,
sizeMemoryTranslate_t *size) {
PIN_CallApplicationFunction(context, PIN_ThreadId(), CALLINGSTD_DEFAULT,
orgFuncptr, NULL, PIN_PARG(memoryTranslate *),
&addrMap, PIN_PARG(sizeMemoryTranslate_t *), size,
PIN_PARG_END());
sizeMap = *size;
MAGIC_LOG(_INFO) << "Get memory map | size : " << sizeMap;
return addrMap;
}
/* ===================================================================== */
/* Commandline Switches */
/* ===================================================================== */
static INT32 Usage() {
cout << "Help message\n"
"\n";
cout << KNOB_BASE::StringKnobSummary();
cout << endl;
return -1;
}
/* ===================================================================== */
// Called every time a new image is loaded.
// Look for routines that we want to replace.
static VOID ImageReplace(IMG img, VOID *v) {
RTN freeRtn = RTN_FindByName(img, NAME_MEMORY_MAP_FUNCTION);
if (RTN_Valid(freeRtn)) {
PROTO proto_free =
PROTO_Allocate(PIN_PARG(memoryTranslate *), CALLINGSTD_DEFAULT,
NAME_MEMORY_MAP_FUNCTION,
PIN_PARG(sizeMemoryTranslate_t *), PIN_PARG_END());
RTN_ReplaceSignature(freeRtn, AFUNPTR(replaceMemoryMapFun), IARG_PROTOTYPE,
proto_free, IARG_CONTEXT, IARG_ORIG_FUNCPTR,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_END);
MAGIC_LOG(_INFO) << "!Replaced " << NAME_MEMORY_MAP_FUNCTION
<< " in:" << IMG_Name(img);
} else {
// todo: handle if $NAME_MEMORY_MAP_FUNCTION is absent
}
}
static VOID EmulateLoad(INS ins, VOID *v) {
// Find the instructions that move a value from memory to a register
if ((INS_Opcode(ins) == XED_ICLASS_MOV ||
INS_Opcode(ins) == XED_ICLASS_MOVZX) &&
INS_IsMemoryRead(ins) && INS_OperandIsReg(ins, 0) &&
INS_OperandIsMemory(ins, 1)) {
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(loadAddr2Reg),
IARG_MEMORYREAD_EA, IARG_MEMORYREAD_SIZE, IARG_RETURN_REGS,
INS_OperandReg(ins, 0), IARG_END);
// Delete the instruction
INS_Delete(ins);
}
}
static VOID EmulateStore(INS ins, VOID *v) {
if (INS_Opcode(ins) == XED_ICLASS_MOV && INS_IsMemoryWrite(ins) &&
INS_OperandIsMemory(ins, 0)) {
if (INS_hasKnownMemorySize(ins)) {
if (INS_OperandIsReg(ins, 1)) {
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(multiMemAccessStore),
IARG_MULTI_MEMORYACCESS_EA, IARG_REG_VALUE,
INS_OperandReg(ins, 1), IARG_END);
} else if (INS_OperandIsImmediate(ins, 1)) {
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)multiMemAccessStore,
IARG_MULTI_MEMORYACCESS_EA, IARG_UINT64,
INS_OperandImmediate(ins, 1), IARG_END);
}
} else {
if (INS_OperandIsReg(ins, 1)) {
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(storeReg2Addr),
IARG_MEMORYWRITE_EA, IARG_REG_VALUE,
INS_OperandReg(ins, 1), IARG_MEMORYWRITE_SIZE, IARG_END);
} else if (INS_OperandIsImmediate(ins, 1)) {
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(storeReg2Addr),
IARG_MEMORYWRITE_EA, IARG_UINT64,
INS_OperandImmediate(ins, 1), IARG_UINT32,
IARG_MEMORYWRITE_SIZE, IARG_END);
}
}
}
}
static bool initFifo() {
// Warning: order call open function
outFifo.open(KnobOutputFifo.Value().c_str());
inFifo.open(KnobInputFifo.Value().c_str());
return inFifo.is_open() && inFifo.good() && outFifo.is_open() &&
outFifo.good();
}
int main(int argc, CHAR *argv[]) {
PIN_InitSymbols();
if (PIN_Init(argc, argv)) {
return Usage();
}
Log::setGLevel((LevelDebug)(KnobLevelDebug.Value() & 0xF));
MAGIC_LOG(_INFO) << "Wait OCD client...";
if (initFifo() == false) {
MAGIC_LOG(_ERROR) << "Error open fifo file: " << KnobInputFifo.Value()
<< " or " << KnobOutputFifo.Value();
return -2;
}
IMG_AddInstrumentFunction(ImageReplace, 0);
INS_AddInstrumentFunction(EmulateLoad, 0);
INS_AddInstrumentFunction(EmulateStore, 0);
MAGIC_LOG(_DEBUG) << "Start...";
PIN_StartProgram();
return 0;
}