MinHook-rs
A Rust implementation of the MinHook library for Windows x64 function hooking.
Installation
[]
= "1.2"
= { = "0.60", = [
"Win32_UI_WindowsAndMessaging",
"Win32_Foundation"
] }
Quick Start
use *;
use c_void;
use ptr;
use *;
use UI*;
use PCSTR;
type MessageBoxAFn = unsafe extern "system" fn ;
static mut ORIGINAL_MESSAGEBOX: = None;
unsafe extern "system"
API Reference
Library Management
initialize // Initialize once at startup
uninitialize // Cleanup at shutdown
is_supported // Check platform compatibility
Hook Creation
// Hook by function address
create_hook // Hook by API name (returns trampoline and target)
create_hook_api
// Remove hook
remove_hook
Hook Control
enable_hook // Use ALL_HOOKS for all
disable_hook // Use ALL_HOOKS for all
// Atomic batch operations
queue_enable_hook
Instruction Analysis
decode_instruction
Examples
Hook by Address
use *;
use c_void;
unsafe extern "system"
Multiple Hooks
unsafe extern "system"
unsafe extern "system"
Queued Operations
Error Handling
match create_hook_api
Running Examples
# Windows native
# Cross-compilation
x86_64 Instruction Format
x86_64 instructions have variable-length encoding:
[Prefixes] [REX] [Opcode] [ModR/M] [SIB] [Displacement] [Immediate]
0-4 0-1 1-3 0-1 0-1 0-8 0-8 (bytes)
Critical Instructions for Hooking
RIP-relative addressing (requires relocation):
mov rax, [rip + offset] ; 48 8B 05 xx xx xx xx
lea rax, [rip + offset] ; 48 8D 05 xx xx xx xx
call [rip + offset] ; FF 15 xx xx xx xx
Control flow instructions:
call rel32 ; E8 xx xx xx xx
jmp rel32 ; E9 xx xx xx xx
jz rel32 ; 0F 84 xx xx xx xx
Instruction Analysis
let code = &;
let inst = decode_instruction;
println!;
println!;
println!;
if can_hook_safely
Hook Implementation
Function hooking works by code patching:
Original: [Target] -> [Function Body] -> [Return]
Hooked: [Target] -> [Hook] -> [Trampoline] -> [Original Body] -> [Return]
Process:
- Analysis: Decode instructions at hook location
- Trampoline: Allocate memory, copy displaced instructions
- Relocation: Fix RIP-relative addresses
- Installation: Replace function start with jump to hook
- Thread Safety: Suspend threads during patching
Error Types
| Error | Description |
|---|---|
NotInitialized |
Call initialize() first |
AlreadyCreated |
Hook already exists |
NotCreated |
Hook doesn't exist |
Enabled/Disabled |
Hook already in requested state |
ModuleNotFound |
Module not loaded |
FunctionNotFound |
Function not exported |
UnsupportedFunction |
Cannot hook this function |
MemoryAlloc/MemoryProtect |
Memory operation failed |
Best Practices
Hook Functions
- Match exact signatures including calling convention
- Use
ptr::addr_of!for thread-safe static access - Keep hook logic minimal for performance
- Handle edge cases gracefully
Memory Management
Safety
- Run as Administrator for system hooks
- Validate target function addresses
- Use correct calling conventions (
extern "system") - Proper cleanup to avoid crashes
Requirements
- Architecture: x86_64
- OS: Windows
- Rust: 1.85.0+
License
MIT License