Weak randomness (RNG)
A huge amount of security depends on unpredictable random numbers: keys, IVs/nonces, session tokens, password resets, OTPs, and identifiers. When the generator is weak or predictable, the attacker predicts those values and breaks the system without touching the cryptography itself.
What it is
Section titled “What it is”A pseudo-random number generator (PRNG) produces a deterministic sequence from a seed. A CSPRNG (cryptographically secure) is designed so that, even knowing previous outputs, it’s infeasible to predict the next one or reconstruct the internal state. A plain PRNG offers no such guarantee.
| Standard PRNG | CSPRNG | |
|---|---|---|
| Examples | rand(), Math.random(), mt_rand(), java.util.Random |
/dev/urandom, getrandom(), secrets (Python), crypto.randomBytes (Node), SecureRandom (Java) |
| Predictable | Yes, given enough output | No |
| Security use | Never | Always |
Threat model
Section titled “Threat model”The attacker doesn’t break the encryption: they guess the random input. If a reset token, an IV, or a session key comes from a predictable PRNG, it’s enough to collect outputs, reconstruct the state, and predict past and future values. The result is usually account takeover, OTP bypass, or impersonation.
Typical flaws
Section titled “Typical flaws”- Predictable seed:
seed = time()or the PID give a small seed space, brute-forceable. - Non-crypto PRNG for tokens: session or reset tokens generated with
rand()or Mersenne Twister → the state is recovered and the rest are predicted. - Insufficient entropy at boot: IoT devices and VMs generate keys with low entropy → repeated or factorable keys.
- Predictable or fixed nonce/IV: breaks CTR/GCM/CBC. See Symmetric vs asymmetric.
- Reused
kin (EC)DSA: reusing the noncekwhen signing reveals the private key.
Red Team
Section titled “Red Team”Predicting Mersenne Twister (lab)
Section titled “Predicting Mersenne Twister (lab)”# MT19937 (PHP's mt_rand, Python's random, many Math.random):# with 624 consecutive 32-bit outputs the state is reconstructed and everything predictedfrom randcrack import RandCrackrc = RandCrack()for v in outputs_624: # 624 observed 32-bit integers rc.submit(v)next_value = rc.predict_randint(0, 2**32 - 1)# Real application: predictable tokens or IDs -> account takeover, OTP bypassReused nonce in ECDSA/DSA signatures
Section titled “Reused nonce in ECDSA/DSA signatures”flowchart TD
A[Signature 1: r, s1 over m1] --> C{Same r value?}
B[Signature 2: r, s2 over m2] --> C
C -->|Yes -> k reused| D[Solve for k from s1, s2, m1, m2]
D --> E[Recover the private key d]
C -->|No| F[Not exploitable this way]
If two signatures use the same k, from (r, s1), (r, s2) and the messages you solve for k and then for the private key. Detection: the same r value in two different signatures betrays a reused k. Historical case: the PlayStation 3 signed with a fixed k.
Twin keys from low entropy
Section titled “Twin keys from low entropy”Large-scale studies of public RSA keys on the Internet found moduli sharing a prime (GCD of two moduli other than 1) due to weak RNG at boot in routers and IoT; that shared prime trivially factors both keys.
Blue Team
Section titled “Blue Team”- Always use a system CSPRNG for keys, IVs/nonces, tokens, resets, and OTPs (
secrets,crypto.randomBytes,SecureRandom). - Unique nonce or
kper operation, or deterministic signing per RFC 6979 in ECDSA; random IV. - Ensure sufficient entropy at boot (hardware RNG; don’t generate keys on first boot without entropy).
- Long (≥ 128-bit), opaque tokens; audit quality with Burp Sequencer.
CVEs and real-world cases
Section titled “CVEs and real-world cases”- Debian OpenSSL (CVE-2008-0166): a patch reduced entropy and left SSH/SSL keys predictable worldwide.
- PS3 / fail0verflow (2010): ECDSA with a fixed
krevealed Sony’s signing key. - Mining your Ps and Qs (2012): RSA/DSA keys factorable via shared primes from weak RNG in IoT.
Checklist
Section titled “Checklist”- Identify what uses randomness (tokens, resets, OTP, IV/nonce, keys)
- Non-cryptographic PRNG (
rand/mt_rand/Math.random)? - Collect outputs and test prediction (randcrack for MT19937)
- Tokens/IDs: sequential or predictable? (Burp Sequencer)
- ECDSA/DSA signatures: repeated
rvalue? → reusedk - RSA keys: do they share primes (GCD)? (low entropy)
- Verify CSPRNG and RFC 6979 use in the defense