Get a Pentest and security assessment of your IT network.

Cyber Security

Server Login Security: Re-Authentication

TL;DR

To protect sensitive server endpoints from attackers who may have stolen long-term login credentials (like passwords or API keys), require users to re-authenticate periodically. This limits the damage an attacker can do if they compromise a user’s account. We’ll cover how to implement this using session timeouts, multi-factor authentication (MFA), and token refresh mechanisms.

1. Understand the Threat

Attackers don’t always try to use stolen credentials immediately. They might wait months or even years before attempting access. This is because immediate use raises red flags. Long-term logins are a prime target, as they provide persistent access.

  • Credential Stuffing: Attackers use lists of usernames and passwords leaked from other breaches to try logging into your system.
  • Key Compromise: API keys or SSH keys can be stolen through malware or insider threats.

2. Session Timeouts

The simplest approach is to automatically log users out after a period of inactivity. This forces them to re-enter their credentials.

  1. Configure your web server: Most web servers (Apache, Nginx, IIS) have settings for session timeout.
  2. Application-level timeouts: Implement session timeouts in your application code as well. This provides an extra layer of security.
  3. Example (Python/Flask):

    from flask import Flask, session, redirect, url_for
    app = Flask(__name__)
    app.secret_key = 'your_secret_key'
    @app.route('/')
    def index():
        if 'username' in session:
            return 'Logged in as {}'.format(session['username'])
        else:
            return redirect(url_for('login'))
    @app.route('/login', methods=['POST'])
    def login():
        # ... your login logic here...
        session.permanent = True # Set session to persist across browser sessions.
        session['username'] = username
        return redirect(url_for('index'))
    
  4. Consider a reasonable timeout: 30-60 minutes is a good starting point, but adjust based on your application’s security needs and user experience.

3. Multi-Factor Authentication (MFA)

MFA adds an extra layer of verification beyond just a password. Even if an attacker has the password, they’ll need access to another factor (like a code from their phone).

  1. Choose an MFA method: Options include time-based one-time passwords (TOTP), SMS codes, push notifications, or hardware security keys.
  2. Integrate with your application: Most identity providers offer libraries and APIs for integrating MFA.
  3. Enforce MFA on sensitive endpoints: Don’t require MFA for every login; focus on critical areas like admin panels or access to financial data.

4. Token Refresh Mechanisms (for API Access)

If your server provides an API, use short-lived access tokens and refresh tokens.

  1. Short-Lived Access Tokens: Issue access tokens that expire quickly (e.g., 15 minutes).
  2. Refresh Tokens: Provide a longer-lived refresh token that can be used to obtain new access tokens without requiring the user to re-enter their credentials.
  3. Secure Refresh Token Storage: Store refresh tokens securely, using encryption and appropriate access controls.
  4. Revocation: Allow users to revoke refresh tokens if they suspect compromise.
  5. Example (simplified):

    # When user logs in:
    access_token = generate_access_token(expiry=15m)
    refresh_token = generate_refresh_token()
    save_refresh_token(user_id, refresh_token)
    
    # API endpoint:
    if is_valid_access_token(access_token):
        # ... process request...
    else:
        if is_valid_refresh_token(refresh_token):
            new_access_token = generate_access_token(expiry=15m)
            # ... return new access token...
    else:
        return 401 Unauthorized
    

5. Monitoring and Logging

Keep an eye out for suspicious activity.

  • Failed Login Attempts: Log and alert on excessive failed login attempts from the same IP address or user account.
  • Login From Unusual Locations: Monitor logins from unexpected geographic locations.
  • Token Usage Patterns: Track how access tokens are being used and identify any anomalies.
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