-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchstring.cpp
83 lines (67 loc) · 2.54 KB
/
watchstring.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
#include "pin.H"
#include <iostream>
#include <cstring>
#include <fstream>
// Launch it with ./pin -pid 43242 -t watchstring.so -s "UniqueString" -f "/path/to/logfile.txt"
// https://www.youtube.com/watch?v=A3iRjGdyoLo
// Download library https://www.intel.com/content/www/us/en/developer/articles/tool/pin-a-dynamic-binary-instrumentation-tool.html
std::ofstream logFile;
KNOB<std::string> TargetString(KNOB_MODE_WRITEONCE, "pintool", "s", "", "target string to detect");
KNOB<std::string> LogFilePath(KNOB_MODE_WRITEONCE, "pintool", "f", "", "path to the log file");
VOID WriteMem(VOID * ip, VOID * addr, UINT32 size) {
const char* target = TargetString.Value().c_str();
size_t target_length = strlen(target);
for (UINT32 i = 0; i <= size - target_length; i++) {
if (strncmp((char*)addr + i, target, target_length) == 0) {
logFile << "Memory write containing '" << target << "' detected at IP: " << ip << std::endl;
break;
}
}
}
VOID ReadMem(VOID * ip, VOID * addr, UINT32 size) {
const char* target = TargetString.Value().c_str();
size_t target_length = strlen(target);
for (UINT32 i = 0; i <= size - target_length; i++) {
if (strncmp((char*)addr + i, target, target_length) == 0) {
logFile << "Memory read containing '" << target << "' detected at IP: " << ip << std::endl;
break;
}
}
}
VOID Instruction(INS ins, VOID *v) {
if (INS_IsMemoryWrite(ins)) {
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)WriteMem,
IARG_INST_PTR,
IARG_MEMORYWRITE_EA,
IARG_MEMORYWRITE_SIZE,
IARG_END);
}
if (INS_IsMemoryRead(ins)) {
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)ReadMem,
IARG_INST_PTR,
IARG_MEMORYREAD_EA,
IARG_MEMORYREAD_SIZE,
IARG_END);
}
}
int main(int argc, char *argv[]) {
if (PIN_Init(argc, argv)) {
std::cerr << "Initialization error" << std::endl;
return -1;
}
if (TargetString.Value().empty()) {
std::cerr << "You must specify the target string with the -s option." << std::endl;
return -1;
}
logFile.open(LogFilePath.Value().c_str(), std::ios::out | std::ios::app);
if (!logFile.is_open()) {
std::cerr << "Failed to open the log file: " << LogFilePath.Value() << std::endl;
return -1;
}
INS_AddInstrumentFunction(Instruction, 0);
PIN_StartProgram();
logFile.close();
return 0;
}