BlackBone
Windows memory hacking library
 All Classes Functions
PEImage.h
1 #pragma once
2 
3 #include "../Config.h"
4 #include "../Include/Winheaders.h"
5 #include "../Include/Types.h"
6 #include "../Misc/Utils.h"
7 
8 #ifdef COMPILER_MSVC
9 #include "ImageNET.h"
10 #endif // COMPILER_MSVC
11 
12 #include <string>
13 #include <memory>
14 #include <vector>
15 #include <map>
16 #include <unordered_map>
17 #include <set>
18 #include <list>
19 
20 namespace blackbone
21 {
22 
23 namespace pe
24 {
25 
26 // Relocation block information
27 struct RelocData
28 {
29  ULONG PageRVA;
30  ULONG BlockSize;
31 
32  struct
33  {
34  WORD Offset : 12;
35  WORD Type : 4;
36  }Item[1];
37 };
38 
42 struct ImportData
43 {
44  std::string importName; // Function name
45  size_t ptrRVA; // Function pointer RVA in
46  WORD importOrdinal; // Function ordinal
47  bool importByOrd; // Function is imported by ordinal
48 };
49 
53 struct ExportData
54 {
55  std::string name;
56  uint32_t RVA = 0;
57 
58  ExportData( const std::string& name_, uint32_t rva_ )
59  : name( name_ )
60  , RVA( rva_ ) { }
61 
62  bool operator == (const ExportData& other)
63  {
64  return name == other.name;
65  }
66 
67  bool operator < (const ExportData& other)
68  {
69  return name < other.name;
70  }
71 };
72 
73 // Imports and sections related
74 typedef std::unordered_map<std::wstring, std::vector<ImportData>> mapImports;
75 typedef std::vector<IMAGE_SECTION_HEADER> vecSections;
76 typedef std::vector<ExportData> vecExports;
77 
81 class PEImage
82 {
83  typedef const IMAGE_NT_HEADERS32* PCHDR32;
84  typedef const IMAGE_NT_HEADERS64* PCHDR64;
85 
86 public:
87  BLACKBONE_API PEImage( void );
88  BLACKBONE_API ~PEImage( void );
89 
96  BLACKBONE_API NTSTATUS Load( const std::wstring& path, bool skipActx = false );
97 
105  BLACKBONE_API NTSTATUS Load( void* pData, size_t size, bool plainData = true );
106 
110  BLACKBONE_API void Release();
111 
116  BLACKBONE_API NTSTATUS Parse( void* pImageBase = nullptr );
117 
123  BLACKBONE_API mapImports& GetImports( bool useDelayed = false );
124 
129  BLACKBONE_API void GetExports( vecExports& exports );
130 
138  BLACKBONE_API int GetTLSCallbacks( module_t targetBase, std::vector<ptr_t>& result ) const;
139 
146  BLACKBONE_API size_t DirectoryAddress( int index, bool keepRelative = false ) const;
147 
153  BLACKBONE_API size_t DirectorySize( int index ) const;
154 
161  BLACKBONE_API size_t ResolveRVAToVA( size_t Rva, bool keepRelative = false ) const;
162 
167  BLACKBONE_API inline const std::wstring& path() const { return _imagePath; }
168 
173  BLACKBONE_API inline std::wstring name() const { return Utils::StripPath( _imagePath ); }
174 
179  BLACKBONE_API inline void* base() const { return _pFileBase; }
180 
185  BLACKBONE_API inline module_t imageBase() const { return _imgBase; }
186 
191  BLACKBONE_API inline size_t imageSize() const { return _imgSize; }
192 
197  BLACKBONE_API inline size_t headersSize() const { return _hdrSize; }
198 
204  BLACKBONE_API inline ptr_t entryPoint( module_t base ) const { return ((_epRVA != 0) ? (_epRVA + base) : 0); };
205 
210  BLACKBONE_API inline const vecSections& sections() const { return _sections; }
211 
216  BLACKBONE_API inline bool IsExe() const { return _isExe; }
217 
222  BLACKBONE_API inline bool IsPureManaged() const { return _isPureIL; }
223 
228  BLACKBONE_API inline eModType mType() const { return _is64 ? mt_mod64 : mt_mod32; }
229 
234  BLACKBONE_API inline HANDLE actx() const { return _hctx; }
235 
240  BLACKBONE_API inline bool isPlainData() const { return _isPlainData; }
241 
246  BLACKBONE_API inline int manifestID() const { return _manifestIdx; }
247 
252  BLACKBONE_API inline uint32_t subsystem() const { return _subsystem; }
253 
258  BLACKBONE_API inline const std::wstring& manifestFile() const { return _manifestPath; }
259 
264  BLACKBONE_API inline bool noPhysFile() const { return _noFile; }
265 
266 #ifdef COMPILER_MSVC
267  BLACKBONE_API ImageNET& net() { return _netImage; }
272 #endif
273 
274 private:
280  NTSTATUS PrepareACTX( const wchar_t* filepath = nullptr );
281 
288  void* GetManifest( uint32_t& size, int& manifestID );
289 
290 private:
291  HANDLE _hFile = INVALID_HANDLE_VALUE; // Target file HANDLE
292  HANDLE _hMapping = NULL; // Memory mapping object
293  void* _pFileBase = nullptr; // Mapping base
294  bool _isPlainData = false; // File mapped as plain data file
295  bool _is64 = false; // Image is 64 bit
296  bool _isExe = false; // Image is an .exe file
297  bool _isPureIL = false; // Pure IL image
298  bool _noFile = false; // Parsed from memory, no underlying PE file available
299  PCHDR32 _pImageHdr32 = nullptr; // PE header info
300  PCHDR64 _pImageHdr64 = nullptr; // PE header info
301  ptr_t _imgBase = 0; // Image base
302  uint32_t _imgSize = 0; // Image size
303  uint32_t _epRVA = 0; // Entry point RVA
304  uint32_t _hdrSize = 0; // Size of headers
305  HANDLE _hctx = INVALID_HANDLE_VALUE; // Activation context
306  int _manifestIdx = 0; // Manifest resource ID
307  uint32_t _subsystem; // Image subsystem
308 
309  vecSections _sections; // Section info
310  mapImports _imports; // Import functions
311  mapImports _delayImports; // Import functions
312 
313  std::wstring _imagePath; // Image path
314  std::wstring _manifestPath; // Image manifest container
315 
316 #ifdef COMPILER_MSVC
317  ImageNET _netImage; // .net image info
318 #endif
319 };
320 
321 }
322 }
BLACKBONE_API bool IsExe() const
Check if image is an executable file and not a dll
Definition: PEImage.h:216
BLACKBONE_API mapImports & GetImports(bool useDelayed=false)
Processes image imports
Definition: PEImage.cpp:218
Import information
Definition: PEImage.h:42
BLACKBONE_API bool isPlainData() const
true if image is mapped as plain data file
Definition: PEImage.h:240
BLACKBONE_API eModType mType() const
Get image type. 32/64 bit
Definition: PEImage.h:228
BLACKBONE_API module_t imageBase() const
Get image base address
Definition: PEImage.h:185
BLACKBONE_API int manifestID() const
Get manifest resource ID
Definition: PEImage.h:246
Export function info
Definition: PEImage.h:53
BLACKBONE_API NTSTATUS Load(const std::wstring &path, bool skipActx=false)
Load image from file
Definition: PEImage.cpp:33
BLACKBONE_API std::wstring name() const
Get image name
Definition: PEImage.h:173
BLACKBONE_API size_t headersSize() const
Get size of image headers
Definition: PEImage.h:197
BLACKBONE_API size_t DirectorySize(int index) const
Get data directory size
Definition: PEImage.cpp:404
BLACKBONE_API void * base() const
Get image load address
Definition: PEImage.h:179
BLACKBONE_API void GetExports(vecExports &exports)
Retrieve all exported functions with names
Definition: PEImage.cpp:332
Definition: PEImage.h:27
Primitive PE parsing class
Definition: PEImage.h:81
BLACKBONE_API size_t DirectoryAddress(int index, bool keepRelative=false) const
Retrieve data directory address
Definition: PEImage.cpp:357
BLACKBONE_API const std::wstring & path() const
Get image path
Definition: PEImage.h:167
BLACKBONE_API HANDLE actx() const
Get activation context handle
Definition: PEImage.h:234
BLACKBONE_API size_t imageSize() const
Get image size in bytes
Definition: PEImage.h:191
BLACKBONE_API size_t ResolveRVAToVA(size_t Rva, bool keepRelative=false) const
Resolve virtual memory address to physical file offset
Definition: PEImage.cpp:378
BLACKBONE_API void Release()
Release mapping, if any
Definition: PEImage.cpp:102
BLACKBONE_API const std::wstring & manifestFile() const
Get manifest resource file
Definition: PEImage.h:258
BLACKBONE_API ptr_t entryPoint(module_t base) const
Get image entry point rebased to another image base
Definition: PEImage.h:204
BLACKBONE_API uint32_t subsystem() const
Get image subsystem
Definition: PEImage.h:252
BLACKBONE_API bool IsPureManaged() const
Check if image is pure IL image
Definition: PEImage.h:222
static BLACKBONE_API std::wstring StripPath(const std::wstring &path)
Get filename from full-qualified path
Definition: Utils.cpp:81
Definition: AsmHelper32.cpp:6
BLACKBONE_API int GetTLSCallbacks(module_t targetBase, std::vector< ptr_t > &result) const
Retrieve image TLS callbacks Callbacks are rebased for target image
Definition: PEImage.cpp:427
BLACKBONE_API NTSTATUS Parse(void *pImageBase=nullptr)
Parses PE image
Definition: PEImage.cpp:145
BLACKBONE_API const vecSections & sections() const
Get image sections
Definition: PEImage.h:210
BLACKBONE_API bool noPhysFile() const
If true - no actual PE file available on disk
Definition: PEImage.h:264