# PlaidCTF - curious - Crypto 70 Point Challenge

## April 21, 2015

Reading time ~2 minutes

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()
``````

### niteCTF - CBC-Jail

A unique combination of Python jailbreak and crypto flaw that had me learning a lot about AES-CBC mode. Super fun for me to get this solu...… Continue reading

#### niteCTF - Rabin to the Rescue

Published on December 12, 2021

#### HTB CyberSanta 2021 - Crypto Writeups

Published on December 04, 2021