|
| 1 | +#include <shlobj_core.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string> |
| 4 | +#include <Windows.h> |
| 5 | + |
| 6 | +static HMODULE WinHttp; |
| 7 | +static bool(__stdcall* oWinHttpGetIEProxyConfigForCurrentUser)(void*); |
| 8 | + |
| 9 | +#define EXPORT comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__) |
| 10 | + |
| 11 | +extern "C" __declspec(dllexport) bool __stdcall WinHttpGetIEProxyConfigForCurrentUser(void* proxyConFig) { |
| 12 | + int result = oWinHttpGetIEProxyConfigForCurrentUser(proxyConFig); |
| 13 | + return result; |
| 14 | +} |
| 15 | + |
| 16 | +void initialize(HINSTANCE hinstDLL) {// allocate console window; find original winhttp dll file; find and load it; |
| 17 | +// store handle to dll file; find original handle version |
| 18 | + AllocConsole(); |
| 19 | + FILE* dummy; |
| 20 | + freopen_s(&dummy, "CONIN$", "r", stdin); |
| 21 | + freopen_s(&dummy, "CONOUT$", "w", stderr); |
| 22 | + freopen_s(&dummy, "CONOUT$", "w", stdout); |
| 23 | + |
| 24 | + //Find original wininet.dll |
| 25 | + char sys32Path[MAX_PATH]; |
| 26 | + |
| 27 | + memset(sys32Path, 0, MAX_PATH); |
| 28 | + |
| 29 | +#if INTPTR_MAX == INT64_MAX |
| 30 | + SHGetFolderPathA(nullptr, CSIDL_SYSTEM, nullptr, SHGFP_TYPE_CURRENT, sys32Path); |
| 31 | +#elif INTPTR_MAX == INT32_MAX |
| 32 | + SHGetFolderPathA(nullptr, CSIDL_SYSTEMX86, nullptr, SHGFP_TYPE_CURRENT, sys32Path); |
| 33 | +#endif |
| 34 | + |
| 35 | + std::string sys32Str(sys32Path); |
| 36 | + std::string winhttpPath = sys32Str + "\\Winhttp.dll"; |
| 37 | + |
| 38 | + WinHttp = LoadLibraryA(winhttpPath.c_str()); |
| 39 | + if (WinHttp == NULL) { |
| 40 | + MessageBoxA(0, "Failed to find Winhttp.dll in System32", "Proxy Error", MB_OK); |
| 41 | + exit(1); |
| 42 | + } |
| 43 | + else { |
| 44 | + printf("Loaded original wininet\n"); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + oWinHttpGetIEProxyConfigForCurrentUser = (bool(__stdcall*)(void*))GetProcAddress(WinHttp, "WinHttpGetIEProxyConfigForCurrentUser"); |
| 49 | + if (oWinHttpGetIEProxyConfigForCurrentUser == NULL) { |
| 50 | + MessageBoxA(0, "Failed to find WinHttpGetIEProxyConfigForCurrentUser", "Proxy Error", MB_OK); |
| 51 | + exit(1); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +// Implement dll main |
| 57 | +BOOL WINAPI DllMain( |
| 58 | + HINSTANCE hinstDLL, // handle to DLL module |
| 59 | + DWORD fdwReason, // reason for calling function |
| 60 | + LPVOID lpvReserved) // reserved |
| 61 | +{ |
| 62 | + // Perform actions based on the reason for calling. |
| 63 | + switch (fdwReason) |
| 64 | + { |
| 65 | + case DLL_PROCESS_ATTACH: |
| 66 | + // Initialize once for each new process. |
| 67 | + // Return FALSE to fail DLL load. |
| 68 | + // Write code here |
| 69 | + initialize(hinstDLL); |
| 70 | + break; |
| 71 | + |
| 72 | + case DLL_THREAD_ATTACH: |
| 73 | + // Do thread-specific initialization. |
| 74 | + break; |
| 75 | + |
| 76 | + case DLL_THREAD_DETACH: |
| 77 | + // Do thread-specific cleanup. |
| 78 | + break; |
| 79 | + |
| 80 | + case DLL_PROCESS_DETACH: |
| 81 | + |
| 82 | + if (lpvReserved != nullptr) |
| 83 | + { |
| 84 | + break; // do not do cleanup if process termination scenario |
| 85 | + } |
| 86 | + |
| 87 | + // Perform any necessary cleanup. |
| 88 | + break; |
| 89 | + } |
| 90 | + return TRUE; // Successful DLL_PROCESS_ATTACH. |
| 91 | +} |
| 92 | + |
| 93 | +//WinHttpGetIEProxyConfigForCurrentUser |
| 94 | + |
| 95 | +/* |
| 96 | +1. Write the function |
| 97 | +2. Tell linker don't change function name |
| 98 | +3. Original function |
| 99 | +*/ |
| 100 | + |
0 commit comments