@@ -19,52 +19,89 @@ static PVOID ReadDllFile(LPCSTR FileName) {
1919}
2020
2121int test () {
22- HMODULE hModule;
23- NTSTATUS status;
24- PVOID buffer = ReadDllFile (" a.dll" );
25- if (!buffer) return 0 ;
22+ LPVOID buffer = ReadDllFile (" a.dll" );
2623
27- status = LdrLoadDllMemoryExW (
28- &hModule, // ModuleHandle
29- nullptr , // LdrEntry
30- 0 , // Flags
31- buffer, // Buffer
32- 0 , // Reserved
33- nullptr , // DllBaseName
34- nullptr // DllFullName
35- );
36- if (NT_SUCCESS (status) && status != STATUS_IMAGE_MACHINE_TYPE_MISMATCH) {
24+ HMEMORYMODULE m1 = nullptr , m2 = m1;
25+ HMODULE hModule = nullptr ;
26+ FARPROC pfn = nullptr ;
27+ DWORD MemoryModuleFeatures = 0 ;
3728
38- typedef int (__stdcall* func)();
39- func test_user32 = (func)GetProcAddress (hModule, " test_user32" );
40- test_user32 ();
29+ typedef int (*_exception)(int code);
30+ _exception exception = nullptr ;
31+ HRSRC hRsrc;
32+ DWORD SizeofRes;
33+ HGLOBAL gRes ;
34+ char str[10 ];
4135
42- //
43- // After calling MessageBox, we can't free it.
44- //
45- // LdrUnloadDllMemory(hModule);
36+ LdrQuerySystemMemoryModuleFeatures (&MemoryModuleFeatures);
37+ if (MemoryModuleFeatures != MEMORY_FEATURE_ALL) {
38+ printf (" not support all features on this version of windows.\n " );
4639 }
4740
48- return 0 ;
49- }
41+ if (!NT_SUCCESS (LdrLoadDllMemoryExW (&m1, nullptr , 0 , buffer, 0 , L" kernel64" , nullptr ))) goto end;
42+ LoadLibraryW (L" wininet.dll" );
43+ if (!NT_SUCCESS (LdrLoadDllMemoryExW (&m2, nullptr , 0 , buffer, 0 , L" kernel128" , nullptr ))) goto end;
5044
51- int main () {
52- if (MmpGlobalDataPtr->WindowsVersion == WINDOWS_VERSION::win11) {
53- auto head = &NtCurrentPeb ()->Ldr ->InLoadOrderModuleList ;
54- auto entry = head->Flink ;
55- while (entry != head) {
56- PLDR_DATA_TABLE_ENTRY_WIN11 __entry = CONTAINING_RECORD (entry, LDR_DATA_TABLE_ENTRY_WIN11, InLoadOrderLinks);
57- wprintf (L" %s\t 0x%08X, 0x%08X, 0x%p, %d\n " ,
58- __entry->BaseDllName .Buffer ,
59- __entry->CheckSum ,
60- RtlImageNtHeader (__entry->DllBase )->OptionalHeader .CheckSum ,
61- __entry->ActivePatchImageBase ,
62- __entry->HotPatchState
63- );
45+ // forward export
46+ hModule = (HMODULE)m1;
47+ pfn = (decltype (pfn))(GetProcAddress (hModule, " Socket" )); // ws2_32.WSASocketW
48+ pfn = (decltype (pfn))(GetProcAddress (hModule, " VerifyTruse" )); // wintrust.WinVerifyTrust
49+ hModule = (HMODULE)m2;
50+ pfn = (decltype (pfn))(GetProcAddress (hModule, " Socket" ));
51+ pfn = (decltype (pfn))(GetProcAddress (hModule, " VerifyTruse" ));
52+
53+ // exception
54+ hModule = (HMODULE)m1;
55+ exception = (_exception)GetProcAddress (hModule, " exception" );
56+ if (exception) {
57+ for (int i = 0 ; i < 5 ; ++i)exception (i);
58+ }
6459
65- entry = entry->Flink ;
60+ // tls
61+ pfn = GetProcAddress (hModule, " thread" );
62+ if (pfn && pfn ()) {
63+ printf (" thread test failed.\n " );
64+ }
65+
66+ // resource
67+ if (!LoadStringA (hModule, 101 , str, 10 )) {
68+ printf (" load string failed.\n " );
69+ }
70+ else {
71+ printf (" %s\n " , str);
72+ }
73+ if (!(hRsrc = FindResourceA (hModule, MAKEINTRESOURCEA (102 ), " BINARY" ))) {
74+ printf (" find binary resource failed.\n " );
75+ }
76+ else {
77+ if ((SizeofRes = SizeofResource (hModule, hRsrc)) != 0x10 ) {
78+ printf (" invalid res size.\n " );
79+ }
80+ else {
81+ if (!(gRes = LoadResource (hModule, hRsrc))) {
82+ printf (" load res failed.\n " );
83+ }
84+ else {
85+ if (!LockResource (gRes ))printf (" lock res failed.\n " );
86+ else {
87+ printf (" resource test success.\n " );
88+ }
89+ }
6690 }
6791 }
6892
93+ end:
94+ delete[] buffer;
95+ if (m1)LdrUnloadDllMemory (m1);
96+ FreeLibrary (LoadLibraryW (L" wininet.dll" ));
97+ FreeLibrary (GetModuleHandleW (L" wininet.dll" ));
98+ if (m2)LdrUnloadDllMemory (m2);
99+
100+ return 0 ;
101+ }
102+
103+ int main () {
104+ test ();
105+
69106 return 0 ;
70107}
0 commit comments