Home
Writeups Misc About
Twin Keys

Twin Keys

In the two keys that we have to "insert", one has to start with the prefix of "CryptoHack Secure Safe", and the other must not have this prefix. The two keys have to pass this check:

The xor is flushed out as the number of iterations is a even number, and note that in the calculation of h2, it reuses the value from h1 in the previous line. We can easily verify this by replacing the initial value of h2 to be h1 also.

Therefore, the task is to find two keys, given the prefix constraint, such that they have the same MD5 hash. We can use hashclash to help us in this challenge. Specifically, we will use the Unicoll implementation in hashclash. After building the source code following the instruction, we will run the poc_no.sh script.

Reading the details on Unicoll, we will run the script by specifying a prefix file with the content of "CryptoHack Secure Safelmao", where the "lmao" part is there as the script will remove the last three bytes in the prefix portion. The commands that we have to run are the following (given that you work in the folder of ipc_workdir, following the instructions to run poc_no.sh from hashclash)

Wait for a while, then the two files with the same MD5 hash will be generated, with the file name of collision1.bin and collision2.bin. Sending the hex representation of the two files to the server will return the flag.

Python Implementation:

My initial approach is to use the other script in hashclash, the cpc.sh script, which allows chosen prefix collision. This is used in generating hashquines. I refer to this article detailing how you can generate two images with the same MD5 hash using the cpc.sh script from hashclash.

It takes roughly 10 hours for the script to generate a collision between two files with the prefixes of CryptoHack Secure Safe and W (yes the letter W because why not). I have to run this on a Macbook using M1 chip as my laptop cannot handle the heavy computation needed for this task. Also, there are a lot of things to be tweaked for the script to run correctly on Mac, refer to this PR for more details on fixing the Bash script. Also, there is a warning of Warning: no OS randomness!, which is due to the fact that in lib/hashclash/rng.cpp, the following check does not work:

The fixed script is in this link, which basically removes the checks for Linux/Windows OS, as we know /dev/urandom exists on MacOS. Build the source code after modifying the rng.cpp file.

After 10 hours, the following two files (represented by the two hex strings) has the same hash, with one starting with CryptoHack Secure Safe, and the other with the letter W. Unfortunately these hex strings are too big to send (CryptoHack has a 1024 byte limit). However I do learn a lot about how we can generate programs/images/GIFs with the same MD5 hash, using this approach.