Rust Hardcoded Rng Seed
Description
This vulnerability detector identifies Rust code that uses hardcoded seeds for random number generators. Using a hardcoded seed makes the random number generation predictable and deterministic, which compromises cryptographic security and makes applications vulnerable to attacks that rely on unpredictable randomness.
Detection Strategy
• Scan Rust source code files that import random number generation libraries with specific crate prefixes
• Identify function calls that appear to be key generation or random number generation operations
• Check if the first argument to these RNG functions is a hardcoded seed value rather than a dynamically generated one
• Report a vulnerability when a key generation function uses a static, predictable seed value that could compromise the randomness of generated keys or random numbers
Vulnerable code example
use rand::rngs::StdRng;
use rand::SeedableRng;
use ed25519_dalek::{Signer, SigningKey};
fn main() {
// Vulnerable: StdRng seeded with hardcoded literal
let seed: u64 = 987654321;
let mut rng = StdRng::seed_from_u64(seed);...✅ Secure code example
use rand::rngs::OsRng;
use ed25519_dalek::{Signer, SigningKey};
fn main() {
// Safe: Using OsRng for cryptographically secure randomness
let mut rng = OsRng;
let signing_key = SigningKey::generate(&mut rng); // Cryptographic key from secure RNG...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Continuous Hacking Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' hacking team, fill out this contact form.