BlackBone
Windows memory hacking library
 All Classes Functions
RemoteContext.hpp
1 #pragma once
2 
3 #include "../ProcessCore.h"
4 #include "../ProcessMemory.h"
5 #include "../Threads/Thread.h"
6 #include "../../Include/Macro.h"
7 
8 namespace blackbone
9 {
10 
15 {
16 public:
17  BLACKBONE_API RemoteContext(
18  ProcessMemory& memory,
19  Thread& thd,
20  _CONTEXT64& ctx,
21  ptr_t frame_ptr,
22  BOOL x64,
23  int wordSize
24  )
25  : _memory( memory )
26  , _thd( thd )
27  , _ctx( ctx )
28  , _x64Target( x64 )
29  , _wordSize( wordSize )
30  , _frame_ptr( frame_ptr != 0 ? frame_ptr : ctx.Rsp )
31  {
32  }
33 
34  BLACKBONE_API ~RemoteContext()
35  {
36  }
37 
38  // Native context
39  BLACKBONE_API inline _CONTEXT64& native()
40  {
41  return _ctx;
42  }
43 
48  BLACKBONE_API inline Thread& getThread()
49  {
50  return _thd;
51  }
52 
57  BLACKBONE_API inline const ptr_t returnAddress() const
58  {
59  ptr_t val = 0;
60  _memory.Read( _frame_ptr, _wordSize, &val );
61 
62  return val;
63  }
64 
70  BLACKBONE_API inline bool returnAddress( ptr_t val ) const
71  {
72  return (_memory.Write( _frame_ptr, _wordSize, &val ) == STATUS_SUCCESS);
73  }
74 
80  BLACKBONE_API inline void setReturnValue( ptr_t val ) const
81  {
82  memcpy( &_ctx.Rax, &val, _wordSize );
83  }
84 
89  BLACKBONE_API ptr_t hookReturn()
90  {
91  ptr_t val = returnAddress();
92  SET_BIT( val, (_wordSize * 8 - 1) );
93  if (returnAddress( val ) == false)
94  return 0;
95 
96  return val;
97  }
98 
103  BLACKBONE_API ptr_t unhookReturn()
104  {
105  auto val = returnAddress();
106  RESET_BIT( val, (_wordSize * 8 - 1) );
107  if (!returnAddress( val ))
108  return 0;
109 
110  return val;
111  }
112 
121  BLACKBONE_API DWORD64 getArg( int index )
122  {
123  if(_x64Target)
124  {
125  switch (index)
126  {
127  case 0:
128  return _ctx.Rcx;
129  case 1:
130  return _ctx.Rdx;
131  case 2:
132  return _ctx.R8;
133  case 3:
134  return _ctx.R9;
135 
136  default:
137  return _memory.Read<DWORD64>( _ctx.Rsp + 0x30 + (index - 4 ) * _wordSize );
138  }
139  }
140  else
141  {
142  DWORD64 val = 0;
143  _memory.Read( _ctx.Rsp + 4 + index * _wordSize, _wordSize, &val );
144  return val;
145  }
146  }
147 
156  BLACKBONE_API bool setArg( int index, DWORD64 val )
157  {
158  if (_x64Target)
159  {
160  switch (index)
161  {
162  case 0:
163  _ctx.Rcx = val;
164  break;
165 
166  case 1:
167  _ctx.Rdx = val;
168  break;
169 
170  case 2:
171  _ctx.R8 = val;
172  break;
173 
174  case 3:
175  _ctx.R9 = val;
176  break;
177 
178  default:
179  return (_memory.Write( _ctx.Rsp + 0x30 + (index - 4) * _wordSize, val ) == STATUS_SUCCESS);
180  }
181 
182  return true;
183  }
184  else
185  {
186  return (_memory.Write( _ctx.Rsp + 4 + index * _wordSize, _wordSize, &val ) == STATUS_SUCCESS);
187  }
188  }
189 
190 
195  BLACKBONE_API DWORD lastError()
196  {
197  ptr_t pteb = 0;
198  LONG offset = 0;
199 
200  if( _x64Target )
201  {
202  pteb = _thd.teb( (_TEB64*)nullptr );
203  offset = FIELD_OFFSET( _TEB64, LastErrorValue );
204  }
205  else
206  {
207  pteb = _thd.teb( (_TEB32*)nullptr );
208  offset = FIELD_OFFSET( _TEB32, LastErrorValue );
209  }
210 
211  if (pteb)
212  return _memory.Read<DWORD>( pteb + offset );
213 
214  return 0xFFFFFFFF;
215  }
216 
221  BLACKBONE_API DWORD lastError( DWORD newError )
222  {
223  ptr_t pteb = 0;
224  LONG offset = 0;
225 
226  if (_x64Target)
227  {
228  pteb = _thd.teb( (_TEB64*)nullptr );
229  offset = FIELD_OFFSET( _TEB64, LastErrorValue );
230  }
231  else
232  {
233  pteb = _thd.teb( (_TEB32*)nullptr );
234  offset = FIELD_OFFSET( _TEB32, LastErrorValue );
235  }
236 
237  if (pteb)
238  return _memory.Write( pteb + offset, newError );
239 
240  return 0xFFFFFFFF;
241  }
242 
243 
248  BLACKBONE_API ptr_t getUserContext()
249  {
250  auto pteb = _thd.teb( (_TEB64*)nullptr );
251 
252  if (pteb)
253  return _memory.Read<ptr_t>( pteb + FIELD_OFFSET( _NT_TIB_T<DWORD64>, ArbitraryUserPointer ) );
254 
255  return 0;
256  }
257 
262  BLACKBONE_API bool setUserContext( ptr_t context )
263  {
264  auto pteb = _thd.teb( (_TEB64*)nullptr );
265  if(pteb)
266  {
267  if (_memory.Write( pteb + FIELD_OFFSET( _NT_TIB_T<DWORD64>, ArbitraryUserPointer ), context ) == STATUS_SUCCESS)
268  return true;
269  }
270 
271  return false;
272  }
273 
274 
275 private:
276  RemoteContext( const RemoteContext& ) = delete;
277  RemoteContext& operator = ( const RemoteContext& ) = delete;
278 
279 private:
280  ProcessMemory& _memory; // Process memory routines
281  Thread& _thd; // Current thread
282  _CONTEXT64& _ctx; // Current thread context
283 
284  BOOL _x64Target = FALSE; // Target process is 64 bit
285  int _wordSize = 4; // 4 for x86, 8 for x64
286  ptr_t _frame_ptr = 0; // Top stack frame pointer
287 };
288 
289 }
BLACKBONE_API ptr_t getUserContext()
Get arbitrary thread data
Definition: RemoteContext.hpp:248
BLACKBONE_API Thread & getThread()
Get current process thread where exception occurred
Definition: RemoteContext.hpp:48
BLACKBONE_API ptr_t hookReturn()
Raise exception on function return
Definition: RemoteContext.hpp:89
BLACKBONE_API DWORD lastError(DWORD newError)
Set last thread error code
Definition: RemoteContext.hpp:221
BLACKBONE_API NTSTATUS Read(ptr_t dwAddress, size_t dwSize, PVOID pResult, bool handleHoles=false)
Read data
Definition: ProcessMemory.cpp:81
Definition: NativeStructures.h:82
BLACKBONE_API bool setArg(int index, DWORD64 val)
Set argument value. For x86 function works only with stack arguments. For x64 only integer arguments ...
Definition: RemoteContext.hpp:156
Definition: NativeStructures.h:55
BLACKBONE_API bool setUserContext(ptr_t context)
Set arbitrary thread data
Definition: RemoteContext.hpp:262
Definition: ProcessMemory.h:13
BLACKBONE_API const ptr_t returnAddress() const
Definition: RemoteContext.hpp:57
BLACKBONE_API NTSTATUS Write(ptr_t pAddress, size_t dwSize, const void *pData)
Write data
Definition: ProcessMemory.cpp:131
Remote function context during hook breakpoint.
Definition: RemoteContext.hpp:14
BLACKBONE_API bool returnAddress(ptr_t val) const
Set return address of current frame
Definition: RemoteContext.hpp:70
BLACKBONE_API DWORD64 getArg(int index)
Get argument value. Argument index is 0 based. For x86 function works only with stack arguments For x...
Definition: RemoteContext.hpp:121
BLACKBONE_API ptr_t teb(_TEB32 *pteb=nullptr) const
Get WOW64 TEB
Definition: Thread.cpp:41
BLACKBONE_API DWORD lastError()
Get last thread error code
Definition: RemoteContext.hpp:195
Thread management
Definition: Thread.h:91
Definition: NativeStructures.h:498
Definition: AsmHelper32.cpp:6
BLACKBONE_API void setReturnValue(ptr_t val) const
Set new integer return value. Has no effect on FPU. Has effect only if called in return callback ...
Definition: RemoteContext.hpp:80
BLACKBONE_API ptr_t unhookReturn()
Remove exception on return
Definition: RemoteContext.hpp:103