Skip to content

Commit 1074c95

Browse files
committed
couple of DBI examples
1 parent 63c0565 commit 1074c95

22 files changed

+107166
-0
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
02-Jul-2018: DBI examples: minesweeper and WinRAR
12
15-Sep-2016: More fundamentals and examples
23
14-Sep-2016: More of my blog posts are copypasted into the book
34
06-Sep-2016: Blog posts about FAT12 and fortune file has been copypasted into the book

DBI/XOR/files/Rar.exe

583 KB
Binary file not shown.

DBI/XOR/files/XOR_ins.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// based on emudiv.cpp
2+
3+
// by dennis(a)yurichev.com
4+
5+
#include <stdio.h>
6+
#include "pin.H"
7+
#include <iostream>
8+
#include <fstream>
9+
10+
std::ofstream TraceFile;
11+
12+
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
13+
"o", "XOR_ins.out", "specify trace file name");
14+
15+
std::map<ADDRINT, int> XORs;
16+
PIN_LOCK lock;
17+
18+
// ==== these functions executed during runtime (begin) ====
19+
VOID log_info (ADDRINT ip, ADDRINT op1, ADDRINT op2)
20+
{
21+
if (op1!=op2 && op1!=0 && op2!=0 && op1!=0xffffffff && op2!=0xffffffff)
22+
{
23+
//TraceFile << "ip=" << ip << " op1=" << op1 << " op2=" << op2 << endl;
24+
XORs[ip]=XORs[ip]+1;
25+
};
26+
};
27+
28+
VOID XOR_reg_reg(ADDRINT ip, ADDRINT op1, ADDRINT op2, THREADID threadid)
29+
{
30+
PIN_GetLock(&lock, threadid+1);
31+
log_info (ip, op1, op2);
32+
PIN_ReleaseLock(&lock);
33+
}
34+
35+
VOID XOR_mem_reg(ADDRINT ip, ADDRINT *op1_addr, unsigned int op1_size, ADDRINT op2, THREADID threadid)
36+
{
37+
PIN_GetLock(&lock, threadid+1);
38+
ADDRINT op1;
39+
PIN_SafeCopy(&op1, op1_addr, op1_size);
40+
log_info (ip, op1, op2);
41+
PIN_ReleaseLock(&lock);
42+
};
43+
44+
// save stat, do not track registers (yet)
45+
VOID PXOR(ADDRINT ip, THREADID threadid)
46+
{
47+
PIN_GetLock(&lock, threadid+1);
48+
//TraceFile << "PXOR at " << ip << endl;
49+
XORs[ip]=XORs[ip]+1;
50+
PIN_ReleaseLock(&lock);
51+
}
52+
// ==== these functions executed during runtime (end) ====
53+
54+
// this function executed only during startup, so no need to optimize anything here:
55+
VOID InstrumentXOR(INS ins, VOID* v)
56+
{
57+
// XOR reg, reg
58+
if ((INS_Mnemonic(ins) == "XOR") && (INS_OperandIsReg(ins, 0)) && (INS_OperandIsReg(ins, 1)))
59+
{
60+
INS_InsertCall(ins,
61+
IPOINT_BEFORE,
62+
AFUNPTR(XOR_reg_reg),
63+
IARG_INST_PTR,
64+
IARG_REG_VALUE, REG(INS_OperandReg(ins, 0)),
65+
IARG_REG_VALUE, REG(INS_OperandReg(ins, 1)),
66+
IARG_THREAD_ID,
67+
IARG_END);
68+
}
69+
70+
// XOR mem, reg
71+
if ((INS_Mnemonic(ins) == "XOR") && INS_OperandIsMemory(ins, 0) && INS_OperandIsReg(ins, 1))
72+
{
73+
INS_InsertCall(ins,
74+
IPOINT_BEFORE,
75+
AFUNPTR(XOR_mem_reg),
76+
IARG_INST_PTR,
77+
IARG_MEMORYREAD_EA,
78+
IARG_MEMORYREAD_SIZE,
79+
IARG_REG_VALUE, REG(INS_OperandReg(ins, 1)),
80+
IARG_THREAD_ID,
81+
IARG_END);
82+
}
83+
84+
// XOR reg, mem
85+
if ((INS_Mnemonic(ins) == "XOR") && INS_OperandIsReg(ins, 0) && INS_OperandIsMemory(ins, 1))
86+
{
87+
INS_InsertCall(ins,
88+
IPOINT_BEFORE,
89+
AFUNPTR(XOR_mem_reg),
90+
IARG_INST_PTR,
91+
IARG_MEMORYREAD_EA,
92+
IARG_MEMORYREAD_SIZE,
93+
IARG_REG_VALUE, REG(INS_OperandReg(ins, 0)),
94+
IARG_THREAD_ID,
95+
IARG_END);
96+
}
97+
98+
if ((INS_Mnemonic(ins) == "PXOR"))
99+
{
100+
INS_InsertCall(ins,
101+
IPOINT_BEFORE,
102+
AFUNPTR(PXOR),
103+
IARG_INST_PTR,
104+
IARG_THREAD_ID,
105+
IARG_END);
106+
}
107+
}
108+
109+
INT32 Usage()
110+
{
111+
cerr << "This tool intercepts XOR/PXOR" << endl;
112+
cerr << KNOB_BASE::StringKnobSummary() << endl << flush;
113+
return -1;
114+
}
115+
116+
VOID Fini(INT32 code, VOID *v)
117+
{
118+
for (auto i=XORs.begin(); i!=XORs.end(); i++)
119+
TraceFile << "ip=" << i->first << " count=" << i->second << endl;
120+
TraceFile.close();
121+
}
122+
123+
int main(int argc, char * argv[])
124+
{
125+
if (PIN_Init(argc, argv))
126+
return Usage();
127+
128+
TraceFile.open(KnobOutputFile.Value().c_str());
129+
130+
TraceFile << std::hex << std::showbase;
131+
INS_AddInstrumentFunction(InstrumentXOR, 0);
132+
PIN_AddFiniFunction(Fini, 0);
133+
PIN_StartProgram();// Never returns
134+
135+
return 0;
136+
}

