3 #include "LocalHook.hpp"
8 template<
typename Fn,
class C = NoClass>
36 bool Hook(
void** ppVtable,
int index, hktype hkPtr,
bool copyVtable =
false,
int vtableLen = 0 )
38 AsmJitHelper jmpToHook;
43 this->_type = HookType::VTable;
44 this->_callOriginal = this->_original = (*(
void***)ppVtable)[index];
45 this->_callback = hkPtr;
47 this->_ppVtable = ppVtable;
48 this->_pVtable = *ppVtable;
49 this->_vtIndex = index;
50 this->_vtCopied = copyVtable;
55 jmpToHook->mov( asmjit::host::rax, (uint64_t)
this );
56 jmpToHook->mov( asmjit::host::qword_ptr_abs( 0x28 ).setSegment( asmjit::host::gs ), asmjit::host::rax );
59 jmpToHook->mov( asmjit::host::dword_ptr_abs( 0x14 ).setSegment( asmjit::host::fs ), (uint32_t)
this );
62 jmpToHook->jmp( (asmjit::Ptr)this->_internalHandler );
63 jmpToHook->relocCode( this->_buf );
69 memset( &ccpad, 0xCC,
sizeof(ccpad) );
74 memcpy( this->_buf + 0x300, *ppVtable, vtableLen *
sizeof(
void* ) );
76 else for (;; vtableLen++)
78 if ((*(
void***)ppVtable)[vtableLen] ==
nullptr ||
79 (*(
void***)ppVtable)[vtableLen] == (
void**)ccpad)
81 memcpy( this->_buf + 0x300, *ppVtable, vtableLen *
sizeof(
void* ) );
87 ((
void**)this->_buf + 0x300 /
sizeof( uintptr_t ))[index] = this->_buf;
88 *ppVtable = this->_buf + 0x300;
95 VirtualProtect( *(uintptr_t**)ppVtable + index,
sizeof(
void*), PAGE_EXECUTE_READWRITE, &flOld );
96 (*(
void***)ppVtable)[index] = this->_buf;
97 VirtualProtect( *(uintptr_t**)ppVtable + index,
sizeof(
void*), flOld, &flOld );
100 return (this->_hooked =
true);
114 bool Hook(
void** ppVtable,
int index, hktypeC hkPtr, C* pClass,
bool copyVtable =
false,
int vtableLen = 0 )
116 this->_callbackClass = pClass;
117 return Hook( ppVtable, index, brutal_cast<hktype>(hkPtr), copyVtable, vtableLen );
131 *this->_ppVtable = this->_pVtable;
137 VirtualProtect( *(uintptr_t**)this->_ppVtable + this->_vtIndex,
sizeof(
void* ), PAGE_EXECUTE_READWRITE, &flOld );
138 (*(
void***)this->_ppVtable)[this->_vtIndex] = this->_original;
139 VirtualProtect( *(uintptr_t**)this->_ppVtable + this->_vtIndex,
sizeof(
void* ), flOld, &flOld );
142 this->_hooked =
false;
147 bool _vtCopied =
false;
148 void** _ppVtable =
nullptr;
149 void* _pVtable =
nullptr;
bool Hook(void **ppVtable, int index, hktype hkPtr, bool copyVtable=false, int vtableLen=0)
Hook function in vtable
Definition: VTableHook.hpp:36
bool Restore()
Restore hooked function
Definition: VTableHook.hpp:124
bool Hook(void **ppVtable, int index, hktypeC hkPtr, C *pClass, bool copyVtable=false, int vtableLen=0)
Hooks function in vtable
Definition: VTableHook.hpp:114
Definition: LocalHook.hpp:10
Definition: HookHandlers.h:10
Definition: VTableHook.hpp:9
Definition: AsmHelper32.cpp:6