logo

Database

Python Pbkdf2 Insufficient Iteration Count

Description

This detector identifies Python code that uses PBKDF2 (Password-Based Key Derivation Function 2) password hashing with an insufficient iteration count. Using too few iterations weakens the key derivation process, making it significantly easier for attackers to perform brute-force or dictionary attacks against hashed passwords. The vulnerability affects any PBKDF2 invocation from known cryptographic libraries where the iteration count parameter falls below a secure minimum threshold.

Weakness:

052 - Insecure encryption algorithm

Category: Information Collection

Detection Strategy

    • Identify PBKDF2-related function imports from known cryptographic libraries (e.g., `hashlib.pbkdf2_hmac`, `Cryptodome`, `passlib`, etc.) by scanning the file's import statements for recognized library names and their corresponding PBKDF2 sink function names.

    • For each function call in the analyzed code whose expression name matches one of the detected PBKDF2 sink functions, inspect the call's arguments to locate the iteration count parameter (either by position or keyword argument name).

    • A vulnerability is reported when the iteration count argument is a statically resolvable numeric literal or constant that is below the required secure minimum threshold (e.g., fewer than 600,000 iterations as recommended by current OWASP/NIST guidelines), or when the iteration count is entirely absent/defaulted to an insecure value.

    • Only files that actually import a recognized PBKDF2-providing library are analyzed; files with no such imports are skipped entirely to avoid false positives.

Vulnerable code example

from Cryptodome.Hash import SHA512
from Cryptodome.Protocol.KDF import PBKDF2

def derive_key(password: str, salt: bytes) -> bytes:
    # UNSAFE: low iteration count makes brute-force attacks feasible
    return PBKDF2(password, salt, 32, count=100_000, hmac_hash_module=SHA512)

✅ Secure code example

from Cryptodome.Hash import SHA512
from Cryptodome.Protocol.KDF import PBKDF2

def derive_key(password: str, salt: bytes) -> bytes:
    # SAFE: 220,000+ iterations with SHA512 makes brute-force attacks infeasible
    return PBKDF2(password, salt, 32, count=220_000, hmac_hash_module=SHA512)