AlexCTF 2017 : crypto200-poor_rsa

#ctf #cryptography #AlexCTF 2017

This time Fady decided to go for modern cryptography implementations, He is fascinated with choosing his own prime numbers, so he picked up RSA once more. Yet he was unlucky again!

Solution

We’ve received this two files below..

flag.b64

Ni45iH4UnXSttNuf0Oy80+G5J7tm8sBJuDNN7qfTIdEKJow4siF2cpSbP/qIWDjSi+w=

This is a base64 encoded flag message..

..decoding it returns nothing the RSA ciphered message.

key.pub

-----BEGIN PUBLIC KEY-----
ME0wDQYJKoZIhvcNAQEBBQADPAAwOQIyUqmeJJ7nzzwMv5Y6AJZhdyvJzfbh4/v8  
bkSgel4PiURXqfgcOuEyrFaD01soulwyQkMCAwEAAQ==  
-----END PUBLIC KEY-----

Using openssl we can get details about the Public key..

openssl rsa -pubin -in key.pub -text -noout

The 399 bits encryption

As you can see from public key, we have a 399 bit encryption..

In 2009, Benjamin Moody has factored an RSA-512 bit key in 73 days using only public software (GGNFS) and his desktop computer (dual-core Athlon64 at 1,900 MHz). (src: Wikipedia/RSA_(cryptosystem))

399bit is clearly insecure.

Source: https://en.wikipedia.org/wiki/RSA_Factoring_Challenge

According to this evolutional graph, > 768bits is actually “secure”, the minimum recommended.

Knowing this, how to decrypt our flag?

Recovering decryption key

The public key consists of the modulus n and the public (or encryption) exponent e. (Remember from the last challenge?)

To recover d we need to discover the two primes used, because the 399bit poor encryption we know this cipher used small prime numbers to generate n, so we don’t need to calculate this, it can be found in factordb.com (a collective factorization data).

Extracting details from private key

Gmpy can automate the process to extract e and n from public key, but i’ll show u how to extract manually using openssl.. It gets better to understand the big picture.

Send the pub.key content to a variable..

PUBKEY=`grep -v -- ----- key.pub | tr -d '\n'`  
echo $PUBKEY | base64 -d | openssl asn1parse -inform DER -i

The modulus and public exponent are in the last BIT STRING, offset 17, so use -strparse:

echo $PUBKEY | base64 -d | openssl asn1parse -inform DER -i -strparse 17

Using factordb.com to get p and q

Just convert our n to integer

python -c "print(int('0x52a99e249ee7cf3c0cbf963a009661772bc9cdf6e1e3fbfc6e44a07a5e0f894457a9f81c3ae132ac5683d35b28ba5c324243', 16))"

n = 833810193564967701912362955539789451139872863794534923259743419423089229206473091408403560311191545764221310666338878019

..and do a search at [factordb.com](and send to factordb.)

Now we have all elements to solve our problem..

To automate some things, Gmpy can help us w/ to generate the d usign p,q and e, and we do not have to worry about the conversions and the rest of boring equations.

Flag: ALEXCTF{CENSORED}

Full script