MinHook-rs
A Rust implementation of the MinHook library for Windows x64 function hooking.
Installation
[]
= "1.3"
= { = "0.60", = [
"Win32_UI_WindowsAndMessaging",
"Win32_Foundation"
] }
Quick Start
//! Basic MessageBox Hook Example
//!
//! Simple MessageBoxA Hook demonstration showing before/after comparison
use *;
use c_void;
use ptr;
use *;
use UI*;
use PCSTR;
// MessageBoxA function signature
type MessageBoxAFn = unsafe extern "system" fn ;
// Store original function pointer
static mut ORIGINAL_MESSAGEBOX: = None;
// Hook function - modify message content
pub unsafe extern "system"
// Test MessageBox call
The output may be
MinHook-rs MessageBox Hook Demo
================================
[PHASE 1] Testing original MessageBox behavior
Showing original MessageBox...
[PHASE 2] Installing hook
Initializing MinHook...
Creating MessageBoxA hook...
Enabling hook...
Hook activated successfully!
[PHASE 3] Testing hook effect
Showing hooked MessageBox...
[HOOK] MessageBoxA intercepted!
[PHASE 4] Testing hook stability
Second hook test...
[HOOK] MessageBoxA intercepted!
Third hook test...
[HOOK] MessageBoxA intercepted!
[PHASE 5] Disabling hook
Hook disabled
[PHASE 6] Verifying hook is disabled
Showing normal MessageBox after disable...
[PHASE 7] Cleanup
Cleanup completed
Demo completed successfully!
Summary:
- Original behavior: Normal MessageBox
- Hook active: Message intercepted and modified
- Hook disabled: Normal behavior restored
- Complete cleanup: System returned to initial state
Examples
Basic Hook Example
Run the comprehensive demonstration example:
This example demonstrates all MinHook-rs features including basic hooks, multiple simultaneous hooks, dynamic enable/disable cycles, queued operations, error handling, and performance testing with detailed output.
DLL Hook Examples
Complete DLL hooking workflow with injector and target programs:
# Build hook DLL and test programs
# Start test program (displays several MessageBox dialogs)
# Find process ID and inject hook DLL
|
Notepad Hook Example
Real-world application hooking - intercepts Notepad's exit confirmation dialog:
# Build Notepad hook DLL and injector
# Start Notepad and inject hook
&
|
# Test: Type text in Notepad, try to close without saving
# You'll see a custom hook message instead of normal save dialog
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
Usage Patterns
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
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