PyJail Terminal
Welcome to the PyJail Terminal! Type your Python commands below.
import base64
import codecs
from io import StringIO
from js import document
from pyodide.ffi import create_proxy
# Simulated File System (Fake Linux root dir structure)
fake_file_system = {
"/flag.txt": "flag{qu1nn_w@s_h3re}",
"/home": ["user1", "user2"],
"/usr": ["bin", "lib", "local"],
"/etc": ["passwd", "shadow"],
"/tmp": [],
}
# Simulate Unix commands like 'ls', 'uname', 'cat', etc.
def mock_os_listdir(path):
if path == "/":
return list(fake_file_system.keys()) # Simulate the root directory
elif path in fake_file_system:
return fake_file_system[path] # Return the contents of the folder
else:
return [] # If the directory doesn't exist, return an empty list
def mock_os_popen(command):
# Simulate output for various Unix commands
command = command.strip()
if command.startswith("cat "):
file_path = command[4:]
return StringIO(fake_file_system.get(file_path, "File not found."))
elif command == "uname":
return StringIO("FakeLinuxOS")
elif command.startswith("ls"):
path = command[3:].strip() if len(command) > 3 else "/"
return StringIO("\n".join(mock_os_listdir(path)))
elif command == "pwd":
return StringIO("/home/user1") # Simulating the current directory
elif command.startswith("echo"):
return StringIO(command[5:].strip()) # Just return the string after 'echo'
return StringIO("Unknown command")
def mock_os_path_exists(path):
# Simulate file existence check
return path in fake_file_system
# Fake os module to mock os functionality
class FakeOS:
def listdir(self, path):
return mock_os_listdir(path)
def popen(self, command):
return mock_os_popen(command)
def path(self):
return mock_os_path_exists
# Instantiate fake os module
os = FakeOS()
# Encrypted flag (Base64 and ROT13 encoded flag)
encrypted_flag = "MzkuM3gkqGShoy93DUAsnQAlMK0=" # ROT13 and Base64 encoded flag
# Mock the decryption of the flag
def decrypt_flag():
decoded_rot13 = base64.b64decode(encrypted_flag).decode('utf-8') # Now this is a string
decoded_flag = codecs.decode(decoded_rot13, 'rot_13') # Reverse ROT13
return decoded_flag
# Override print to send output to the terminal
def custom_print(*args, **kwargs):
output = " ".join(map(str, args))
terminal.innerHTML += f"\n{output}"
# Replace print with custom_print
print = custom_print
terminal = document.getElementById("terminal")
command_input = document.getElementById("command")
def safe_exec(code):
try:
restricted_locals = {"os": os, "print": print}
exec(code, restricted_locals)
return "" # Just return an empty string to avoid "Code executed safely."
except Exception as e:
return f"Error: {e}"
def execute_command(event=None):
command = command_input.value.strip()
if command:
terminal.innerHTML += f"\n>>> {command}"
# Check if the command requires displaying results in the terminal
if "cat /flag.txt" in command:
result = fake_file_system["/flag.txt"]
else:
result = safe_exec(command) # Safe execution for other commands
# Ensure that the result is properly displayed
if isinstance(result, str):
terminal.innerHTML += f"\n{result}" # Display directly if it's a string
else:
terminal.innerHTML += f"\n{result.read()}" # If it's a file-like object, use read()
command_input.value = "" # Clear the input field after execution
terminal.scrollTop = terminal.scrollHeight # Scroll to the latest output
execute_command_proxy = create_proxy(execute_command)
command_input_proxy = create_proxy(lambda event: execute_command_proxy(event) if event.key == "Enter" else None)
document.getElementById("runButton").addEventListener("click", execute_command_proxy)
command_input.addEventListener("keydown", command_input_proxy)