|
1 | 1 | #include "../MemoryModule/stdafx.h" |
2 | 2 | #include "../MemoryModule/LoadDllMemoryApi.h" |
3 | 3 | #include <cstdio> |
| 4 | +#pragma comment(lib,"ntdll.lib") |
4 | 5 |
|
5 | 6 | //PMMP_GLOBAL_DATA MmpGlobalDataPtr = *(PMMP_GLOBAL_DATA*)GetProcAddress(GetModuleHandleA("MemoryModule.dll"), "MmpGlobalDataPtr"); |
6 | 7 |
|
@@ -36,24 +37,107 @@ static void DisplayStatus() { |
36 | 37 | ); |
37 | 38 | } |
38 | 39 |
|
| 40 | +PVOID ReadDllFile2(LPCSTR FileName) { |
| 41 | + CHAR path[MAX_PATH + 4]; |
| 42 | + DWORD len = GetModuleFileNameA(nullptr, path, sizeof(path)); |
| 43 | + |
| 44 | + if (len) { |
| 45 | + while (len && path[len] != '\\') --len; |
| 46 | + |
| 47 | + if (len) { |
| 48 | + strcpy_s(&path[len + 1], sizeof(path) - len - 1, FileName); |
| 49 | + return ReadDllFile(path); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return nullptr; |
| 54 | +} |
| 55 | + |
39 | 56 | int test() { |
40 | | - LPVOID buffer = ReadDllFile("a.dll"); |
| 57 | + LPVOID buffer = ReadDllFile2("a.dll"); |
| 58 | + |
| 59 | + HMEMORYMODULE m1 = nullptr, m2 = m1; |
| 60 | + HMODULE hModule = nullptr; |
| 61 | + FARPROC pfn = nullptr; |
| 62 | + DWORD MemoryModuleFeatures = 0; |
| 63 | + |
| 64 | + typedef int(*_exception)(int code); |
| 65 | + _exception exception = nullptr; |
| 66 | + HRSRC hRsrc; |
| 67 | + DWORD SizeofRes; |
| 68 | + HGLOBAL gRes; |
| 69 | + char str[10]; |
| 70 | + |
| 71 | + LdrQuerySystemMemoryModuleFeatures(&MemoryModuleFeatures); |
| 72 | + if (MemoryModuleFeatures != MEMORY_FEATURE_ALL) { |
| 73 | + printf("not support all features on this version of windows.\n"); |
| 74 | + } |
| 75 | + |
| 76 | + if (!NT_SUCCESS(LdrLoadDllMemoryExW(&m1, nullptr, 0, buffer, 0, L"kernel64", nullptr))) goto end; |
| 77 | + LoadLibraryW(L"wininet.dll"); |
| 78 | + if (!NT_SUCCESS(LdrLoadDllMemoryExW(&m2, nullptr, 0, buffer, 0, L"kernel128", nullptr))) goto end; |
41 | 79 |
|
42 | | - HMODULE hm = LoadLibraryMemory(buffer); |
43 | | - FARPROC fp = GetProcAddress(hm, "GdiplusTest"); |
44 | | - bool c = true; |
45 | | - while (c) { |
46 | | - fp(); |
| 80 | + //forward export |
| 81 | + hModule = (HMODULE)m1; |
| 82 | + pfn = (decltype(pfn))(GetProcAddress(hModule, "Socket")); //ws2_32.WSASocketW |
| 83 | + pfn = (decltype(pfn))(GetProcAddress(hModule, "VerifyTruse")); //wintrust.WinVerifyTrust |
| 84 | + hModule = (HMODULE)m2; |
| 85 | + pfn = (decltype(pfn))(GetProcAddress(hModule, "Socket")); |
| 86 | + pfn = (decltype(pfn))(GetProcAddress(hModule, "VerifyTruse")); |
| 87 | + |
| 88 | + //exception |
| 89 | + hModule = (HMODULE)m1; |
| 90 | + exception = (_exception)GetProcAddress(hModule, "exception"); |
| 91 | + if (exception) { |
| 92 | + for (int i = 0; i < 5; ++i)exception(i); |
| 93 | + } |
| 94 | + |
| 95 | + //tls |
| 96 | + pfn = GetProcAddress(hModule, "thread"); |
| 97 | + if (pfn && pfn()) { |
| 98 | + printf("thread test failed.\n"); |
| 99 | + } |
| 100 | + |
| 101 | + //resource |
| 102 | + if (!LoadStringA(hModule, 101, str, 10)) { |
| 103 | + printf("load string failed.\n"); |
| 104 | + } |
| 105 | + else { |
| 106 | + printf("%s\n", str); |
47 | 107 | } |
48 | | - |
| 108 | + if (!(hRsrc = FindResourceA(hModule, MAKEINTRESOURCEA(102), "BINARY"))) { |
| 109 | + printf("find binary resource failed.\n"); |
| 110 | + } |
| 111 | + else { |
| 112 | + if ((SizeofRes = SizeofResource(hModule, hRsrc)) != 0x10) { |
| 113 | + printf("invalid res size.\n"); |
| 114 | + } |
| 115 | + else { |
| 116 | + if (!(gRes = LoadResource(hModule, hRsrc))) { |
| 117 | + printf("load res failed.\n"); |
| 118 | + } |
| 119 | + else { |
| 120 | + if (!LockResource(gRes))printf("lock res failed.\n"); |
| 121 | + else { |
| 122 | + printf("resource test success.\n"); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +end: |
| 129 | + delete[]buffer; |
| 130 | + if (m1)LdrUnloadDllMemory(m1); |
| 131 | + FreeLibrary(LoadLibraryW(L"wininet.dll")); |
| 132 | + FreeLibrary(GetModuleHandleW(L"wininet.dll")); |
| 133 | + if (m2)LdrUnloadDllMemory(m2); |
| 134 | + |
49 | 135 | return 0; |
50 | 136 | } |
51 | 137 |
|
52 | 138 | int main() { |
53 | 139 | DisplayStatus(); |
54 | 140 | test(); |
55 | 141 |
|
56 | | - WaitForSingleObject(NtCurrentProcess(), INFINITE); |
57 | | - |
58 | 142 | return 0; |
59 | 143 | } |
0 commit comments