-
Notifications
You must be signed in to change notification settings - Fork 1
/
patcher.cpp
265 lines (234 loc) · 6.74 KB
/
patcher.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
#include <stdlib.h>
#include "patcher.h"
#define DEBUG_PATCH
using namespace std;
Patcher::Patcher(LineProcess* process) : Logger("Patcher")
{
m_process = process;
}
Patcher::~Patcher()
{
}
bool Patcher::patch(uint64_t start)
{
uint64_t ptr = start;
if (
(start >= IMAGE_BASE && start <= (IMAGE_BASE + 0xffffff)) ||
(start >= 0x700000000000))
{
// Line binary or kernel
return true;
}
if (isPatched(ptr))
{
log("patch: Code is already patched: 0x%llx", ptr);
return true;
}
log("patch: Start: 0x%llx", ptr);
PatchRange* range = new PatchRange();
range->start = start;
range->end = start + 1;
m_patchRanges.push_back(range);
while (true)
{
uint8_t* p = (uint8_t*)ptr;
if (*p == 0xcc)
{
log("patch: found patch instruction: 0x%llx", ptr);
if (range->end > ptr)
{
log("patch: But I think the end is beyond 0x%llx", range->end);
if (isPatched(range->end))
{
log("patch: But that's ok because the end is already patched");
}
else
{
std::vector<PatchRange*>::iterator it;
PatchRange* closest = NULL;
for (it = m_patchRanges.begin(); it != m_patchRanges.end(); it++)
{
PatchRange* itrange = *it;
if (itrange->end <= range->end)// && itrange->start >= end)
{
if (closest == NULL || itrange->end > closest->end)
{
closest = itrange;
}
}
}
if (closest != NULL)
{
log("patch: Found closest previous range: 0x%llx-0x%llx", closest->start, closest->end);
return patch(closest->end);
}
else
{
log("patch: No closest range!?");
}
}
}
return false;
}
x86_insn_t insn;
int size = 0;
size = x86_disasm((unsigned char*)ptr, 0x10000, ptr, 0, &insn );
if (size <= 0)
{
log("0x%llx: Invalid instruction", ptr);
exit(255);
return false;
}
uint64_t end = range->end;
if (ptr + size > range->end)
{
range->end = ptr + size;
}
#if 0
log("patch: 0x%llx-0x%llx: 0x%llx", range->start, range->end, ptr);
#endif
#if 1
char line[4096];
//int i;
if (x86_format_insn(&insn, line, 4096, att_syntax) <= 0 )
{
log("0x%x: Unable to format instruction", ptr);
exit(255);
return false;
}
log("0x%llx: %s", ptr, line);
#endif
if (insn.type == insn_return)
{
if (end <= ptr)
{
#ifdef DEBUG_PATCH
log("patch: %p: Found end of function");
#endif
break;
}
}
else if (insn.type == insn_syscall)
{
#ifdef DEBUG_PATCH
log("patch: 0x%llx: Patching SYSCALL", ptr);
#endif
patch(PATCH_SYSCALL, insn, ptr);
}
else if (insn.type == insn_jmp || insn.type == insn_jcc|| insn.type == insn_call)
{
const char* insntype;
if (insn.type == insn_jmp || insn.type == insn_jcc)
{
insntype = "BRANCH";
}
else
{
insntype = "CALL";
}
//log("patch: 0x%llx: %s: operand_count=%d", ptr, insntype, insn.operand_count);
x86_op_t* target = x86_get_branch_target(&insn);
if (target->datatype != op_byte )
{
// FAR !
#ifdef DEBUG_PATCH
log("patch: 0x%llx: -> Patching %s...", ptr, insntype);
#endif
patch(PATCH_CALL, insn, ptr);
if (insn.type == insn_jmp && end < ptr)
{
break;
}
}
else
{
uint64_t destAddr = insn.addr + insn.size + target->data.sbyte;
#ifdef DEBUG_PATCH
log("patch: 0x%llx: %s: near branch to 0x%llx", ptr, insntype, destAddr);
#endif
if (range->end < destAddr)
{
range->end = destAddr;
}
else if (destAddr < start)
{
#ifdef DEBUG_PATCH
log("patch: Jump to 0x%llx is before this range");
#endif
bool patched = isPatched(destAddr);
#ifdef DEBUG_PATCH
log("patch: -> isPatched=%d", patched);
#endif
if (!patched)
{
patch(destAddr);
}
}
else if (p[size] == 0 && p[size + 1] == 0)
{
log("patch: FUNCTION END??");
break;
}
}
}
else if (insn.prefix & op_fs_seg)
{
if (insn.type != insn_nop)
{
log("patch: 0x%llx: Patching FS instruction", ptr);
patch(PATCH_FS, insn, ptr);
}
}
ptr += size;
}
log("patch: Patched range: 0x%llx-0x%llx", start, ptr);
return true;
}
void Patcher::patch(PatchType type, x86_insn_t insn, uint64_t pos)
{
uint8_t* p = (uint8_t*)pos;
uint8_t original = *p;
*p = 0xcc;
Patch* patch = new Patch();
patch->type = type;
patch->insn = insn;
patch->patchedByte = original;
m_patches.insert(make_pair(pos, patch));
}
PatchRange* Patcher::findPatchRange(uint64_t ptr)
{
std::vector<PatchRange*>::iterator it;
for (it = m_patchRanges.begin(); it != m_patchRanges.end(); it++)
{
PatchRange* range = *it;
if (ptr >= range->start && ptr < range->end)
{
return range;
}
}
return NULL;
}
bool Patcher::isPatched(uint64_t ptr)
{
std::vector<PatchRange*>::iterator it;
for (it = m_patchRanges.begin(); it != m_patchRanges.end(); it++)
{
PatchRange* range = *it;
if (ptr >= range->start && ptr < range->end)
{
return true;
}
}
return false;
}
Patch* Patcher::getPatch(uint64_t patchedAddr)
{
map<uint64_t, Patch*>::iterator it;
it = m_patches.find(patchedAddr);
if (it == m_patches.end())
{
log("trap: Invalid patch? patchedAddr=%p", patchedAddr);
exit(255);
}
return it->second;
}