forked from Force67/DawnHook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHooking.h
More file actions
761 lines (593 loc) · 16.1 KB
/
Hooking.h
File metadata and controls
761 lines (593 loc) · 16.1 KB
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
* This file is part of the CitizenFX project - http://citizen.re/
*
* See LICENSE and MENTIONS in the root of the source tree for information
* regarding licensing.
*/
#pragma once
#include <stdint.h>
#ifndef IS_FXSERVER
#include <jitasm.h>
#include <memory>
#include <functional>
namespace hook
{
// for link /DYNAMICBASE executables
static ptrdiff_t baseAddressDifference;
//#define OFS 0x180000000
#define OFS 0x140000000
// sets the base address difference based on an obtained pointer
inline void set_base(uintptr_t address)
{
#ifdef _M_IX86
uintptr_t addressDiff = (address - 0x400000);
#elif defined(_M_AMD64)
uintptr_t addressDiff = (address - OFS);
#endif
// pointer-style cast to ensure unsigned overflow ends up copied directly into a signed value
baseAddressDifference = *(ptrdiff_t*)&addressDiff;
}
// sets the base to the process main base
inline void set_base()
{
set_base((uintptr_t)GetModuleHandle(NULL));
}
// adjusts the address passed to the base as set above
template<typename T>
inline void adjust_base(T& address)
{
#if 1
*(uintptr_t*)&address += baseAddressDifference;
#endif
}
// returns the adjusted address to the stated base
template<typename T>
inline uintptr_t get_adjusted(T address)
{
return (uintptr_t)address + baseAddressDifference;
}
struct pass
{
template<typename ...T> pass(T...) {}
};
#pragma region assembly generator
class FunctionAssembly
{
private:
void* m_code;
public:
inline FunctionAssembly(jitasm::Frontend& frontend)
{
frontend.Assemble();
void* code;
code = VirtualAlloc(0, frontend.GetCodeSize(), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
memcpy(code, frontend.GetCode(), frontend.GetCodeSize());
m_code = code;
}
inline ~FunctionAssembly()
{
VirtualFree(m_code, 0, MEM_RELEASE);
}
inline void* GetCode()
{
return m_code;
}
};
#pragma endregion
template<typename ValueType, typename AddressType>
inline void write(AddressType address, ValueType value)
{
adjust_base(address);
memcpy((void*)address, &value, sizeof(value));
}
template<typename ValueType, typename AddressType>
inline void writeVP(AddressType address, ValueType value)
{
adjust_base(address);
DWORD oldProtect;
VirtualProtect((void*)address, sizeof(value), PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy((void*)address, &value, sizeof(value));
VirtualProtect((void*)address, sizeof(value), oldProtect, &oldProtect);
}
template <typename TRet = void, typename TAddr, typename... TArgs>
constexpr inline TRet call(TAddr address, TArgs... args)
{
return reinterpret_cast<TRet(*)(TArgs...)>(address)(args ...);
}
template <typename TRet = void, typename... TArgs>
constexpr inline TRet this_call(TRet address, TArgs... args)
{
return reinterpret_cast<TRet(__thiscall *)(TArgs...)>(address)(args ...);
}
template<typename AddressType>
inline void nop(AddressType address, size_t length)
{
adjust_base(address);
DWORD op;
VirtualProtect((void*)address, length, PAGE_EXECUTE_READWRITE, &op);
memset((void*)address, 0x90, length);
}
template<typename AddressType>
inline void return_function(AddressType address, uint16_t stackSize = 0)
{
if (stackSize == 0)
{
writeVP<uint8_t>(address, 0xC3);
}
else
{
writeVP<uint8_t>(address, 0xC2);
writeVP<uint16_t>((uintptr_t)address + 1, stackSize);
}
}
template<typename T>
inline T* getRVA(uintptr_t rva)
{
#ifdef _M_IX86
return (T*)(baseAddressDifference + 0x400000 + rva);
#elif defined(_M_AMD64)
return (T*)(GetModuleHandle(L"FC_m64.dll") + rva);
#endif
}
namespace
{
template<typename TOrdinal>
inline bool iat_matches_ordinal(uintptr_t* nameTableEntry, TOrdinal ordinal)
{
}
template<>
inline bool iat_matches_ordinal(uintptr_t* nameTableEntry, int ordinal)
{
if (IMAGE_SNAP_BY_ORDINAL(*nameTableEntry))
{
return IMAGE_ORDINAL(*nameTableEntry) == ordinal;
}
return false;
}
template<>
inline bool iat_matches_ordinal(uintptr_t* nameTableEntry, const char* ordinal)
{
if (!IMAGE_SNAP_BY_ORDINAL(*nameTableEntry))
{
auto import = getRVA<IMAGE_IMPORT_BY_NAME>(*nameTableEntry);
return !_stricmp(import->Name, ordinal);
}
return false;
}
}
template<typename T, typename TOrdinal>
T iat(const char* moduleName, T function, TOrdinal ordinal)
{
#ifdef _M_IX86
IMAGE_DOS_HEADER* imageHeader = (IMAGE_DOS_HEADER*)(baseAddressDifference + 0x400000);
#elif defined(_M_AMD64)
IMAGE_DOS_HEADER* imageHeader = (IMAGE_DOS_HEADER*)GetModuleHandle(L"FC_m64.dll");
#endif
IMAGE_NT_HEADERS* ntHeader = getRVA<IMAGE_NT_HEADERS>(imageHeader->e_lfanew);
IMAGE_IMPORT_DESCRIPTOR* descriptor = getRVA<IMAGE_IMPORT_DESCRIPTOR>(ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
while (descriptor->Name)
{
const char* name = getRVA<char>(descriptor->Name);
if (_stricmp(name, moduleName))
{
descriptor++;
continue;
}
if (descriptor->OriginalFirstThunk == 0)
{
return nullptr;
}
auto nameTableEntry = getRVA<uintptr_t>(descriptor->OriginalFirstThunk);
auto addressTableEntry = getRVA<uintptr_t>(descriptor->FirstThunk);
while (*nameTableEntry)
{
if (iat_matches_ordinal(nameTableEntry, ordinal))
{
T origEntry = (T)*addressTableEntry;
DWORD oldProtect;
VirtualProtect(addressTableEntry, sizeof(T), PAGE_READWRITE, &oldProtect);
*addressTableEntry = (uintptr_t)function;
VirtualProtect(addressTableEntry, sizeof(T), oldProtect, &oldProtect);
return origEntry;
}
nameTableEntry++;
addressTableEntry++;
}
return nullptr;
}
return nullptr;
}
#ifndef _M_AMD64
// a context for the hook function to party on
struct HookContext
{
uint32_t m_jumpRet;
uint32_t m_edi, m_esi, m_ebp, m_esp, m_ebx, m_edx, m_ecx, m_eax; // matches pushad format
};
class inject_hook
{
friend struct inject_hook_frontend;
private:
HookContext* m_curContext;
std::shared_ptr<FunctionAssembly> m_assembly;
uintptr_t m_address;
public:
// return value type container
typedef int ReturnType;
private:
// set context and run
ReturnType do_run(HookContext* context)
{
m_curContext = context;
return run();
}
protected:
// return type values for use in return;
// return without jumping anywhere
inline ReturnType DoNowt()
{
return ReturnType(0);
}
// jump to this place after the hook
inline ReturnType JumpTo(uint32_t address)
{
m_curContext->m_eax = address;
return ReturnType(1);
}
// accessors for context registers
inline uint32_t Eax() { return m_curContext->m_eax; }
inline void Eax(uint32_t a) { m_curContext->m_eax = a; }
inline uint32_t Ebx() { return m_curContext->m_ebx; }
inline void Ebx(uint32_t a) { m_curContext->m_ebx = a; }
inline uint32_t Ecx() { return m_curContext->m_ecx; }
inline void Ecx(uint32_t a) { m_curContext->m_ecx = a; }
inline uint32_t Edx() { return m_curContext->m_edx; }
inline void Edx(uint32_t a) { m_curContext->m_edx = a; }
inline uint32_t Esi() { return m_curContext->m_esi; }
inline void Esi(uint32_t a) { m_curContext->m_esi = a; }
inline uint32_t Edi() { return m_curContext->m_edi; }
inline void Edi(uint32_t a) { m_curContext->m_edi = a; }
inline uint32_t Esp() { return m_curContext->m_esp; }
inline void Esp(uint32_t a) { m_curContext->m_esp = a; }
inline uint32_t Ebp() { return m_curContext->m_ebp; }
inline void Ebp(uint32_t a) { m_curContext->m_ebp = a; }
public:
virtual ReturnType run() = 0;
inject_hook(uint32_t address)
{
m_address = address;
}
void inject();
void injectCall();
};
struct inject_hook_frontend : jitasm::Frontend
{
private:
inject_hook* m_hook;
static inject_hook::ReturnType CallHookFunction(inject_hook* hook, HookContext* context)
{
return hook->do_run(context);
}
public:
inject_hook_frontend(inject_hook* hook)
{
m_hook = hook;
}
void InternalMain()
{
// set up the context stack frame
pushad(); // registers
sub(esp, 4); // jump target area
mov(dword_ptr[esp], 0);
// load the context address into eax
lea(eax, dword_ptr[esp]);
// push eax (second argument to our call)
push(eax);
// push the (softcoded, heh) hook function
push((uint32_t)m_hook);
// call the call stub
mov(eax, (uint32_t)CallHookFunction);
call(eax);
// remove garbage from the stack
add(esp, 8);
// do we want to jump somewhere?
test(eax, eax);
jnz("actuallyJump");
// guess not, remove jump target area and popad
add(esp, 4);
popad();
// get esp back from the context bit
mov(esp, dword_ptr[esp - 20]);
ret();
L("actuallyJump");
add(esp, 4);
popad();
mov(esp, dword_ptr[esp - 20]);
AppendInstr(jitasm::I_CALL, 0xFF, 0, jitasm::Imm8(4), R(eax));
}
};
#define DEFINE_INJECT_HOOK(hookName, hookAddress) class _zz_inject_hook_##hookName : public hook::inject_hook { public: _zz_inject_hook_##hookName(uint32_t address) : hook::inject_hook(address) {}; ReturnType run(); }; \
static _zz_inject_hook_##hookName hookName(hookAddress); \
_zz_inject_hook_##hookName::ReturnType _zz_inject_hook_##hookName::run()
#if 0
struct AsmHookStub : jitasm::Frontend
{
private:
hook_function* m_safeFunction;
static bool CallHookFunction(hook_function* function, HookContext& context)
{
return (*function)(context);
}
public:
AsmHookStub(hook_function* function)
{
m_safeFunction = function;
}
void InternalMain()
{
// set up the context stack frame
pushad(); // registers
sub(esp, 4); // jump target area
mov(dword_ptr[esp], 0);
// load the jump target into eax
lea(eax, dword_ptr[esp]);
// push eax (second argument to our call)
push(eax);
// push the (softcoded, heh) hook function
push((uint32_t)m_safeFunction);
// call the call stub
mov(eax, (uint32_t)CallHookFunction);
call(eax);
// remove garbage from the stack
add(esp, 8);
// check if we want to execute the original trampoline
test(al, al);
jnz("noTrampolineReturn");
L("noTrampolineReturn");
}
};
#endif
template<typename T, typename AT>
inline void jump(AT address, T func)
{
writeVP<uint8_t>(address, 0xE9);
writeVP<int>((uintptr_t)address + 1, (intptr_t)func - (intptr_t)get_adjusted(address) - 5);
}
template<typename T, typename AT>
inline void put_call(AT address, T func)
{
writeVP<uint8_t>(address, 0xE8);
writeVP<int>((uintptr_t)address + 1, (intptr_t)func - (intptr_t)get_adjusted(address) - 5);
}
template<typename T>
inline T get_call(T address)
{
intptr_t target = *(uintptr_t*)(get_adjusted(address) + 1);
target += (get_adjusted(address) + 5);
return (T)target;
}
template<typename TTarget, typename T>
inline void set_call(TTarget* target, T address)
{
*(T*)target = get_call(address);
}
namespace vp
{
template<typename T, typename AT>
inline void jump(AT address, T func)
{
putVP<uint8_t>(address, 0xE9);
putVP<int>((uintptr_t)address + 1, (intptr_t)func - (intptr_t)get_adjusted(address) - 5);
}
template<typename T, typename AT>
inline void call(AT address, T func)
{
putVP<uint8_t>(address, 0xE8);
putVP<int>((uintptr_t)address + 1, (intptr_t)func - (intptr_t)get_adjusted(address) - 5);
}
}
#pragma region inject call: call stub
template<typename R, typename... Args>
struct CallStub : jitasm::function<void, CallStub<R, Args...>>
{
private:
void* m_target;
public:
CallStub(void* target)
: m_target(target)
{
}
void main()
{
uint32_t stackOffset = 0;
uint32_t argOffset = sizeof(uintptr_t) * 2; // as frame pointers are also kept here
uint32_t argCleanup = 0;
pass{([&]
{
int size = std::min(sizeof(Args), sizeof(uintptr_t));
argOffset += size;
}(), 1)...};
// as this is the end, and the last argument isn't past the end
argOffset -= 4;
pass{([&]
{
mov(eax, dword_ptr[esp + stackOffset + argOffset]);
push(eax);
int size = std::max(sizeof(Args), sizeof(uintptr_t));
stackOffset += size;
argCleanup += size;
argOffset -= size;
}(), 1)...};
mov(eax, (uintptr_t)m_target);
call(eax);
add(esp, argCleanup);
}
};
#pragma endregion
#pragma region inject call
template<typename R, typename... Args>
class inject_call
{
private:
R (*m_origAddress)(Args...);
uintptr_t m_address;
std::shared_ptr<FunctionAssembly> m_assembly;
public:
inject_call(uintptr_t address)
{
if (*(uint8_t*)address != 0xE8)
{
//FatalError("inject_call attempted on something that was not a call. Are you sure you have a compatible version of the game executable? You might need to try poking the guru.");
}
m_address = address;
}
void inject(R (*target)(Args...))
{
CallStub<R, Args...> stub(target);
m_assembly = std::make_shared<FunctionAssembly>(stub);
// store original
int origAddress = *(int*)(m_address + 1);
origAddress += 5;
origAddress += m_address;
m_origAddress = (R(*)(Args...))origAddress;
// and patch
writeVP<int>(m_address + 1, (uintptr_t)m_assembly->GetCode() - (uintptr_t)get_adjusted(m_address) - 5);
}
R call()
{
return ((R(*)())m_origAddress)();
}
R call(Args... args)
{
return m_origAddress(args...);
}
};
#pragma endregion
#else
void* AllocateFunctionStub(void* ptr, int type = 0);
template<typename T>
struct get_func_ptr
{
static void* get(T func)
{
return (void*)func;
}
};
template<typename T, typename AT>
inline void put_ljump(AT address, T func)
{
LPVOID funcStub = AllocateFunctionStub(get_func_ptr<T>::get(func));
writeVP<uint8_t>(address, 0xE9);
writeVP<int>((uintptr_t)address + 1, (intptr_t)funcStub- (intptr_t)get_adjusted(address) - 5);
}
template<typename T, typename AT>
inline void put_ljump_inline(AT address, T func)
{
char* code = (char*)address;
DWORD oldProtect;
VirtualProtect(code, 15, PAGE_EXECUTE_READWRITE, &oldProtect);
*(uint8_t*)code = 0x48;
*(uint8_t*)(code + 1) = 0xb8 | 0;
*(uint64_t*)(code + 2) = (uint64_t)func;
*(uint16_t*)(code + 10) = 0xE0FF | (0 << 8);
*(uint64_t*)(code + 12) = 0xCCCCCCCCCCCCCCCC;
}
template<typename T, typename AT>
inline void jump_rcx(AT address, T func)
{
LPVOID funcStub = AllocateFunctionStub(get_func_ptr<T>::get(func), 1);
write<uint8_t>(address, 0xE9);
write<int>((uintptr_t)address + 1, (intptr_t)funcStub - (intptr_t)get_adjusted(address) - 5);
}
template<int Register, typename T, typename AT>
inline std::enable_if_t<(Register < 8 && Register >= 0)> call_reg(AT address, T func)
{
LPVOID funcStub = AllocateFunctionStub(get_func_ptr<T>::get(func), Register);
writeVP<uint8_t>(address, 0xE8);
writeVP<int>((uintptr_t)address + 1, (intptr_t)funcStub - (intptr_t)get_adjusted(address) - 5);
}
template<typename T, typename AT>
inline void put_call(AT address, T func)
{
call_reg<0>(address, func);
}
template<typename T, typename AT>
inline void call_rcx(AT address, T func)
{
call_reg<1>(address, func);
}
template<typename T, typename TAddr>
inline T get_address(TAddr address)
{
intptr_t target = *(int32_t*)(get_adjusted(address));
target += (get_adjusted(address) + 4);
return (T)target;
}
template<typename T>
inline T get_call(T address)
{
intptr_t target = *(int32_t*)(get_adjusted(address) + 1);
target += (get_adjusted(address) + 5);
return (T)target;
}
template<typename T>
inline T get_doublejmp(T address)
{
return get_call(get_call(address));
}
template<typename T, typename Target, typename AT>
inline void put_fatjmp(AT address, Target *target, T func)
{
target = (decltype(target))get_call(address);
put_ljump_inline(address, func);
}
template<typename TTarget, typename T>
inline void set_call(TTarget* target, T address)
{
*(T*)target = get_call(address);
}
template<typename TTarget, typename T>
inline void set_dcall(TTarget *target, T address)
{
*(T*)target = get_doublejmp(address);
}
inline uintptr_t get_member_internal(void* function)
{
return *(uintptr_t*)function;
}
template<typename T>
inline uintptr_t get_member_old(T function)
{
return ((uintptr_t(*)(T))get_member_internal)(function);
}
template<typename TClass, typename TMember>
inline uintptr_t get_member(TMember TClass::*function)
{
union member_cast
{
TMember TClass::*function;
void* ptr;
};
member_cast cast;
if (sizeof(cast.function) != sizeof(cast.ptr))
{
return get_member_old(function);
}
cast.function = function;
return (uintptr_t)cast.ptr;
}
template<typename TClass, typename TMember>
struct get_func_ptr<TMember TClass::*>
{
static void* get(TMember TClass::* function)
{
return (void*)get_member(function);
}
};
#endif
}
#include "Hooking.Invoke.h"
#include "Hooking.Patterns.h"
#endif
// haha, hell !
namespace nio = hook;