From bae707f865dbd2d39e2b846c6533b4354d0f751e Mon Sep 17 00:00:00 2001 From: FunkyFr3sh Date: Mon, 23 Oct 2023 08:20:25 +0200 Subject: [PATCH] use different function to enumarte modules for mingw build --- inc/utils.h | 1 + src/hook.c | 28 ++------------------------ src/utils.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/inc/utils.h b/inc/utils.h index 4eee8a6..2e2e779 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -5,6 +5,7 @@ #include +HMODULE WINAPI util_enumerate_modules(_In_opt_ HMODULE hModuleLast); BOOL util_is_bad_read_ptr(void* p); BOOL util_is_minimized(HWND hwnd); BOOL util_is_avx_supported(); diff --git a/src/hook.c b/src/hook.c index ebd1c26..b6a7a6a 100644 --- a/src/hook.c +++ b/src/hook.c @@ -474,17 +474,8 @@ void hook_create(HOOKLIST* hooks, BOOL initial_hook) char mod_dir[MAX_PATH] = { 0 }; char mod_filename[MAX_PATH] = { 0 }; HMODULE hmod = NULL; - HANDLE process = NULL; -#ifndef _MSC_VER - HMODULE mods[512]; - memset(mods, 0, sizeof(mods)); - process = GetCurrentProcess(); - EnumProcessModules(process, mods, sizeof(mods) - sizeof(mods[0]), NULL); - for (int i = 0; i < sizeof(mods) / sizeof(mods[0]) && (hmod = mods[i]); i++) -#else - while (hmod = DetourEnumerateModules(hmod)) -#endif + while ((hmod = util_enumerate_modules(hmod))) { if (hmod == g_ddraw_module) continue; @@ -515,9 +506,6 @@ void hook_create(HOOKLIST* hooks, BOOL initial_hook) } } } - - if (process) - CloseHandle(process); } } @@ -561,17 +549,8 @@ void hook_revert(HOOKLIST* hooks) char mod_dir[MAX_PATH] = { 0 }; char mod_filename[MAX_PATH] = { 0 }; HMODULE hmod = NULL; - HANDLE process = NULL; -#ifndef _MSC_VER - HMODULE mods[512]; - memset(mods, 0, sizeof(mods)); - process = GetCurrentProcess(); - EnumProcessModules(process, mods, sizeof(mods) - sizeof(mods[0]), NULL); - for (int i = 0; i < sizeof(mods) / sizeof(mods[0]) && (hmod = mods[i]); i++) -#else - while (hmod = DetourEnumerateModules(hmod)) -#endif + while ((hmod = util_enumerate_modules(hmod))) { if (hmod == g_ddraw_module) continue; @@ -591,9 +570,6 @@ void hook_revert(HOOKLIST* hooks) } } } - - if (process) - CloseHandle(process); } } diff --git a/src/utils.c b/src/utils.c index 2f97674..41a4c08 100644 --- a/src/utils.c +++ b/src/utils.c @@ -11,6 +11,63 @@ #include "config.h" +/* + The following code is licensed under the MIT license: + DetourEnumerateModules + Copyright (c) Microsoft Corporation + https://github.com/microsoft/Detours +*/ +#define MM_ALLOCATION_GRANULARITY 0x10000 + +HMODULE WINAPI util_enumerate_modules(_In_opt_ HMODULE hModuleLast) +{ + PBYTE pbLast = (PBYTE)hModuleLast + MM_ALLOCATION_GRANULARITY; + + MEMORY_BASIC_INFORMATION mbi; + ZeroMemory(&mbi, sizeof(mbi)); + + // Find the next memory region that contains a mapped PE image. + // + for (;; pbLast = (PBYTE)mbi.BaseAddress + mbi.RegionSize) { + if (VirtualQuery(pbLast, &mbi, sizeof(mbi)) <= 0) { + break; + } + + // Skip uncommitted regions and guard pages. + // + if ((mbi.State != MEM_COMMIT) || + ((mbi.Protect & 0xff) == PAGE_NOACCESS) || + (mbi.Protect & PAGE_GUARD)) { + continue; + } + + __try { + PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)pbLast; + if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE || + (DWORD)pDosHeader->e_lfanew > mbi.RegionSize || + (DWORD)pDosHeader->e_lfanew < sizeof(*pDosHeader)) { + continue; + } + + PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader + + pDosHeader->e_lfanew); + if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) { + continue; + } + + return (HMODULE)pDosHeader; + } +#if defined(_MSC_VER) +#pragma prefast(suppress:28940, "A bad pointer means this probably isn't a PE header.") +#endif + __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ? + EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) { + continue; + } + } + return NULL; +} + BOOL util_is_bad_read_ptr(void* p) { MEMORY_BASIC_INFORMATION mbi = { 0 };