BlackBone
Windows memory hacking library
 All Classes Functions
DriverControl.h
1 #pragma once
2 
3 #include "../Include/Winheaders.h"
4 #include "../Include/Types.h"
5 #include "../../BlackBoneDrv/BlackBoneDef.h"
6 
7 #include <string>
8 #include <map>
9 
10 namespace blackbone
11 {
12 
13 // [Original ptr, size] <--> [Mapped ptr]
14 typedef std::map < std::pair<ptr_t, uint32_t>, ptr_t > mapMemoryMap;
15 
17 {
18  ptr_t hostSharedPage; // Shared page address in current process
19  ptr_t targetSharedPage; // Shared page address in target process
20  HANDLE targetPipe; // Hook pipe handle in the target process
21 
22  mapMemoryMap regions; // Mapped regions info
23 };
24 
26 {
27  ptr_t originalPtr; // Address of region in the target process
28  ptr_t newPtr; // Address of mapped region in the current process
29  ptr_t removedPtr; // Address of region unmapped because of address conflict
30  uint32_t size; // Size of mapped region
31  uint32_t removedSize; // Size of unmapped region
32 };
33 
34 
36 {
37 public:
38  BLACKBONE_API DriverControl();
39  BLACKBONE_API ~DriverControl();
40 
41  BLACKBONE_API static DriverControl& Instance();
42 
48  BLACKBONE_API NTSTATUS EnsureLoaded( const std::wstring& path = L"" );
49 
55  BLACKBONE_API NTSTATUS Reload( std::wstring path = L"" );
56 
61  BLACKBONE_API NTSTATUS Unload();
62 
69  BLACKBONE_API NTSTATUS DisableDEP( DWORD pid );
70 
77  BLACKBONE_API NTSTATUS ProtectProcess( DWORD pid, bool enable );
78 
86  BLACKBONE_API NTSTATUS PromoteHandle( DWORD pid, HANDLE handle, DWORD access );
87 
97  BLACKBONE_API NTSTATUS AllocateMem( DWORD pid, ptr_t& base, ptr_t& size, DWORD type, DWORD protection, bool physical = false );
98 
107  BLACKBONE_API NTSTATUS FreeMem( DWORD pid, ptr_t base, ptr_t size, DWORD type );
108 
117  BLACKBONE_API NTSTATUS ReadMem( DWORD pid, ptr_t base, ptr_t size, PVOID buffer );
118 
127  BLACKBONE_API NTSTATUS WriteMem( DWORD pid, ptr_t base, ptr_t size, PVOID buffer );
128 
137  BLACKBONE_API NTSTATUS ProtectMem( DWORD pid, ptr_t base, ptr_t size, DWORD protection );
138 
147  BLACKBONE_API NTSTATUS MapMemory( DWORD pid, const std::wstring& pipeName, bool mapSections, MapMemoryResult& result );
148 
157  BLACKBONE_API NTSTATUS MapMemoryRegion( DWORD pid, ptr_t base, uint32_t size, MapMemoryRegionResult& result );
158 
164  BLACKBONE_API NTSTATUS UnmapMemory( DWORD pid );
165 
176  BLACKBONE_API NTSTATUS UnmapMemoryRegion( DWORD pid, ptr_t base, uint32_t size );
177 
188  BLACKBONE_API NTSTATUS InjectDll(
189  DWORD pid,
190  const std::wstring& path,
191  InjectType itype,
192  uint32_t initRVA = 0,
193  const std::wstring& initArg = L"",
194  bool wait = true
195  );
196 
202  BLACKBONE_API NTSTATUS MMapDriver( const std::wstring& path );
203 
211  BLACKBONE_API NTSTATUS ConcealVAD( DWORD pid, ptr_t base, uint32_t size );
212 
217  BLACKBONE_API inline bool loaded() const { return _hDriver != INVALID_HANDLE_VALUE; }
218 
219 private:
220  DriverControl( const DriverControl& ) = delete;
221  DriverControl& operator = (const DriverControl&) = delete;
222 
229  NTSTATUS LoadDriver( const std::wstring& svcName, const std::wstring& path );
230 
236  NTSTATUS UnloadDriver( const std::wstring& svcName );
237 
244  LSTATUS PrepareDriverRegEntry( const std::wstring& svcName, const std::wstring& path );
245 
246 private:
247  HANDLE _hDriver = INVALID_HANDLE_VALUE;
248 };
249 
250 // Syntax sugar
251 inline DriverControl& Driver() { return DriverControl::Instance(); }
252 
253 }
BLACKBONE_API NTSTATUS UnmapMemoryRegion(DWORD pid, ptr_t base, uint32_t size)
Unmap single memory region If unmapped region size is smaller than the size specified during map...
Definition: DriverControl.cpp:228
BLACKBONE_API bool loaded() const
Check if driver is loaded
Definition: DriverControl.h:217
BLACKBONE_API NTSTATUS Unload()
Unload driver
Definition: DriverControl.cpp:105
BLACKBONE_API NTSTATUS MMapDriver(const std::wstring &path)
Manually map another system driver into system space
Definition: DriverControl.cpp:516
BLACKBONE_API NTSTATUS ProtectProcess(DWORD pid, bool enable)
Change process protection flag
Definition: DriverControl.cpp:275
BLACKBONE_API NTSTATUS MapMemoryRegion(DWORD pid, ptr_t base, uint32_t size, MapMemoryRegionResult &result)
Maps single memory region into current process
Definition: DriverControl.cpp:170
BLACKBONE_API NTSTATUS AllocateMem(DWORD pid, ptr_t &base, ptr_t &size, DWORD type, DWORD protection, bool physical=false)
Allocate virtual memory
Definition: DriverControl.cpp:325
BLACKBONE_API NTSTATUS EnsureLoaded(const std::wstring &path=L"")
Try to load driver if it isn't loaded
Definition: DriverControl.cpp:38
BLACKBONE_API NTSTATUS Reload(std::wstring path=L"")
Reload driver
Definition: DriverControl.cpp:60
BLACKBONE_API NTSTATUS PromoteHandle(DWORD pid, HANDLE handle, DWORD access)
Change handle access rights
Definition: DriverControl.cpp:297
BLACKBONE_API NTSTATUS ConcealVAD(DWORD pid, ptr_t base, uint32_t size)
Make VAD region appear as PAGE_NO_ACESS to NtQueryVirtualMemory
Definition: DriverControl.cpp:547
BLACKBONE_API NTSTATUS InjectDll(DWORD pid, const std::wstring &path, InjectType itype, uint32_t initRVA=0, const std::wstring &initArg=L"", bool wait=true)
Inject DLL into arbitrary process
Definition: DriverControl.cpp:482
Definition: DriverControl.h:16
BLACKBONE_API NTSTATUS UnmapMemory(DWORD pid)
Unmap memory of the target process from current
Definition: DriverControl.cpp:203
BLACKBONE_API NTSTATUS FreeMem(DWORD pid, ptr_t base, ptr_t size, DWORD type)
Free virtual memory
Definition: DriverControl.cpp:363
BLACKBONE_API NTSTATUS ReadMem(DWORD pid, ptr_t base, ptr_t size, PVOID buffer)
Read process memory
Definition: DriverControl.cpp:394
BLACKBONE_API NTSTATUS WriteMem(DWORD pid, ptr_t base, ptr_t size, PVOID buffer)
Write process memory
Definition: DriverControl.cpp:423
BLACKBONE_API NTSTATUS ProtectMem(DWORD pid, ptr_t base, ptr_t size, DWORD protection)
Change memory protection
Definition: DriverControl.cpp:452
Definition: DriverControl.h:35
BLACKBONE_API NTSTATUS DisableDEP(DWORD pid)
Disable DEP for process Has no effect on native x64 processes
Definition: DriverControl.cpp:254
Definition: AsmHelper32.cpp:6
Definition: DriverControl.h:25
BLACKBONE_API NTSTATUS MapMemory(DWORD pid, const std::wstring &pipeName, bool mapSections, MapMemoryResult &result)
Maps target process memory into current process
Definition: DriverControl.cpp:125