Skip to main content
Be Bitwise

Inline Hooking & IAT/PLT Hooking

Function Hooking & Instrumentation · Reverse Engineering track

6 min read

In this module

Rewriting Code in Memory

Two hooking techniques dominate: inline hooking, which patches the function itself, and import table hooking, which patches the table of function pointers. They trade off in different directions.

Inline Hooking

Inline hooking works by overwriting the first few bytes of the target function with a jump instruction that redirects execution to your hook function.

asm
Before hooking:
target_function:
push ebp ; 55
mov ebp, esp ; 89 E5
sub esp, 0x20 ; 83 EC 20
...
After hooking:
target_function:
jmp hook_function ; E9 XX XX XX XX (5 bytes on x86)
<remaining original bytes may be partially overwritten>
...

The 5-byte JMP instruction replaces the function prologue. When any code calls target_function, it immediately jumps to hook_function instead.

The Trampoline

If your hook needs to call the original (and it usually does), you need a trampoline: a small stub holding the original overwritten instructions, which then jumps back into the rest of the function.

asm
trampoline:
push ebp ; saved original bytes
mov ebp, esp ; saved original bytes
sub esp, 0x20 ; saved original bytes
jmp target_function + 6 ; first whole instruction after them

Count the bytes. The prologue is 1 + 2 + 3 = 6 bytes, and the 5-byte JMP ends partway through sub esp, 0x20. So the trampoline has to copy all three instructions and jump back to offset 6, the first instruction boundary at or after 5. Jumping to +5 would land on the 0x20 immediate and execute garbage.

Your hook calls the trampoline to invoke the original function, which executes the saved prologue and then continues normally. Follow one call all the way round:

The caller's call pushed a return address pointing back into the caller, but it's the hook's own call trampoline whose return address the original's ret consumes. That's why the result comes back to the hook first, giving it a chance to inspect or change it.

Key Concept

Inline hooking overwrites a function's first bytes with a JMP to your hook. A trampoline preserves the original bytes so the original function can still be called. This is the most general hooking technique - it works on any function at any address, not just imported functions.

Inline Hooking Challenges

Instruction boundaries: x86 instructions are variable-length (1 to 15 bytes). A 5-byte JMP might land mid-instruction, corrupting whatever follows. A disassembler tells you where the safe cut points are.

Thread safety: If another thread is mid-execution inside the function while you're writing the JMP, a partial write can crash the process. Suspend all threads during patching, or write atomically.

Short functions: Targets shorter than 5 bytes leave nowhere for a JMP to live. Hot-patching-friendly code (like MSVC's /hotpatch flag) pads the prologue specifically to dodge this.

RIP-relative addressing: On x86-64, many instructions reference addresses relative to the instruction pointer. Move them to a trampoline at a different address and the RIP shifts, silently breaking the offset. The trampoline has to fix these up byte by byte.

Jump range on x86-64: the 5-byte JMP rel32 only reaches ±2 GB, and a hook in a DLL or a freshly allocated page is often further away than that. Hooking libraries either allocate the hook stub (a small "island") within 2 GB of the target, or patch in a 14-byte absolute jump (jmp [rip+0] followed by the 8-byte address), which means overwriting and relocating more of the prologue.

IAT Hooking (Windows)

On Windows, when a program imports a function from a DLL, the loader populates the Import Address Table (IAT) with the function's actual address. When the program calls the imported function, it goes through the IAT:

text
CALL [IAT_entry_for_MessageBoxA] ; indirect call through IAT

IAT hooking replaces the address in the IAT with a pointer to your hook:

text
original_addr = IAT[MessageBoxA]
IAT[MessageBoxA] = address_of_my_hook
function my_hook(hWnd, text, caption, type) {
log("MessageBoxA called:", text, caption)
return original_addr(hWnd, text, caption, type)
}

Advantages: Clean, no code modification, easy to implement, easy to reverse. Limitations: Only works for imported functions. Direct calls (within the same module) bypass the IAT.

PLT/GOT Hooking (Linux)

On Linux, shared library functions are called through the Procedure Linkage Table (PLT) and Global Offset Table (GOT). The PLT contains stub code that jumps through the GOT:

text
call printf@plt ; call the PLT stub
...
printf@plt:
jmp [printf@got] ; jump to the address stored in the GOT

The first call triggers lazy binding: the dynamic linker resolves printf's address and writes it into the GOT. Subsequent calls go directly through the GOT.

GOT hooking replaces the resolved address in the GOT with your hook's address - the Linux equivalent of IAT hooking. With Full RELRO, the GOT is read-only after start-up, so the hooking code has to mprotect it writable first (from inside the process, that's allowed; an exploit usually can't). The Windows IAT is normally read-only too, which is why IAT hooks call VirtualProtect before the write.

Comparison

AspectInline HookingIAT/GOT Hooking
ScopeAny functionOnly imported functions
ComplexityHigh (instruction handling)Low (pointer replacement)
DetectabilityModifies code bytesModifies data pointers
Thread safetyRequires careAtomic if the pointer write is a single aligned store
ReversibilityMust restore original bytesRestore original pointer

Reach for IAT/GOT hooking first when you can — it's the cleaner approach by a wide margin. Inline hooking earns its keep when you need to intercept internal functions or any call site that bypasses the import table.

Combining the Approaches

Production-grade tools like Detours, MinHook, and frida-gum bake all of this into one library: trampoline management for variable-length instructions, thread-safe installation, and hot-patching while other threads keep running. They give you a clean API and hide the gore. But the gore matters. Knowing what's going on under the hood is what lets you debug a hook that didn't take, spot when antivirus or a rootkit has already hooked the function you wanted, and pick the right tool for the situation.