-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhotpatch.c
482 lines (413 loc) · 12.2 KB
/
hotpatch.c
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/******************************************************************************
* Copyright (c) 2025 Jaroslav Hensl *
* *
* Permission is hereby granted, free of charge, to any person *
* obtaining a copy of this software and associated documentation *
* files (the "Software"), to deal in the Software without *
* restriction, including without limitation the rights to use, *
* copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be *
* included in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, *
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
* OTHER DEALINGS IN THE SOFTWARE. *
* *
******************************************************************************/
#include <windows.h>
#include <stddef.h>
#include <stdint.h>
#include <ddraw.h>
#include <ddrawi.h>
#include <stddef.h>
#include <stdint.h>
#include <math.h>
#include <tlhelp32.h>
#include "vmdahal32.h"
#include "vmhal9x.h"
#include "hotpatch.h"
#include "nocrt.h"
#ifndef P_SIZE
#define P_SIZE 4096
#endif
static int codepos = 0;
static const BYTE patchablecode[] = {0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x8B, 0xFF};
static BYTE codejmp[P_SIZE] __attribute__ ((aligned (P_SIZE))) = {0};
#define PATCHABLE_POS (-5)
#define RETURN_POS 2
#define PATCH_SIZE sizeof(patchablecode)
BOOL patch_init()
{
DWORD old;
return VirtualProtect(&codejmp[0], P_SIZE, PAGE_EXECUTE_READWRITE, &old);
}
#define WRITE_OP_1B(_b) codejmp[codepos] = _b; codepos++;
#define WRITE_OP_DW(_dw) *((DWORD*)&codejmp[codepos]) = _dw; codepos += sizeof(DWORD);
#define RELATIVE_ADDR(_adr) (((DWORD)(_adr)) - ((DWORD)&codejmp[codepos+4]))
#define WRITE_OP_2B(_b1, _b2) WRITE_OP_1B(_b1) WRITE_OP_1B(_b2)
#define WRITE_OP_3B(_b1, _b2, _b3) WRITE_OP_2B(_b1, _b2) WRITE_OP_1B(_b3)
#define WRITE_OP_4B(_b1, _b2, _b3, _b4) WRITE_OP_2B(_b1, _b2) WRITE_OP_2B(_b3, _b4)
void *patch_make_trampoline(const char *libname, const char *procname, patchedAddProc_h proc)
{
void *patch_code = &codejmp[codepos];
//WRITE_OP_4B(0x8b, 0x44, 0x24, 0x04) // mov eax, [esp+4] 4
WRITE_OP_2B(0x89, 0xE0) // mov eax, esp 2
WRITE_OP_3B(0x83, 0xC0, 0x04) // add eax, 0x4 5
WRITE_OP_1B(0x50) // push eax 6
WRITE_OP_1B(0x68) // push 6
WRITE_OP_DW((DWORD)procname) // arg procname 10
WRITE_OP_1B(0x68) // push 11
WRITE_OP_DW((DWORD)libname) // arg libname 15
WRITE_OP_1B(0xE8) // call 16
WRITE_OP_DW(RELATIVE_ADDR(proc)) // rel proc 20
WRITE_OP_2B(0x85, 0xC0) // test eax, eax 22
WRITE_OP_2B(0x74, 0x05) // jz +5 24
WRITE_OP_3B(0x83, 0xc4, 0x04) // add esp, 4 27
WRITE_OP_2B(0xFF, 0xE0) // jmp eax 29
WRITE_OP_1B(0x58) // pop eax 30
WRITE_OP_3B(0x83, 0xC0, RETURN_POS) // add eax, 2 33
WRITE_OP_2B(0xff, 0xE0) // jmp eax 35
//WRITE_OP_1B(0xCC) // align 36
HANDLE hProc = GetCurrentProcess();
FlushInstructionCache(hProc, &codejmp, P_SIZE);
return patch_code;
}
#define BUILD_PATCH(_buf, _trampoline, _original) \
(_buf)[0] = 0xE8; /* call */ \
*((DWORD*)&(_buf)[1]) = ((DWORD)_trampoline) - ((DWORD)_original); /* (+ PATCHABLE_POS + 5) */ \
(_buf)[5] = 0xEB; /* jmp near */ \
(_buf)[6] = 0xF9; /* -7 */
static BOOL patch_install_address(BYTE *original, void *trampoline)
{
BOOL rc = FALSE;
if(trampoline == NULL)
{
TOPIC("PATCH", "trampoline is NULL");
return FALSE;
}
if(memcmp(original+PATCHABLE_POS, patchablecode, PATCH_SIZE) == 0)
{
BYTE patch[PATCH_SIZE];
BUILD_PATCH(patch, trampoline, original);
if((DWORD)original >= 0x80000000)
{
TOPIC("PATCH", "patch shared=0x%X", original);
FBHDA_page_modify((DWORD)(original+PATCHABLE_POS), PATCH_SIZE, patch);
rc = TRUE;
}
else
{
TOPIC("PATCH", "patch local=0x%X", original);
DWORD oldp;
if(VirtualProtect(original+PATCHABLE_POS, PATCH_SIZE, PAGE_EXECUTE_READWRITE, &oldp))
{
memcpy(original+PATCHABLE_POS, &patch[0], PATCH_SIZE);
rc = TRUE;
VirtualProtect(original+PATCHABLE_POS, PATCH_SIZE, oldp, &oldp);
}
}
if(rc)
{
HANDLE hProc = GetCurrentProcess();
FlushInstructionCache(hProc, original+PATCHABLE_POS, 0);
}
return rc;
}
return FALSE;
}
static BOOL patch_uninstall_address(BYTE *original, void *trampoline)
{
BOOL rc = FALSE;
if(trampoline == NULL)
{
TOPIC("PATCH", "trampoline is NULL");
return FALSE;
}
BYTE patch[PATCH_SIZE];
BUILD_PATCH(patch, trampoline, original);
if(memcmp(original+PATCHABLE_POS, patch, PATCH_SIZE) == 0)
{
if((DWORD)original >= 0x80000000)
{
TOPIC("PATCH", "patch shared=0x%X", original);
FBHDA_page_modify((DWORD)(original+PATCHABLE_POS), PATCH_SIZE, patchablecode);
rc = TRUE;
}
else
{
TOPIC("PATCH", "patch local=0x%X", original);
DWORD oldp;
if(VirtualProtect(original+PATCHABLE_POS, PATCH_SIZE, PAGE_EXECUTE_READWRITE, &oldp))
{
memcpy(original+PATCHABLE_POS, &patchablecode[0], PATCH_SIZE);
rc = TRUE;
VirtualProtect(original+PATCHABLE_POS, PATCH_SIZE, oldp, &oldp);
}
}
if(rc)
{
HANDLE hProc = GetCurrentProcess();
FlushInstructionCache(hProc, original+PATCHABLE_POS, 0);
}
return rc;
}
return FALSE;
}
static int patch_validate_address(BYTE *original, void *trampoline)
{
if(memcmp(original+PATCHABLE_POS, patchablecode, PATCH_SIZE) == 0)
{
return PATCH_PATCHABLE;
}
if(trampoline != NULL)
{
BYTE patch[PATCH_SIZE];
BUILD_PATCH(patch, trampoline, original);
if(memcmp(original+PATCHABLE_POS, patch, PATCH_SIZE) == 0)
{
return PATCH_PATCHED;
}
}
return PATCH_NOTAPPLICABLE;
}
#pragma pack(push)
#pragma pack(1)
/* http://www.delorie.com/djgpp/doc/exe/ */
typedef struct EXE_header {
uint16_t signature; /* == 0x5a4D */
uint16_t bytes_in_last_block;
uint16_t blocks_in_file;
uint16_t num_relocs;
uint16_t header_paragraphs;
uint16_t min_extra_paragraphs;
uint16_t max_extra_paragraphs;
uint16_t ss;
uint16_t sp;
uint16_t checksum;
uint16_t ip;
uint16_t cs;
uint16_t reloc_table_offset;
uint16_t overlay_number;
uint16_t e_res[4];
uint16_t e_oemid;
uint16_t e_oeminfo;
uint16_t e_res2[10];
uint32_t e_lfanew;
} EXE_header_t;
typedef struct PE_signature
{
uint16_t signature;
uint16_t zero;
} PE_signature_t;
#define PE_SIGN 0x4550
typedef struct COFF_header
{
uint16_t Machine;
uint16_t NumberOfSections;
uint32_t TimeDateStamp;
uint32_t PointerToSymbolTable;
uint32_t NumberOfSymbols;
uint16_t SizeOfOptionalHeader;
uint16_t Characteristics;
} COFF_header_t;
typedef struct PE_header_small
{
/* Header Standard Fields */
uint16_t Magic;
uint8_t MajorLinkerVersion;
uint8_t MinorLinkerVersion;
uint32_t SizeOfCode;
uint32_t SizeOfInitializedData;
uint32_t SizeOfUninitializedData;
uint32_t AddressOfEntryPoint;
uint32_t BaseOfCode;
uint32_t BaseOfData;
} PE_header_small_t;
#pragma pack(pop)
#define PE32 0x10b
#ifndef IMAGE_FILE_MACHINE_I386
#define IMAGE_FILE_MACHINE_I386 0x14c
#endif
#define SIZE_OF_PE32 224
#define EXE_SIGN 0x5a4D
static long EXE_offset(FILE *f, EXE_header_t *outEXE)
{
long offset = 0;
if(fread(outEXE, sizeof(EXE_header_t), 1, f) == 1)
{
if(outEXE->signature == EXE_SIGN)
{
if(outEXE->e_lfanew == 0)
{
offset = outEXE->blocks_in_file * 512;
if(outEXE->bytes_in_last_block)
{
offset -= (512 - outEXE->bytes_in_last_block);
}
}
else
{
offset = outEXE->e_lfanew;
}
}
}
return offset;
}
static DWORD GetEntryPoint(HMODULE hModule)
{
DWORD result = -1;
if(hModule)
{
char path[MAX_PATH];
if(GetModuleFileNameA(hModule, path, sizeof(path)) != 0)
{
FILE *fr = fopen(path, "rb");
if(fr)
{
EXE_header_t exe;
long offset = EXE_offset(fr, &exe);
if(offset > 0)
{
fseek(fr, offset, SEEK_SET);
PE_signature_t pe_sign;
fread(&pe_sign, sizeof(pe_sign), 1, fr);
if(pe_sign.signature == PE_SIGN && pe_sign.zero == 0)
{
COFF_header_t coff;
fread(&coff, sizeof(COFF_header_t), 1, fr);
if(coff.Machine == IMAGE_FILE_MACHINE_I386)
{
PE_header_small_t pe;
fread(&pe, sizeof(PE_header_small_t), 1, fr);
if(pe.Magic == PE32)
{
result = pe.AddressOfEntryPoint;
}
}
}
}
fclose(fr);
}
}
}
return result;
}
static DWORD GetBaseAddress(HMODULE hModule)
{
DWORD result = -1;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0);
MODULEENTRY32 modentry = { 0 };
if(hSnapshot != NULL)
{
modentry.dwSize = sizeof(MODULEENTRY32);
Module32First(hSnapshot, &modentry);
do
{
if(modentry.hModule == hModule)
{
result = (DWORD)modentry.modBaseAddr;
break;
}
} while(Module32Next(hSnapshot, &modentry));
CloseHandle(hSnapshot);
}
return result;
}
BOOL patch_install(const char *libname, const char *procname, void *trampoline)
{
HMODULE dll = GetModuleHandleA(libname);
BOOL rc = FALSE;
if(dll != NULL)
{
void *addr = NULL;
if(procname != NULL)
{
addr = GetProcAddress(dll, procname);
}
else
{
DWORD base = GetBaseAddress(dll);
DWORD entry = GetEntryPoint(dll);
if(base != -1 && entry != -1)
{
addr = (void*)(base + entry);
}
}
if(addr)
{
rc = patch_install_address(addr, trampoline);
}
else
{
TRACE("PATCH", "addr is NULL!");
}
}
return rc;
}
BOOL patch_uninstall(const char *libname, const char *procname, void *trampoline)
{
HMODULE dll = GetModuleHandleA(libname);
BOOL rc = FALSE;
if(dll != NULL)
{
void *addr = NULL;
if(procname != NULL)
{
addr = GetProcAddress(dll, procname);
}
else
{
DWORD base = GetBaseAddress(dll);
DWORD entry = GetEntryPoint(dll);
if(base != -1 && entry != -1)
{
addr = (void*)(base + entry);
}
}
if(addr)
{
rc = patch_uninstall_address(addr, trampoline);
}
else
{
TRACE("PATCH", "addr is NULL!");
}
}
return rc;
}
int patch_validate(const char *libname, const char *procname, void *trampoline)
{
HMODULE dll = GetModuleHandleA(libname);
int rc = PATCH_NOTAPPLICABLE;
if(dll != NULL)
{
void *addr = NULL;
if(procname != NULL)
{
addr = GetProcAddress(dll, procname);
}
else
{
DWORD base = GetBaseAddress(dll);
DWORD entry = GetEntryPoint(dll);
if(base != -1 && entry != -1)
{
addr = (void*)(base + entry);
}
}
if(addr)
{
rc = patch_validate_address(addr, trampoline);
}
}
return rc;
}