@@ -18,7 +18,89 @@ static PVOID ReadDllFile(LPCSTR FileName) {
1818 return buffer;
1919}
2020
21- int main () {
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 () {
22104 HMODULE hModule;
23105 NTSTATUS status;
24106 PVOID buffer = ReadDllFile (" C:\\ Windows\\ System32\\ user32.dll" );
@@ -49,3 +131,8 @@ int main() {
49131
50132 return 0 ;
51133}
134+
135+ int main () {
136+ test_a_dll ();
137+ return 0 ;
138+ }
0 commit comments