11#include " ../MemoryModule/stdafx.h"
22#include " ../MemoryModule/LoadDllMemoryApi.h"
33#include < cstdio>
4+ #pragma comment(lib,"ntdll.lib")
45
5- // PMMP_GLOBAL_DATA MmpGlobalDataPtr = *(PMMP_GLOBAL_DATA*)GetProcAddress(GetModuleHandleA("MemoryModule.dll"), "MmpGlobalDataPtr");
6+ PMMP_GLOBAL_DATA MmpGlobalDataPtr;
7+
8+ decltype (&LdrLoadDllMemoryExW)__LdrLoadDllMemoryExW;
9+ decltype (&LdrUnloadDllMemory)__LdrUnloadDllMemory;
610
711static void DisplayStatus () {
812 printf (
@@ -41,7 +45,10 @@ static PVOID ReadDllFile(LPCSTR FileName) {
4145 return 0 ;
4246 }
4347 _fseeki64 (f, 0 , SEEK_SET);
44- fread (buffer = new char [size], 1 , size, f);
48+
49+ buffer = VirtualAlloc (0 , size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
50+
51+ fread (buffer, 1 , size, f);
4552 fclose (f);
4653 return buffer;
4754}
@@ -63,11 +70,10 @@ PVOID ReadDllFile2(LPCSTR FileName) {
6370}
6471
6572int test () {
66- LPVOID buffer = ReadDllFile2 (" a.vmp. dll" );
73+ LPVOID buffer = ReadDllFile2 (" a.dll" );
6774
6875 HMODULE hModule = nullptr ;
6976 FARPROC pfn = nullptr ;
70- DWORD MemoryModuleFeatures = 0 ;
7177
7278 typedef int (*_exception)(int code);
7379 _exception exception = nullptr ;
@@ -76,12 +82,7 @@ int test() {
7682 HGLOBAL gRes ;
7783 char str[10 ];
7884
79- LdrQuerySystemMemoryModuleFeatures (&MemoryModuleFeatures);
80- if (MemoryModuleFeatures != MEMORY_FEATURE_ALL) {
81- printf (" not support all features on this version of windows.\n " );
82- }
83-
84- if (!NT_SUCCESS (LdrLoadDllMemoryExW (&hModule, nullptr , 0 , buffer, 0 , L" kernel64" , nullptr ))) goto end;
85+ if (!NT_SUCCESS (__LdrLoadDllMemoryExW (&hModule, nullptr , 0 , buffer, 0 , L" kernel64" , nullptr ))) goto end;
8586
8687 // forward export
8788 pfn = (decltype (pfn))(GetProcAddress (hModule, " Socket" )); // ws2_32.WSASocketW
@@ -127,12 +128,44 @@ int test() {
127128 }
128129
129130end:
130- LdrUnloadDllMemory (hModule);
131- delete[] buffer;
131+ __LdrUnloadDllMemory (hModule);
132+ VirtualFree ( buffer, 0 , MEM_RELEASE) ;
132133 return 0 ;
133134}
134135
136+ ULONG_PTR ReflectiveLoaderOffset () {
137+ ULONG_PTR offset = 0 ;
138+
139+ auto hm = LoadLibrary (L" MemoryModule.dll" );
140+ if (hm) {
141+ auto pfn = GetProcAddress (hm, " ReflectiveLoader" );
142+ offset = ULONG_PTR (pfn) - ULONG_PTR (hm);
143+
144+ auto header = RtlImageNtHeader (hm);
145+ auto section = IMAGE_FIRST_SECTION (header);
146+ for (int i = 0 ; i < header->FileHeader .NumberOfSections ; ++i, ++section) {
147+ if (offset >= section->VirtualAddress && offset < section->VirtualAddress + section->SizeOfRawData ) {
148+ offset = ULONG_PTR (pfn) - (ULONG_PTR (hm) + section->VirtualAddress ) + section->PointerToRawData ;
149+ break ;
150+ }
151+ }
152+ }
153+
154+ return offset;
155+ }
156+
157+ typedef ULONG_PTR (WINAPI* LOADER)(PVOID);
158+
135159int main () {
160+ printf (" %08x\n " , ReflectiveLoaderOffset ());
161+ auto buffer = ReadDllFile2 (" MemoryModule.dll" );
162+ auto loader = LOADER (ULONG_PTR (buffer) + 0x96e0 ); // ReflectiveLoaderOffset() -> 0x96e0
163+ auto hm = (HMODULE)loader (buffer);
164+
165+ MmpGlobalDataPtr = *(PMMP_GLOBAL_DATA*)GetProcAddress (hm, " MmpGlobalDataPtr" );
166+ __LdrLoadDllMemoryExW = (decltype (&LdrLoadDllMemoryExW))GetProcAddress (hm, " LdrLoadDllMemoryExW" );
167+ __LdrUnloadDllMemory = (decltype (&LdrUnloadDllMemory))GetProcAddress (hm, " LdrUnloadDllMemory" );
168+
136169 DisplayStatus ();
137170 test ();
138171
0 commit comments