Mirror of safetyhook, slightly modified to build on C++11
  • C 94.3%
  • C++ 5.2%
  • CMake 0.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-06-27 19:13:33 +02:00
.githooks Add clang-format enforcement (#128) 2026-06-18 19:56:23 -07:00
.github/workflows Generate amalgamation outputs in build tree (#131) 2026-06-19 02:14:15 -07:00
cmake Generate amalgamation outputs in build tree (#131) 2026-06-19 02:14:15 -07:00
docs Migrate to plain CMake (#121) 2026-06-18 18:49:09 -07:00
example Add strict warning configuration (#132) 2026-06-19 18:22:19 -07:00
include Merge with upstream 2026-06-27 16:43:56 +00:00
module Add strict warning configuration (#132) 2026-06-19 18:22:19 -07:00
src Avoid W^X violation in trap_threads (#11) 2026-06-27 19:13:33 +02:00
test Add strict warning configuration (#132) 2026-06-19 18:22:19 -07:00
zydis Setup AMBuild (#2) 2024-05-22 21:39:01 -07:00
.clang-format Initial commit 2021-12-28 18:47:59 -08:00
.gitignore Migrate to plain CMake (#121) 2026-06-18 18:49:09 -07:00
amalgamate.py Add strict warning configuration (#132) 2026-06-19 18:22:19 -07:00
AMBuilder Merge pull request #4 from GAMMACASE/fix-gcc-compilation 2024-06-01 09:52:22 -04:00
AMBuildScript Setup AMBuild (#2) 2024-05-22 21:39:01 -07:00
CMakeLists.txt Add strict warning configuration (#132) 2026-06-19 18:22:19 -07:00
configure.py Setup AMBuild (#2) 2024-05-22 21:39:01 -07:00
LICENSE Initial commit 2021-12-28 18:47:59 -08:00
README.md Add hacks 2024-05-20 22:05:56 -07:00
vcpkg.json Update CPM bootstrap and dependencies. (#129) 2026-06-18 20:23:26 -07:00

SafetyHook

SM Compatibility notes

This is hacked apart to build on c++11 - execute |python amalgamate.py --polyfill| to regenerate the files used by sourcemod

Intro

SafetyHook is a C++23 procedure hooking library for Windows x86 and x86_64 systems. It aims to make runtime procedure hooking as safe as possible while maintaining simplicity of its implementation. To that end it currently does:

  • Stops all other threads when creating or deleting hooks
  • Fixes the IP of threads that may be affected by the creation or deletion of hooks
  • Fixes IP relative displacements of relocated instructions (eg. lea rax, [rip + 0x1234])
  • Fixes relative offsets of relocated instructions (eg. jmp 0x1234)
  • Widens short branches into near branches
  • Handles short branches that land within the trampoline
  • Uses a modern disassembler engine that supports the latest instructions
  • Has a carefully designed API that is hard to misuse

Installation

SafetyHook can be added via CMake's FetchContent, git submodules, or copied directly into your project using the amalgamated builds. SafetyHook requires Zydis to function.

Amalgamated builds

This is the easiest way to use safety hook. You can find amalgamated builds on the releases page. Simply download the ZIP file containing Zydis (or without Zydis if you already have it in your project) and copy the files into your project.

You may need to define ZYDIS_STATIC_BUILD if you're using the build with Zydis included.

CMake FetchContent

include(FetchContent)

# Safetyhook
FetchContent_Declare(
    safetyhook
    GIT_REPOSITORY "https://github.com/cursey/safetyhook.git"
    GIT_TAG "origin/main"
)
FetchContent_MakeAvailable(safetyhook)

If you want SafetyHook to fetch Zydis you must enable the CMake option -DSAFETYHOOK_FETCH_ZYDIS=ON.

Usage

#include <iostream>

#include <safetyhook.hpp>

__declspec(noinline) int add(int x, int y) {
    return x + y;
}

SafetyHookInline g_add_hook{};

int hook_add(int x, int y) {
    return g_add_hook.call<int>(x * 2, y * 2);
}

int main() {
    std::cout << "unhooked add(2, 3) = " << add(2, 3) << "\n";

    // Create a hook on add (This uses SafetyHook's easy API).
    g_add_hook = safetyhook::create_inline(reinterpret_cast<void*>(add), reinterpret_cast<void*>(hook_add));

    std::cout << "hooked add(3, 4) = " << add(3, 4) << "\n";

    g_add_hook = {};

    std::cout << "unhooked add(5, 6) = " << add(5, 6) << "\n";

    return 0;
}