Overview
Passwords should never be stored in plaintext. When a database is compromised, attackers gain immediate access to all user credentials, which can then be used to compromise other accounts (since users often reuse passwords). This page covers the critical password storage vulnerability in the application and how to implement secure password hashing using bcrypt.The Plaintext Password Problem
Vulnerable User Registration
TheinsertUser() function in user_management.py:6-14 stores passwords directly without any encryption:
user_management.py
This function takes the password directly from the signup form and stores it in the database without any hashing, encryption, or protection.
Vulnerable Authentication
TheretrieveUsers() function in user_management.py:17-39 compares passwords in plaintext:
user_management.py
Impact of Plaintext Storage
- Database Breach
- SQL Injection
- Log Exposure
- Insider Threats
If the database is compromised, attackers get:
- All usernames in plaintext
- All passwords in plaintext
- Complete account access
- Ability to use credentials on other sites
Understanding Password Security Concepts
Encryption vs Hashing vs Salting
Why BCrypt?
Encryption: Converts data that can be decrypted back to original with a key- ❌ Not suitable for passwords (if key is stolen, all passwords compromised)
- ✅ Good foundation for password storage
- ❌ Vulnerable to rainbow tables without salt
- ✅ Makes rainbow tables ineffective
- ✅ Same password produces different hashes
- ✅ Automatically handles salting
- ✅ Configurable computational cost
- ✅ Resistant to brute force attacks
- ✅ Industry standard for password storage
Byte Strings Explained
To store anything in a computer, you must first encode it (convert it to bytes):BCrypt works with byte strings, so passwords must be encoded before hashing and decoded for display.
Implementing Secure Password Storage with BCrypt
Installation
Basic BCrypt Usage
Here’s the complete example from.student_resources/encrypting_passwords/example.py:
example.py
Secure User Registration
Updateuser_management.py to hash passwords during registration:
Secure Authentication
UpdateretrieveUsers() to verify hashed passwords:
Database Schema Update
Update your database schema to store binary hashed passwords:Complete Secure Implementation
Here’s a complete, production-ready implementation:Password Hashing Best Practices
Use BCrypt or Argon2
Use industry-standard password hashing algorithms:
- ✅ BCrypt - Battle-tested, configurable cost factor
- ✅ Argon2 - Winner of Password Hashing Competition
- ❌ MD5 - Cryptographically broken
- ❌ SHA-1 - Deprecated for security
- ❌ SHA-256 - Too fast, vulnerable to brute force
Never Store Plaintext Passwords
Never, ever store passwords in plaintext:
- Not in databases
- Not in log files
- Not in configuration files
- Not in error messages
- Not in backup files
Testing Password Security
test_passwords.py
Salt Demonstration
Here’s a visual demonstration of how salting works:Related Vulnerabilities
- Broken Authentication - Overall authentication vulnerabilities
- Session Management - Session handling issues
References
- OWASP Password Storage Cheat Sheet
- BCrypt Python Library
- NIST Digital Identity Guidelines
- Have I Been Pwned - Check if passwords have been breached
