Get a Pentest and security assessment of your IT network.

Cyber Security

Secure API Authentication: Stop Storing Passwords as Arrays

TL;DR

Storing passwords in an array within your authentication API is a major cyber security risk. This guide shows you how to fix it by using proper hashing, salting, and secure storage techniques.

Solution Guide: Secure Authentication

  1. Understand the Problem
    • Storing passwords as plain text or even in a simple array is incredibly dangerous. If your database is compromised, all user accounts are immediately at risk.
    • Arrays offer no protection against common attacks like rainbow table attacks and brute-force attempts.
  2. Choose a Strong Hashing Algorithm
    • Don’t use outdated algorithms like MD5 or SHA1. These are easily cracked.
    • Recommended algorithms include: bcrypt, Argon2, and scrypt. bcrypt is often a good starting point due to its widespread availability and reasonable performance.
  3. Implement Salting
    • A salt is a random string added to each password before hashing. This makes rainbow table attacks much harder.
    • Each user should have a unique salt. Store the salt alongside the hashed password (not with the original password!).
    • Example using Python and bcrypt:
    import bcrypt
    
    salt = bcrypt.gensalt()
    hashed_password = bcrypt.hashpw(password.encode('utf-8'), salt)
    
  4. Secure Password Storage
    • Never store passwords in plain text, even with salting and hashing.
    • Store the hashed password and the salt in your database.
    • Consider using a dedicated password storage library or framework provided by your programming language/framework (e.g., Django’s authentication system).
  5. Authentication Process
    • When a user logs in:
      1. Retrieve the salt for that user from the database.
      2. Hash the entered password using the retrieved salt.
      3. Compare the newly hashed password with the stored hashed password.
      4. If they match, authentication is successful.
    # Example (Python/bcrypt)
    stored_salt = user.salt
    hashed_attempt = bcrypt.hashpw(attempted_password.encode('utf-8'), stored_salt)
    if hashed_attempt == user.hashed_password:
      # Authentication successful
    else:
      # Authentication failed
    
  6. Database Security
    • Ensure your database is properly secured:
      • Use strong passwords for the database user.
      • Restrict access to the database server.
      • Keep your database software up-to-date with security patches.
      • Consider encrypting sensitive data at rest.
  7. Rate Limiting & Account Lockout
    • Implement rate limiting to prevent brute-force attacks (e.g., limit the number of login attempts per IP address).
    • Lock accounts after a certain number of failed login attempts.
  8. Regular Security Audits
    • Periodically review your authentication code and infrastructure for vulnerabilities.
    • Consider penetration testing to identify weaknesses.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation