-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatcher.h
295 lines (236 loc) · 8.13 KB
/
patcher.h
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
#pragma once
#include <new.h>
#include <string.h>
class StaticPatcher
{
private:
static StaticPatcher *ms_head;
using Patcher = void(*)();
Patcher m_func;
StaticPatcher *m_next;
void Run() { m_func(); }
public:
static void Apply();
StaticPatcher(Patcher func);
};
#define STARTPATCHES static StaticPatcher Patcher([]() {
#define ENDPATCHES });
#define NAKED __declspec(naked)
#define WRAPPER NAKED
#define DEPRECATED __declspec(deprecated)
#define EAXJMP(a) { _asm mov eax, a _asm jmp eax }
#define VARJMP(a) { _asm jmp a }
#define WRAPARG(a) UNREFERENCED_PARAMETER(a)
#define RET(n) { __asm push n __asm retn }
#define ASM(name) void __declspec(naked) name(void)
#define NOVMT __declspec(novtable)
#define SETVMT(a) *((uintptr_t *)this) = (uintptr_t)a
#define FIELD(type, var, offset) *(type *)((unsigned char *)var + offset)
#define GETMEM8(offset) *(uint8_t *)(offset)
#define GETMEM16(offset) *(uint16_t *)(offset)
#define GETMEM32(offset) *(uint32_t *)(offset)
#define MEMCMP8(offset, val) (GETMEM8(offset) == (uint8_t)(val))
#define MEMCMP16(offset, val) (GETMEM16(offset) == (uint16_t)(val))
#define MEMCMP32(offset, val) (GETMEM32(offset) == (uint32_t)(val))
#pragma warning(disable : 4731) // -- suppress C4731:"frame pointer register 'ebp' modified by inline assembly code"
#define XCALL(uAddr) \
__asm { mov esp, ebp } \
__asm { pop ebp } \
__asm { mov eax, uAddr } \
__asm { jmp eax }
#define DSCALL(uAddr) \
__asm { mov esp, ebp } \
__asm { pop ebp } \
__asm { jmp dword ptr ds:uAddr }
int Unprotect_internal(void *address, size_t size);
int Protect_internal(void *address, size_t);
template<typename T> __forceinline void Patch(uintptr_t address, T value)
{
Unprotect_internal((void *)address, sizeof(T));
*(T *)address = value;
Protect_internal((void *)address, sizeof(T));
}
__forceinline void PatchByte(uintptr_t address, unsigned char value)
{
Patch(address, value);
}
__forceinline void PatchBytes(uintptr_t address, unsigned char *value, size_t size)
{
Unprotect_internal((void *)address, size);
memcpy((void *)address, value, size);
Protect_internal((void *)address, size);
}
template<size_t size> __forceinline void PatchBytes(uintptr_t address, unsigned char (&value)[size])
{
PatchBytes(address, value, size);
}
__forceinline void PatchString(uintptr_t address, char *value)
{
PatchBytes(address, (unsigned char *)value, strlen(value) + sizeof(char));
}
__forceinline void PatchWideString(uintptr_t address, wchar_t *value)
{
PatchBytes(address, (unsigned char *)value, wcslen(value) * sizeof(wchar_t) + sizeof(wchar_t));
}
__forceinline void ReadBytes(uintptr_t address, void *out, size_t size)
{
memcpy(out, (void *)address, size);
}
__forceinline void SetBytes(uintptr_t address, int value, size_t size)
{
Unprotect_internal((void *)address, size);
memset((void *)address, value, size);
Protect_internal((void *)address, size);
}
__forceinline void Nop(uintptr_t address, size_t count = 1)
{
SetBytes(address, 0x90, count);
}
__forceinline void NopTo(uintptr_t address, uintptr_t to)
{
Nop(address, to - address);
}
enum
{
PATCH_EXISTING,
PATCH_CALL,
PATCH_JUMP,
HOOK_SIZE = 5,
};
template<typename T> __forceinline void InjectHook(uintptr_t address, T hook, int type = PATCH_EXISTING)
{
switch (type)
{
case PATCH_EXISTING:
Unprotect_internal((void *)(address + 1), HOOK_SIZE - 1);
break;
case PATCH_CALL:
Unprotect_internal((void *)address, HOOK_SIZE);
*(unsigned char *)address = 0xE8;
break;
case PATCH_JUMP:
Unprotect_internal((void *)address, HOOK_SIZE);
*(unsigned char *)address = 0xE9;
break;
}
*(ptrdiff_t *)(address + 1) = (uintptr_t)(void *&)hook - address - HOOK_SIZE;
switch (type)
{
case PATCH_EXISTING:
Protect_internal((void *)(address + 1), HOOK_SIZE - 1);
break;
case PATCH_CALL:
case PATCH_JUMP:
Protect_internal((void *)address, HOOK_SIZE);
break;
}
}
__forceinline void PatchJump(uintptr_t address, uintptr_t to)
{
InjectHook(address, to, PATCH_JUMP);
}
__forceinline void ExtractCall(void *dst, uintptr_t a)
{
*(uintptr_t *)dst = (uintptr_t)(*(uintptr_t *)(a + 1) + a + 5);
}
template<typename T> __forceinline void InterceptCall(void *dst, T func, uintptr_t a)
{
ExtractCall(dst, a);
InjectHook(a, func);
}
template<typename T> __forceinline void InterceptVmethod(void *dst, T func, uintptr_t a)
{
*(uintptr_t *)dst = *(uintptr_t *)a;
Patch(a, func);
}
extern volatile uintptr_t patcher_arg;
#define PATCHER_ARG(type) (type)patcher_arg
#define InjectHook_Overload(offset, funcname, type, rettype, ...) \
InjectHook(offset, ( ## rettype ## (*)(__VA_ARGS__))funcname, type)
#define InjectHook_Overload_Member(offset, classname, funcname, type, rettype, ...) \
InjectHook(offset, ( ## rettype ## (classname ## ::*)(__VA_ARGS__))& classname ## :: ## funcname, type)
#define InjectHook_Constructor_Init(offset, classname, type, ...) \
static NAKED void classname ## _Constructor_ ## offset (classname *_) \
{ \
__asm { mov eax, end } \
::new (_) classname(__VA_ARGS__); \
__asm { end: } \
} \
\
static void InjectHook_ ## classname ## _Constructor_ ## offset (void) \
{ \
uintptr_t calladdr = (*(uintptr_t *)((uintptr_t)& classname ## _Constructor_ ## offset + 1)) - 5; \
uintptr_t ctoraddr; \
ExtractCall(&ctoraddr, calladdr); \
InjectHook(offset, ctoraddr, type); \
\
}
#define InjectHook_Constructor(offset, classname) InjectHook_ ## classname ## _Constructor_ ## offset()
#define InjectHook_Destructor_Init(offset, classname, type) \
static NAKED void classname ## _Destructor_ ## offset(classname *_) \
{ \
__asm { mov eax, end } \
_->classname::~classname(); \
__asm { end: } \
} \
\
static void InjectHook_ ## classname ## _Destructor_ ## offset (void) \
{ \
uintptr_t calladdr = (*(uintptr_t *)((uintptr_t)& classname ## _Destructor_ ## offset + 1)) - 5; \
uintptr_t dtoraddr; \
ExtractCall(&dtoraddr, calladdr); \
InjectHook(offset, dtoraddr, type); \
\
}
#define InjectHook_Destructor(offset, classname) InjectHook_ ## classname ## _Destructor_ ## offset()
#define InjectHook_VirtualDestructor_Init(offset, classname, type) \
static NAKED void classname ## _VirtualDestructor_ ## offset (classname *_) \
{ \
__asm { mov eax, end } \
_->classname::~classname(); \
__asm { end: } \
} \
\
static void InjectHook_ ## classname ## _VirtualDestructor_ ## offset (void) \
{ \
uintptr_t calladdr = (*(uintptr_t *)((uintptr_t)& classname ## _VirtualDestructor_ ## offset + 1)) - 5; \
uintptr_t vmthdaddr; \
ExtractCall(&vmthdaddr, calladdr); \
InjectHook(offset, vmthdaddr, type); \
\
}
#define InjectHook_VirtualDestructor(offset, classname) InjectHook_ ## classname ## _VirtualDestructor_ ## offset()
#define InjectHook_VirtualMethod_Init(offset, classname, funcname, type, ...) \
static NAKED void classname ## __ ## funcname ## _ ## offset (classname *_) \
{ \
__asm { mov eax, end } \
_->classname::funcname(__VA_ARGS__); \
__asm { end: } \
} \
\
static void InjectHook_ ## classname ## __ ## funcname ## _ ## offset (void) \
{ \
uintptr_t calladdr = (*(uintptr_t *)((uintptr_t)& classname ## __ ## funcname ## _ ## offset + 1)) - 5; \
uintptr_t vmthdaddr; \
ExtractCall(&vmthdaddr, calladdr); \
InjectHook(offset, vmthdaddr, type); \
\
}
#define InjectHook_VirtualMethod(offset, classname, funcname) InjectHook_ ## classname ## __ ## funcname ## _ ## offset()
#define Patch_VirtualMethod_Init(offset, classname, funcname, ...) \
static NAKED void classname ## __ ## funcname ## _ ## offset (classname *_) \
{ \
__asm { mov eax, end } \
_->classname::funcname(__VA_ARGS__); \
__asm { end: } \
} \
\
static void Patch_ ## classname ## __ ## funcname ## _ ## offset (void) \
{ \
uintptr_t calladdr = (*(uintptr_t *)((uintptr_t)& classname ## __ ## funcname ## _ ## offset + 1)) - 5; \
uintptr_t vmthdaddr; \
ExtractCall(&vmthdaddr, calladdr); \
Patch(offset, vmthdaddr); \
\
}
#define Patch_VirtualMethod(offset, classname, funcname) Patch_ ## classname ## __ ## funcname ## _ ## offset()