Labs / Linux Exploitation / Frameworks & memory corruption

Lab 3 of 3 · Frameworks & memory corruption

A format string that reads a process's memory

Feed format specifiers to a greeting program instead of a name, walk the stack with %p, and read a root-only secret with %s.

Start this lab 5 steps

The idea

Rune has a signed engagement against Cresswell Telecom and a foothold on a provisioning host as the service account rune. On the box is a small service binary, /opt/challenge/leak-vuln, that greets you by name. The bug is in how it builds the greeting: it passes your name straight to printf as the format string — printf(name) instead of printf("%s", name).

That is a format-string bug. printf acts on whatever conversion specifiers the string contains, reading its arguments off the stack: %p prints a stack word, %s treats a stack word as an address and prints the string there, and %7$p jumps straight to the seventh word. The binary is setuid root and holds a secret it read from a root-only file — a file rune cannot read with cat. You will walk the stack with %p, find where the secret sits, and read it back out with %s. No crash, no code — only reading memory the program never meant to show you.

Steps

  1. The foothold and a door you cannot open

    You are on Rune's Kali box. The foothold is an SSH login on the provisioning host at 10.0.1.20 as the service account rune. Click Start to open a shell there, confirm who you are with id, and try to read the secret file /opt/challenge/token.txt directly. It is owned by root and closed to you — that is the door the format-string bug will open from the inside.

    kali:~# ssh box
  2. A greeting that trusts your name

    Run the portal the way it expects — give it a name, and it answers Welcome, <name>. Now give it a name made of conversion specifiers instead: %p %p %p. If the program were printing your name safely it would echo %p %p %p back verbatim. Instead it prints addresses — proof that your input is being used as the format string, and that printf is reading words off the stack for each %p.

    kali:~# ssh box 'printf "%%p %%p %%p %%p %%p %%p\n" | /opt/challenge/leak-vuln'
  3. Walk the stack to the secret

    You do not have to count %ps by hand. %N$p jumps straight to the Nth stack word — %7$p is the seventh — so you can probe positions directly. The setuid binary loaded the root-only secret into memory before greeting you, and a pointer to it sits on the stack. Probe positions with %N$p until one holds a pointer into that secret; here it is position 15. (You can confirm the layout in gdb with processTracing on, but the %N$p walk finds it without one.)

    kali:~# ssh box 'for n in 13 14 15 16; do printf "slot %s: " "$n"; printf "%%$n\$p\n" | /opt/challenge/leak-vuln | sed "s/.*Welcome, //"; done'
  4. Read the secret with %s

    %p printed the pointer; %s follows it. Send %15$s and printf takes the word at position 15 as an address and prints the string there — the root-only secret the setuid process is holding. You read a file rune was refused, without ever touching the file: you read it out of the privileged process's memory through the format-string bug.

    kali:~# ssh box 'printf "%%15\$s\n" | /opt/challenge/leak-vuln'
  5. What Cresswell's developers should change

    Rune's finding is one line of code: untrusted input was used as a format string. The fixes:

    • Never let input be the format. printf("%s", name) instead of printf(name). The name is then only ever data, and %p/%s in it are printed as the literal text they are. This one change closes the bug.
    • Turn on the compiler's warnings. -Wformat -Wformat-security flags printf(name) at build time, and _FORTIFY_SOURCE aborts some format-string misuse at runtime. They exist precisely to catch this class.
    • Remember why a read matters. Even when a leak reveals "nothing useful", it often reveals an address — and one real address defeats ASLR, turning an unpredictable overflow target into a known one. A disclosure is routinely the first half of a larger exploit, which is why it is worth finding on its own.

    One honest line: running any of this against a system whose owner has not authorised you in writing is unauthorised access, an offence under laws like the UK Computer Misuse Act and the United States' CFAA. Rune has a signed engagement; that permission is the only thing separating this from a crime.

    kali:~# ssh box 'ls -l /opt/challenge/leak-vuln /opt/challenge/token.txt'

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab