Skip to content
Pwnversity
CRYPTO-RNGadvanced · 5 min · upd. 2026-10

Weak randomness (RNG)

CWE-338

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.

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

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.

  • 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 k in (EC)DSA: reusing the nonce k when signing reveals the private key.
# MT19937 (PHP's mt_rand, Python's random, many Math.random):
# with 624 consecutive 32-bit outputs the state is reconstructed and everything predicted
from randcrack import RandCrack
rc = 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 bypass
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.

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.

  • Always use a system CSPRNG for keys, IVs/nonces, tokens, resets, and OTPs (secrets, crypto.randomBytes, SecureRandom).
  • Unique nonce or k per 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.
  • Debian OpenSSL (CVE-2008-0166): a patch reduced entropy and left SSH/SSL keys predictable worldwide.
  • PS3 / fail0verflow (2010): ECDSA with a fixed k revealed Sony’s signing key.
  • Mining your Ps and Qs (2012): RSA/DSA keys factorable via shared primes from weak RNG in IoT.
  • 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 r value? → reused k
  • RSA keys: do they share primes (GCD)? (low entropy)
  • Verify CSPRNG and RFC 6979 use in the defense