Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/TsudaKageyu/minhook
Browse files Browse the repository at this point in the history
  • Loading branch information
TsudaKageyu committed Dec 31, 2016
2 parents cc01975 + fea705c commit bbadef6
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 32 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions build/VC15/MinHook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@
<ConfigurationType>DynamicLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140_xp</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140_xp</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140_xp</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140_xp</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -186,4 +186,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
10 changes: 5 additions & 5 deletions build/VC15/libMinHook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140_xp</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140_xp</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v140_xp</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v140_xp</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -171,4 +171,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion include/MinHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/hde/hde32.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*
*/

#if defined(_M_IX86) || defined(__i386__)

#include "hde32.h"
#include "table32.h"

Expand Down Expand Up @@ -320,3 +322,5 @@ unsigned int hde32_disasm(const void *code, hde32s *hs)

return (unsigned int)hs->len;
}

#endif // defined(_M_IX86) || defined(__i386__)
4 changes: 4 additions & 0 deletions src/hde/hde64.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*
*/

#if defined(_M_X64) || defined(__x86_64__)

#include "hde64.h"
#include "table64.h"

Expand Down Expand Up @@ -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__)
8 changes: 4 additions & 4 deletions src/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions src/trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)));
Expand All @@ -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)
Expand Down Expand Up @@ -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)));
Expand All @@ -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)));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/trampoline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit bbadef6

Please sign in to comment.