Technique:Function hooking/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 GOT/lazy-pointer slots (plthook, fishhook), AArch64 inline hooks (Dobby) and Frida.
Four levels, from least to most invasive
- Interposition by load order. The dynamic linker resolves a symbol to the first definition in its search order, so a library loaded first wins. On Linux,
LD_PRELOAD=libhook.soputs your library first; inside itdlsym(RTLD_NEXT, "name")finds the next definition (the real one) to call through. On macOS,DYLD_INSERT_LIBRARIESdoes the same, and a__DATA,__interposesection holds an array of (replacement, replacee) pointer pairs that dyld applies to all images. Unverified: the layout of__interposeand the exact precedence rules are from memory of Apple's dyld sources (apple-oss-distributions/dyld, APSL-2.0), not read here. - Patch the import slot of one module. The program calls library functions through the GOT/PLT (ELF) or lazy and non-lazy symbol pointers (Mach-O). Overwriting one pointer redirects calls made from that module only; no code bytes change. See Library:plthook (ELF) and Library:fishhook (Mach-O). Calls made inside the library itself, or through a pointer taken earlier, are not seen.
- Inline hook. Overwrite the first instructions of the target with a branch and keep a trampoline with the displaced instructions, as on Windows (Technique:Function hooking). On AArch64 see Library:Dobby.
- Dynamic instrumentation framework. Tool:Frida injects an agent (or the Gadget library) and offers Interceptor, Stalker and a JavaScript API over inline hooks.
ELF: what plthook-style libraries patch
The slots live in the GOT, listed by the relocation tables of Format:ELF (Executable and Linkable Format)/Relocations and PLT: DT_JMPREL entries (JUMP_SLOT, lazy imports) and .rela.dyn entries of type GLOB_DAT. plthook finds the module through dl_iterate_phdr, dlinfo(RTLD_DI_LINKMAP) or dladdr1, walks those tables (plthook_enum), compares the symbol name (an @version suffix is allowed) and writes the new pointer; if the page is not writable it mprotects it for the write and restores the old protection. Unverified limits: with full RELRO and BIND_NOW the GOT is read-only after startup (the library above changes protection itself, which a hardened system may refuse); a symbol bound at link time (static, -Bsymbolic, hidden) has no slot to patch.
Mach-O: lazy and non-lazy pointers
fishhook walks every loaded image (_dyld_register_func_for_add_image, so images loaded later are handled too), finds __DATA and __DATA_CONST sections of type S_LAZY_SYMBOL_POINTERS and S_NON_LAZY_SYMBOL_POINTERS, maps each pointer to a symbol name through the indirect symbol table (section->reserved1 is the first index; entries ABS/LOCAL are skipped) and swaps pointers whose name matches (Mach-O symbol names carry a leading underscore, which is skipped). __DATA_CONST is made writable with vm_protect(... VM_PROT_COPY) before writing. See Format:Mach-O.
Why hardened runtime and SIP limit injection
Unverified (Apple documentation, not fetched here): on macOS, programs with the hardened runtime ignore DYLD_* variables unless they carry an entitlement allowing them, library validation refuses to load libraries not signed by the same team, and System Integrity Protection strips these variables for protected system binaries. Practical consequence: for your own program, sign a test build without the hardened runtime or with the com.apple.security.cs.allow-dyld-environment-variables entitlement. On iOS, injection into other apps needs a jailbroken or developer-provisioned device; for your own app, embed the Frida Gadget framework.
AArch64 inline hooks
Every instruction is 4 bytes, so a hook needs few bytes but must be placed by reach:
- Direct
breaches +-128 MiB (26-bit word offset: Dobby'sARM64_B_XXX_RANGEis(1<<25)<<2, tested as|dst-src| < range). One instruction is overwritten. adrp x16, page; add x16, x16, off; br x16reaches +-4 GiB (Dobby'sadrp_rangeis1<<(2+19+12-1)); three instructions, Dobby's ARM64_TMP_REG_NDX_0 is 17 (x17); Unverified: x16/x17 are the intra-procedure-call scratch registers of the AArch64 ABI.ldr x16, #8; br x16; <8-byte absolute address>reaches everywhere; the literal is embedded after the branch. This is the branch-plus-literal shape.
The displaced instructions go into a trampoline elsewhere, and any PC-relative one changes meaning when moved, so it must be rewritten: b/bl (re-encode the offset, or load the absolute target if out of range), b.cond, cbz/cbnz, tbz/tbnz (Unverified: conditional branches have short ranges, +-1 MiB and +-32 KiB, so a relocator typically emits an inverted conditional skipping an absolute jump; how Dobby does it was not read), ldr literal, adr and adrp. Dobby's relocator handles exactly these classes. Instructions that read PC in other ways, exclusive load/store pairs (ldxr/stxr, which must not be split) and a target shorter than the patch need care. On Apple silicon and iOS, code pages cannot simply be made writable and executable: a W^X-respecting writer (or a debugger/jailbreak) is needed. Unverified.
Frida in one paragraph
See Tool:Frida: Interceptor.attach(target, {onEnter(args){}, onLeave(retval){}}) observes, Interceptor.replace swaps the implementation. For apps you cannot start under frida-server, the Gadget shared library is loaded by the app (e.g. through a LIEF-edited DT_NEEDED entry on ELF or an added LC_LOAD_DYLIB on Mach-O) and reads a .config file next to it. Unverified: LIEF can add a DT_NEEDED or a load command (lief.parse(...).add_library(...)); the call names are from memory of its documentation.
Scope
Only for programs you own or are permitted to analyse. Techniques that hide a hook from security software are out of scope of this wiki.
Code & repositories
- https://github.com/kubo/plthook (BSD-2-Clause)
- https://github.com/facebook/fishhook (BSD-3-Clause)
- https://github.com/jmpews/Dobby (Apache-2.0)
- https://github.com/kubo/plthook @
24c69df003310fb91f9c2950b5a659ff70e9dfb9· license: BSD-2-Clause (declared by the contributor; GitHub's detector did not recognize a license file)
plthook: GOT/PLT/IAT entry replacement (plthook_elf.c, plthook_osx.c).
Tested on: source read, not run: source read, not run · checked 2026-10-18; archived; last activity 2025-10-05 - https://github.com/facebook/fishhook @
aadc161ac3b80db07a9908851839a17ba63a9eb1· license: BSD-3-Clause
fishhook.c: Mach-O lazy/non-lazy symbol pointer rebinding.
Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2024-08-01 - https://github.com/jmpews/Dobby @
5dfc8546954ce3b3198132ab13fddb89ee92cdd7· license: Apache-2.0
Dobby: inline hook framework; ARM64 trampolines and instruction relocation.
Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2025-01-26