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 
86 private:
87 
92  bool FindLdrpHashTable();
93 
98  bool FindLdrpModuleIndexBase();
99 
104  bool ScanPatterns();
105 
110  bool FindLdrHeap();
111 
121  _LDR_DATA_TABLE_ENTRY_W8* InitW8Node(
122  void* ModuleBase,
123  size_t ImageSize,
124  const std::wstring& dllpath,
125  size_t entryPoint,
126  ULONG& outHash
127  );
128 
138  _LDR_DATA_TABLE_ENTRY_W7* InitW7Node(
139  void* ModuleBase,
140  size_t ImageSize,
141  const std::wstring& dllpath,
142  size_t entryPoint,
143  ULONG& outHash
144  );
145 
150  void InsertTreeNode( _LDR_DATA_TABLE_ENTRY_W8* pNode, size_t modBase );
151 
157  void InsertHashNode( size_t pNodeLink, ULONG hash );
158 
164  void InsertMemModuleNode( size_t pNodeMemoryOrderLink, size_t pNodeLoadOrderLink, size_t pNodeInitOrderLink );
165 
171  void InsertTailList( size_t ListHead, size_t Entry );
172 
178  ULONG HashString( const std::wstring& str );
179 
186  template<typename T>
187  T* SetNode( T* ptr, void* pModule );
188 
194  template<typename T>
195  ptr_t UnlinkFromLdr( ptr_t baseAddress, const std::wstring& name );
196 
205  template<typename T>
206  ptr_t UnlinkListEntry( _LIST_ENTRY_T<T> pListEntry, ptr_t head, size_t ofst, ptr_t baseAddress );
207 
208  template<typename T>
209  void UnlinkListEntry( ptr_t pListLink );
210 
216  ptr_t UnlinkTreeNode( ptr_t ldrEntry );
217 
218  NtLdr( const NtLdr& ) = delete;
219  NtLdr& operator =(const NtLdr&) = delete;
220 
221 private:
222  class Process& _process; // Process memory routines
223 
224  size_t _LdrpHashTable = 0; // LdrpHashTable address
225  size_t _LdrpModuleIndexBase = 0; // LdrpModuleIndex address
226  size_t _LdrpModuleBase = 0; // PEB->Ldr->InLoadOrderModuleList 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 to 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 
234  std::map<HMODULE, void*> _nodeMap; // Allocated native structures
235 };
236 
237 }
238 
Definition: Win8Specific.h:59
Definition: Process.h:43
Definition: NativeStructures.h:29
Definition: NtLoader.h:23
Definition: Win7Specific.h:9
Definition: AsmHelper32.cpp:6