HookLib

The functions interception library written on pure C and NativeAPI with UserMode and KernelMode support

Github星跟踪图

HookLib

The Win32 lightweight functions interception library

✔ Advantages:

  • Written on pure C
  • Extremely lightweight
  • Based on the fastest and lightweight Zydis disassembler
  • Uses only NativeAPI functions
  • Has no other dependencies
  • Kernelmode support
  • Supports instructions relocation and thread's contexts fixup

⚙️ How it works:

TargetFunction():                                 ^ ; return
-> jmp Interceptor ------> Interceptor():, ??? ; Broken bytes        ... Handler code ..., ... ; Continuation <--+   CallOriginal() ------, --> OriginalBeginning():
   ...         +---------, -> ..., ... Original beginning ...
   ret --------+, ret -----------------+      ... of TargetFunction ...
                         +------------------------------ jmp Continuation
   

? Trampolines:

Types:

  • E9 44 33 22 11, jmp 0x11223344 - Relative jump to the +-2Gb
  • FF 25 00 00 00 00 88 77 66 55 44 33 22 11, jmp [rip+00h] - Absolute jump to the address stored after the jmp as raw bytes (4 bytes in x32 and 8 bytes in x64)

x32:

  • jmp rel Interceptor only one

x64:

  • jmp rel Interceptor if Abs(Interceptor - Target) <= 2Gb
  • jmp rel Intermediate -> jmp abs Interceptor if Abs(Interceptor - Target) > 2Gb and we have free space for the intermediate trampoline buffer
  • jmp abs Interceptor if we have no free space for the intermediate buffer in +- 2Gb interval

? Using:

Open the HookLib.sln and build it.
Add Zydis.lib, HookLib.lib and HookLib.h to your project.

#include <cstdio>

#include <Windows.h>

#include <HookLib.h>
#pragma comment(lib, "Zydis.lib")
#pragma comment(lib, "HookLib.lib")

using _ExitProcess = VOID(WINAPI*)(ULONG ExitCode);
_ExitProcess OriginalExitProcess = NULL;
VOID WINAPI ExitProcessHook(ULONG ExitCode)
{
    printf("ExitCode: %ul\r\n", ExitCode);
    RemoveHook(OriginalExitProcess);
    ExitProcess(0);
}

int main()
{
    PVOID Target = GetProcAddress(GetModuleHandle(L"kernel32.dll"), "ExitProcess");
    SetHook(Target, ExitProcessHook, reinterpret_cast<PVOID*>(&OriginalExitProcess));
    ExitProcess(0);
    return 0;
}

主要指标

概览
名称与所有者HoShiMin/HookLib
主编程语言C
编程语言C (语言数: 3)
平台
许可证MIT License
所有者活动
创建于2019-02-10 21:55:47
推送于2025-04-24 18:46:49
最后一次提交2025-04-24 21:46:48
发布数0
用户参与
星数746
关注者数24
派生数152
提交数39
已启用问题?
问题数19
打开的问题数4
拉请求数3
打开的拉请求数2
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?