Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Technique:Function hooking

From ReverseEngineering.wiki
Technique
On this wiki Facts · Code · Notes · Topics

Function hooking redirects calls of a function to your own code; the common inline (detour) form overwrites the first bytes of the target with a jump and keeps a trampoline with the displaced instructions to call the original.

Methods

  • Inline / detour hook: patch the target's first bytes with a JMP to the detour; a trampoline holds the overwritten instructions plus a JMP back. Used by Library:MinHook and Microsoft Detours.
  • Import address table (IAT) hook: replace the pointer in the import table of one module; no code is patched, only that module's calls are affected.
  • Vtable hook: replace a slot in a C++ virtual table.
  • Hot-patch hook: Windows system DLL functions begin with a 2-byte mov edi,edi and 5 bytes of padding before them, so a 5-byte jump fits above the function and a 2-byte short jump replaces the first instruction.

Inline hook anatomy (x86/x64)

  • A relative E9 rel32 jump needs 5 bytes and reaches +-2 GiB, so on x64 the trampoline memory must be allocated near the target (MinHook searches within 1 GiB), with an absolute FF 25 jump in a relay for the detour.
  • The overwritten instructions must be copied whole: a length disassembler decodes them, and rip-relative operands and relative jumps/calls inside them must be fixed up.
  • Hooking is refused if the function is shorter than the jump or an instruction boundary cannot be kept.
  • Other threads may be executing the bytes being patched: suspend them first (MinHook does) and flush the instruction cache.

Related

Library:MinHook, Library:Microsoft Detours (trampoline layout, .detour section, DetourCreateProcessWithDll), Technique:DLL proxy loading, Technique:Library function recognition

Unix, macOS, Android and iOS (interposition, GOT and lazy-pointer patching, AArch64 inline hooks, Frida): Technique:Function hooking/Unix and mobile.

Topics

  • Unix and mobile — Hooking on Linux, macOS, Android and iOS for analysing or modding your own programs: symbol interposition (LD_PRELOAD, DYLD_INSERT_LIBRARIES), patching…

See also

Code & repositories

  • https://github.com/TsudaKageyu/minhook @ 8af6b4acae5a9388fd742b56fa79ece89d96f823 · license: none
    MinHook source: inline hooking, trampoline builder, HDE disassembler.
    Tested on: source read, not run: read the source at the pinned commit · checked 2026-10-18; last activity 2026-09-07; Note: the latest release ships binaries; the wiki links only the source, never the binaries; no license: not reusable
  • https://github.com/microsoft/Detours @ 1dbd4ea281d73d478aa8d09a8b83fbd265f715f7 · license: MIT
    Microsoft Detours source (MIT): detours.h/.cpp trampolines, creatwth.cpp process creation with DLL, image.cpp .detour section editing.
    Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2026-10-14