plai_n_rsa
2 minutes to read
We are given the Python source code to encrypt the flag:
import os
from Crypto.Util.number import bytes_to_long, getPrime
flag = os.getenvb(b"FLAG", b"SECCON{THIS_IS_FAKE}")
assert flag.startswith(b"SECCON{")
m = bytes_to_long(flag)
e = 0x10001
p = getPrime(1024)
q = getPrime(1024)
n = p * q
e = 65537
phi = (p-1)*(q-1)
d = pow(e, -1, phi)
hint = p+q
c = pow(m,e,n)
print(f"e={e}")
print(f"d={d}")
print(f"hint={hint}")
print(f"c={c}")
And the output of the script:
e=65537
d=15353693384417089838724462548624665131984541847837698089157240133474013117762978616666693401860905655963327632448623455383380954863892476195097282728814827543900228088193570410336161860174277615946002137912428944732371746227020712674976297289176836843640091584337495338101474604288961147324379580088173382908779460843227208627086880126290639711592345543346940221730622306467346257744243136122427524303881976859137700891744052274657401050973668524557242083584193692826433940069148960314888969312277717419260452255851900683129483765765679159138030020213831221144899328188412603141096814132194067023700444075607645059793
hint=275283221549738046345918168846641811313380618998221352140350570432714307281165805636851656302966169945585002477544100664479545771828799856955454062819317543203364336967894150765237798162853443692451109345096413650403488959887587524671632723079836454946011490118632739774018505384238035279207770245283729785148
c=8886475661097818039066941589615421186081120873494216719709365309402150643930242604194319283606485508450705024002429584410440203415990175581398430415621156767275792997271367757163480361466096219943197979148150607711332505026324163525477415452796059295609690271141521528116799770835194738989305897474856228866459232100638048610347607923061496926398910241473920007677045790186229028825033878826280815810993961703594770572708574523213733640930273501406675234173813473008872562157659306181281292203417508382016007143058555525203094236927290804729068748715105735023514403359232769760857994195163746288848235503985114734813
The server uses RSA to encrypt the flag. However, we are not given the public modulus $n$. Instead, the server provides us with the private exponent $d$ and a hint which is $p + q$ (the sum of the two private prime numbers).
RSA background
RSA works so that, given a message $m$ in decimal format, we can encrypt it as follows:
$$ c = m^e \mod{n} $$
The public key is formed by $n$ and $e$. And $n = p \cdot q$, which are two big primes (kept as private key).
On the other hand, the decryption needs two more values: $\phi(n) = (p - 1) (q - 1)$ and $d = e^{-1} \mod{\phi(n)}$, so that:
$$ m = c^d \mod{n} $$
Solution
Since we have $e$ and $d$, we know that $d = e^{-1} \mod{\phi(n)}$, which is equivalent to
$$ ed - 1 = k \phi(n) $$
For some $k \in \mathbb{Z}$. Since $ed - 1$ is not much greater than $\phi(n)$, we can do a bit of brute force on $k$ until we find that $k | ed - 1$, so we might have got $\phi(n)$.
Assuming we have the correct $\phi(n)$, we know that $\phi(n) = (p - 1) (q - 1)$. If we expand the product, we get:
$$ \phi(n) = (p - 1) (q - 1) = pq - (p + q) + 1 = n - (p + q) + 1 $$
Since we have the hint ($p + q$), we can find $n$, which is the only thing we need to decrypt the ciphertext $c$:
$$ m = c^d \mod{n} $$
If the obtained $\phi(n)$ is not correct, then the message $m$ won’t contain SECCON{
, and we will need to continue trying $k$ values.
Flag
After a bit of brute force, we will get the flag:
$ python3 solve.py
[+] k: 53137
[+] SECCON{thank_you_for_finding_my_n!!!_GOOD_LUCK_IN_SECCON_CTF}
The full script can be found in here: solve.py
.