@@ -18,110 +18,26 @@ static PVOID ReadDllFile(LPCSTR FileName) {
1818 return buffer;
1919}
2020
21- int test_a_dll () {
22- LPVOID buffer = ReadDllFile (" a.dll" );
23-
24- HMEMORYMODULE m1 = nullptr , m2 = m1;
25- HMODULE hModule = nullptr ;
26- FARPROC pfn = nullptr ;
27- DWORD MemoryModuleFeatures = 0 ;
28-
29- typedef int (*_exception)(int code);
30- _exception exception = nullptr ;
31- HRSRC hRsrc;
32- DWORD SizeofRes;
33- HGLOBAL gRes ;
34- char str[10 ];
35-
36- LdrQuerySystemMemoryModuleFeatures (&MemoryModuleFeatures);
37- if (MemoryModuleFeatures != MEMORY_FEATURE_ALL) {
38- printf (" not support all features on this version of windows.\n " );
39- }
40-
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;
44-
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- }
59-
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- }
90- }
91- }
92-
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 test_user32 () {
21+ int test () {
10422 HMODULE hModule;
10523 NTSTATUS status;
106- PVOID buffer = ReadDllFile (" C: \\ Windows \\ System32 \\ user32 .dll" );
24+ PVOID buffer = ReadDllFile (" a .dll" );
10725 if (!buffer) return 0 ;
10826
109- hModule = GetModuleHandleA (" user32.dll" );
110- if (hModule)return 0 ;
111-
11227 status = LdrLoadDllMemoryExW (
11328 &hModule, // ModuleHandle
11429 nullptr , // LdrEntry
11530 0 , // Flags
11631 buffer, // Buffer
11732 0 , // Reserved
118- L" user32 .dll" , // DllBaseName
119- L" C:\\ Windows\\ System32\\ user32 .dll" // DllFullName
33+ L" a .dll" , // DllBaseName
34+ L" C:\\ Windows\\ System32\\ a .dll" // DllFullName
12035 );
12136 if (NT_SUCCESS (status) && status != STATUS_IMAGE_MACHINE_TYPE_MISMATCH) {
12237
123- auto _MessageBoxW = (decltype (&MessageBoxW))GetProcAddress (hModule, " MessageBoxW" );
124- _MessageBoxW (nullptr , L" Hello, from memory user32!" , L" Caption" , MB_OK);
38+ typedef int (__stdcall* func)();
39+ func test_user32 = (func)GetProcAddress (hModule, " test_user32" );
40+ test_user32 ();
12541
12642 //
12743 // After calling MessageBox, we can't free it.
@@ -133,6 +49,6 @@ int test_user32() {
13349}
13450
13551int main () {
136- test_a_dll ();
52+ test ();
13753 return 0 ;
13854}
0 commit comments