BlackBone
Windows memory hacking library
 All Classes Functions
MMap.h
1 #pragma once
2 
3 #include "../Config.h"
4 #include "../Include/Winheaders.h"
5 #include "../Include/Macro.h"
6 #include "../PE/PEImage.h"
7 
8 #include "../Process/MemBlock.h"
9 #include "MExcept.h"
10 
11 #include <map>
12 #include <stack>
13 
14 namespace blackbone
15 {
16 
17 // Loader flags
18 
19 enum eLoadFlags
20 {
21  NoFlags = 0x00, // No flags
22  ManualImports = 0x01, // Manually map import libraries
23  CreateLdrRef = 0x02, // Create module references for native loader
24  WipeHeader = 0x04, // Wipe image PE headers
25  HideVAD = 0x10, // Make image appear as PAGE_NOACESS region
26  MapInHighMem = 0x20, // Try to map image in address space beyond 4GB limit
27  RebaseProcess = 0x40, // If target image is an .exe file, process base address will be replaced with mapped module value
28 
29  NoExceptions = 0x01000, // Do not create custom exception handler
30  PartialExcept = 0x02000, // Only create Inverted function table, without VEH
31  NoDelayLoad = 0x04000, // Do not resolve delay import
32  NoSxS = 0x08000, // Do not apply SxS activation context
33  NoTLS = 0x10000, // Skip TLS initialization and don't execute TLS callbacks
34 };
35 
36 ENUM_OPS( eLoadFlags )
37 
38 // Native loader flags callback
39 typedef enum LdrRefFlags( *LdrCallback )(void* context, const ModuleData& modInfo);
40 
41 
46 {
47  typedef std::vector<ptr_t> vecPtr;
48 
49  pe::PEImage peImage; // PE image data
50  MemBlock imgMem; // Target image memory region
51  std::wstring FilePath; // path to image being mapped
52  std::wstring FileName; // File name string
53  vecPtr tlsCallbacks; // TLS callback routines
54  ptr_t pExpTableAddr = 0; // Exception table address (amd64 only)
55  ptr_t EntryPoint = 0; // Target image entry point
56  eLoadFlags flags = NoFlags; // Image loader flags
57  bool initialized = false; // Image entry point was called
58 };
59 
60 typedef std::vector<std::unique_ptr<ImageContext>> vecImageCtx;
61 
65 class MMap : public MExcept
66 {
67 
68 public:
69  BLACKBONE_API MMap( class Process& proc );
70  BLACKBONE_API ~MMap( void );
71 
80  BLACKBONE_API const ModuleData* MapImage(
81  const std::wstring& path,
82  eLoadFlags flags = NoFlags,
83  LdrCallback ldrCallback = nullptr,
84  void* ldrContext = nullptr
85  );
86 
97  BLACKBONE_API const ModuleData* MapImage(
98  size_t size, void* buffer,
99  bool asImage = false,
100  eLoadFlags flags = NoFlags,
101  LdrCallback ldrCallback = nullptr,
102  void* ldrContext = nullptr
103  );
104 
109  BLACKBONE_API bool UnmapAllModules();
110 
115  BLACKBONE_API void Cleanup();
116 
120  BLACKBONE_API inline void reset() { _images.clear(); _pAContext.Reset(); _usedBlocks.clear(); }
121 
122 private:
134  const ModuleData* MapImageInternal(
135  const std::wstring& path,
136  void* buffer, size_t size,
137  bool asImage = false,
138  eLoadFlags flags = NoFlags,
139  LdrCallback ldrCallback = nullptr,
140  void* ldrContext = nullptr
141  );
142 
149  const ModuleData* FindOrMapModule(
150  const std::wstring& path,
151  void* buffer, size_t size, bool asImage,
152  eLoadFlags flags = NoFlags
153  );
154 
166  bool RunModuleInitializers( ImageContext* pImage, DWORD dwReason );
167 
173  bool CopyImage( ImageContext* pImage );
174 
180  bool ProtectImageMemory( ImageContext* pImage );
181 
187  bool RelocateImage( ImageContext* pImage );
188 
195  bool ResolveImport( ImageContext* pImage, bool useDelayed = false );
196 
202  bool InitStaticTLS( ImageContext* pImage );
203 
209  NTSTATUS EnableExceptions( ImageContext* pImage );
210 
216  NTSTATUS DisableExceptions( ImageContext* pImage );
217 
229  bool CreateActx( const std::wstring& path, int id = 2, bool asImage = true );
230 
236  bool InitializeCookie( ImageContext* pImage );
237 
243  NTSTATUS ConcealVad( const MemBlock& imageMem );
244 
251  NTSTATUS AllocateInHighMem( MemBlock& imageMem, size_t size );
252 
259  const ModuleData* FindOrMapDependency( ImageContext* pImage, std::wstring& path );
260 
266  module_t MapPureManaged();
267 
273  DWORD GetSectionProt( DWORD characteristics );
274 
275 private:
276  vecImageCtx _images; // Mapped images
277  class Process& _process; // Target process manager
278  MemBlock _pAContext; // SxS activation context memory address
279  LdrCallback _ldrCallback = nullptr; // Loader callback for adding image into loader lists
280  void* _ldrContext = nullptr; // user context for _ldrCallback
281 
282  std::vector<std::pair<ptr_t, size_t>> _usedBlocks; // Used memory blocks
283 };
284 
285 }
286 
Exception handling support for arbitrary code
Definition: MExcept.h:31
BLACKBONE_API const ModuleData * MapImage(const std::wstring &path, eLoadFlags flags=NoFlags, LdrCallback ldrCallback=nullptr, void *ldrContext=nullptr)
Manually map PE image into underlying target process
Definition: MMap.cpp:34
Image data
Definition: MMap.h:45
Definition: Process.h:80
Primitive PE parsing class
Definition: PEImage.h:81
Manual image mapper
Definition: MMap.h:65
BLACKBONE_API void Cleanup()
Remove any traces from remote process
Definition: MMap.cpp:1133
Definition: MemBlock.h:39
BLACKBONE_API void reset()
Reset local data
Definition: MMap.h:120
BLACKBONE_API bool UnmapAllModules()
Unmap all manually mapped modules
Definition: MMap.cpp:371
Definition: AsmHelper32.cpp:6
BLACKBONE_API void Reset()
Try to free memory and reset pointers
Definition: MemBlock.cpp:198
Definition: Types.h:57