822
This code generates a unique random identifier for a user's session.
Example Language: PHP (Bad)
function generateSessionID($userID){
srand($userID);
return rand();
}
Because the seed for the PRNG is always the user's ID, the session ID will always be the same. An
attacker could thus predict any user's session ID and potentially hijack the session.
This example also exhibits a Small Seed Space (CWE-339).
Example 2:
The following code uses a statistical PRNG to create a URL for a receipt that remains active for
some period of time after a purchase.
Example Language: Java (Bad)
String GenerateReceiptURL(String baseUrl) {
Random ranGen = new Random();
ranGen.setSeed((new Date()).getTime());
return(baseUrl + ranGen.nextInt(400000000) + ".html");
}
This code uses the Random.nextInt() function to generate "unique" identifiers for the receipt pages
it generates. Because Random.nextInt() is a statistical PRNG, it is easy for an attacker to guess
the strings it generates. Although the underlying design of the receipt system is also faulty, it would
be more secure if it used a random number generator that did not produce predictable receipt
identifiers, such as a cryptographic PRNG.
Observed Examples
Reference Description
CVE-2001-0950 Insufficiently random data used to generate session tokens using C rand().
Also, for certificate/key generation, uses a source that does not block when
entropy is low.
https://www.cve.org/CVERecord?id=CVE-2001-0950
CVE-2008-2108 Chain: insufficient precision (CWE-1339) in random-number generator causes
some zero bits to be reliably generated, reducing the amount of entropy
(CWE-331)
https://www.cve.org/CVERecord?id=CVE-2008-2108
MemberOf Relationships
This MemberOf relationships table shows additional CWE Catgeories and Views that reference this
weakness as a member. This information is often useful in understanding where a weakness fits
within the context of external information sources.
Nature Type ID Name Page
MemberOf 884 CWE Cross-section 884 2567
MemberOf 905 SFP Primary Cluster: Predictability 888 2388
MemberOf 1170 SEI CERT C Coding Standard - Guidelines 48.
Miscellaneous (MSC)
1154 2463
MemberOf 1346 OWASP Top Ten 2021 Category A02:2021 -
Cryptographic Failures
1344 2488
MemberOf 1414 Comprehensive Categorization: Randomness 1400 2543
Notes
Maintenance
CWE Version 4.14
CWE-332: Insufficient Entropy in PRNG
CWE-332: Insufficient Entropy in PRNG