# VNX-CRYPTO-007: Weak password hashing (insufficient iterations or missing KDF)

Reports a password-named value hashed with a fast general-purpose hash (MD5, SHA-1, SHA-2, SHA-3, BLAKE2) in Python, Node.js, Java, PHP, Ruby and Go.

| Field | Value |
|---|---|
| Rule ID | `VNX-CRYPTO-007` |
| Severity | High |
| SARIF level | `error` |
| Kind | `sast` |
| Languages | Python, JavaScript / TypeScript, Java, PHP, Ruby, Go |
| CWE | [CWE-916](https://cwe.mitre.org/data/definitions/916.html) |
| CAPEC | [CAPEC-16](https://capec.mitre.org/data/definitions/16.html) |
| ATT&CK | [T1110.002](https://attack.mitre.org/techniques/T1110/002/) |
| CWSS vector | `CWSS:1.0/TI:H/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:H/DI:H/EX:H/EC:N/P:H` |
| Tags | `crypto` `password-hashing` `weak-crypto` |
| Source | [`internal/sast/rules/vnx-crypto-007.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-crypto-007.rego) |

## Overview

This rule detects password hashing operations using general-purpose cryptographic hash functions: MD5, SHA-1, or SHA-256 applied directly to passwords in Python (`hashlib`), Java (`MessageDigest`), and Node.js (`crypto.createHash`). General-purpose hash functions are designed to be fast, which is the opposite of what you want for password hashing. A GPU can compute billions of MD5 or SHA-256 hashes per second, making a database of hashed passwords recoverable in hours or days. This maps to CWE-916 (Use of Password Hash With Insufficient Computational Effort).

## Why this matters

When attackers breach a database containing password hashes, the speed of cracking depends almost entirely on the hashing algorithm used. An MD5 hash of `password123` can be reversed in milliseconds using precomputed rainbow tables. SHA-256 without a salt fares little better. Even with per-user salts, MD5 and SHA-1 remain orders of magnitude too fast, modern GPU clusters crack salted SHA-256 passwords at rates exceeding 10 billion hashes per second.

The impact is not limited to users who reuse passwords. Cracked passwords reveal patterns: users who use `CompanyName2024!` in one place often use similar patterns elsewhere. A breach of your password database can become a starting point for credential stuffing attacks against every other service your users touch. The OWASP Password Storage Cheat Sheet is explicit: use bcrypt, scrypt, or Argon2id for all new password storage.

## What gets flagged

```python
# FLAGGED: SHA-256 applied directly to password
import hashlib
hashed = hashlib.sha256(password.encode()).hexdigest()
```

```java
// FLAGGED: MD5 for password hashing in Java
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hash = md.digest(password.getBytes());
```

```javascript
// FLAGGED: SHA-1 applied to password in Node.js
const hash = crypto.createHash('sha1').update(password).digest('hex');
```

```php
// FLAGGED: MD5 of a password in PHP
$pass_new = md5($pass_new);
$hash = sha1($_POST['password'] . $salt);
```

```go
// FLAGGED: Go fast hashes and a hash-named helper over a password
pass := Md5Sum(r.FormValue("password"))
sum := sha256.Sum256([]byte(password + salt))
hashed, _ := bcrypt.GenerateFromPassword(pw, bcrypt.MinCost)
```

```python
# FLAGGED: PBKDF2 with too few iterations, bcrypt with too low a cost
hashlib.pbkdf2_hmac("sha256", password.encode(), salt, 1000)
bcrypt.hashpw(password, bcrypt.gensalt(rounds=4))
```

Not flagged: bcrypt, scrypt and argon2 at normal cost, PBKDF2 with 10,000 or more iterations, hashing values that are not passwords (cache keys, request bodies, reset tokens), the SHA-1 prefix lookup against the Have I Been Pwned range API, comments, strings and test files. A password hashed through a project helper defined in another function (`security.hash(password)`) is not followed.

## Remediation

1. **Use bcrypt for most applications.** The cost factor lets you tune computational expense upward as hardware improves:

   ```python
   # SAFE: bcrypt with cost factor 12
   import bcrypt
   hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt(rounds=12))

   # Verification
   bcrypt.checkpw(password.encode('utf-8'), hashed)
   ```

   ```javascript
   // SAFE: bcrypt in Node.js
   const bcrypt = require('bcrypt');
   const SALT_ROUNDS = 12;
   const hash = await bcrypt.hash(password, SALT_ROUNDS);

   // Verification (constant-time comparison built in)
   const match = await bcrypt.compare(password, hash);
   ```

2. **Use Argon2id for new projects.** It is the winner of the Password Hashing Competition and is recommended by OWASP as the first choice:

   ```python
   # SAFE: Argon2id
   from argon2 import PasswordHasher
   ph = PasswordHasher(time_cost=2, memory_cost=65536, parallelism=1)
   hash = ph.hash(password)
   ph.verify(hash, password)
   ```

3. **Migrate existing MD5/SHA hashes.** On each successful login with the old hash, rehash with bcrypt/Argon2id and store the new hash. After a grace period, invalidate all accounts that still have the old hash format.

4. **Never use raw hash output for password comparison**, always use the library's built-in comparison function, which incorporates constant-time comparison (see VNX-CRYPTO-008).

## References

- [CWE-916: Use of Password Hash With Insufficient Computational Effort](https://cwe.mitre.org/data/definitions/916.html)
- [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)
- [NIST SP 800-63B – Memorized Secret Verifiers](https://pages.nist.gov/800-63-3/sp800-63b.html#sec5)
- [bcrypt package (Python)](https://pypi.org/project/bcrypt/)
- [argon2-cffi package (Python)](https://pypi.org/project/argon2-cffi/)
- [MITRE ATT&CK T1110.002 – Password Cracking](https://attack.mitre.org/techniques/T1110/002/)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-crypto-007
// nosec vnx-crypto-007
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-crypto-007 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
