Skip to content
Pwnversity
PWN-FORMATadvanced · 6 min · upd. 2026-10

Format String

CWE-134

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.

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.

printf(user_input); // VULNERABLE: the user controls the format
printf("%s", user_input); // CORRECT: fixed format, the user is just an argument

printf 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"]
  • %p %p %p %p — dump stack values as pointers (better than %x on 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$s reads 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()

%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:

  • %n writes an int (4 bytes); %hn a short (2); %hhn a byte.
  • Technique: set the count with %<N>c and the target with %<pos>$n pointing 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.

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 system
payload = 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:

  1. Confirm the vulnerability (feed %p %p and see if it dumps the stack).
  2. Find the offset of your input (%N$p until you see your marker).
  3. LEAK: leak canary, libc, and/or PIE (bypass mitigations).
  4. WRITE: %n/fmtstr_payload over the GOT or the retaddr → redirect to system/one_gadget.
  5. Shell.
  • Always use a fixed format: printf("%s", x), never printf(x). Same for fprintf, 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 %n in format strings stored in writable memory.
  • Full RELRO (-Wl,-z,relro,-z,now) makes the GOT read-only and cuts the classic %n target.
  • Code review / SAST to detect user-controlled formats.
  • Confirm the format string (%p %p dumps 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?