DBI/XOR/files/XOR_ins.diff

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
372,377c381,386
2+
< ip=0x140011902 count=0x2
3+
< ip=0x140011d31 count=0x2
4+
< ip=0x140011e75 count=0x1
5+
< ip=0x140012186 count=0x1
6+
< ip=0x140012b5a count=0x1
7+
< ip=0x140012f1f count=0x1
8+
---
9+
> ip=0x140011902 count=0x11
10+
> ip=0x140011d31 count=0x11
11+
> ip=0x140011e75 count=0x59
12+
> ip=0x140012186 count=0x59
13+
> ip=0x140012b5a count=0x59
14+
> ip=0x140012f1f count=0x59
15+
394,395c403,404
16+
< ip=0x1400175e2 count=0x9
17+
< ip=0x140017770 count=0x9
18+
---
19+
> ip=0x1400175e2 count=0xa
20+
> ip=0x140017770 count=0xa
21+
398,405c407,414
22+
< ip=0x140017b21 count=0xd84
23+
< ip=0x140017b48 count=0x81f
24+
< ip=0x140017b59 count=0x858
25+
< ip=0x140017b6a count=0xc13
26+
< ip=0x140017b7b count=0xefc
27+
< ip=0x140017b8a count=0xefd
28+
< ip=0x140017b92 count=0xb86
29+
< ip=0x140017ba1 count=0xf01
30+
---
31+
> ip=0x140017b21 count=0x9eab5
32+
> ip=0x140017b48 count=0x79863
33+
> ip=0x140017b59 count=0x862e8
34+
> ip=0x140017b6a count=0x99495
35+
> ip=0x140017b7b count=0xa891c
36+
> ip=0x140017b8a count=0xa89f4
37+
> ip=0x140017b92 count=0x8ed72
38+
> ip=0x140017ba1 count=0xa8a8a
39+
435,445c444,456
40+
< ip=0x140025ede count=0x5
41+
< ip=0x140026000 count=0x5
42+
< ip=0x140026050 count=0x5
43+
< ip=0x14002622e count=0x5
44+
< ip=0x1400277b7 count=0x1
45+
< ip=0x1400278e9 count=0x1
46+
< ip=0x14002c4f1 count=0x4fce
47+
< ip=0x14002c586 count=0x1
48+
< ip=0x14002c73f count=0x1
49+
< ip=0x14002e006 count=0x1
50+
< ip=0x14002e416 count=0x1
51+
---
52+
> ip=0x140025ede count=0x1bd
53+
> ip=0x140026000 count=0x1bd
54+
> ip=0x140026050 count=0x1bd
55+
> ip=0x14002622e count=0x1bd
56+
> ip=0x1400277b7 count=0x55
57+
> ip=0x1400278e9 count=0x55
58+
> ip=0x14002c4f1 count=0x4463be
59+
> ip=0x14002c586 count=0x35
60+
> ip=0x14002c73f count=0x35
61+
> ip=0x14002e006 count=0x55
62+
> ip=0x14002e416 count=0x55
63+
> ip=0x14002e47e count=0x20
64+
> ip=0x14002e677 count=0x20
65+
480,481c491,492
66+
< ip=0x14004104a count=0x367
67+
< ip=0x140041057 count=0x367
68+
---
69+
> ip=0x14004104a count=0x24193
70+
> ip=0x140041057 count=0x24193
71+
497c508
72+
< ip=0x140043e10 count=0x23006
73+
---
74+
> ip=0x140043e10 count=0x23004
75+
499c510
76+
< ip=0x140043e56 count=0x22ffd
77+
---
78+
> ip=0x140043e56 count=0x23002
79+
501c512
80+
< ip=0x140043e95 count=0x23005
81+
---
82+
> ip=0x140043e95 count=0x22ffd
83+
503c514
84+
< ip=0x140043ece count=0x23001
85+
---
86+
> ip=0x140043ece count=0x22fff
87+
505c516
88+
< ip=0x140043f19 count=0x23004
89+
---
90+
> ip=0x140043f19 count=0x22ffe
91+
507c518
92+
< ip=0x140043f3f count=0x23004
93+
---
94+
> ip=0x140043f3f count=0x23003
95+
509c520
96+
< ip=0x140043f83 count=0x22fff
97+
---
98+
> ip=0x140043f83 count=0x23004
99+
511c522
100+
< ip=0x140043fba count=0x2300a
101+
---
102+
> ip=0x140043fba count=0x23001
103+
513c524
104+
< ip=0x140043ffb count=0x23009
105+
---
106+
> ip=0x140043ffb count=0x23003
107+
515c526
108+
< ip=0x140044013 count=0x23008
109+
---
110+
> ip=0x140044013 count=0x22ffc
111+
517c528
112+
< ip=0x14004407c count=0x23002
113+
---
114+
> ip=0x14004407c count=0x23009
115+
519c530
116+
< ip=0x14004409f count=0x23004
117+
---
118+
> ip=0x14004409f count=0x22ffd
119+
521c532
120+
< ip=0x1400440f3 count=0x23009
121+
---
122+
> ip=0x1400440f3 count=0x22fff
123+
523c534
124+
< ip=0x14004412d count=0x23001
125+
---
126+
> ip=0x14004412d count=0x22ffd
127+
525c536
128+
< ip=0x140044146 count=0x22ffc
129+
---
130+
> ip=0x140044146 count=0x22ffe
131+
527c538
132+
< ip=0x14004419c count=0x23002
133+
---
134+
> ip=0x14004419c count=0x23005
135+
529c540
136+
< ip=0x1400441b9 count=0x23003
137+
---
138+
> ip=0x1400441b9 count=0x23006
139+
537c548
140+
< ip=0x140044224 count=0x23005
141+
---
142+
> ip=0x140044224 count=0x23006
143+
542c553
144+
< ip=0x140044247 count=0x23004
145+
---
146+
> ip=0x140044247 count=0x23006
147+
547c558
148+
< ip=0x140044280 count=0x23003
149+
---
150+
> ip=0x140044280 count=0x23007
151+
846,849c857,860
152+
< ip=0x14005bec3 count=0xd
153+
< ip=0x14005c143 count=0xd
154+
< ip=0x14005d19c count=0xa
155+
< ip=0x14005d387 count=0xa
156+
---
157+
> ip=0x14005bec3 count=0xe
158+
> ip=0x14005c143 count=0xe
159+
> ip=0x14005d19c count=0xb
160+
> ip=0x14005d387 count=0xb
161+
862c873
162+
< ip=0x14006292c count=0x5f
163+
---
164+
> ip=0x14006292c count=0x60

0 commit comments

Comments
 (0)