Get a Pentest and security assessment of your IT network.

Cyber Security

Secure User Login

TL;DR

This guide shows you how to make your user login system more secure against common attacks like brute-force and password guessing. We’ll cover strong passwords, rate limiting, two-factor authentication (2FA), and monitoring for suspicious activity.

1. Strong Password Policies

  1. Minimum Length: Enforce a minimum password length of 12 characters. Longer is better!
  2. Complexity Requirements: Require a mix of uppercase letters, lowercase letters, numbers, and symbols.
  3. Password History: Prevent users from reusing recent passwords (e.g., the last 5-10 passwords).
  4. Regular Password Updates: Encourage or require periodic password changes (every 90 days is a common practice).
  5. Avoid Common Passwords: Check against lists of known compromised passwords during registration and login. Libraries exist for this purpose in most programming languages.

Example code snippet (Python):

import zxcvbn

password = "P@sswOrd123"
strength = zxcvbn(password)
if strength['score'] < 3:
  print("Weak password. Please choose a stronger one.")
else:
  print("Password is strong enough.")

2. Rate Limiting

Rate limiting restricts the number of login attempts from a single IP address or user account within a specific timeframe.

  1. Identify Attempts: Track failed and successful login attempts, recording the IP address and username/email.
  2. Set Limits: Define reasonable limits (e.g., 5-10 attempts per minute).
  3. Implement Blocking: Temporarily block IPs or accounts exceeding the limit.

Example code snippet (using a simple dictionary in Python):

login_attempts = {}

def check_rate_limit(ip_address):
  if ip_address in login_attempts:
    if login_attempts[ip_address] > 5:
      return False # Rate limit exceeded
    else:
      login_attempts[ip_address] += 1
      return True
  else:
    login_attempts[ip_address] = 1
    return True

3. Two-Factor Authentication (2FA)

2FA adds an extra layer of security by requiring a second verification method, such as a code sent to the user’s phone or generated by an authenticator app.

  1. Choose a Method: Common options include SMS codes, authenticator apps (Google Authenticator, Authy), and email codes.
  2. Enable 2FA: Allow users to opt-in to 2FA during account setup or later in their settings.
  3. Generate Codes: Implement a secure mechanism for generating and verifying 2FA codes. Libraries are available for most languages (e.g., TOTP).
  4. Recovery Codes: Provide users with recovery codes to regain access if they lose their second factor device. Store these securely!

4. Account Lockout

Lock accounts after a certain number of failed login attempts.

  1. Track Failed Attempts: Keep a record of unsuccessful logins for each account.
  2. Set Lockout Threshold: Define the maximum number of allowed failed attempts (e.g., 5-10).
  3. Lock Account: Disable login access after exceeding the threshold.
  4. Unlock Mechanism: Provide a way for users to unlock their accounts, such as via email verification or contacting support.

5. Monitoring and Logging

Regularly monitor your logs for suspicious activity.

  1. Log Login Attempts: Record all login attempts, including the username/email, IP address, timestamp, and success/failure status.
  2. Monitor for Brute-Force Attacks: Look for patterns of repeated failed logins from the same IP address or user account.
  3. Detect Account Takeovers: Watch for unusual login locations or times.
  4. Alerting: Set up alerts to notify you of suspicious activity in real time.

6. Secure Password Storage

Never store passwords in plain text!

  1. Hashing: Use a strong hashing algorithm (e.g., bcrypt, Argon2) to hash passwords before storing them.
  2. Salting: Add a unique random salt to each password before hashing it. This prevents rainbow table attacks.
  3. Key Stretching: Increase the computational cost of hashing to slow down brute-force attacks.

Example code snippet (Python using bcrypt):

import bcrypt

password = b"mysecretpassword"
salt = bcrypt.gensalt()
hashed_password = bcrypt.hashpw(password, salt)

# To verify:
if bcrypt.checkpw(b"mysecretpassword", hashed_password):
  print("Password matches!")
else:
  print("Incorrect password.")

7. Cyber security Best Practices

  1. Keep Software Updated: Regularly update your operating system, web server, and any libraries or frameworks you are using to patch security vulnerabilities.
  2. Use HTTPS: Encrypt all communication between the user’s browser and your server with HTTPS.
  3. Input Validation: Validate all user input to prevent injection attacks (e.g., SQL injection, cross-site scripting).
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