logo

Database

Python Insufficient Bcrypt Cost

Description

This detector identifies Python code where bcrypt hashing is used with an insufficient work factor (cost parameter). Using a low bcrypt cost/rounds value weakens the password hashing mechanism, making it significantly faster for attackers to perform brute-force or dictionary attacks against stolen password hashes.

Weakness:

052 - Insecure encryption algorithm

Category: Information Collection

Detection Strategy

    • A bcrypt-related library (e.g., `bcrypt`, `passlib`, or similar) must be imported in the file. If no such library is detected as imported, the analysis is skipped entirely.

    • Each function call or expression in the file is examined to determine if it represents a bcrypt hashing operation (e.g., calls to `bcrypt.hashpw`, `bcrypt.hash`, or equivalent password hashing functions that accept a cost/rounds parameter).

    • A bcrypt call is flagged as vulnerable if the cost/rounds parameter is explicitly set to a value considered insufficient (i.e., below a secure threshold, commonly accepted as 10 or higher). Calls where the cost is too low or uses a weak default are reported.

    • A vulnerability is reported for each individual bcrypt call site where the weak cost condition is met, pointing directly to the offending call expression in the source code.

Vulnerable code example

import bcrypt
from bcrypt import gensalt as bcrypt_gensalt
from flask import Flask
from flask_bcrypt import Bcrypt, generate_password_hash

LEGACY_ROUNDS = 8

app = Flask(__name__)...

✅ Secure code example

import bcrypt
from bcrypt import gensalt as bcrypt_gensalt
from flask import Flask
from flask_bcrypt import Bcrypt, generate_password_hash

LEGACY_ROUNDS = 12  # SAFE: raised constant to meet minimum recommended rounds

app = Flask(__name__)...