Blog | G5 Cyber Security

Partial Password Authentication

TL;DR

This guide shows you how to allow users to log in even if they only remember part of their password. We’ll use a technique called fuzzy matching, but this significantly reduces security and should be used with extreme caution (e.g., for low-sensitivity accounts or as a temporary recovery option). We will focus on implementing this using Python and the `fuzzywuzzy` library.

Prerequisites

Step-by-Step Guide

  1. Install the FuzzyWuzzy Library
  2. FuzzyWuzzy is a library that helps with fuzzy string matching. Install it using pip:

    pip install fuzzywuzzy python-Levenshtein

    The python-Levenshtein package provides significant performance improvements to FuzzyWuzzy, so it’s highly recommended.

  3. Fetch User Passwords from the Database
  4. You need to retrieve the stored passwords for comparison. The exact method depends on your database setup. Here’s a simplified example assuming you have a function get_passwords() that returns a list of hashed passwords:

    def get_passwords():
        # Replace with your actual database query
        return ['hashed_password_1', 'another_hashed_password', 'yet_another_hash']
    
  5. Implement the Fuzzy Matching Function
  6. This function will compare the user’s input against the stored passwords. We’ll use a ratio threshold to determine if there’s a match.

    from fuzzywuzzy import fuzz
    
    def authenticate_partial(user_input, known_passwords, threshold=70):
        for password in known_passwords:
            ratio = fuzz.ratio(user_input.lower(), password.lower())
            if ratio >= threshold:
                return True  # Potential match found
        return False # No match found
    

    Important: The threshold value (70 in this example) controls how close the input needs to be to a stored password to be considered a match. Lowering this threshold increases the risk of false positives.

  7. Integrate into Your Authentication System
  8. Modify your login process to use the authenticate_partial function *in addition* to (or instead of) standard password verification. Here’s a conceptual example:

    passwords = get_passwords()
    user_input = request.form['password'] # Get user input
    
    if authenticate_partial(user_input, passwords):
        # Log the user in (with caution!)
        print("Partial match found - logging in...")
    else:
        # Standard password verification or error message
        print("Authentication failed.")
    

    Warning: Always log attempts using partial authentication for auditing and security monitoring.

  9. Hashing Considerations
  10. The above code compares strings directly. In a real-world application, you should be comparing the hash of the user input with the stored password hashes. This requires hashing the user’s input using the same algorithm used to store passwords (e.g., bcrypt, Argon2). You would then compare the fuzzy matching score between the hashed input and the stored hashes.

    import hashlib
    
    def hash_password(password):
        # Example using SHA-256 - use a stronger algorithm like bcrypt in production!
        return hashlib.sha256(password.encode()).hexdigest()
    
  11. Security Implications and Mitigation
Exit mobile version