BlackBone
Windows memory hacking library
 All Classes Functions
TraceHook.h
1 #pragma once
2 
3 #include "../Include/WinHeaders.h"
4 
5 #include <stdint.h>
6 #include <vector>
7 #include <map>
8 #include <unordered_map>
9 
10 namespace blackbone
11 {
12 
13 enum TraceState
14 {
15  TS_Start, // Initial state. Internal use only
16  TS_Step, // Do single-step
17  TS_StepOut, // Break on function return
18  TS_StepInto, // Step into specific function
19  TS_WaitReturn, // Wait for break-on-return
20 };
21 
22 struct PathNode
23 {
24  TraceState action;
25  uintptr_t arg;
26 
27  PathNode( TraceState _action, uintptr_t _arg = 0 )
28  : action( _action )
29  , arg( _arg ) { }
30 };
31 
32 
37 {
38  typedef std::unordered_map<uintptr_t, std::pair<uintptr_t, bool>> mapHooks;
39  typedef std::vector<PathNode> vecState;
40 
41  uintptr_t lastIP = 0; // Previous EIP/RIP value
42  uintptr_t lastSP = 0; // Previous ESP/RSP value
43  uintptr_t targetPtr = 0; // Address causing exception
44  uintptr_t origPtrVal = 0; // Original pointer value
45  uintptr_t checkIP = 0; // Address of instruction that checks target pointer
46  uintptr_t breakValue = 0; // Value used to generate exception
47  uintptr_t stateIdx = 0; // Current state index in state vector
48 
49  TraceState state = TS_Start; // Current tracing state
50  vecState tracePath; // Function trace path
51  mapHooks hooks; // List of hooks associated with current pointer
52 
53 
57  void reset()
58  {
59  state = TS_Start;
60  lastIP = lastSP = 0;
61  stateIdx = 0;
62 
63  // Mark hooks as non-called
64  for (auto& item : hooks)
65  item.second.second = false;
66  }
67 };
68 
69 class TraceHook
70 {
71 
72 public:
73  typedef std::map<uintptr_t, HookContext> mapContext;
74  typedef std::vector <std::pair<uintptr_t, uintptr_t>> vecStackFrames;
75 
76 public:
77  ~TraceHook();
78  static TraceHook& Instance();
79 
89  BLACKBONE_API bool ApplyHook( void* targetFunc,
90  void* hookFunc,
91  void* ptrAddress,
92  const HookContext::vecState& tracePath = HookContext::vecState(),
93  void* checkIP = 0 );
94 
100  BLACKBONE_API bool RemoveHook( void* targetFunc );
101 
102 private:
103  //
104  // Singleton
105  //
106  TraceHook();
107  TraceHook( const TraceHook& ) = delete;
108  TraceHook& operator =( const TraceHook& ) = delete;
109 
110  //
111  // Exception handlers
112  //
113  static LONG __stdcall VecHandler( PEXCEPTION_POINTERS ExceptionInfo );
114  LONG VecHandlerP( PEXCEPTION_POINTERS ExceptionInfo );
115 
124  size_t StackBacktrace( uintptr_t ip, uintptr_t sp, vecStackFrames& results, uintptr_t depth = 10 );
125 
130  inline void BreakOnReturn( uintptr_t sp );
131 
139  bool CheckBranching( const HookContext& ctx, uintptr_t ip, uintptr_t sp );
140 
146  void HandleBranch( HookContext& ctx, PCONTEXT exptContex );
147 
154  bool RestorePtr( const HookContext& ctx, PEXCEPTION_POINTERS ExceptionInfo );
155 
156 private:
157  PVOID _pExptHandler = nullptr; // Exception handler
158  mapContext _contexts; // Hook contexts
159  uintptr_t _breakPtr = 0x2000; // Exception pointer generator
160 };
161 
162 }
BLACKBONE_API bool RemoveHook(void *targetFunc)
Remove existing hook
Definition: TraceHook.cpp:98
BLACKBONE_API bool ApplyHook(void *targetFunc, void *hookFunc, void *ptrAddress, const HookContext::vecState &tracePath=HookContext::vecState(), void *checkIP=0)
Setup hook
Definition: TraceHook.cpp:50
void reset()
Reset tracing state
Definition: TraceHook.h:57
Definition: TraceHook.h:22
Hook-related data
Definition: TraceHook.h:36
Definition: TraceHook.h:69
Definition: AsmHelper32.cpp:6