from pwn import *

# p.recv() read all avaiable output
# p.recv(n) read n bytes
# p.recvuntil(string) read until string
# p.recvline() read a line
# p.recvline(False) read a line (discard newline char)
# p.recvall() read until EOF

# p.send(string) send
# p.sendline(string) send string + "\n"

# p.close()
# p.interactive() open a shell-like prompt

# e.symbols a dict { symbol_name: address }
# e.got a dict { got_entry_name: address }

e = ELF("/bin/cat")
p = process("/bin/cat", env={"MY_ENV_VAR": "1"})
# p = process("/bin/cat", env={"MY_ENV_VAR": "1"}, shell=True) you can also start cat as sh subprocess

p.sendline("hi")
assert p.recvline() == "hi\n"

p.sendline("hi")
assert p.recvline(False) == "hi" # do not read trailing \n

p.send("hey lascia entrare ascanio plis")
print "-------------------"
print p.recvuntil("lascia")
print "-------------------"
print p.recv()
print "-------------------"

print "all the 'cat' symbols are:"
for name in e.symbols:
    print "  *", name, "at address 0x%x" % e.symbols[name]

print "all the 'cat' imports in .got.plt are:"
for name in e.got:
    print "  *", name, "at address 0x%x" % e.symbols[name]

p.interactive()

p.close()

