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
-
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.20as the service accountalma. Click Start to open a shell there, confirm who you are withid, 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
-
A soft binary with a dead-code win()
Before attacking, read the binary.
fileshows it is not a position-independent executable (no PIE) — its code sits at fixed addresses every run.nmlists its symbols, and among them is a function calledwin— 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)$"'
-
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 yourAs. The boundary between "returns" and "crashes" is the offset. The professional way to pin it exactly isgdbwith a cyclic pattern (cyclic 200in 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'
-
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 iswin. Build that with Python and pipe it into the binary. Because/opt/challenge/stack-vulnis setuid root, when control reacheswin()it runs with root's effective id — andwin()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'
-
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 sizedread,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 sowin'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"'
- Bound the read. The root cause is a read with no length limit writing into a fixed buffer. Read at most
Run these steps on real machines