BlackBone
Windows memory hacking library
 All Classes Functions
RemoteMemory.h
1 #pragma once
2 
3 #include "../../Config.h"
4 #include "../../DriverControl/DriverControl.h"
5 
6 #include <string>
7 #include <map>
8 
9 namespace blackbone
10 {
11 
12 
14 {
15 public:
16 #ifdef USE64
17  struct HookData
18  {
19  uint8_t original_code[24]; // Original 5 bytes + jmp
20  uint8_t jump_buf[12]; // Trampoline
21  uint8_t hook_code[384]; // Hook function
22  };
23 #else
24  struct HookData
25  {
26  uint8_t original_code[10]; // Original 5 bytes + jmp
27  uint8_t jump_buf[5]; // Trampoline
28  uint8_t hook_code[384]; // Hook function
29  };
30 #endif
31 
32  enum OperationType
33  {
34  MemVirtualAlloc = 0, // MemVirtualAlloc
35  MemVirtualFree, // MemVirtualFree
36  MemMapSection, // MemMapSection
37  MemUnmapSection, // MemUnmapSection
38  };
39 
41  {
42  ptr_t allocAddress; // Allocated region base
43  uint32_t allocSize; // Allocated region size
44  OperationType allocType; // Operation type
45  };
46 
47  struct PageContext
48  {
49  HookData hkVirtualAlloc; // NtAllocateVirtualMemory context
50  HookData hkVirtualFree; // NtFreeVirtualMemory context
51  HookData hkMapSection; // NtMapViewOfSection context
52  HookData hkUnmapSection; // NtUnmapViewOfSection context
53  CRITICAL_SECTION csLock; // Lock to sync multi-threaded mappings
54  };
55 
56 public:
57  BLACKBONE_API RemoteMemory( class Process* process );
58  BLACKBONE_API ~RemoteMemory();
59 
65  BLACKBONE_API NTSTATUS Map( bool mapSections );
66 
73  BLACKBONE_API NTSTATUS Map( ptr_t base, uint32_t size );
74 
79  BLACKBONE_API NTSTATUS Unmap();
80 
87  BLACKBONE_API NTSTATUS Unmap( ptr_t base, uint32_t size );
88 
95  BLACKBONE_API ptr_t TranslateAddress( ptr_t address, bool resolveFault = true );
96 
108  BLACKBONE_API NTSTATUS SetupHook( OperationType hkType );
109 
115  BLACKBONE_API bool RestoreHook( OperationType hkType );
116 
120  BLACKBONE_API void reset();
121 
122 private:
128  static DWORD CALLBACK HookThreadWrap( LPVOID lpParam );
129 
133  void HookThread();
134 
139  void BuildGenericHookFn( OperationType opType );
140 
147  void BuildTrampoline( OperationType opType, uintptr_t pOriginal, uint8_t* pOriginalLocal );
148 
149 private:
150  class Process* _process = nullptr; // Target process
151  mapMemoryMap _mapDatabase; // Region map
152  std::wstring _pipeName; // Pipe name used to gather hook data
153  HANDLE _hPipe = NULL; // Hook pipe handle
154  HANDLE _targetPipe = NULL; // Hook pipe handle in target process
155  HANDLE _hThread = NULL; // Hook thread listener
156  PageContext* _pSharedData = nullptr; // Hook related data, shared between processes
157  ptr_t _targetShare = 0; // Address of shared in data in target process
158  bool _active = false; // Hook thread activity flag
159  bool _hooked[4]; // Hook state
160 };
161 
162 }
BLACKBONE_API void reset()
Unmap any mapped memory, restore hooks and free resources
Definition: RemoteMemory.cpp:244
BLACKBONE_API NTSTATUS SetupHook(OperationType hkType)
Setup one of the 4 possible memory hooks:
Definition: RemoteMemory.cpp:159
Definition: RemoteMemory.h:13
BLACKBONE_API bool RestoreHook(OperationType hkType)
Restore previously hooked function
Definition: RemoteMemory.cpp:211
BLACKBONE_API NTSTATUS Map(bool mapSections)
Map entire process address space
Definition: RemoteMemory.cpp:31
BLACKBONE_API ptr_t TranslateAddress(ptr_t address, bool resolveFault=true)
Translate target address accordingly to current address space
Definition: RemoteMemory.cpp:128
Definition: Process.h:80
Definition: RemoteMemory.h:24
BLACKBONE_API NTSTATUS Unmap()
Unmap process address space from current process
Definition: RemoteMemory.cpp:79
Definition: AsmHelper32.cpp:6
Definition: RemoteMemory.h:40
Definition: RemoteMemory.h:47