BlackBone
Windows memory hacking library
 All Classes Functions
NtLoader.h
1 #pragma once
2 
3 #include "../../Include/Winheaders.h"
4 #include "../../PE/PEImage.h"
5 #include "../../Include/NativeStructures.h"
6 #include "../../Include/Macro.h"
7 
8 namespace blackbone
9 {
10 
11 enum LdrRefFlags
12 {
13  Ldr_None = 0x00, // Do not create any reference
14  Ldr_ModList = 0x01, // Add to module list - LdrpModuleIndex( win8 only ), InMemoryOrderModuleList( win7 only )
15  Ldr_HashTable = 0x02, // Add to LdrpHashTable
16  Ldr_ThdCall = 0x04, // Add to thread callback list (dllmain will be called with THREAD_ATTACH/DETACH reasons)
17  Ldr_All = 0xFF // Add to everything
18 };
19 
20 ENUM_OPS( LdrRefFlags )
21 
22 
23 class NtLdr
24 {
25 public:
26  BLACKBONE_API NtLdr( class Process& proc );
27  BLACKBONE_API ~NtLdr( void );
28 
33  BLACKBONE_API bool Init();
34 
45  BLACKBONE_API bool CreateNTReference(
46  HMODULE hMod,
47  size_t ImageSize,
48  const std::wstring& DllBasePath,
49  size_t entryPoint,
50  LdrRefFlags flags = Ldr_All
51  );
52 
59  BLACKBONE_API bool AddStaticTLSEntry( void* pModule, IMAGE_TLS_DIRECTORY *pTls );
60 
69  BLACKBONE_API bool InsertInvertedFunctionTable( void* ModuleBase, size_t ImageSize, bool& safeseh );
70 
77  BLACKBONE_API bool Unlink( ptr_t baseAddress, const std::wstring& name, eModType type );
78 
79  //
80  // Get some not exported values
81  //
82  BLACKBONE_API inline size_t LdrpInvertedFunctionTable( ) const { return _LdrpInvertedFunctionTable; }
83  BLACKBONE_API inline size_t LdrKernel32PatchAddress() const { return _LdrKernel32PatchAddress; }
84  BLACKBONE_API inline size_t APC64PatchAddress() const { return _APC64PatchAddress; }
85  BLACKBONE_API inline size_t LdrProtectMrdata() const { return _LdrProtectMrdata; }
86 
87 private:
88 
93  bool FindLdrpHashTable();
94 
99  bool FindLdrpModuleIndexBase();
100 
105  bool ScanPatterns();
106 
111  bool FindLdrHeap();
112 
122  _LDR_DATA_TABLE_ENTRY_W8* InitW8Node(
123  void* ModuleBase,
124  size_t ImageSize,
125  const std::wstring& dllpath,
126  size_t entryPoint,
127  ULONG& outHash
128  );
129 
139  _LDR_DATA_TABLE_ENTRY_W7* InitW7Node(
140  void* ModuleBase,
141  size_t ImageSize,
142  const std::wstring& dllpath,
143  size_t entryPoint,
144  ULONG& outHash
145  );
146 
151  void InsertTreeNode( _LDR_DATA_TABLE_ENTRY_W8* pNode, size_t modBase );
152 
158  void InsertHashNode( size_t pNodeLink, ULONG hash );
159 
165  void InsertMemModuleNode( size_t pNodeMemoryOrderLink, size_t pNodeLoadOrderLink, size_t pNodeInitOrderLink );
166 
172  void InsertTailList( size_t ListHead, size_t Entry );
173 
179  ULONG HashString( const std::wstring& str );
180 
187  template<typename T>
188  T* SetNode( T* ptr, void* pModule );
189 
195  template<typename T>
196  ptr_t UnlinkFromLdr( ptr_t baseAddress, const std::wstring& name );
197 
206  template<typename T>
207  ptr_t UnlinkListEntry( _LIST_ENTRY_T<T> pListEntry, ptr_t head, size_t ofst, ptr_t baseAddress );
208 
209  template<typename T>
210  void UnlinkListEntry( ptr_t pListLink );
211 
217  ptr_t UnlinkTreeNode( ptr_t ldrEntry );
218 
219  NtLdr( const NtLdr& ) = delete;
220  NtLdr& operator =(const NtLdr&) = delete;
221 
222 private:
223  class Process& _process; // Process memory routines
224 
225  size_t _LdrpHashTable = 0; // LdrpHashTable address
226  size_t _LdrpModuleIndexBase = 0; // LdrpModuleIndex address
227  size_t _LdrHeapBase = 0; // Loader heap base address
228  size_t _LdrKernel32PatchAddress = 0; // Address to patch to enable kernel32 loading under win7
229  size_t _APC64PatchAddress = 0; // Address to patch for x64->WOW64 APC dispatching under win7
230  size_t _LdrpHandleTlsData = 0; // LdrpHandleTlsData address
231  size_t _LdrpInvertedFunctionTable = 0; // LdrpInvertedFunctionTable address
232  size_t _RtlInsertInvertedFunctionTable = 0; // RtlInsertInvertedFunctionTable address
233  size_t _LdrProtectMrdata = 0; // LdrProtectMrdata address
234 
235  std::map<HMODULE, void*> _nodeMap; // Allocated native structures
236 };
237 
238 }
239 
Definition: Win8Specific.h:59
Definition: Process.h:80
Definition: NativeStructures.h:40
Definition: NtLoader.h:23
Definition: Win7Specific.h:9
Definition: AsmHelper32.cpp:6