hooking-rs
Function hooking in rust
A library for hooking and intercepting functions in rust for windows and linux, on x86_64 and x86.
- Hook by name or address:
Hook::by_namefinds a symbol in a loaded module,Hook::createtakes a function pointer. - Call the original: the overwritten instructions are relocated, so the original function still works while hooked.
- Attribute macros:
#[hook]and#[mini_hook]create the hook when the program starts and generateenable_hook/disable_hook. original_function_ptr(x86_64): a hook can get its original function from registers, with no global state.- Mini hooks: hooks without a trampoline, which jump straight to the destination and leave r10/r11 untouched.
HookKindtrait: create and manage any kind of hook generically, or keep mixed hooks asBox<dyn HookKind>.- Save-registers hooks (x86_64): one dispatch function reads and rewrites any hooked function's arguments, without knowing its signature.
- Pluggable memory handling: choose where hook code lives and how targets are patched.
[]
= "0.6"
Quick start
use hook;
unsafe extern "C"
Types in a hooked function's signature are copied into a generated module, so write them as
full paths (std::ffi::c_int, not c_int).
Examples
Hook a function by address, and call the original
On x86_64, hooking::original_function_ptr() returns the original function from inside the
hook. Call it first, before anything else in the hook can overwrite the registers it reads.
On x86, store hook.original_function() somewhere the hook can read it, such as a OnceLock.
extern "C"
let mut hook = unsafe ;
unsafe ;
assert_eq!;
unsafe ;
Hook without a trampoline
A MiniHook is a Hook without the trampoline: the target jumps straight to the
destination, and r10/r11 are left untouched. original_function_ptr isn't available in the
destination, but original_function() still calls the original.
Use a MiniHook over a Hook when:
- The caller passes values in r10 or r11. A
Hook's trampoline overwrites them (on x86_64) to pass the original function along, so the hook, and the original it calls, never see them. Some code does pass hidden values there, such as GCC's static chain pointer for nested functions (r10) and hidden arguments to stubs in JIT runtimes like .NET's CoreCLR. - You don't need
original_function_ptr. Without the trampoline each call makes one jump fewer, and the hook takes up less memory.
extern "C"
unsafe ;
// std::process::id() now reports the real pid plus a million.
Or without the macro:
let mut hook = unsafe ;
let original = hook.original_function; // store it where loaded_dice can read it
unsafe ;
Work with any kind of hook
Hook and MiniHook both implement HookKind, which has their constructors and their
operations (apply_hook, remove_hook, original_function, is_applied, target).
let mut hooks: = vec!;
for hook in &mut hooks
Inspect and rewrite arguments without knowing the signature (x86_64)
Arguments are saved in the platform's argument order: [rdi, rsi, rdx, rcx, r8, r9] and
xmm0-7 on linux, [rcx, rdx, r8, r9] and xmm0-3 on windows, where the Nth argument uses
slot N of whichever set matches its type.
extern "C"
let mut hook = unsafe ;
How it works
Applying a hook writes a jump over the start of the target: a 5 byte jmp rel32, or a 14
byte jmp [rip+0] on x86_64 when the hook's code couldn't be placed within 2GB of the target.
A Hook places this in an arena:
| Section | Description |
|---|---|
| Original function address | Where the relocated original is, read by the trampoline on x86_64 |
| Trampoline | Jumps to the hook. On x86_64 it first passes the original function in r10, with a checksum in r11, for original_function_ptr |
| Original function | The instructions the jump overwrote, relocated to run here, then a jump back to the rest of the target |
A MiniHook has no trampoline: the target jumps straight to the destination, through a small
thunk in the arena only when the destination is more than 2GB away. Its arena code is just
that thunk and the relocated original.
Arenas and patchers
Creating or applying a hook never makes code that may be running non-executable, so hooks can be added and removed while hooked functions are being called on other threads.
Where the generated hook code lives is chosen by a CodeArena:
| Arena | Description |
|---|---|
DualMappedArena (default) |
Maps memory twice, writable and executable. Hooks are written through the writable view, so executable memory never changes protection and hooks share pages. |
SealedArena |
Each hook gets fresh memory that is made executable once and never written again. Uses at least a page per hook. |
How the jump is written into the hooked function is chosen by a PatchWriter:
| Patcher | Description |
|---|---|
AliasPatcher (default on linux) |
Swaps the target's pages for an identical memfd-backed copy with a writable alias, then patches through the alias. Protection never changes. |
ProtectPatcher (default on windows) |
Makes the target's pages read/write/execute while patching, then restores their original protection. |
use ;
static ARENA: SealedArena = new;
static PATCHER: ProtectPatcher = new;
let writer = new.with_patcher;
let mut hook = unsafe ;
unsafe ;
More examples
The examples directory
has runnable examples (cargo run -p hooking --example <name>):
| Example | Platforms | Shows |
|---|---|---|
hook_puts |
linux | Hook::by_name on libc's puts, keeping the original in a OnceLock |
hook_msgboxa |
windows | Hooking MessageBoxA in user32.dll, keeping the original in a OnceLock |
macros |
linux | #[hook], with the macro's original_function() and enable_hook/disable_hook |
macro_original_function_ptr |
linux x86_64 | #[hook] reaching the original with original_function_ptr instead |
original_function_ptr |
x86_64 | Hook::create on a local function, reaching the original with no stored state |
skip_original |
linux | A hook on getenv that answers some calls itself instead of calling the original |
mini_hook |
all | MiniHook::create, calling the original through original_function() |
mini_hook_macro |
linux | #[mini_hook] on libc's getpid, calling the original |
hook_kind |
all | Creating a Hook and a MiniHook generically, and managing them together as Box<dyn HookKind> |
custom_arena |
all | HookWriter with SealedArena and ProtectPatcher |
write_hook_table |
all | Writing a hook's code without applying it, and calling its trampoline directly |
save_registers |
x86_64 | create_save_registers: rewriting arguments, overriding the return value, and call_with_saved_regs |
Testing the Windows build on linux
The Windows build can be tested on linux with mingw-w64 and
Wine. Install mingw-w64's gcc (mingw-w64-gcc on Arch,
gcc-mingw-w64-x86-64 on Debian/Ubuntu) and Wine, then add the target:
Run the tests or an example through Wine with cargo's runner setting:
WINEDEBUG=-all hides Wine's own logging. Wine isn't Windows, so check anything that depends on
the details of memory protection or loaded modules on real Windows too.