Intro
Epreuve 12-3 – Coffre
En tant que stagiaire vous avez accès aux locaux de la NSB. Vous allez collecter des informations dans les locaux. Un coffre est présent dans les locaux en salle rideau. Il appartient à Richard Cresus de la Tune. Essayez d’ouvrir ce coffre. Quel est l’IBAN contenu dans le coffre ? Format de la réponse : IBAN sans séparateur.
Basically, we have to crack open an electronic safe. It’s locked with an electromagnet and requires a pin to open, moreover it prints an id right before asking for the pin. We previously were given a link to the download page one of the safe’s software update (http://safe-locks.tracs.viarezo.fr/download
).
Reversing the custom libcrypto.so library
The software update comes in the from of a .maj
archive that we extracted to get two libcrypto.so
libraries (one for x86, the other one for arm64 v7). We checked if the files were equivalent by looking at their code structure, and we finally choose to reverse the x86 library (even though the safe probably used the arm one) because it was easier.
Firstly, we looked at how the pin was checked, more specifically at the libsafe_test_passcode
in IDA:
1 | _BOOL8 __fastcall libsafe_test_passcode(const char *a1) |
We assume the argument is a pointer to the pin, for which we compute its sha256sum
. And if it is equal to buf[4:0x24]
, it means the pin correct! So we have to understand what buf[4:0x24]
is, which is stored in the .safe_db
file. To do so we look at the libsafe_generate_new_passcode
function:
1 | __int64 __fastcall libsafe_generate_new_passcode(unsigned __int8 *a1) |
The function is very basic:
- It takes as argument a pointer to the buffer to cipher for which we compute the hash to fill out the
.safe_db
file. - It initializes the PRNG with
time(NULL)
passed as an argument tosrand
. It then creates an array of1024
random bytes with the use ofrand
. - Then, this array is hashed with
sha256sum
and its hash is given to the_build_passcode
function. The result is stored in thea1
argument. - The argument is hashed again and in the target file we write at
file_content[:4]
the firstrand
value and atfile_content[4:0x24]
the hash of the previous ciphered buffer.
The core of the encryption algorithm is in the build_passcode
function:
1 | __int64 __fastcall build_passcode( |
That’s just basically filling out the out
buffer with base[hash_rand_buf[i % length_hash] % lenght_base]
.
Now we have a good understanding of the encryption algorithm, we can take a look at what exactly the id
printed right before the pin input is. The function that generates the id
is libsafe_get_userid
:
1 | __int64 __fastcall libsafe_get_userid(_DWORD *id) |
The function is very basic, it opens the .safe_db
file and initializes the id
to the first four bytes of the file which is the first value of rand as seen in the previous functions.
Cracking the seed
To recover the pin, we have to know what hash the hash of the pin will be compared to. To do so, we have to recover the random buffer, hash it, give it to the “core” encryption layer and hash what it outputs. That will be the final hash which will be compared to the hash of the pin we send. The main part of the challenge is so to recover the rand
values, more specifically the seed given to srand
to initialize the PRNG. We know the seed in the program is time(NULL)
. Which means that this is a timestamp that can be bruteforced in a reasonable amount of time (the 2020 edition of the CTF was cancelled because of COVID so we took as range the date of the software update until today). The bruteforce is very fast because given we know the id
which is the value for the first call to rand
, we have just to ensure the first value of rand
for the seed we bruteforce is equal to the id
value.
Which gives:
1 | from tqdm import tqdm |
And when we found the right seed, we just have to generate, hash, cipher and hash again the right random buffer to get the right hash to which the hash of the pin will be compared to.
1 | python3 solve.py |
Hashcat + PROFIT
Now we know the final hash to which the hash of the pin is compared to, we can just run a mask attack using hashcat with a mask of 8 hexadecimal characters in uppercase (we tried for every length up to the right size: 8).
1 | $ hashcat -a 3 -m 1400 pincode.hash ?H?H?H?H?H?H?H?H |
The challenge was pretty funny because of the IRL part, and because we solved it together (nasm and Alol).