Insecure Randomness for Security Values
Tokens, identifiers or keys that must be unguessable are generated with a non-cryptographic random number generator whose output can be predicted.
| Severity | Medium |
| CVSS 3.1 | 4.8 CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:L/I:L/A:N Open in the calculator |
| CVSS 4.0 | 6.3 CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:L/VI:L/VA:N/SC:N/SI:N/SA:N Open in the calculator |
| CWE | CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG) |
| OWASP Top 10 | A04:2025 Cryptographic Failures |
| Category | Cryptographic Failures |
Description
The application generates values that must be unpredictable, such as session identifiers, password reset and email verification tokens, one-time codes, API keys, nonces or encryption keys, with a general-purpose pseudo-random number generator (PRNG) that is not designed for security. Examples include Math.random() in JavaScript, java.util.Random in Java, System.Random in .NET, Python's random module, and rand() or mt_rand() in C and PHP. These generators produce output that looks random but is fully determined by a small internal state or seed.
Depending on the generator, an attacker who observes enough outputs can recover its state and predict past and future values, and generators seeded with predictable values such as the current time can be brute-forced over a far smaller range than the token's length suggests. An attacker who can predict security values can forge them instead of having to steal them: for example, predicting a password reset token issued to another user to take over their account, guessing valid session identifiers, or deriving encryption keys or nonces. How serious this is depends on what the value protects, and it is greatest for password reset tokens, session identifiers and keys.
Exploitation usually requires the attacker to collect several outputs of the same generator, for instance by requesting tokens for their own account, and some knowledge of the generator, which makes it harder than many other attacks, but tools exist to recover the state of common generators. Related weaknesses include tokens built from timestamps, counters or hashes of predictable data, and output from a secure generator truncated to too few characters.
The weakness is usually found by source code review, tracing where each security value is created. In testing it can show as tokens that share structure or increase over time; collecting a large sample and analysing it, for example with the sequencer in Burp Suite, can reveal low entropy, although a sample that appears random does not prove that the generator is secure.
Remediation
Generate every security-sensitive value with a cryptographically secure random number generator (CSPRNG) provided by the platform:
- Use
crypto.randomBytes()in Node.js andcrypto.getRandomValues()in browsers;java.security.SecureRandomin Java;RandomNumberGeneratorin .NET; thesecretsmodule in Python;random_bytes()andrandom_int()in PHP; and the operating system's generator, such asgetrandom()on Linux, in C. - Give tokens at least 128 bits of randomness, and encode the random bytes directly rather than deriving tokens from timestamps, counters, user data or hashes of predictable values.
- Use the framework's own session management and token features where they exist, as these already use a secure generator.
- Do not seed secure generators with fixed or predictable values, and do not swap them for a faster general-purpose generator for performance.
- Once the change is deployed, invalidate tokens, sessions and keys issued by the weak generator, and give single-use tokens a short expiry.
Add a static analysis rule that flags non-cryptographic generators in security-relevant code. Confirm the fix by reviewing the code that creates each security value and by analysing a fresh sample of tokens.
References
Add This Finding to Your Library
In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.
Free trialYour trial starts when your portal goes live, not when you sign up, so the time you spend setting up does not count. · No card required
Only need the reporting? Our reporting tool is free and open source, with the same library.