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.
この脆弱性の修正方法
4 件の Shoulder 検出ルールに基づく Weak PRNG の予防策。
Use crypto/rand instead of math/rand for security-sensitive values
- 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 }
Use crypto.randomBytes() or crypto.randomUUID() for security-sensitive random values
- const token = Math.random().toString(36).substring(2); + const crypto = require('crypto'); + const token = crypto.randomBytes(32).toString('hex');
Use the secrets module for tokens, passwords, and all security-sensitive randomness
- import random - - def generate_token(): - token = random.randint(100000, 999999) - return str(token) + import secrets + + def generate_token(): + return secrets.token_urlsafe(32)
Use the secrets module instead of random for security-sensitive operations
- 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)
主要なプラクティス
- Use of Math
コードの脆弱性を見つける
Shoulderを使用してコードのUse of Cryptographically Weak Pseudo-Random Number Generator (PRNG)パターンをスキャンしましょう。 4 ルール.
# Scan with Shoulder CLI npx @shoulderdev/cli trust --cwe=338 # Or scan entire project npx @shoulderdev/cli trust .
検出ルール (4)
コードレビューで注目すべき点
これらのパターンはUse of Cryptographically Weak Pseudo-Random Number Generator (PRNG)の潜在的な脆弱性を示しています。コードレビューとセキュリティ監査中に探してください。
コードベースをスキャン: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)
Shoulder CLI はコードベース全体から脆弱なパターンを見つけます。