This challenge was solved by one of my teammates, vek and me, and the writeup was written by vek.
The Choices binary was bascially a giant switch case in a loop and the executed cases were determined by the user’s input at each iteration.
Based on the user inputs, the binary generated a 56-byte flag from an array of constants that’s the same size. What we needed to figure out is the order in which the switch cases needed to be executed. The flag was compared to the correct flag’s hash at the end, so we would know when we’re right.
The binary was compiled with the clang plugin lib0opspass.so. After reversing the library, we found that it took the original program’s basic blocks and turned them into individual cases of the giant switch statement. The user input for a corresponding switch case was generated by a scramble32 function from the BBs’ ID.
scramble32 used a key generated from a constant seed BAADF00DCAFEBABE3043544620170318 to scramble the BB IDs, but it didn’t change the scrambler’s internal state, so a given (key, ID) pair always gives the same scrambled output. This means that we only had to extract the key and use it to call scramble32() on the first few integers to get the correct input for each BB.
The key could be extracted by running the supplied compilation command on a dummy source in gdb and breaking right before a call to scramble32(). After this, we could extract the scrambling code and write a C program that generated the scrambled version of the numbers from 0 to 1000. It seemed that the number for 0, 1, 2, etc. all matched a possible user input in the binary, so we found the IDs of all the original basic blocks! We still didn’t know the original control flow graph from this, but we assumed that each BB followed one with one smaller ID, so we ran:
And it gave us the flag, so it turned out we were right!
After reversing the binary it turned out the structure of the challenge is the usual menu system based pwn challenge.
We had the following menus:
“1 :) Fill your information”
It reads your team name to a 20 byte buffer and prints it out. It can be used to leak data from stack if you don’t fill the 20 byte buffer. For example it can be used to leak the heap base address. But we never used this vulnerability in the final exploit.
“2 :) Upload and parse File”
This is where the main functionality lies: you can upload a zlib-compressed PNG image, it will parse its headers, allocate a width*height-sized buffer with mmap, and copies the content into it (but limits the size to the buffer length, so it does not cause overflow). It also puts various metadata into a linked list about these uploaded files, including the decompressed content’s size.
“3 :) Show File info”
Prints out the width, height and “pixels” (bit depth) properties of the chosen file. Not used in the exploit.
“4 :) Delete File”
Deletes a selected file. Munmaps its content (based on the decompressed content’s length), frees it’s metadata and unlinks the item from the linked list. We will use the munmap in the exploit.
“5 :) Commit task”
Creates a thread which prints out every file’s width, height, bit depth. You can only call this method six times. Not used in the exploit, probably just a red herring :)
“6 :) Monitor File”
Create a thread which notifies you if a new file was uploaded. It uses a conditional variable (and mutex) to block until the main thread notifies it.
8 - a hidden “backdoor”
It’s a gets-based stack overflow, but exits immediately after it overwrites the stack. Although the deinitialization logic runs in the exit call, the stack overflow is still not exploitable this way. It’s obviously there just for misleading us.
There are a few misleading / non-exploitable functionality (including the whole PNG parsing), but there is something fishy going on with the content length calculation when it allocates the buffer based on the width and height instead of the decompressed content length.
Also despite that the PNG is a compressed file format, there is an additional layer of compression which is a hint about that we may have to send a lot of data. As we will see later, our hunch was not unfounded.
If we take a closer look at the buffer allocation, it turns out fast that the allocation size and the length used for munmap can differ.
This is exactly the same situation that my teammate tukan described in one of his ptmalloc fanzine back in 2016, so he could point us into the correct direction right away.
Although the next mapped memory segment after our mmap’d allocation is either the read-only data section of the libc or the guard page before the stack of a thread created by either the 5th or 6th menu item, the munmap can remove them if we use a bigger length than the original allocation was.
So our plan is the following:
create a long-running thread (with 6th menu item), which will mmap a thread stack (the size is architecture dependent, but was 8MB on x64).
the thread waits for the condition variable until we upload a new file
upload a file whose decompressed length is bigger than the allocation size, so munmap will remove the following mmap’d segment too
I used the following values: width = 1024, height = 1024, width * height = 1MB, decompressed length = 9MB (1MB my content + 8MB overflow into the thread’s stack)
delete this file with the 4th menu item
although the thread’s stack is unmapped, the thread won’t wake up, so this won’t cause any problem (segmentation fault for example)
upload a new, fake thread stack (8MB), and putting a ROP chain in the place of the original stack where the execution returns
we also have to restore a few variables (like mutex and cond) so the waking up thread won’t crash immediately or behave incorrectly
the ROP chain does the following:
leaks libc address (puts in our case)
unlocks the mutex, so the main thread won’t block anymore
make the thread sleep forever, so its broken state won’t cause any problems for us (for example race condition)
I opened the webpage from the challenge description: https://52.196.116.69/index.php which resulted in Chrome showing SSL cert error: NET::ERR_CERT_AUTHORITY_INVALID.
I checked the CN of the SSL cert which was: very-secret-area-for-ctf.orange.tw so I created the following entry in my host file:
Then I opened the https://very-secret-area-for-ctf.orange.tw/ URL, which gave me the flag:
While reversing the code it became clear that the most part of the code are for handling BigInteger operations.
The only interestring structure is this, it contains the current digit count of the BigInteger and the digits as dwords (one dword contains one digit 0-9):
The functions were pretty trivial after finding what’s going on, there were basic BigInteger methods like Add, Subtract, Multiply, Modulo, Remainder, ModPow, GCD, comparisons (Eq, Neq), etc.
The only weird one (0xBDB) was the one which calculated this: (x ** (n!)) % N. (x and n are parameters the function and N is a global constant modulus).
After finished reversing these methods, only main remained which does the following:
converts N to BigInteger format
N = 70175232617155622721369403112218008731727137018442195462238305570433409024579, it can be factorized into (I used factordb.com):
p = 208467877680031083617459630285634936973 and
q = 336623720633184311690592367893275345423
calls srand(33177711) and generates 256 rand() numbers and converts them to BigInteger
runs the following loop:
Of course it will never finish as it would take too long time.
So I asked my cryptopal (@ngg) how to solve this and he said, let’s get the factors of (p-1)*(q-1) and probably the largest one will the flag.
These were the factors:
So I tried to submit: hitcon{3b72110158a74799635e71} (71865286827970831870811761 = 0x3b72110158a74799635e71) which was not the correct flag :(
Then I tried the second largest factor (3981441750675421269733) which gave us the correct flag:
After checking the source code of the challenge, it was clear that the flag was used for two purposes: as AES key and as an IV.
AES is secure enough not to crack the key, but we can find out the IV with the following ‘trick’:
(the images at https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#CBC is always a good source of material to help thinking through what happens)
I replaced the first block of example encrypted text with the second block (so the blocks were in the following order now: c2|c2|c3), I kept the third block, so the padding remained correct and I decrypted it.
If we write down how the encrypted blocks created, we get this (p = plaintext, c = ciphertext, ^ = xor, E = AES encrypt, || = block boundary):
So if we want to know the IV (=FLAG) we can do this this way: IV = dec1 ^ p2 ^ c1 as we know all the ‘variables’.