CWE-330 使用不充分的随机数

Use of Insufficiently Random Values

结构: Simple

Abstraction: Class

状态: Stable

被利用可能性: High

基本描述

The software may use insufficiently random numbers or values in a security context that depends on unpredictable numbers.

扩展描述

When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information.

适用平台

Language: {'cwe_Class': 'Language-Independent', 'cwe_Prevalence': 'Undetermined'}

常见的影响

范围 影响 注释
['Confidentiality', 'Other'] Other When a protection mechanism relies on random values to restrict access to a sensitive resource, such as a session ID or a seed for generating a cryptographic key, then the resource being protected could be accessed by guessing the ID or key.
['Access Control', 'Other'] ['Bypass Protection Mechanism', 'Other'] If software relies on unique, unguessable IDs to identify a resource, an attacker might be able to guess an ID for a resource that is owned by another user. The attacker could then read the resource, or pre-create a resource with the same ID to prevent the legitimate program from properly sending the resource to the intended user. For example, a product might maintain session information in a file whose name is based on a username. An attacker could pre-create this file for a victim user, then set the permissions so that the application cannot generate the session for the victim, preventing the victim from using the application.
Access Control ['Bypass Protection Mechanism', 'Gain Privileges or Assume Identity'] When an authorization or authentication mechanism relies on random values to restrict access to restricted functionality, such as a session ID or a seed for generating a cryptographic key, then an attacker may access the restricted functionality by guessing the ID or key.

检测方法

DM-11.4 Black Box

Use monitoring tools that examine the software's process as it interacts with the operating system and the network. This technique is useful in cases when source code is unavailable, if the software was not developed by you, or if you want to verify that the build phase did not introduce any new weaknesses. Examples include debuggers that directly attach to the running process; system-call tracing utilities such as truss (Solaris) and strace (Linux); system activity monitors such as FileMon, RegMon, Process Monitor, and other Sysinternals utilities (Windows); and sniffers and protocol analyzers that monitor network traffic.

Attach the monitor to the process and look for library functions that indicate when randomness is being used. Run the process multiple times to see if the seed changes. Look for accesses of devices or equivalent resources that are commonly used for strong (or weak) randomness, such as /dev/urandom on Linux. Look for library or system calls that access predictable information such as process IDs and system time.

Automated Static Analysis - Binary or Bytecode

According to SOAR, the following detection techniques may be useful:

Cost effective for partial coverage:
  • Bytecode Weakness Analysis - including disassembler + source code weakness analysis
  • Binary Weakness Analysis - including disassembler + source code weakness analysis

Manual Static Analysis - Binary or Bytecode

According to SOAR, the following detection techniques may be useful:

Cost effective for partial coverage:
  • Binary / Bytecode disassembler - then use manual analysis for vulnerabilities & anomalies

Dynamic Analysis with Manual Results Interpretation

According to SOAR, the following detection techniques may be useful:

Cost effective for partial coverage:
  • Man-in-the-middle attack tool

Manual Static Analysis - Source Code

According to SOAR, the following detection techniques may be useful:

Highly cost effective:
  • Focused Manual Spotcheck - Focused manual analysis of source
  • Manual Source Code Review (not inspections)

Automated Static Analysis - Source Code

According to SOAR, the following detection techniques may be useful:

Cost effective for partial coverage:
  • Source code Weakness Analyzer
  • Context-configured Source Code Weakness Analyzer

Architecture or Design Review

According to SOAR, the following detection techniques may be useful:

Highly cost effective:
  • Inspection (IEEE 1028 standard) (can apply to requirements, design, source code, etc.)

可能的缓解方案

Architecture and Design

策略:

Use a well-vetted algorithm that is currently considered to be strong by experts in the field, and select well-tested implementations with adequate length seeds. In general, if a pseudo-random number generator is not advertised as being cryptographically secure, then it is probably a statistical PRNG and should not be used in security-sensitive contexts. Pseudo-random number generators can produce predictable numbers if the generator is known and the seed can be guessed. A 256-bit seed is a good starting point for producing a "random enough" number.

Implementation

策略:

Consider a PRNG that re-seeds itself as needed from high quality pseudo-random output sources, such as hardware devices.

Testing

策略:

Use automated static analysis tools that target this type of weakness. Many modern techniques use data flow analysis to minimize the number of false positives. This is not a perfect solution, since 100% accuracy and coverage are not feasible.

MIT-2 ['Architecture and Design', 'Requirements']

策略: Libraries or Frameworks

Use products or modules that conform to FIPS 140-2 [REF-267] to avoid obvious entropy problems. Consult FIPS 140-2 Annex C ("Approved Random Number Generators").

Testing

策略:

Use tools and techniques that require manual (human) analysis, such as penetration testing, threat modeling, and interactive tools that allow the tester to record and modify an active session. These may be more effective than strictly automated techniques. This is especially the case with weaknesses that are related to design and business rules.

示例代码

This code generates a unique random identifier for a user's session.

bad PHP

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).

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.

bad Java

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.

分析过的案例

标识 说明 链接
CVE-2009-3278 Crypto product uses rand() library function to generate a recovery key, making it easier to conduct brute force attacks. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-3278
CVE-2009-3238 Random number generator can repeatedly generate the same value. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-3238
CVE-2009-2367 Web application generates predictable session IDs, allowing session hijacking. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-2367
CVE-2009-2158 Password recovery utility generates a relatively small number of random passwords, simplifying brute force attacks. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-2158
CVE-2009-0255 Cryptographic key created with a seed based on the system time. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2009-0255
CVE-2008-5162 Kernel function does not have a good entropy source just after boot. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5162
CVE-2008-4905 Blogging software uses a hard-coded salt when calculating a password hash. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4905
CVE-2008-4929 Bulletin board application uses insufficiently random names for uploaded files, allowing other users to access private files. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4929
CVE-2008-3612 Handheld device uses predictable TCP sequence numbers, allowing spoofing or hijacking of TCP connections. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-3612
CVE-2008-2433 Web management console generates session IDs based on the login time, making it easier to conduct session hijacking. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-2433
CVE-2008-0166 SSL library uses a weak random number generator that only generates 65,536 unique keys. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-0166
CVE-2008-2108 Chain: insufficient precision causes extra zero bits to be assigned, reducing entropy for an API function that generates random numbers. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-2108
CVE-2008-2020 CAPTCHA implementation does not produce enough different images, allowing bypass using a database of all possible checksums. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-2020
CVE-2008-0087 DNS client uses predictable DNS transaction IDs, allowing DNS spoofing. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-0087
CVE-2008-0141 Application generates passwords that are based on the time of day. https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-0141

Notes

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
PLOVER Randomness and Predictability
7 Pernicious Kingdoms Insecure Randomness
OWASP Top Ten 2004 A2 CWE More Specific Broken Access Control
CERT C Secure Coding CON33-C Imprecise Avoid race conditions when using library functions
CERT C Secure Coding MSC30-C CWE More Abstract Do not use the rand() function for generating pseudorandom numbers
CERT C Secure Coding MSC32-C CWE More Abstract Properly seed pseudorandom number generators
WASC 11 Brute Force
WASC 18 Credential/Session Prediction
The CERT Oracle Secure Coding Standard for Java (2011) MSC02-J Generate strong random numbers

相关攻击模式

  • CAPEC-112
  • CAPEC-485
  • CAPEC-59

引用