Basic Function Hooking
This example demonstrates hooking atake_damage function to prevent a player from taking damage - a classic “god mode” implementation.
Take Damage Hook Example
How It Works
Find the Target Module
Locate the module (DLL/SO) containing the function you want to hook using
LM_FindModule.Find the Function Address
Use
LM_FindSymbolAddress to locate the function by its symbol name. If the function isn’t exported, you can use pattern scanning instead.Create Hook Function
Write a function with the same signature as the original. This is what will execute when the original function is called.
Hook with Trampoline
If you want to call the original function from your hook, use a trampoline:Hooking External Processes
You can also hook functions in external processes using theEx variants:
Unhooking
To remove a hook and restore the original function:Best Practices
Match Function Signatures
Match Function Signatures
Ensure your hook function has the exact same calling convention and parameters as the original. Mismatches can cause crashes.
Handle Thread Safety
Handle Thread Safety
If the hooked function is called from multiple threads, ensure your hook is thread-safe.
Save Trampoline and Size
Save Trampoline and Size
Always save the trampoline address and hook size if you plan to unhook later.
Test Thoroughly
Test Thoroughly
Hooks can crash the target process if implemented incorrectly. Test in a safe environment first.
Use Symbol Names When Available
Use Symbol Names When Available
If the function is exported, use
LM_FindSymbolAddress. Otherwise, use pattern scanning to locate it.Common Use Cases
- God Mode: Prevent damage by hooking health reduction functions
- Unlimited Resources: Hook resource consumption functions and prevent deduction
- Function Logging: Hook functions to log when they’re called and with what parameters
- Behavior Modification: Change function behavior by modifying parameters or return values
- Anti-Cheat Bypass: Hook detection functions (educational purposes only)
Next Steps
Pattern Scanning
Find functions without symbol information
Hook API
Explore all hooking functions including VMT hooks