logo

Database

Python Weak Pbkdf2 Iterations

Description

This detector identifies uses of Python's `hashlib.pbkdf2_hmac` (or equivalent PBKDF2 implementations) where a weak or insufficient hashing method/algorithm is passed as the second argument. Using weak hash algorithms in PBKDF2 key derivation (e.g., MD5 or SHA1) reduces the cryptographic strength of the derived key, making password storage or key derivation vulnerable to brute-force or preimage attacks.

Weakness:

052 - Insecure encryption algorithm

Category: Information Collection

Detection Strategy

    • Scan imports to confirm that a PBKDF2-related function (e.g., `hashlib.pbkdf2_hmac`) is imported and used in the file; if no such import is found, no analysis is performed.

    • Identify all call expressions in the code where the called function matches the imported PBKDF2 sink name.

    • For each matching call, extract the second positional argument or the keyword argument named `method`, which specifies the hash algorithm to use.

    • Evaluate whether the resolved value of that argument is a weak or insecure hash algorithm (e.g., 'md5', 'sha1') by tracing it back to its definition or literal value.

    • Report a vulnerability when all conditions are met: the call targets the PBKDF2 function, the `method` argument is resolvable, and its value corresponds to a known weak hashing algorithm.

Vulnerable code example

from werkzeug import security
from werkzeug.security import generate_password_hash

LEGACY_HASH_METHOD = "pbkdf2:sha256:100000"

def hash_password(password: str) -> str:
    return generate_password_hash(password, "pbkdf2:sha256:1000")  # UNSAFE: too few iterations
...

✅ Secure code example

from werkzeug import security
from werkzeug.security import generate_password_hash

LEGACY_HASH_METHOD = "pbkdf2:sha256:600000"  # Safe: meets minimum iteration count

def hash_password(password: str) -> str:
    return generate_password_hash(password, "pbkdf2:sha256:600000")  # Safe: 600k iterations meets SHA256 minimum
...