132 lines
4.2 KiB
C++
132 lines
4.2 KiB
C++
#pragma once
|
|
|
|
// Size of each memory slot.
|
|
#if defined(_M_X64) || defined(__x86_64__)
|
|
#define MEMORY_SLOT_SIZE 64
|
|
#else
|
|
#define MEMORY_SLOT_SIZE 32
|
|
#endif
|
|
|
|
// Can be passed as a parameter to MH_EnableHook, MH_DisableHook,
|
|
// MH_QueueEnableHook or MH_QueueDisableHook.
|
|
#define MH_ALL_HOOKS NULL
|
|
|
|
namespace mh
|
|
{
|
|
typedef enum MH_STATUS
|
|
{
|
|
MH_UNKNOWN = -1,
|
|
MH_OK = 0,
|
|
MH_ERROR_ALREADY_INITIALIZED,
|
|
MH_ERROR_NOT_INITIALIZED,
|
|
MH_ERROR_ALREADY_CREATED,
|
|
MH_ERROR_NOT_CREATED,
|
|
MH_ERROR_ENABLED,
|
|
MH_ERROR_DISABLED,
|
|
MH_ERROR_NOT_EXECUTABLE,
|
|
MH_ERROR_UNSUPPORTED_FUNCTION,
|
|
MH_ERROR_MEMORY_ALLOC,
|
|
MH_ERROR_MEMORY_PROTECT,
|
|
MH_ERROR_MODULE_NOT_FOUND,
|
|
MH_ERROR_FUNCTION_NOT_FOUND
|
|
}
|
|
MH_STATUS;
|
|
|
|
MH_STATUS WINAPI MH_Initialize(VOID);
|
|
MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID* ppOriginal, PVOID pTargetReal);
|
|
MH_STATUS WINAPI MH_CreateHookApi(
|
|
LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID* ppOriginal);
|
|
MH_STATUS WINAPI MH_CreateHookApiEx(
|
|
LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID* ppOriginal, LPVOID* ppTarget);
|
|
MH_STATUS WINAPI MH_RemoveHook(LPVOID pTarget);
|
|
MH_STATUS WINAPI MH_EnableHook(LPVOID pTarget, PVOID pSideBuffer);
|
|
MH_STATUS WINAPI MH_DisableHook(LPVOID pTarget);
|
|
MH_STATUS WINAPI MH_QueueEnableHook(LPVOID pTarget);
|
|
MH_STATUS WINAPI MH_QueueDisableHook(LPVOID pTarget);
|
|
MH_STATUS WINAPI MH_ApplyQueued(VOID);
|
|
|
|
VOID InitializeBuffer(VOID);
|
|
VOID UninitializeBuffer(VOID);
|
|
LPVOID AllocateBuffer(LPVOID pOrigin);
|
|
VOID FreeBuffer(LPVOID pBuffer);
|
|
BOOL IsExecutableAddress(LPVOID pAddress);
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
// Structs for writing x86/x64 instructions.
|
|
|
|
// 8-bit relative jump.
|
|
typedef struct _JMP_REL_SHORT
|
|
{
|
|
UINT8 opcode; // EB xx: JMP +2+xx
|
|
UINT8 operand;
|
|
} JMP_REL_SHORT, * PJMP_REL_SHORT;
|
|
|
|
// 32-bit direct relative jump/call.
|
|
typedef struct _JMP_REL
|
|
{
|
|
UINT8 opcode; // E9/E8 xxxxxxxx: JMP/CALL +5+xxxxxxxx
|
|
UINT32 operand; // Relative destination address
|
|
} JMP_REL, * PJMP_REL, CALL_REL;
|
|
|
|
// 64-bit indirect absolute jump.
|
|
typedef struct _JMP_ABS
|
|
{
|
|
UINT8 opcode0; // FF25 00000000: JMP [+6]
|
|
UINT8 opcode1;
|
|
UINT32 dummy;
|
|
UINT64 address; // Absolute destination address
|
|
} JMP_ABS, * PJMP_ABS;
|
|
|
|
// 64-bit indirect absolute call.
|
|
typedef struct _CALL_ABS
|
|
{
|
|
UINT8 opcode0; // FF15 00000002: CALL [+6]
|
|
UINT8 opcode1;
|
|
UINT32 dummy0;
|
|
UINT8 dummy1; // EB 08: JMP +10
|
|
UINT8 dummy2;
|
|
UINT64 address; // Absolute destination address
|
|
} CALL_ABS;
|
|
|
|
// 32-bit direct relative conditional jumps.
|
|
typedef struct _JCC_REL
|
|
{
|
|
UINT8 opcode0; // 0F8* xxxxxxxx: J** +6+xxxxxxxx
|
|
UINT8 opcode1;
|
|
UINT32 operand; // Relative destination address
|
|
} JCC_REL;
|
|
|
|
// 64bit indirect absolute conditional jumps that x64 lacks.
|
|
typedef struct _JCC_ABS
|
|
{
|
|
UINT8 opcode; // 7* 0E: J** +16
|
|
UINT8 dummy0;
|
|
UINT8 dummy1; // FF25 00000000: JMP [+6]
|
|
UINT8 dummy2;
|
|
UINT32 dummy3;
|
|
UINT64 address; // Absolute destination address
|
|
} JCC_ABS;
|
|
|
|
#pragma pack(pop)
|
|
|
|
typedef struct _TRAMPOLINE
|
|
{
|
|
LPVOID pTarget; // [In] Address of the target function.
|
|
LPVOID pDetour; // [In] Address of the detour function.
|
|
LPVOID pTrampoline; // [In] Buffer address for the trampoline and relay function.
|
|
|
|
#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?
|
|
UINT nIP; // [Out] Number of the instruction boundaries.
|
|
UINT8 oldIPs[8]; // [Out] Instruction boundaries of the target function.
|
|
UINT8 newIPs[8]; // [Out] Instruction boundaries of the trampoline function.
|
|
} TRAMPOLINE, * PTRAMPOLINE;
|
|
|
|
BOOL CreateTrampolineFunction(PTRAMPOLINE ct);
|
|
|
|
|
|
}
|