## Tuesday, April 21, 2015

### PlaidCTF - curious - Crypto 70 Point Challenge

A 70 point crytpo challenge with the the following clue:

Curious
Crypto (70 pts)
The curious case of the random e.

We've captured the flag encrypted several times... do you think you can recover it?

A very quick challenge for me. We are only presented with a clue that the flag had been captured encrypted several times. The included file is a list of 101 different "N : e : c" values such as these:

`````` {N : e : c}
``````

Given that we have the values n, e and c we assume we're talking RSA here as these are commonly used to represent the values of the modulus (N), the exponent (e) and the ciphertext (c). In RSA the modulus and the exponent together form the public key.

So given that it is RSA we start thinking about all the different types of cryptanalysis we can do when we have many keys and one plaintext. I thought about a common factor attack but quickly discounted it when I looked at the size of the exponents.

Just like the recent BCTF warmup challenge, we have here a case of overly large exponents. It would be interesting to check these exponents for a vulnerability to Wiener's attack.

I re-used my warmup exploit but modified it to read the curious file and iterate over the exponents to see if we could crack any of them for the value of "d".

After 59 failures we reach success as the 60th exponent in the list was vulnerable to Wiener's attack. We then quickly solved for "M" to receive the flag:

`````` root@mankrik:~/plaid/curious# ./curiouspwner.py
[+] Loaded 101 ciphertexts and public keys.
[+] Attacking n and e
[+] Wiener attack in progress...
[+] Attacking n and e
[+] Wiener attack in progress...
[+] Attacking n and e
[+] Wiener attack in progress...
[+] Attacking n and e
[+] Wiener attack in progress...
...
[+] Attacking n and e
[+] Wiener attack in progress...
[+] Attacking n and e
[+] Wiener attack in progress...
[+] Found d = 23974584842546960047080386914966001070087596246662608796022581200084145416583
[+] Flag:
flag_S0Y0UKN0WW13N3R\$4TT4CK!
``````

And here's the exploit we used to get there, again based on the code from https://github.com/pablocelayes/rsa-wiener-attack:

`````` #!/usr/bin/python
import ContinuedFractions, Arithmetic
import sys
import base64
import gmpy
import sympy
import math
import fractions
import struct
sys.setrecursionlimit(100000)
f = open('cap','rb')
f.close()
rsatrunk = rsastuff.splitlines()
modulii = []
exponents = []
ciphers = []
for junk in rsatrunk:
gear = junk.split(":")
gear = gear.replace("{","")
gear = gear.lstrip()
gear = gear.replace("}","")
if "N" in gear:     # handle the header
continue
modint = long(gear,16)
expint = long(gear,16)
ciphint = long(gear,16)
modulii.append(modint)
exponents.append(expint)
ciphers.append(ciphint)
print "[+] Loaded " + str(len(ciphers)) + " ciphertexts and public keys."
def hack_RSA(e,n):
print "[+] Wiener attack in progress..."
frac = ContinuedFractions.rational_to_contfrac(e, n)
convergents = ContinuedFractions.convergents_from_contfrac(frac)
for (k,d) in convergents:
#check if d is actually the key
if k!=0 and (e*d-1)%k == 0:
phi = (e*d-1)//k
s = n - phi + 1
# check if the equation x^2 - s*x + n = 0
# has integer roots
discr = s*s - 4*n
if(discr>=0):
t = Arithmetic.is_perfect_square(discr)
if t!=-1 and (s+t)%2==0:
return d
for a in range(len(modulii)):
print "[+] Attacking n["+str(a)+"] and e["+str(a)+"]"
hacked_d = hack_RSA(exponents[a], modulii[a])
testd = str(hacked_d)
if "None" in testd:
continue
else:
print "[+] Found d = " + str(hacked_d)
m = pow(ciphers[a], hacked_d, modulii[a])
print "[+] Flag:"
print("%0512x" %m).decode("hex")
quit()
``````

1. hey! Boneh and Durfee is more efficient than Wiener (and more recent), I wrote about it here: cryptologie.net/article/265/small-rsa-private-key-problem/

2. Thanks David! I love reading more about RSA cryptanalysis. I'll check out your writeup!

3. ^^ by the way how long did it take you to run your wiener's attack? I think that would be a good measure to compare to boneh and durfee attack as well.

4. To check all exponents including calculating d on the 60th took just 18 seconds on my VM.

5. wow pretty fast! Didn't know wiener was that fast :)