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( class ProcessMemory* mem, ptr_t ptr,
62  size_t size, DWORD prot, bool own = true,
63  bool physical = false );
64 
65  BLACKBONE_API ~MemBlock();
66 
75  BLACKBONE_API static MemBlock Allocate( class ProcessMemory& process, size_t size, ptr_t desired = 0, DWORD protection = PAGE_EXECUTE_READWRITE );
76 
84  BLACKBONE_API ptr_t Realloc( size_t size, ptr_t desired = 0, DWORD protection = PAGE_EXECUTE_READWRITE );
85 
94  BLACKBONE_API NTSTATUS Protect( DWORD protection, size_t offset = 0, size_t size = 0, DWORD* pOld = nullptr );
95 
100  BLACKBONE_API NTSTATUS Free( size_t size = 0 );
101 
113  BLACKBONE_API NTSTATUS Read( size_t offset, size_t size, PVOID pResult, bool handleHoles = false );
114 
122  BLACKBONE_API NTSTATUS Write( size_t offset, size_t size, const void* pData );
123 
130  template<class T>
131  T Read( size_t offset, const T& def_val )
132  {
133  T res = def_val;
134  Read( offset, sizeof(T), &res );
135  return res;
136  };
137 
144  template<class T>
145  NTSTATUS Write( size_t offset, const T& data )
146  {
147  return Write( offset, sizeof(T), &data );
148  }
149 
153  BLACKBONE_API void Reset();
154 
158  BLACKBONE_API inline void Release() { _own = false; }
159 
164  template< typename T = ptr_t >
165  inline T ptr() const { return (T)_ptr; }
166 
171  BLACKBONE_API inline size_t size() const { return _size; }
172 
177  BLACKBONE_API inline DWORD protection() const { return _protection; }
178 
182  BLACKBONE_API inline bool valid() const { return (_memory != nullptr && _ptr != 0); }
183 
188  BLACKBONE_API inline operator ptr_t() const { return _ptr; }
189 
190  BLACKBONE_API MemBlock& operator =(const MemBlock& other)
191  {
192  _memory = other._memory;
193  _ptr = other._ptr;
194  _size = other._size;
195  _protection = other._protection;
196  _own = true;
197 
198  // Transfer memory ownership
199  const_cast<MemBlock&>(other).Release();
200 
201  return *this;
202  }
203 
204 private:
205  ptr_t _ptr = 0; // Raw memory pointer
206  size_t _size = 0; // Region size
207  DWORD _protection = 0; // Region protection
208  bool _own = true; // Memory will be freed in destructor
209  bool _physical = false; // Memory allocated as direct physical
210  class ProcessMemory* _memory; // Target process routines
211 };
212 
213 }
BLACKBONE_API NTSTATUS Free(size_t size=0)
Free memory
Definition: MemBlock.cpp:140
T ptr() const
Get memory pointer
Definition: MemBlock.h:165
Definition: ProcessMemory.h:13
BLACKBONE_API NTSTATUS Read(size_t offset, size_t size, PVOID pResult, bool handleHoles=false)
Read data
Definition: MemBlock.cpp:178
BLACKBONE_API NTSTATUS Protect(DWORD protection, size_t offset=0, size_t size=0, DWORD *pOld=nullptr)
Change memory protection
Definition: MemBlock.cpp:125
BLACKBONE_API NTSTATUS Write(size_t offset, size_t size, const void *pData)
Write data
Definition: MemBlock.cpp:191
T Read(size_t offset, const T &def_val)
Read data
Definition: MemBlock.h:131
NTSTATUS Write(size_t offset, const T &data)
Write data
Definition: MemBlock.h:145
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:66
BLACKBONE_API void Release()
Memory will not be deallocated upon object destruction
Definition: MemBlock.h:158
BLACKBONE_API DWORD protection() const
Get block memory protection
Definition: MemBlock.h:177
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:90
Definition: AsmHelper32.cpp:6
BLACKBONE_API bool valid() const
Validate memory block true if memory pointer isn't 0
Definition: MemBlock.h:182
BLACKBONE_API size_t size() const
Get block size
Definition: MemBlock.h:171
BLACKBONE_API void Reset()
Try to free memory and reset pointers
Definition: MemBlock.cpp:199