BETA Shoulder is in beta — Findings may sometimes be wrong. Your feedback shapes what we fix next. Share feedback
🎲

Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)

🛡️ 4 rules detect this

Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)

The product uses a Pseudo-Random Number Generator (PRNG) in a security context, but the PRNG's algorithm is not cryptographically strong.

When a non-cryptographic PRNG is used in a security context (such as generating session tokens or cryptographic keys), an attacker may be able to predict its output and compromise the security mechanism.

Prevalence
High
Frequently exploited
Impact
High
2 high-severity rules
Prevention
Documented
4 fix examples
2 Prevention
2 Prevention

How to fix this vulnerability

Prevention strategies for Weak PRNG based on 4 Shoulder detection rules.

Weak Random Number Generation for Security HIGH

Use crypto/rand instead of math/rand for security-sensitive values

+8 -6 go
- import "math/rand"
- 
- func generateToken() string {
-     token := make([]byte, 32)
-     rand.Read(token)
-     return hex.EncodeToString(token)
+ import "crypto/rand"
+ 
+ func generateToken() (string, error) {
+     token := make([]byte, 32)
+     if _, err := rand.Read(token); err != nil {
+         return "", err
+     }
+     return hex.EncodeToString(token), nil
  }
  
Weak Random Number Generation in Security Context HIGH

Use crypto.randomBytes() or crypto.randomUUID() for security-sensitive random values

+2 -1 javascript
- const token = Math.random().toString(36).substring(2);
+ const crypto = require('crypto');
+ const token = crypto.randomBytes(32).toString('hex');
  
Insecure Random Number Generation MEDIUM

Use the secrets module for tokens, passwords, and all security-sensitive randomness

+4 -5 python
- import random
- 
- def generate_token():
-     token = random.randint(100000, 999999)
-     return str(token)
+ import secrets
+ 
+ def generate_token():
+     return secrets.token_urlsafe(32)
  
Cryptographically Weak Random Number Generation MEDIUM

Use the secrets module instead of random for security-sensitive operations

+4 -5 python
- import random
- 
- def generate_token():
-     chars = 'abcdef0123456789'
-     return ''.join(random.choice(chars) for _ in range(32))
+ import secrets
+ 
+ def generate_token():
+     return secrets.token_hex(32)
  

Key Practices

  • Use of Math
3 Detection
3 Detection

Find vulnerabilities in your code

Use Shoulder to scan your codebase for Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG) patterns. 4 rules.

terminal
# Scan with Shoulder CLI
npx @shoulderdev/cli trust --cwe=338

# Or scan entire project
npx @shoulderdev/cli trust .
4 Warning Signs
4 Warning Signs

What to watch for in code reviews

These patterns indicate potential Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG) vulnerabilities. Look for these during code reviews and security audits.

🟠
math/rand used for security-sensitive random values go-weak-random-number-generation
🟠
Math.random() used for security-sensitive operation: ... javascript-weak-random
🟠
🟡
use of insecure random number generators (random module) for security-critical operations python-insecure-randomness
🟡
use of the random module for security-sensitive operations like tokens, passwords, or cryptographic python-weak-random
🔍

Scan your codebase for Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)

Shoulder CLI finds vulnerable patterns across your entire codebase.