Pwn - I want to break free

204 solves | 205 points

Description

I want to break free... from this Python jail.

Downloads

1KB
Open

Solution

This time we need to escape a python jail. We are provided the source code and server address so, the goal is likely to gain code execution on the server. Taking a look at the source code:

And the blacklist:

The code is very simple and given the limited attack surface area, we can immediately tell that vulnerable code is in the exec command. The first thing we want to do is to try to use the import command as that would allow us to do many things. However, it didn't work because the import command is blacklisted. A quick search of python sandbox escape payloads, gives us the __import__ keyword and it bypassed the blacklist!

Hmm, that's weird though. Shouldn't line 18 of the source code prevent this since import is in the __import__ string? Let's print out the blacklist array at line 17 by adding print(badwords).

Ahh, now it all makes sense. The python file readlines() method not only read in the blacklisted words but also the newline character. Since __import__ in not in the import\n string and import\n string is not in the __import__ string, the sanitization is bypassed. Afterwards, gaining access is trivial. There are probably many ways to read the flag but during the CTF, we used python's default base64 library to encode and decode our payload. The first command we sent was to list the server's directory:

There is a suspicious text file with a really long file name. Our next payload is to read that file:

Flag: kqctf{0h_h0w_1_w4n7_70_br34k_fr33_e73nfk1788234896a174nc}

Last updated