diff --git a/README.md b/README.md index 91578066..6f5f5416 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # MinHook +[![License](https://img.shields.io/badge/License-BSD%202--Clause-orange.svg)](https://opensource.org/licenses/BSD-2-Clause) + The Minimalistic x86/x64 API Hooking Library for Windows http://www.codeproject.com/KB/winsdk/LibMinHook.aspx @@ -14,6 +16,7 @@ I need some funds to continue developing this library. All contributions gratefu - ####v1.3.3 - ?? Jan 2017 (Probably 7 or 8) * Added a helper function ```MH_CreateHookApiEx```. (Thanks to asm256) + * Support Visual Studio 2017 RC. - ####v1.3.2.1 - 9 Nov 2015 (Nuget package only) * Fixed an insufficient support for Visual Studio 2015. diff --git a/build/VC15/MinHook.vcxproj b/build/VC15/MinHook.vcxproj index d5ecda27..1d518333 100644 --- a/build/VC15/MinHook.vcxproj +++ b/build/VC15/MinHook.vcxproj @@ -28,23 +28,23 @@ DynamicLibrary Unicode true - v140_xp + v141_xp DynamicLibrary Unicode - v140_xp + v141_xp DynamicLibrary Unicode true - v140_xp + v141_xp DynamicLibrary Unicode - v140_xp + v141_xp @@ -186,4 +186,4 @@ - \ No newline at end of file + diff --git a/build/VC15/libMinHook.vcxproj b/build/VC15/libMinHook.vcxproj index 263b811a..0d4352e6 100644 --- a/build/VC15/libMinHook.vcxproj +++ b/build/VC15/libMinHook.vcxproj @@ -28,23 +28,23 @@ StaticLibrary Unicode true - v140_xp + v141_xp StaticLibrary Unicode - v140_xp + v141_xp StaticLibrary Unicode true - v140_xp + v141_xp StaticLibrary Unicode - v140_xp + v141_xp @@ -171,4 +171,4 @@ - \ No newline at end of file + diff --git a/include/MinHook.h b/include/MinHook.h index 6ab9e451..6b410f3d 100644 --- a/include/MinHook.h +++ b/include/MinHook.h @@ -28,7 +28,7 @@ #pragma once -#if !(defined _M_IX86) && !(defined _M_X64) +#if !(defined _M_IX86) && !(defined _M_X64) && !(defined __i386__) && !(defined __x86_64__) #error MinHook supports only x86 and x64 systems. #endif diff --git a/src/buffer.c b/src/buffer.c index 45b63aa2..db3f92ae 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -85,7 +85,7 @@ VOID UninitializeBuffer(VOID) } //------------------------------------------------------------------------- -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) static LPVOID FindPrevFreeRegion(LPVOID pAddress, LPVOID pMinAddr, DWORD dwAllocationGranularity) { ULONG_PTR tryAddr = (ULONG_PTR)pAddress; @@ -116,7 +116,7 @@ static LPVOID FindPrevFreeRegion(LPVOID pAddress, LPVOID pMinAddr, DWORD dwAlloc #endif //------------------------------------------------------------------------- -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) static LPVOID FindNextFreeRegion(LPVOID pAddress, LPVOID pMaxAddr, DWORD dwAllocationGranularity) { ULONG_PTR tryAddr = (ULONG_PTR)pAddress; @@ -151,7 +151,7 @@ static LPVOID FindNextFreeRegion(LPVOID pAddress, LPVOID pMaxAddr, DWORD dwAlloc static PMEMORY_BLOCK GetMemoryBlock(LPVOID pOrigin) { PMEMORY_BLOCK pBlock; -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) ULONG_PTR minAddr; ULONG_PTR maxAddr; @@ -174,7 +174,7 @@ static PMEMORY_BLOCK GetMemoryBlock(LPVOID pOrigin) // Look the registered blocks for a reachable one. for (pBlock = g_pMemoryBlocks; pBlock != NULL; pBlock = pBlock->pNext) { -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) // Ignore the blocks too far. if ((ULONG_PTR)pBlock < minAddr || (ULONG_PTR)pBlock >= maxAddr) continue; @@ -184,7 +184,7 @@ static PMEMORY_BLOCK GetMemoryBlock(LPVOID pOrigin) return pBlock; } -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) // Alloc a new block above if not found. { LPVOID pAlloc = pOrigin; diff --git a/src/buffer.h b/src/buffer.h index f3e9acca..8961b74d 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -29,7 +29,7 @@ #pragma once // Size of each memory slot. -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) #define MEMORY_SLOT_SIZE 64 #else #define MEMORY_SLOT_SIZE 32 diff --git a/src/hde/hde32.c b/src/hde/hde32.c index c3c62e86..08fa25bd 100644 --- a/src/hde/hde32.c +++ b/src/hde/hde32.c @@ -5,6 +5,8 @@ * */ +#if defined(_M_IX86) || defined(__i386__) + #include "hde32.h" #include "table32.h" @@ -320,3 +322,5 @@ unsigned int hde32_disasm(const void *code, hde32s *hs) return (unsigned int)hs->len; } + +#endif // defined(_M_IX86) || defined(__i386__) diff --git a/src/hde/hde64.c b/src/hde/hde64.c index 72f9517d..c23e2fc6 100644 --- a/src/hde/hde64.c +++ b/src/hde/hde64.c @@ -5,6 +5,8 @@ * */ +#if defined(_M_X64) || defined(__x86_64__) + #include "hde64.h" #include "table64.h" @@ -331,3 +333,5 @@ unsigned int hde64_disasm(const void *code, hde64s *hs) return (unsigned int)hs->len; } + +#endif // defined(_M_X64) || defined(__x86_64__) diff --git a/src/hook.c b/src/hook.c index f1b8859c..a28849f1 100644 --- a/src/hook.c +++ b/src/hook.c @@ -173,7 +173,7 @@ static DWORD_PTR FindOldIP(PHOOK_ENTRY pHook, DWORD_PTR ip) return (DWORD_PTR)pHook->pTarget + pHook->oldIPs[i]; } -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) // Check relay function. if (ip == (DWORD_PTR)pHook->pDetour) return (DWORD_PTR)pHook->pTarget; @@ -202,7 +202,7 @@ static void ProcessThreadIPs(HANDLE hThread, UINT pos, UINT action) // move IP to the proper address. CONTEXT c; -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) DWORD64 *pIP = &c.Rip; #else DWORD *pIP = &c.Eip; @@ -239,7 +239,7 @@ static void ProcessThreadIPs(HANDLE hThread, UINT pos, UINT action) enable = TRUE; break; - case ACTION_APPLY_QUEUED: + default: // ACTION_APPLY_QUEUED enable = pHook->queueEnable; break; } @@ -559,7 +559,7 @@ MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID *ppOrigina if (pHook != NULL) { pHook->pTarget = ct.pTarget; -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) pHook->pDetour = ct.pRelay; #else pHook->pDetour = ct.pDetour; diff --git a/src/trampoline.c b/src/trampoline.c index 2e264c1a..807b9c97 100644 --- a/src/trampoline.c +++ b/src/trampoline.c @@ -32,7 +32,7 @@ #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0])) #endif -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) #include "./hde/hde64.h" typedef hde64s HDE; #define HDE_DISASM(code, hs) hde64_disasm(code, hs) @@ -46,7 +46,7 @@ #include "buffer.h" // Maximum size of a trampoline function. -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) #define TRAMPOLINE_MAX_SIZE (MEMORY_SLOT_SIZE - sizeof(JMP_ABS)) #else #define TRAMPOLINE_MAX_SIZE MEMORY_SLOT_SIZE @@ -71,7 +71,7 @@ static BOOL IsCodePadding(LPBYTE pInst, UINT size) //------------------------------------------------------------------------- BOOL CreateTrampolineFunction(PTRAMPOLINE ct) { -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) CALL_ABS call = { 0xFF, 0x15, 0x00000002, // FF15 00000002: CALL [RIP+8] 0xEB, 0x08, // EB 08: JMP +10 @@ -105,7 +105,7 @@ BOOL CreateTrampolineFunction(PTRAMPOLINE ct) UINT8 newPos = 0; ULONG_PTR jmpDest = 0; // Destination address of an internal jump. BOOL finished = FALSE; // Is the function completed? -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) UINT8 instBuf[16]; #endif @@ -129,7 +129,7 @@ BOOL CreateTrampolineFunction(PTRAMPOLINE ct) { // The trampoline function is long enough. // Complete the function with the jump to the target function. -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) jmp.address = pOldInst; #else jmp.operand = (UINT32)(pOldInst - (pNewInst + sizeof(jmp))); @@ -139,7 +139,7 @@ BOOL CreateTrampolineFunction(PTRAMPOLINE ct) finished = TRUE; } -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) else if ((hs.modrm & 0xC7) == 0x05) { // Instructions using RIP relative addressing. (ModR/M = 00???101B) @@ -169,7 +169,7 @@ BOOL CreateTrampolineFunction(PTRAMPOLINE ct) { // Direct relative CALL ULONG_PTR dest = pOldInst + hs.len + (INT32)hs.imm.imm32; -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) call.address = dest; #else call.operand = (UINT32)(dest - (pNewInst + sizeof(call))); @@ -196,7 +196,7 @@ BOOL CreateTrampolineFunction(PTRAMPOLINE ct) } else { -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) jmp.address = dest; #else jmp.operand = (UINT32)(dest - (pNewInst + sizeof(jmp))); @@ -236,7 +236,7 @@ BOOL CreateTrampolineFunction(PTRAMPOLINE ct) else { UINT8 cond = ((hs.opcode != 0x0F ? hs.opcode : hs.opcode2) & 0x0F); -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) // Invert the condition in x64 mode to simplify the conditional jump logic. jcc.opcode = 0x71 ^ cond; jcc.address = dest; @@ -304,7 +304,7 @@ BOOL CreateTrampolineFunction(PTRAMPOLINE ct) ct->patchAbove = TRUE; } -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) // Create a relay function. jmp.address = (ULONG_PTR)ct->pDetour; diff --git a/src/trampoline.h b/src/trampoline.h index 0b16d6c7..e0038dad 100644 --- a/src/trampoline.h +++ b/src/trampoline.h @@ -93,7 +93,7 @@ typedef struct _TRAMPOLINE LPVOID pDetour; // [In] Address of the detour function. LPVOID pTrampoline; // [In] Buffer address for the trampoline and relay function. -#ifdef _M_X64 +#if defined(_M_X64) || defined(__x86_64__) LPVOID pRelay; // [Out] Address of the relay function. #endif BOOL patchAbove; // [Out] Should use the hot patch area?