BlackBone
Windows memory hacking library
 All Classes Functions
MemBlock.h
1 #pragma once
2 
3 #include "../Include/Winheaders.h"
4 #include "../Include/Macro.h"
5 #include "../Include/Types.h"
6 
7 #include <stdint.h>
8 #include <map>
9 #include <set>
10 
11 namespace blackbone
12 {
13 
20 inline DWORD CastProtection( DWORD prot, bool bDEP )
21 {
22  if (bDEP == true)
23  {
24  return prot;
25  }
26  else
27  {
28  if (prot == PAGE_EXECUTE_READ)
29  return PAGE_READONLY;
30  else if (prot == PAGE_EXECUTE_READWRITE)
31  return PAGE_READWRITE;
32  else if (prot == PAGE_EXECUTE_WRITECOPY)
33  return PAGE_WRITECOPY;
34  else
35  return prot;
36  }
37 }
38 
39 class MemBlock
40 {
41 public:
42 
43  BLACKBONE_API MemBlock();
44 
51  BLACKBONE_API MemBlock( class ProcessMemory* mem, ptr_t ptr, bool own = true );
52 
61  BLACKBONE_API MemBlock(
62  class ProcessMemory* mem,
63  ptr_t ptr,
64  size_t size,
65  DWORD prot,
66  bool own = true,
67  bool physical = false
68  );
69 
70  BLACKBONE_API ~MemBlock();
71 
80  BLACKBONE_API static MemBlock Allocate(
81  class ProcessMemory& process,
82  size_t size,
83  ptr_t desired = 0,
84  DWORD protection = PAGE_EXECUTE_READWRITE
85  );
86 
94  BLACKBONE_API ptr_t Realloc( size_t size, ptr_t desired = 0, DWORD protection = PAGE_EXECUTE_READWRITE );
95 
104  BLACKBONE_API NTSTATUS Protect( DWORD protection, size_t offset = 0, size_t size = 0, DWORD* pOld = nullptr );
105 
110  BLACKBONE_API NTSTATUS Free( size_t size = 0 );
111 
123  BLACKBONE_API NTSTATUS Read( size_t offset, size_t size, PVOID pResult, bool handleHoles = false );
124 
132  BLACKBONE_API NTSTATUS Write( size_t offset, size_t size, const void* pData );
133 
140  template<class T>
141  T Read( size_t offset, const T& def_val )
142  {
143  T res = def_val;
144  Read( offset, sizeof( T ), &res );
145  return res;
146  }
147 
154  template<class T>
155  NTSTATUS Read( size_t offset, T& val )
156  {
157  return Read( offset, sizeof( val ), &val );
158  }
159 
166  template<class T>
167  NTSTATUS Write( size_t offset, const T& data )
168  {
169  return Write( offset, sizeof( data ), &data );
170  }
171 
175  BLACKBONE_API void Reset();
176 
180  BLACKBONE_API inline void Release() { _own = false; }
181 
186  template< typename T = ptr_t >
187  inline T ptr() const { return (T)_ptr; }
188 
193  BLACKBONE_API inline size_t size() const { return _size; }
194 
199  BLACKBONE_API inline DWORD protection() const { return _protection; }
200 
204  BLACKBONE_API inline bool valid() const { return (_memory != nullptr && _ptr != 0); }
205 
210  BLACKBONE_API inline operator ptr_t() const { return _ptr; }
211 
212  BLACKBONE_API MemBlock& operator =(const MemBlock& other)
213  {
214  _memory = other._memory;
215  _ptr = other._ptr;
216  _size = other._size;
217  _protection = other._protection;
218  _own = true;
219 
220  // Transfer memory ownership
221  const_cast<MemBlock&>(other).Release();
222 
223  return *this;
224  }
225 
226 private:
227  ptr_t _ptr = 0; // Raw memory pointer
228  size_t _size = 0; // Region size
229  DWORD _protection = 0; // Region protection
230  bool _own = true; // Memory will be freed in destructor
231  bool _physical = false; // Memory allocated as direct physical
232  class ProcessMemory* _memory; // Target process routines
233 };
234 
235 }
BLACKBONE_API NTSTATUS Free(size_t size=0)
Free memory
Definition: MemBlock.cpp:138
T ptr() const
Get memory pointer
Definition: MemBlock.h:187
Definition: ProcessMemory.h:13
BLACKBONE_API NTSTATUS Read(size_t offset, size_t size, PVOID pResult, bool handleHoles=false)
Read data
Definition: MemBlock.cpp:177
BLACKBONE_API NTSTATUS Protect(DWORD protection, size_t offset=0, size_t size=0, DWORD *pOld=nullptr)
Change memory protection
Definition: MemBlock.cpp:123
BLACKBONE_API NTSTATUS Write(size_t offset, size_t size, const void *pData)
Write data
Definition: MemBlock.cpp:190
T Read(size_t offset, const T &def_val)
Read data
Definition: MemBlock.h:141
NTSTATUS Write(size_t offset, const T &data)
Write data
Definition: MemBlock.h:167
static BLACKBONE_API MemBlock Allocate(class ProcessMemory &process, size_t size, ptr_t desired=0, DWORD protection=PAGE_EXECUTE_READWRITE)
Allocate new memory block
Definition: MemBlock.cpp:64
BLACKBONE_API void Release()
Memory will not be deallocated upon object destruction
Definition: MemBlock.h:180
BLACKBONE_API DWORD protection() const
Get block memory protection
Definition: MemBlock.h:199
Definition: MemBlock.h:39
BLACKBONE_API ptr_t Realloc(size_t size, ptr_t desired=0, DWORD protection=PAGE_EXECUTE_READWRITE)
Reallocate existing block for new size
Definition: MemBlock.cpp:88
Definition: AsmHelper32.cpp:6
NTSTATUS Read(size_t offset, T &val)
Read data
Definition: MemBlock.h:155
BLACKBONE_API bool valid() const
Validate memory block true if memory pointer isn't 0
Definition: MemBlock.h:204
BLACKBONE_API size_t size() const
Get block size
Definition: MemBlock.h:193
BLACKBONE_API void Reset()
Try to free memory and reset pointers
Definition: MemBlock.cpp:198