BlackBone
Windows memory hacking library
 All Classes Functions
LocalHookBase.h
1 #pragma once
2 
3 #include "../Config.h"
4 #include "../Include/Winheaders.h"
5 #include "../Asm/AsmHelper.h"
6 #include "../Asm/LDasm.h"
7 #include "../Include/Macro.h"
8 
9 #include <tuple>
10 #include <unordered_map>
11 
12 namespace blackbone
13 {
14 
15 namespace CallOrder
16 {
17 enum e
18 {
19  HookFirst, // Hook called before original function
20  HookLast, // Hook called after original function
21  NoOriginal, // Original function doesn't get called
22 };
23 }
24 
25 namespace HookType
26 {
27  enum e
28  {
29  Inline, // Patch first few bytes
30  Int3, // Place Int3 breakpoint
31  HWBP, // Set hardware breakpoint
32 
33  // Reserved for internal use
34  VTable,
35  InternalInline
36  };
37 }
38 
39 namespace ReturnMethod
40 {
41  enum e
42  {
43  UseNew, // Return value returned by hook
44  UseOriginal // Return original function value
45  };
46 }
47 
49 {
50  typedef std::unordered_map<DWORD, int> mapIdx;
51 
52 public:
53  BLACKBONE_API DetourBase();
54  BLACKBONE_API ~DetourBase();
55 
56 protected:
57 
63  BLACKBONE_API bool AllocateBuffer( uint8_t* nearest );
64 
69  BLACKBONE_API bool DisableHook();
70 
75  BLACKBONE_API bool EnableHook();
76 
83  BLACKBONE_API bool ToggleHBP( int index, bool enable );
84 
89  BLACKBONE_API void CopyOldCode( uint8_t* Ptr );
90 
96  BLACKBONE_API static LONG NTAPI VectoredHandler ( PEXCEPTION_POINTERS excpt );
97  BLACKBONE_API static LONG NTAPI Int3Handler ( PEXCEPTION_POINTERS excpt );
98  BLACKBONE_API static LONG NTAPI AVHandler ( PEXCEPTION_POINTERS excpt );
99  BLACKBONE_API static LONG NTAPI StepHandler ( PEXCEPTION_POINTERS excpt );
100 
101 protected:
102  bool _hooked = false; // Hook is installed
103 
104  void* _callback = nullptr; // User supplied hook function
105  void* _callbackClass = nullptr; // Class pointer for user hook
106  void* _original = nullptr; // Original function address
107  void* _internalHandler = nullptr; // Pointer to hook handler
108  void* _callOriginal = nullptr; // Pointer to original function
109 
110  mapIdx _hwbpIdx; // Thread HWBP index
111  size_t _origSize = 0; // Original code size
112  uint8_t* _buf = nullptr; // Trampoline buffer
113  uint8_t* _origCode = nullptr; // Original function bytes
114  uint8_t* _newCode = nullptr; // Trampoline bytes
115 
116  HookType::e _type = HookType::Inline;
117  CallOrder::e _order = CallOrder::HookFirst;
118  ReturnMethod::e _retType = ReturnMethod::UseOriginal;
119 
120  // Global hook instances relationship
121  BLACKBONE_API static std::unordered_map<void*, DetourBase*> _breakpoints;
122 
123  // Exception handler
124  BLACKBONE_API static void* _vecHandler;
125 };
126 
127 }
static BLACKBONE_API LONG NTAPI Int3Handler(PEXCEPTION_POINTERS excpt)
Int3 exception handler
Definition: LocalHookBase.cpp:208
Definition: LocalHookBase.h:48
static BLACKBONE_API LONG NTAPI StepHandler(PEXCEPTION_POINTERS excpt)
Single step exception handler
Definition: LocalHookBase.cpp:238
BLACKBONE_API bool EnableHook()
Enable disabled hook
Definition: LocalHookBase.cpp:74
static BLACKBONE_API LONG NTAPI AVHandler(PEXCEPTION_POINTERS excpt)
Access violation handler
Definition: LocalHookBase.cpp:228
BLACKBONE_API bool ToggleHBP(int index, bool enable)
Toggle hardware breakpoint for current thread
Definition: LocalHookBase.cpp:90
static BLACKBONE_API LONG NTAPI VectoredHandler(PEXCEPTION_POINTERS excpt)
Exception handlers
Definition: LocalHookBase.cpp:185
BLACKBONE_API bool AllocateBuffer(uint8_t *nearest)
Allocate detour buffer as close to target as possible
Definition: LocalHookBase.cpp:22
BLACKBONE_API void CopyOldCode(uint8_t *Ptr)
Copy original function bytes
Definition: LocalHookBase.cpp:110
Definition: AsmHelper32.cpp:6
BLACKBONE_API bool DisableHook()
Temporarily disable hook
Definition: LocalHookBase.cpp:60