From 5294ba71d81ca1f0ed631c738120307e33d63f2f Mon Sep 17 00:00:00 2001 From: Rene Nyffenegger Date: Sat, 2 Feb 2019 15:42:35 +0100 Subject: [PATCH 1/5] Cast function pointers to PVOID --- mhook-test/mhook-test.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mhook-test/mhook-test.cpp b/mhook-test/mhook-test.cpp index 06c0b2d..fe5eb03 100644 --- a/mhook-test/mhook-test.cpp +++ b/mhook-test/mhook-test.cpp @@ -127,7 +127,7 @@ int wmain(int argc, WCHAR* argv[]) HANDLE hProc = NULL; // Set the hook - if (Mhook_SetHook((PVOID*)&TrueNtOpenProcess, HookNtOpenProcess)) + if (Mhook_SetHook((PVOID*)&TrueNtOpenProcess, (PVOID) HookNtOpenProcess)) { // Now call OpenProcess and observe NtOpenProcess being redirected // under the hood. @@ -165,7 +165,7 @@ int wmain(int argc, WCHAR* argv[]) // extra work under the hood to make things work properly. This really // is more of a test case rather than a demo.) printf("Testing SelectObject.\n"); - if (Mhook_SetHook((PVOID*)&TrueSelectObject, HookSelectobject)) + if (Mhook_SetHook((PVOID*)&TrueSelectObject, (PVOID) HookSelectobject)) { // error checking omitted for brevity. doesn't matter much // in this context anyway. @@ -182,7 +182,7 @@ int wmain(int argc, WCHAR* argv[]) } printf("Testing getaddrinfo.\n"); - if (Mhook_SetHook((PVOID*)&Truegetaddrinfo, Hookgetaddrinfo)) + if (Mhook_SetHook((PVOID*)&Truegetaddrinfo, (PVOID) Hookgetaddrinfo)) { // error checking omitted for brevity. doesn't matter much // in this context anyway. @@ -214,7 +214,7 @@ int wmain(int argc, WCHAR* argv[]) } printf("Testing HeapAlloc.\n"); - if (Mhook_SetHook((PVOID*)&TrueHeapAlloc, HookHeapAlloc)) + if (Mhook_SetHook((PVOID*)&TrueHeapAlloc, (PVOID) HookHeapAlloc)) { free(malloc(10)); // Remove the hook @@ -222,7 +222,7 @@ int wmain(int argc, WCHAR* argv[]) } printf("Testing NtClose.\n"); - if (Mhook_SetHook((PVOID*)&TrueNtClose, HookNtClose)) + if (Mhook_SetHook((PVOID*)&TrueNtClose, (PVOID) HookNtClose)) { CloseHandle(NULL); // Remove the hook From 22e0a78a187104d7a00d2441e660be8d8f74e666 Mon Sep 17 00:00:00 2001 From: Rene Nyffenegger Date: Sat, 2 Feb 2019 16:17:18 +0100 Subject: [PATCH 2/5] MAX rather than max according to definition in misc.h --- mhook-lib/mhook.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mhook-lib/mhook.cpp b/mhook-lib/mhook.cpp index d87615c..054ba7c 100644 --- a/mhook-lib/mhook.cpp +++ b/mhook-lib/mhook.cpp @@ -463,7 +463,7 @@ static MHOOKS_TRAMPOLINE* BlockAlloc(PBYTE pSystemFunction, PBYTE pbLower, PBYTE ::GetSystemInfo(&sSysInfo); // Always allocate in bulk, in case the system actually has a smaller allocation granularity than MINALLOCSIZE. - const ptrdiff_t cAllocSize = max(sSysInfo.dwAllocationGranularity, MHOOK_MINALLOCSIZE); + const ptrdiff_t cAllocSize = MAX(sSysInfo.dwAllocationGranularity, MHOOK_MINALLOCSIZE); MHOOKS_TRAMPOLINE* pRetVal = NULL; PBYTE pModuleGuess = (PBYTE) RoundDown((size_t)pSystemFunction, cAllocSize); From e905712cc2f5d17d4491b8b3c8b26f8b84ae7a0d Mon Sep 17 00:00:00 2001 From: Rene Nyffenegger Date: Sat, 2 Feb 2019 16:20:30 +0100 Subject: [PATCH 3/5] Align forward declaration with definition --- disasm-lib/disasm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/disasm-lib/disasm.c b/disasm-lib/disasm.c index 03076b1..14899dc 100644 --- a/disasm-lib/disasm.c +++ b/disasm-lib/disasm.c @@ -37,7 +37,7 @@ typedef struct _DISASM_ARG_INFO ////////////////////////////////////////////////////////////////////// BOOL InitInstruction(INSTRUCTION *Instruction, DISASSEMBLER *Disassembler); -struct _ARCHITECTURE_FORMAT *GetArchitectureFormat(ARCHITECTURE_TYPE Type); +static struct _ARCHITECTURE_FORMAT *GetArchitectureFormat(ARCHITECTURE_TYPE Type); ////////////////////////////////////////////////////////////////////// // Disassembler setup From a16fa7dea1a81a4308ee7e00bd40ec6367856ad8 Mon Sep 17 00:00:00 2001 From: Rene Nyffenegger Date: Sat, 2 Feb 2019 17:58:18 +0100 Subject: [PATCH 4/5] A pointer to the function to be hooked is sufficient --- mhook-lib/mhook.cpp | 4 ++-- mhook-lib/mhook.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mhook-lib/mhook.cpp b/mhook-lib/mhook.cpp index 054ba7c..a66ec38 100644 --- a/mhook-lib/mhook.cpp +++ b/mhook-lib/mhook.cpp @@ -1216,7 +1216,7 @@ int Mhook_SetHookEx(HOOK_INFO* hooks, int hookCount) for (int idx = 0; idx < hookCount; idx++) { - hookCtx[idx].pSystemFunction = *hooks[idx].ppSystemFunction; + hookCtx[idx].pSystemFunction = hooks[idx].pSystemFunction; hookCtx[idx].pHookFunction = hooks[idx].pHookFunction; hookCtx[idx].pTrampoline = NULL; hookCtx[idx].dwInstructionLength = 0; @@ -1365,7 +1365,7 @@ int Mhook_SetHookEx(HOOK_INFO* hooks, int hookCount) { // this is what the application will use as the entry point // to the "original" unhooked function. - *hooks[i].ppSystemFunction = hookCtx[i].pTrampoline->codeTrampoline; + hooks[i].pSystemFunction = hookCtx[i].pTrampoline->codeTrampoline; } // flush instruction cache and restore original protection diff --git a/mhook-lib/mhook.h b/mhook-lib/mhook.h index 5332e17..77f6264 100644 --- a/mhook-lib/mhook.h +++ b/mhook-lib/mhook.h @@ -26,7 +26,7 @@ struct HOOK_INFO { - PVOID *ppSystemFunction; // pointer to pointer to function to be hooked + PVOID pSystemFunction; // pointer to function to be hooked PVOID pHookFunction; // hook function }; From 32b45fa6f9627cd6b9ad741510fd65b2deefb423 Mon Sep 17 00:00:00 2001 From: Rene Nyffenegger Date: Sat, 2 Feb 2019 19:31:15 +0100 Subject: [PATCH 5/5] use const char* --- mhook-test/mhook-test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mhook-test/mhook-test.cpp b/mhook-test/mhook-test.cpp index fe5eb03..84dd524 100644 --- a/mhook-test/mhook-test.cpp +++ b/mhook-test/mhook-test.cpp @@ -188,7 +188,7 @@ int wmain(int argc, WCHAR* argv[]) // in this context anyway. WSADATA wd = {0}; WSAStartup(MAKEWORD(2, 2), &wd); - char* ip = "localhost"; + const char* ip = "localhost"; struct addrinfo aiHints; struct addrinfo *res = NULL; memset(&aiHints, 0, sizeof(aiHints));