HookLib

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

Github stars Tracking Chart

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;
}

Main metrics

Overview
Name With OwnerHoShiMin/HookLib
Primary LanguageC
Program languageC (Language Count: 3)
Platform
License:MIT License
所有者活动
Created At2019-02-10 21:55:47
Pushed At2025-04-24 18:46:49
Last Commit At2025-04-24 21:46:48
Release Count0
用户参与
Stargazers Count743
Watchers Count24
Fork Count151
Commits Count39
Has Issues Enabled
Issues Count19
Issue Open Count4
Pull Requests Count3
Pull Requests Open Count2
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private