BlackBone
Windows memory hacking library
 All Classes Functions
VTableHook.hpp
1 #pragma once
2 
3 #include "LocalHook.hpp"
4 
5 namespace blackbone
6 {
7 
8 template<typename Fn, class C = NoClass>
9 class VTableDetour : public Detour<Fn, C>
10 {
11 public:
12  typedef typename HookHandler<Fn, C>::type type;
13  typedef typename HookHandler<Fn, C>::hktype hktype;
14  typedef typename HookHandler<Fn, C>::hktypeC hktypeC;
15 
16 public:
17  VTableDetour()
18  {
19  }
20 
21  ~VTableDetour()
22  {
23  Restore();
24  }
25 
36  bool Hook( void** ppVtable, int index, hktype hkPtr, bool copyVtable = false, int vtableLen = 0 )
37  {
38  AsmJitHelper jmpToHook;
39 
40  //_order = CallOrder::HookFirst;
41  //_retType = ReturnMethod::UseOriginal;
42 
43  this->_type = HookType::VTable;
44  this->_callOriginal = this->_original = (*(void***)ppVtable)[index];
45  this->_callback = hkPtr;
46  this->_internalHandler = &HookHandler<Fn, C>::Handler;
47  this->_ppVtable = ppVtable;
48  this->_pVtable = *ppVtable;
49  this->_vtIndex = index;
50  this->_vtCopied = copyVtable;
51 
52  // Construct jump to hook handler
53 #ifdef USE64
54  // mov gs:[0x28], this
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 );
57 #else
58  // mov fs:[0x14], this
59  jmpToHook->mov( asmjit::host::dword_ptr_abs( 0x14 ).setSegment( asmjit::host::fs ), (uint32_t)this );
60 #endif // USE64
61 
62  jmpToHook->jmp( (asmjit::Ptr)this->_internalHandler );
63  jmpToHook->relocCode( this->_buf );
64 
65  // Modify VTable copy
66  if (copyVtable)
67  {
68  uintptr_t ccpad;
69  memset( &ccpad, 0xCC, sizeof(ccpad) );
70 
71  // Copy VTable
72  if (vtableLen != 0)
73  {
74  memcpy( this->_buf + 0x300, *ppVtable, vtableLen * sizeof( void* ) );
75  }
76  else for (;; vtableLen++)
77  {
78  if ((*(void***)ppVtable)[vtableLen] == nullptr ||
79  (*(void***)ppVtable)[vtableLen] == (void**)ccpad)
80  {
81  memcpy( this->_buf + 0x300, *ppVtable, vtableLen * sizeof( void* ) );
82  break;
83  }
84  }
85 
86  // Replace pointer to VTable
87  ((void**)this->_buf + 0x300 / sizeof( uintptr_t ))[index] = this->_buf;
88  *ppVtable = this->_buf + 0x300;
89  }
90  // Modify pointer in-place
91  else
92  {
93  DWORD flOld = 0;
94 
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 );
98  }
99 
100  return (this->_hooked = true);
101  }
102 
114  bool Hook( void** ppVtable, int index, hktypeC hkPtr, C* pClass, bool copyVtable = false, int vtableLen = 0 )
115  {
116  this->_callbackClass = pClass;
117  return Hook( ppVtable, index, brutal_cast<hktype>(hkPtr), copyVtable, vtableLen );
118  }
119 
124  bool Restore()
125  {
126  if (!this->_hooked)
127  return false;
128 
129  if (this->_vtCopied)
130  {
131  *this->_ppVtable = this->_pVtable;
132  }
133  else
134  {
135  DWORD flOld = 0;
136 
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 );
140  }
141 
142  this->_hooked = false;
143  return true;
144  }
145 
146 private:
147  bool _vtCopied = false; // VTable was copied
148  void** _ppVtable = nullptr; // Pointer to VTable pointer
149  void* _pVtable = nullptr; // Pointer to VTable
150  int _vtIndex = 0; // VTable function index
151 };
152 
153 }
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