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 "../Include/Macro.h"
6 #include "../../BlackBoneDrv/BlackBoneDef.h"
7 
8 #include <string>
9 #include <map>
10 
11 ENUM_OPS( KMmapFlags );
12 
13 namespace blackbone
14 {
15 // [Original ptr, size] <--> [Mapped ptr]
16 typedef std::map < std::pair<ptr_t, uint32_t>, ptr_t > mapMemoryMap;
17 
19 {
20  ptr_t hostSharedPage; // Shared page address in current process
21  ptr_t targetSharedPage; // Shared page address in target process
22  HANDLE targetPipe; // Hook pipe handle in the target process
23 
24  mapMemoryMap regions; // Mapped regions info
25 };
26 
28 {
29  ptr_t originalPtr; // Address of region in the target process
30  ptr_t newPtr; // Address of mapped region in the current process
31  ptr_t removedPtr; // Address of region unmapped because of address conflict
32  uint32_t size; // Size of mapped region
33  uint32_t removedSize; // Size of unmapped region
34 };
35 
36 
38 {
39 public:
40  BLACKBONE_API DriverControl();
41  BLACKBONE_API ~DriverControl();
42 
43  BLACKBONE_API static DriverControl& Instance();
44 
50  BLACKBONE_API NTSTATUS EnsureLoaded( const std::wstring& path = L"" );
51 
57  BLACKBONE_API NTSTATUS Reload( std::wstring path = L"" );
58 
63  BLACKBONE_API NTSTATUS Unload();
64 
71  BLACKBONE_API NTSTATUS DisableDEP( DWORD pid );
72 
79  BLACKBONE_API NTSTATUS ProtectProcess( DWORD pid, bool enable );
80 
88  BLACKBONE_API NTSTATUS PromoteHandle( DWORD pid, HANDLE handle, DWORD access );
89 
99  BLACKBONE_API NTSTATUS AllocateMem( DWORD pid, ptr_t& base, ptr_t& size, DWORD type, DWORD protection, bool physical = false );
100 
109  BLACKBONE_API NTSTATUS FreeMem( DWORD pid, ptr_t base, ptr_t size, DWORD type );
110 
119  BLACKBONE_API NTSTATUS ReadMem( DWORD pid, ptr_t base, ptr_t size, PVOID buffer );
120 
129  BLACKBONE_API NTSTATUS WriteMem( DWORD pid, ptr_t base, ptr_t size, PVOID buffer );
130 
139  BLACKBONE_API NTSTATUS ProtectMem( DWORD pid, ptr_t base, ptr_t size, DWORD protection );
140 
149  BLACKBONE_API NTSTATUS MapMemory( DWORD pid, const std::wstring& pipeName, bool mapSections, MapMemoryResult& result );
150 
159  BLACKBONE_API NTSTATUS MapMemoryRegion( DWORD pid, ptr_t base, uint32_t size, MapMemoryRegionResult& result );
160 
166  BLACKBONE_API NTSTATUS UnmapMemory( DWORD pid );
167 
178  BLACKBONE_API NTSTATUS UnmapMemoryRegion( DWORD pid, ptr_t base, uint32_t size );
179 
192  BLACKBONE_API NTSTATUS InjectDll(
193  DWORD pid,
194  const std::wstring& path,
195  InjectType itype,
196  uint32_t initRVA = 0,
197  const std::wstring& initArg = L"",
198  bool unlink = false,
199  bool erasePE = false,
200  bool wait = true
201  );
202 
212  BLACKBONE_API NTSTATUS MmapDll(
213  DWORD pid,
214  const std::wstring& path,
215  KMmapFlags flags,
216  uint32_t initRVA = 0,
217  const std::wstring& initArg = L""
218  );
219 
231  BLACKBONE_API NTSTATUS MmapDll(
232  DWORD pid,
233  void* address,
234  uint32_t size,
235  bool asImage,
236  KMmapFlags flags,
237  uint32_t initRVA = 0,
238  const std::wstring& initArg = L""
239  );
240 
246  BLACKBONE_API NTSTATUS MMapDriver( const std::wstring& path );
247 
255  BLACKBONE_API NTSTATUS ConcealVAD( DWORD pid, ptr_t base, uint32_t size );
256 
262  BLACKBONE_API NTSTATUS UnlinkHandleTable( DWORD pid );
263 
268  BLACKBONE_API inline bool loaded() const { return _hDriver != INVALID_HANDLE_VALUE; }
269  BLACKBONE_API inline NTSTATUS status() const { return _loadStatus; }
270 
271 private:
272  DriverControl( const DriverControl& ) = delete;
273  DriverControl& operator = (const DriverControl&) = delete;
274 
281  NTSTATUS LoadDriver( const std::wstring& svcName, const std::wstring& path );
282 
288  NTSTATUS UnloadDriver( const std::wstring& svcName );
289 
296  LSTATUS PrepareDriverRegEntry( const std::wstring& svcName, const std::wstring& path );
297 private:
298  HANDLE _hDriver = INVALID_HANDLE_VALUE;
299  NTSTATUS _loadStatus = STATUS_NOT_FOUND;
300 };
301 
302 // Syntax sugar
303 inline DriverControl& Driver() { return DriverControl::Instance(); }
304 
305 }
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:243
BLACKBONE_API NTSTATUS UnlinkHandleTable(DWORD pid)
Unlink process handle table from HandleListHead
Definition: DriverControl.cpp:678
BLACKBONE_API bool loaded() const
Check if driver is loaded
Definition: DriverControl.h:268
BLACKBONE_API NTSTATUS Unload()
Unload driver
Definition: DriverControl.cpp:120
BLACKBONE_API NTSTATUS MMapDriver(const std::wstring &path)
Manually map another system driver into system space
Definition: DriverControl.cpp:625
BLACKBONE_API NTSTATUS ProtectProcess(DWORD pid, bool enable)
Change process protection flag
Definition: DriverControl.cpp:290
BLACKBONE_API NTSTATUS MapMemoryRegion(DWORD pid, ptr_t base, uint32_t size, MapMemoryRegionResult &result)
Maps single memory region into current process
Definition: DriverControl.cpp:185
BLACKBONE_API NTSTATUS MmapDll(DWORD pid, const std::wstring &path, KMmapFlags flags, uint32_t initRVA=0, const std::wstring &initArg=L"")
Manually map PE image
Definition: DriverControl.cpp:541
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:340
BLACKBONE_API NTSTATUS InjectDll(DWORD pid, const std::wstring &path, InjectType itype, uint32_t initRVA=0, const std::wstring &initArg=L"", bool unlink=false, bool erasePE=false, bool wait=true)
Inject DLL into arbitrary process
Definition: DriverControl.cpp:499
BLACKBONE_API NTSTATUS EnsureLoaded(const std::wstring &path=L"")
Try to load driver if it isn't loaded
Definition: DriverControl.cpp:41
BLACKBONE_API NTSTATUS Reload(std::wstring path=L"")
Reload driver
Definition: DriverControl.cpp:67
BLACKBONE_API NTSTATUS PromoteHandle(DWORD pid, HANDLE handle, DWORD access)
Change handle access rights
Definition: DriverControl.cpp:312
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:654
Definition: DriverControl.h:18
BLACKBONE_API NTSTATUS UnmapMemory(DWORD pid)
Unmap memory of the target process from current
Definition: DriverControl.cpp:218
BLACKBONE_API NTSTATUS FreeMem(DWORD pid, ptr_t base, ptr_t size, DWORD type)
Free virtual memory
Definition: DriverControl.cpp:378
BLACKBONE_API NTSTATUS ReadMem(DWORD pid, ptr_t base, ptr_t size, PVOID buffer)
Read process memory
Definition: DriverControl.cpp:409
BLACKBONE_API NTSTATUS WriteMem(DWORD pid, ptr_t base, ptr_t size, PVOID buffer)
Write process memory
Definition: DriverControl.cpp:438
BLACKBONE_API NTSTATUS ProtectMem(DWORD pid, ptr_t base, ptr_t size, DWORD protection)
Change memory protection
Definition: DriverControl.cpp:467
Definition: DriverControl.h:37
BLACKBONE_API NTSTATUS DisableDEP(DWORD pid)
Disable DEP for process Has no effect on native x64 processes
Definition: DriverControl.cpp:269
Definition: AsmHelper32.cpp:6
Definition: DriverControl.h:27
BLACKBONE_API NTSTATUS MapMemory(DWORD pid, const std::wstring &pipeName, bool mapSections, MapMemoryResult &result)
Maps target process memory into current process
Definition: DriverControl.cpp:140