BlackBone
Windows memory hacking library
 All Classes Functions
LocalHook.hpp
1 #pragma once
2 
3 #include "HookHandlers.h"
4 #include "../Process/Process.h"
5 
6 namespace blackbone
7 {
8 
9 template<typename Fn, class C = NoClass>
10 class Detour: public HookHandler<Fn, C>
11 {
12 public:
13  typedef typename HookHandler<Fn, C>::type type;
14  typedef typename HookHandler<Fn, C>::hktype hktype;
15  typedef typename HookHandler<Fn, C>::hktypeC hktypeC;
16 
17 public:
18  Detour()
19  {
20  this->_internalHandler = &HookHandler<Fn, C>::Handler;
21  }
22 
23  ~Detour()
24  {
25  Restore();
26  }
27 
37  bool Hook(
38  type ptr,
39  hktype hkPtr,
40  HookType::e type,
41  CallOrder::e order = CallOrder::HookFirst,
42  ReturnMethod::e retType = ReturnMethod::UseOriginal
43  )
44  {
45  if (this->_hooked)
46  return false;
47 
48  this->_type = type;
49  this->_order = order;
50  this->_retType = retType;
51  this->_callOriginal = this->_original = ptr;
52  this->_callback = hkPtr;
53 
54  if (!AllocateBuffer( reinterpret_cast<uint8_t*>(ptr) ))
55  return false;
56 
57  switch (this->_type)
58  {
59  case HookType::Inline:
60  return HookInline();
61 
62  case HookType::Int3:
63  return HookInt3();
64 
65  case HookType::HWBP:
66  return HookHWBP();
67 
68  default:
69  return false;
70  }
71  }
72 
83  bool Hook(
84  type Ptr,
85  hktypeC hkPtr,
86  C* pClass,
87  HookType::e type,
88  CallOrder::e order = CallOrder::HookFirst,
89  ReturnMethod::e retType = ReturnMethod::UseOriginal
90  )
91  {
92  this->_callbackClass = pClass;
93  return Hook( Ptr, brutal_cast<hktype>(hkPtr), type, order, retType );
94  }
95 
96 
101  bool Restore()
102  {
103  if (!this->_hooked)
104  return false;
105 
106  switch (this->_type)
107  {
108  case HookType::Inline:
109  case HookType::InternalInline:
110  case HookType::Int3:
111  WriteProcessMemory( GetCurrentProcess(), this->_original, this->_origCode, this->_origSize, NULL );
112  break;
113 
114  case HookType::HWBP:
115  {
116  Process thisProc;
117  thisProc.Attach( GetCurrentProcessId() );
118 
119  for (auto& thd : thisProc.threads().getAll())
120  thd.RemoveHWBP( reinterpret_cast<ptr_t>(this->_original) );
121 
122  this->_hwbpIdx.clear();
123  }
124  break;
125 
126  default:
127  break;
128  }
129 
130  this->_hooked = false;
131  return true;
132  }
133 
134 private:
135 
140  bool HookInline()
141  {
142  AsmJitHelper jmpToHook, jmpToThunk;
143 
144  //
145  // Construct jump to thunk
146  //
147 #ifdef USE64
148  jmpToThunk->mov( asmjit::host::rax, (uint64_t)this->_buf );
149  jmpToThunk->jmp( asmjit::host::rax );
150 
151  this->_origSize = jmpToThunk->getCodeSize( );
152 #else
153  jmpToThunk->jmp( (asmjit::Ptr)this->_buf );
154  this->_origSize = jmpToThunk->getCodeSize();
155 #endif
156 
157  DetourBase::CopyOldCode( (uint8_t*)this->_original );
158 
159  // Construct jump to hook handler
160 #ifdef USE64
161  // mov gs:[0x28], this
162  jmpToHook->mov( asmjit::host::rax, (uint64_t)this );
163  jmpToHook->mov( asmjit::host::qword_ptr_abs( 0x28 ).setSegment( asmjit::host::gs ), asmjit::host::rax );
164 #else
165  // mov fs:[0x14], this
166  jmpToHook->mov( asmjit::host::dword_ptr_abs( 0x14 ).setSegment( asmjit::host::fs ) , (uint32_t)this );
167 #endif // USE64
168 
169  jmpToHook->jmp( (asmjit::Ptr)&HookHandler<Fn, C>::Handler );
170  jmpToHook->relocCode( this->_buf );
171 
172  jmpToThunk->setBaseAddress( (uintptr_t)this->_original );
173  auto codeSize = jmpToThunk->relocCode( this->_newCode );
174 
175  DWORD flOld = 0;
176  if (!VirtualProtect( this->_original, codeSize, PAGE_EXECUTE_READWRITE, &flOld ))
177  return false;
178 
179  memcpy( this->_original, this->_newCode, codeSize );
180 
181  VirtualProtect( this->_original, codeSize, flOld, &flOld );
182 
183  return (this->_hooked = codeSize != 0);
184  }
185 
190  bool HookInt3()
191  {
192  this->_newCode[0] = 0xCC;
193  this->_origSize = sizeof( this->_newCode[0] );
194 
195  // Setup handler
196  if (this->_vecHandler == nullptr)
197  this->_vecHandler = AddVectoredExceptionHandler( 1, &DetourBase::VectoredHandler );
198 
199  if (!this->_vecHandler)
200  return false;
201 
202  this->_breakpoints.insert( std::make_pair( this->_original, (DetourBase*)this ) );
203 
204  // Save original code
205  memcpy( this->_origCode, this->_original, this->_origSize );
206 
207  // Write break instruction
208  DWORD flOld = 0;
209  VirtualProtect( this->_original, this->_origSize, PAGE_EXECUTE_READWRITE, &flOld );
210  memcpy( this->_original, this->_newCode, this->_origSize );
211  VirtualProtect( this->_original, this->_origSize, flOld, &flOld );
212 
213  WriteProcessMemory( GetCurrentProcess(), this->_original, this->_newCode, this->_origSize, NULL );
214 
215  return this->_hooked = TRUE;
216  }
217 
222  bool HookHWBP()
223  {
224  Process thisProc;
225  thisProc.Attach( GetCurrentProcessId() );
226 
227  // Setup handler
228  if (this->_vecHandler == nullptr)
229  this->_vecHandler = AddVectoredExceptionHandler( 1, &DetourBase::VectoredHandler );
230 
231  if (!this->_vecHandler)
232  return false;
233 
234  this->_breakpoints.insert( std::make_pair( this->_original, (DetourBase*)this ) );
235 
236  // Add breakpoint to every thread
237  for (auto& thd : thisProc.threads().getAll())
238  this->_hwbpIdx[thd.id()] = thd.AddHWBP( reinterpret_cast<ptr_t>(this->_original), hwbp_execute, hwbp_1 );
239 
240  return this->_hooked = true;
241  }
242 };
243 
244 }
bool Hook(type ptr, hktype hkPtr, HookType::e type, CallOrder::e order=CallOrder::HookFirst, ReturnMethod::e retType=ReturnMethod::UseOriginal)
Hook function
Definition: LocalHook.hpp:37
BLACKBONE_API std::vector< Thread > & getAll(bool dontUpdate=false)
Gets all process threads
Definition: Threads.cpp:49
Definition: Process.h:80
bool Restore()
Restore hooked function
Definition: LocalHook.hpp:101
bool Hook(type Ptr, hktypeC hkPtr, C *pClass, HookType::e type, CallOrder::e order=CallOrder::HookFirst, ReturnMethod::e retType=ReturnMethod::UseOriginal)
Hook function
Definition: LocalHook.hpp:83
Definition: LocalHook.hpp:10
Definition: HookHandlers.h:10
static BLACKBONE_API LONG NTAPI VectoredHandler(PEXCEPTION_POINTERS excpt)
Exception handlers
Definition: LocalHookBase.cpp:185
BLACKBONE_API void CopyOldCode(uint8_t *Ptr)
Copy original function bytes
Definition: LocalHookBase.cpp:110
BLACKBONE_API NTSTATUS Attach(DWORD pid, DWORD access=DEFAULT_ACCESS_P)
Attach to existing process
Definition: Process.cpp:39
Definition: AsmHelper32.cpp:6