Cryptography / Incorrect Usage of Seeds in Pseudo-Random Number Generator
Incorrect Usage of Seeds in Pseudo-Random Number Generator (CWE-331) is a vulnerability that occurs when a seed value used to initialize a Pseudo-Random Number Generator (PRNG) is not generated or chosen properly. This can allow a malicious actor to predict the output of the PRNG and exploit the generated numbers to gain access to restricted resources or data. The vulnerability typically affects web and API applications which use PRNGs to generate numbers or tokens for authentication or other purposes. According to the Common Weakness Enumeration (CWE) directory, the incorrect usage of seeds in PRNGs can occur due to the lack of entropy in the seed value or an insufficient number of bits in the seed value. This vulnerability is also covered in the OWASP Testing Guide.
The incorrect usage of seeds in PRNGs can lead to the loss of confidential data and resources. Attackers can predict and exploit the number patterns to gain access to a system and its data. This vulnerability can be exploited by attackers to gain access to confidential data, customer records, and other resources from a vulnerable system.
The best solution to this vulnerability is to use a cryptographically secure PRNG. Cryptographically secure PRNGs use a secure entropy source to generate a random seed value. The seed value should also be long enough to ensure that it is unpredictable. Additionally, web and API applications should also ensure that the PRNG is not used to generate authentication credentials or other sensitive data.
Below is an example of a vulnerable piece of code that uses a weak PRNG to generate a token.
long seed = System.currentTimeMillis(); Random rnd = new Random(seed); String token = Long.toString(Math.abs(rnd.nextLong()), 36);
In the example, the seed value is initialized with
System.currentTimeMillis(), which is not a secure entropy source. As a result, the generated token can be predicted by an attacker.