BlackBone
Windows memory hacking library
 All Classes Functions
RemoteHook.h
1 #pragma once
2 
3 #include "../../Config.h"
4 #include "RemoteContext.hpp"
5 
6 #include "../../Include/Winheaders.h"
7 #include "../../Include/Macro.h"
8 #include "../Threads/Threads.h"
9 
10 #include <map>
11 #include <set>
12 #include <stdint.h>
13 
14 namespace blackbone
15 {
16 
18 {
19 public:
20 
21  // Hook type
22  enum eHookType
23  {
24  int3 = 0, // Default int 3 breakpoint
25  hwbp // Hardware breakpoint
26  };
27 
28  enum eHookFlags
29  {
30  none = 0, // No flags
31  returnHook = 1, // Hook function return
32  };
33 
34  // Hook callback prototype
35  typedef void( *fnCallback )(RemoteContext& context);
36  typedef void( __thiscall* fnClassCallback )(const void* __this, RemoteContext& context);
37 
41  struct HookData
42  {
43  // Callback pointer
44  union callback
45  {
46  struct _classFn
47  {
48  fnClassCallback ptr; // Class member function pointer
49  const void* classPtr; // Class instance
50  };
51 
52  fnCallback freeFn; // Free function pointer
53  _classFn classFn; // Class member pointer
54  };
55 
56  callback onExecute; // Callback called upon address breakpoint
57  callback onReturn; // Callback called upon function return
58  eHookType type; // int 3 or HWBP
59  eHookFlags flags; // Some hooking flags
60  uint8_t oldByte; // Original byte in case of int 3 hook
61  DWORD threadID; // Thread id for HWBP (0 means global hook for all threads)
62  int hwbp_idx; // Index of HWBP if applied to one thread only
63  };
64 
65  typedef std::map<ptr_t, HookData> mapHook;
66  typedef std::map<ptr_t, ptr_t> mapAddress;
67  typedef std::map<ptr_t, bool> setAddresses;
68 
69 public:
70  BLACKBONE_API RemoteHook( class ProcessMemory& memory );
71  BLACKBONE_API ~RemoteHook();
72 
81  BLACKBONE_API inline NTSTATUS Apply( eHookType type, uint64_t ptr, fnCallback newFn, Thread* pThread = nullptr )
82  {
83  return ApplyP( type, ptr, newFn, nullptr, pThread );
84  }
85 
93  BLACKBONE_API inline NTSTATUS AddReturnHook( uint64_t ptr, fnCallback newFn )
94  {
95  return AddReturnHookP( ptr, newFn, nullptr );
96  }
97 
98  // FIXME: Alternative for MinGW
99 #ifdef COMPILER_MSVC
100  template<typename C>
111  inline NTSTATUS Apply( eHookType type, uint64_t ptr, void(C::* newFn)(RemoteContext& ctx), const C& classRef, Thread* pThread = nullptr )
112  {
113  return ApplyP( type, ptr, brutal_cast<fnCallback>(newFn), &classRef, pThread );
114  }
115 
124  template<typename C>
125  inline NTSTATUS AddReturnHook( uint64_t ptr, void(C::* newFn)(RemoteContext& ctx), const C& classRef )
126  {
127  return AddReturnHookP( ptr, brutal_cast<fnCallback>(newFn), &classRef );
128  }
129 #endif // COMPILER_MSVC
130 
135  BLACKBONE_API void Remove( uint64_t ptr );
136 
140  BLACKBONE_API void reset();
141 
142 private:
143 
153  BLACKBONE_API NTSTATUS ApplyP( eHookType type, uint64_t ptr, fnCallback newFn, const void* pClass = nullptr, Thread* pThread = nullptr );
154 
163  BLACKBONE_API NTSTATUS AddReturnHookP( uint64_t ptr, fnCallback newFn, const void* pClass = nullptr );
164 
170  BLACKBONE_API void Restore( const HookData &hook, uint64_t ptr );
171 
176  NTSTATUS EnsureDebug();
177 
181  void EndDebug();
182 
188  static DWORD __stdcall EventThreadWrap( LPVOID lpParam );
189 
194  DWORD EventThread();
195 
201  DWORD OnDebugEvent( const DEBUG_EVENT& DebugEv );
202 
208  DWORD OnBreakpoint( const DEBUG_EVENT& DebugEv );
209 
215  DWORD OnSinglestep( const DEBUG_EVENT& DebugEv );
216 
223  DWORD OnAccessViolation( const DEBUG_EVENT& DebugEv );
224 
234  DWORD StackBacktrace( ptr_t ip, ptr_t sp, Thread& thd, std::vector<std::pair<ptr_t, ptr_t>>& results, int depth = 100 );
235 
236  RemoteHook( const RemoteHook& ) = delete;
237  RemoteHook& operator =( const RemoteHook& ) = delete;
238 
239 private:
240  class ProcessMemory& _memory;
241  class ProcessCore& _core;
242 
243  DWORD _debugPID = 0; // PID of process being debugged
244  HANDLE _hEventThd = NULL; // Debug Event thread
245  BOOL _x64Target = FALSE; // Target is x64 process
246  int _wordSize = 4; // 4 or 8 bytes
247  bool _active = false; // Event thread activity flag
248  mapHook _hooks; // Hooked callbacks
249  setAddresses _repatch; // Pending repatch addresses
250  mapAddress _retHooks; // Hooked return addresses
251 };
252 
253 ENUM_OPS( RemoteHook::eHookFlags )
254 
255 }
Definition: RemoteHook.h:17
BLACKBONE_API NTSTATUS AddReturnHook(uint64_t ptr, fnCallback newFn)
Hook function return This hook will only work if function is hooked normally
Definition: RemoteHook.h:93
Hook descriptor
Definition: RemoteHook.h:41
Definition: ProcessMemory.h:13
BLACKBONE_API void Remove(uint64_t ptr)
Remove existing hook
Definition: RemoteHook.cpp:177
Remote function context during hook breakpoint.
Definition: RemoteContext.hpp:14
BLACKBONE_API void reset()
Stop debug and remove all hooks
Definition: RemoteHook.cpp:644
Thread management
Definition: Thread.h:91
Definition: AsmHelper32.cpp:6
BLACKBONE_API NTSTATUS Apply(eHookType type, uint64_t ptr, fnCallback newFn, Thread *pThread=nullptr)
Hook specified address
Definition: RemoteHook.h:81