The third edition of the famous Deloitte Hacky Holidays CTF competition start July 8 and finish July 26, 2022. This Year it's "Unlock the city!", It was 18 days of challenges, divided in 4 districts, 33 challenges in total, where 1242 active players tried to unlock the city. Very well elaborated with an excellent platform and well thought out challengess. We chose this CRYPTO challenge for the writeup because it's a very good practical example that no matter how many resources you have, a BAD implementation of an algorithm will always be a persistent threat to you.
Most of the RSA vulnerabilities have been due to poor implementation of the protocol by software designers.
One of the best known vulnerabilities is the Factorisation attack. (If attacker will able to know P and Q using N, then he could find out value of private key)
Letβs test to see if the AI made the same implementation mistake. For this, we use factorDB that will allow us to obtain (if they exist) the primes of a number
Bingo! We have found that N is fully factored, so we know p and q. With these values we will determine a totiem function named phi(n) and of course d (the private key) which will give us access to the message encrypted by the AI.
Decryption Process
We use python to help us get the rest of the values needed to decipher the message.
and when we ran the script we had the deciphered message:
After several tests we realized that the AI had encoded the message in integers that can be decoded in ascii characters.
We created a list of what we thought might be printable ascii characters and made a small script in python
First we need to iterate over the msg_encode list and convert each item, creating a new list, and them, we use the join method of the empty string to join all of the strings together
When we run the script and VOILA!!
and thus we got the encrypted message left by the IA
CTF{RSA_br0ken}
The CTFβs Way
After all itβs a CTF!!, itβs important to solve the challenges by knowing what it is all about, interacting with the different components and most importantly, Learning.
That said it is also important that every CTF player has a complete arsenal of tools, and as a part of that arsenal, RsaCtfTool is one of the favorite tools among the CTF Players for solving RSA challenges. This challenge is no exception.
If we look at the last three numbers of the βINT (big endian) valueβ, (125) we see that it is a printable ascii character whose value is β}β. If we make a list separating each character we get the following:
We take that list to CyberChef and we get the flag
Some Theory behind RSA
RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means that it works on two different keys i.e. Public Key and Private Key. As the name describes that the Public Key is given to everyone and Private key is kept private.
The idea of RSA is based on the fact that it is difficult to factorize a large integer. The public key consists of two numbers where one number is multiplication of two large prime numbers. And private key is also derived from the same two prime numbers. So if somebody can factorize the large number, the private key is compromised. Therefore encryption strength totally lies on the key size and if we double or triple the key size, the strength of encryption increases exponentially.
RSA cryptosystem
Key generation
Choose two distinct primes p and q of approximately equal size so that their product n=pq is of the required bit length.
Compute Ο(n)=(pβ1)(qβ1).
Choose a public exponent e,1<e<Ο(n), which is coprime to Ο(n), that is, gcd(e,Ο(n))=1.
Compute a private exponent d that satisfies the congruence edβ‘1(modΟ(n)).
Make the public key (n,e) available to others. Keep the private values d, p, q, and Ο(n) secret.
RSA Encryption scheme
Encryption rule: ciphertext, c = RsaPublic(m) = memodn, where 1<m<nβ1.
Decryption rule: plaintext, m = RsaPrivate(c) = cdmodn.
Inverse transformation: m = RsaPrivate(RsaPublic(m)).