Labs / Linux Exploitation / Frameworks & memory corruption

Lab 2 of 3 · Frameworks & memory corruption

A stack overflow that returns into win()

Overrun a stack buffer onto the saved return address, and send a setuid-root binary into a win() it never calls — landing as root.

Start this lab 5 steps

The idea

Alma has a signed engagement against Ashcombe Instruments and a foothold on a calibration host as the service account alma. On that host sits a small service binary, /opt/challenge/stack-vuln, that reads a "unit serial" into a fixed stack buffer — with no check on how long the serial is. That is a [[stack-overflow|stack buffer overflow]]: write past the buffer and you keep writing, over the function's saved return address, the slot that says where the function returns to. Control that slot and you control where the program goes next.

You will not inject any code. The binary already contains a win() routine its own control flow never reaches — so the exercise is ret2win: overrun the buffer with the right amount of padding, then overwrite the saved return address with the address of win(). The binary is setuid root, so when win() runs, it runs as root. It is built deliberately soft — no stack canary, no PIE — so the mechanism is visible; the last step names the standard defences that would stop it cold.

Steps

  1. The foothold and the console

    You are on Alma's Kali machine. The foothold is an SSH login on the calibration host at 10.0.1.20 as the service account alma. Click Start to open a shell there, confirm who you are with id, and run the service binary once the way it expects — type a short serial. It takes your input and returns normally. That ordinary run is the thing you are about to break.

    kali:~# ssh box
  2. A soft binary with a dead-code win()

    Before attacking, read the binary. file shows it is not a position-independent executable (no PIE) — its code sits at fixed addresses every run. nm lists its symbols, and among them is a function called win — which, if you read the program's logic, is never called. That is the target: a routine already compiled in, sitting at a fixed, printable address, that the program will happily run if only control reached it. It does not, by design — so you will make it.

    kali:~# ssh box 'file /opt/challenge/stack-vuln; nm /opt/challenge/stack-vuln | grep -E " T (win|vuln|main)$"'
  3. Find the offset to the return address

    The buffer is small, but how far is it from the start of your input to the saved return address? That distance is the offset, and you find it by watching when the program stops returning cleanly. Feed it a run of bytes that fits — it prints Returned normally. Feed it a longer run that overruns the return slot with junk, and the program crashes (segmentation fault), because it tries to return to an address made of your As. The boundary between "returns" and "crashes" is the offset. The professional way to pin it exactly is gdb with a cyclic pattern (cyclic 200 in pwndbg, then read which 8 bytes landed in $rsp/$rip); here that boundary is 88 bytes — 88 of input fills everything up to, but not into, the return address.

    kali:~# ssh box 'for n in 40 88 96 120; do printf "A%.0s" $(seq 1 $n) | /opt/challenge/stack-vuln >/dev/null 2>&1; echo "len=$n exit=$?"; done'
  4. Return into win() — as root

    Now put the two numbers together: 88 bytes of padding to reach the saved return address, then the 8-byte address of win() (0x4011a6) in little-endian so the overwritten return address is win. Build that with Python and pipe it into the binary. Because /opt/challenge/stack-vuln is setuid root, when control reaches win() it runs with root's effective id — and win() raises its real id to match and reads the root-only flag. This is the whole exploit, and it is the moment the overflow becomes a privilege gain.

    kali:~# ssh box 'python3 -c "import sys,struct; sys.stdout.buffer.write(b\"A\"*88+struct.pack(\"<Q\",0x4011a6))" | /opt/challenge/stack-vuln'
  5. What Ashcombe's developers should change

    Alma's finding is not "someone typed a long serial". It is that unchecked input reaches the stack, and that the binary was built without any of the defences that would have stopped the overwrite. The fixes, in order:

    • Bound the read. The root cause is a read with no length limit writing into a fixed buffer. Read at most sizeof(buf) bytes (fgets, a sized read, snprintf) and the overflow is gone at the source.
    • Turn the compiler defences back on. This binary was built -fno-stack-protector -no-pie. A stack canary puts a secret value between the buffer and the saved return address and checks it before returning — a linear overflow smashes it and the program aborts. PIE + ASLR randomise the addresses so win's location is not a constant. A non-executable stack (NX) stops injected shellcode. Toolchains set these by default; keep them.
    • Drop the setuid bit. A service that parses input has no business being setuid root. Without the bit the same overflow is a crash, not a root shell — defence in depth behind the real fix.

    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. Alma has a signed engagement; that permission is the only thing separating this from a crime.

    kali:~# ssh box 'ls -l /opt/challenge/stack-vuln; file /opt/challenge/stack-vuln | grep -o "no pie\|pie"'

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