Even RSA Can Be Broken??
- Vulnerability
- Broken key generation — even modulus (p = 2)
- Impact
- Full private-key recovery & flag decryption
- Tools
- Python, netcat, pwntools-style socket
- Given
- Only N, e and the ciphertext C
Vulnerability Analysis
Root Cause: The key generator never enforces that both RSA primes are large — it accepts p = 2, which collapses the factorization problem to a single division.
- CWE-326: Inadequate Encryption Strength — A modulus with one 1-bit "prime" (
p = 2) carries none of the strength RSA depends on - CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG) — The hidden
get_primes()gives no guarantee its output is large or non-trivial
Severity: No CVSS score applies — this is a lab-built key generator, not a scored vulnerability in a shipped product. The practical effect is a full break: any ciphertext from this service decrypts in milliseconds.
Summary
The service hands you an RSA public modulus N, the exponent e, and an encrypted
flag C, and challenges you to decrypt it. The catch: its key generator is broken. Every
N it produces is even. For a product of two primes that can only mean one
prime is 2 — which collapses the whole scheme. From there, factoring N is a single
division, the private key falls out, and the flag decrypts.
Key insight: RSA security rests on N = p × q being hard to factor.
If N is even, then p = 2 and q = N / 2 — no factoring needed at all.
Recon
The hints point straight at the modulus: “How much do we trust randomness?”, “Notice anything interesting about N?”, and “Try comparing N across multiple requests.” First I pulled the encryption source the challenge provides to see how keys are made.
wget https://challenge-files.picoctf.net/.../encrypt.py
The generator calls a hidden get_primes() and multiplies the two results. Nothing in the
script guarantees the primes are large or odd — which is exactly the weakness.
Show the vulnerable key generator (encrypt.py)
from Crypto.Util.number import bytes_to_long, inverse
from setup import get_primes # <-- hidden; no guarantee the primes are safe
e = 65537
def gen_key(k):
p, q = get_primes(k // 2)
N = p * q
d = inverse(e, (p - 1) * (q - 1))
return ((N, e), d)
def encrypt(pubkey, m):
N, e = pubkey
return pow(bytes_to_long(m.encode()), e, N)
Finding the flaw
Connecting to the service a few times and looking at N each time, the pattern is obvious —
every modulus ends in an even digit:
nc verbal-sleep.picoctf.net 59661
# N: 2393684804...383658 <-- ends in 8, so N is even
I confirmed it by collecting several samples and checking N % 2 — all even. (I also ran a
GCD across samples to check for a shared large prime; there wasn’t one. The only shared factor was
2, which is all we need.)
The math
Because N is even and the product of two primes, one prime must be 2:
p = 2,q = N / 2φ(N) = (p − 1)(q − 1) = 1 × (q − 1) = q − 1d = e⁻¹ mod φ(N)m = Cd mod N→ convert to bytes → flag
Exploit
Putting it together: pull a fresh (N, e, C) from the service, factor, derive d,
decrypt, and decode.
import socket, re
def long_to_bytes(n):
return n.to_bytes((n.bit_length() + 7) // 8, 'big')
# pull fresh N, e, C from the service
s = socket.socket(); s.connect(("verbal-sleep.picoctf.net", 59661))
data = s.recv(4096).decode()
n = int(re.search(r"N: (\d+)", data).group(1))
e = int(re.search(r"e: (\d+)", data).group(1))
c = int(re.search(r"cyphertext: (\d+)", data).group(1))
# N is even -> p = 2
q = n // 2
phi = q - 1
d = pow(e, -1, phi)
m = pow(c, d, n)
print("Flag:", long_to_bytes(m).decode())
$ python3 solve.py
Flag: <flag redacted>
Takeaways
- Prime selection is the whole game. Primes must be large and odd; the moment
2sneaks in, RSA offers no protection at all. - Read the hints literally. “Notice anything interesting about N?” was pointing at a single property — its parity.
- One bit can be fatal. Leaking just the parity of
Nwas enough for a complete break.
References & Standards
- Cryptography Standards
- FIPS 186-5: Digital Signature Standard (DSS) — RSA key-pair generation requirements
- RFC 8017: PKCS #1 — RSA Cryptography Specifications
- Common Weakness Enumeration
- CWE-326: Inadequate Encryption Strength
- CWE-338: Use of Cryptographically Weak PRNG