0x6461726B/Omnihook

GitHub: 0x6461726B/Omnihook

一个轻量级的 Windows x64 函数 Hook 库,支持 Inline、Mid-Function、硬件断点和 VMT 等多种拦截方式。

Stars: 0 | Forks: 0

# Omnihook Omnihook 是一个轻量级、多功能的 Windows x64 函数 Hook 库。它提供了多种拦截执行的方式,从标准的 inline hooking 到寄存器级别的函数中途 hook。 它在内部使用 **Zydis** 来解码和重定位指令,以便在放置 hook 时使用。 ## 功能 * **Inline Hooking**:标准的 detour hook,通过修补函数 prologue 实现。使用反汇编器安全地重定位被覆盖的指令。 * **Mid-Function Hooking**:Hook 函数体内的任意指令。向回调函数传递完整的 `RegisterContext`(包括 RAX-R15、EFLAGS 和 YMM0-YMM15),允许你在恢复执行前读写寄存器。 * **Hardware Breakpoints**:使用 CPU 调试寄存器(`DR0` - `DR3`)和 Vectored Exception Handler (VEH) 来 Hook 执行。 * **VMT (Virtual Method Table) Hooking**:Hook 对象实例的虚函数。支持直接替换 vtable 和复制 shadow vtable。 ## API 与用法 所有函数均可在 `Omnihook` 命名空间下使用。 ### 1. Inline Hooking 用跳转到你的 detour 的指令替换函数的开头。 ``` #include "Omnihook.h" #include typedef int(__fastcall* tMessageBoxA)(HWND, LPCSTR, LPCSTR, UINT); tMessageBoxA oMessageBoxA = nullptr; int __fastcall hkMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) { std::cout << "Intercepted MessageBoxA!" << std::endl; return oMessageBoxA(hWnd, "Hooked!", lpCaption, uType); } void Setup() { auto status = Omnihook::CreateInlineHook( &MessageBoxA, &hkMessageBoxA, reinterpret_cast(&oMessageBoxA) ); if (status == Omnihook::Status::Success) { if (Omnihook::EnableHook(&MessageBoxA) == Omnihook::Status::Success) { std::cout << "Successfully hooked MessageBoxA!" << std::endl; } } } ``` ### 2. Mid-Function Hooking 在函数中间拦截执行,让你完全控制 CPU 寄存器。 ``` #include "Omnihook.h" #include void MyMidHookCallback(Omnihook::RegisterContext* regs) { // Read registers std::cout << "RAX: 0x" << std::hex << regs->rax << std::endl; // Modify registers if needed regs->rax = 0x1337; } void Setup() { void* targetAddress = ...; // Address inside function body auto status = Omnihook::CreateMidHook(targetAddress, &MyMidHookCallback); if (status == Omnihook::Status::Success) { if (Omnihook::EnableHook(targetAddress) == Omnihook::Status::Success) { std::cout << "Mid-hook successfully enabled!" << std::endl; } } } ``` ### 3. Hardware Breakpoints 在特定地址设置断点,无需修改任何代码字节。 ``` #include "Omnihook.h" void __fastcall hkTargetFunction() { // Note: Control flow redirections using HWBP will jump directly here } void Setup() { void* targetAddress = ...; auto status = Omnihook::CreateHWBPHook(targetAddress, &hkTargetFunction); if (status == Omnihook::Status::Success) { if (Omnihook::EnableHook(targetAddress) == Omnihook::Status::Success) { std::cout << "Hardware breakpoint hook successfully enabled!" << std::endl; } } } ``` ### 4. VMT Hooking 重定向对象实例上的虚函数。 ``` #include "Omnihook.h" // Original virtual function detour signature typedef void(* tVirtualFunc)(void*); void Setup(void* objectInstance) { void* detourFunc = ...; int vtableIndex = 2; // Index of the function in the vtable // HookType::Default (Direct replacement) or HookType::Shadow (Copy vtable) auto status = Omnihook::CreateVMTHook(objectInstance, detourFunc, vtableIndex, Omnihook::HookType::Shadow); if (status == Omnihook::Status::Success) { if (Omnihook::EnableHook(objectInstance) == Omnihook::Status::Success) { std::cout << "VMT hook successfully enabled!" << std::endl; } } } ``` ### Hook 生命周期管理 ``` // Enable/Disable individual hooks by their target address Omnihook::EnableHook(targetAddress); Omnihook::DisableHook(targetAddress); // Completely remove a hook Omnihook::RemoveHook(targetAddress); // Clean up all hooks (recommended on DLL unload) Omnihook::RemoveAll(); ``` ## 构建 Omnihook 专为 **x64 Windows** 目标平台设计。 1. 在 Visual Studio 中打开 `Omnihook.slnx` 或加载 `Omnihook.vcxproj`(推荐使用 2026 版)。 2. 配置你的构建选项(Debug/Release,x64)。 3. 构建解决方案。该项目将编译为静态库(`.lib`)。
标签:C++, Hook, Linux, Zydis, 云资产清单, 客户端加密, 底层开发, 数据擦除, 逆向工程