This weekend I played the Codegate CTF Junior Qualfiers. I finished in 11th place and so I’ll writeup the two challenges that I’ve solved from the qualifiers.
Let the hacking begins ~ (Solves: ?, 3.0 pts)
Decode it :
Looks like some encoding format, it has too many different special characters so it can’t be base64. Try base85.
Let the hacking begins ~ *turns out the description has the flag itself
nc 220.127.116.11 15959 (Solves: ?, 6.8 pts)
The challenge provides us with the main driver binary and 20000 accompanying .so files.
This is pretty straightforward to reverse. In short, the program requests a number from the user. The corresponding .so file will then be loaded into memory. For example, if the user provides the number
./20000_so/lib_1337.so will be loaded into memory. The
test function from this shared object will then be called.
Now that we understand the driver program, it will be a good idea to open up a few of the shared objects to see what they are doing. Reversing the
test function from
lib_4323.so gives the following code.
lib_1337.so has the following in the test function
While these two might look quite different, there can be a few observations and assumptions we can make. Seeing that both .so files call
system, we can probably assume that that should be the case for the other .so files too. We can crudely verify this by running
grep -rnw "system" | wc -l, which shows us
20000, a good indication that all the files at least have the string “system” in them and are likely to call the function. Another observation that can be made is that the .so files may also call filter functions from other .so files.
With this in mind, we should try to figure out how to automate the criteria for determining if a shared object’s test function will be exploitable. This is the main difficulty of the challenge. There are two aspects we can consider for this, if a filter function is loose, that may allow for us to provide arbitrary system commands. The other case would be the shared object calling the
system function with an argument that allows us to control code execution. In order to proceed, I decided to open up more shared objects to find more patterns.
After opening more shared objects, I noticed that there are 2 general patterns that appear. The binaries all either called
system("ls \"%s\"") where
%s is user input, OR
system("exit"). Now in this case, we know for sure that
system("exit") is not going to allow us any arbitrary control on it’s own.
Since we know that shared objects with
system("exit") will be useless, let’s try to prune out these shared objects in order to reduce our number of potential vulnerable files.
I was originally thinking of using some frameworks or other complicated scripting in order to determine the argument of system. However, I was lazy to learn something new and so I made a greedy assumption, “binaries that do not call
system("exit") will not contain the string
exit”. I can simply list the binaries that contain exit using
grep -rnw "exit" which gives me 15000 matches already. This looks like good progress!
Now the next thing we would have to consider are the shared objects that use
system("ls \"%s\""). Now these might be exploitable as they contain some portion of user input. If the user is able to supply backticks or double quotes, we could escape the argument and call arbitrary sh commands. Thus we would have to figure out if the filter functions blocked these characters. This seemed complicated, so I wanted to first check whether there was a third possible argument that is passed into system, other than
exit. I did the same command
grep -rnw "ls \"%s\"" and to my surprise it gave me 4999 matches! Immediately, I knew I had gotten the answer. Earlier I had pruned 15000 shared objects, now that I remove 4999 objects, I am only left with one! The only remaining shared object file is
lib_17394.so. Quickly reversing this shows us that it has the following lines.
We can run any arbitrary command! However, it does in fact have a filter that filters out some characters and the string
bash. One string that bypasses this is simply “sh”. With this, we can pop a shell and get the flag!
I like an algorithm
nc 18.104.22.168 15712
nc 22.214.171.124 15712 (Solves: ?, 7.0 pts)
==> Hi, I like an algorithm. So, i make a new authentication system. ==> It has a total of 100 stages. ==> Each stage gives a 7 by 7 matrix below sample. ==> Find the smallest path sum in matrix, by starting in any cell in the left column and finishing in any cell in the right column, and only moving up, down, and right. ==> The answer for the sample matrix is 12. ==> If you clear the entire stage, you will be able to authenticate. [sample] 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 1 1 1 99 1 1 1 1 99 1 99 1 99 99 99 99 1 1 1 99
Essentially, we have to find a path from a square on the left column travel to the right column using the least sum possible. Then we submit this sum and repeat 100 times. This obviously has to be automated as it’s pretty impossible to do 100 by hand within the timeframe.
Having done some PPC stuff before, I immediately thought of using Djikstra to solve this challenge. Djikstra is a graph theory algorithm which allows us to find the shortest path between two nodes in a graph. To adapt it to this challenge, we can assign the weight or distance between two nodes to just be the number on the node. For example with the following board
1 2 3 4 5 6 7 8 9
We can just say that the distance to go from 1 to 2 is 2 units. This allows us to apply Djikstra to find the lowest sum path between the starting and ending node.
Now that we can find the lowest sum path between 2 nodes, we just need to repeat this for every possible starting and ending point and find the minimum sum as our answer. I didn’t implement the solution for this challenge and just took the code from a nice site I like to use called GeekForGeeks.
After solving 100 levels, the server tells us “@@@@@ Congratz! Your answers are an answer”. Thus we simply have to convert each answer to it corresponding ASCII character to get a base64 encoded flag!