Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 38 ยท Vulnerable Components and Auth Failures

Sessions, MFA and Account Lockout

Secure what happens after login: session ID rotation, cookie flags, expiry, multi-factor authentication with TOTP, and rate limiting that does not lock out real users.

Logging in is only the start. After the password check, the server gives the browser a session identifier, and from then on that identifier is the user's identity. If an attacker steals or predicts it, they are logged in without ever knowing the password. This lesson covers the three controls that complete the picture for authentication failures: safe sessions, multi-factor authentication (MFA) and rate limiting with lockout.

All code here is a simulation using in-memory data. It demonstrates how defenses work and does not touch any real system.

Secure session handling

A session ID must be:

  • Random and long: generated by a cryptographically secure generator, at least 128 bits of entropy. Never derived from the user name or a counter.
  • Sent in a cookie, not in the URL (URLs leak into logs, history and referrer headers).
  • Rotated at login (and on privilege change) to prevent session fixation, where an attacker plants a known ID before the victim logs in.
  • Expired: an idle timeout (minutes to hours depending on risk) and an absolute lifetime.
  • Destroyed on the server at logout, not just deleted in the browser.

Cookie flags matter:

HTTP
Set-Cookie: session=9f3c...; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=3600
  • Secure: sent only over HTTPS.
  • HttpOnly: not readable by JavaScript, so a cross-site scripting flaw cannot simply read it.
  • SameSite: limits cross-site sending, reducing cross-site request forgery.

Session fixation and rotation in code

Python Why rotating the session ID at login matters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

After a safe login, the ID the attacker knows is worthless. Frameworks provide a function for this (for example session()->regenerate() in Laravel); call it.

Multi-factor authentication

MFA requires two or more independent kinds of proof:

  • Something you know: a password or PIN.
  • Something you have: a phone, a hardware key or a passkey.
  • Something you are: a fingerprint or face (usually checked by the device).

Strength differs. From strongest to weakest for phishing resistance: passkeys / hardware security keys (WebAuthn), app-based TOTP codes, push approvals, and SMS codes (vulnerable to SIM swapping and interception). Prefer phishing-resistant options for administrators.

How TOTP works

A time-based one-time password (RFC 6238) is derived from a shared secret and the current 30-second time step. Both server and authenticator app compute the same code. The stand-in below follows the algorithm with the standard library, using a fictional secret and a fixed timestamp so the output is deterministic.

Python Compute a TOTP code (RFC 6238 style)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real deployments also need: secrets stored encrypted, one-time use of each accepted code (to stop replay inside the window), rate limits on code entry, and one-time recovery codes stored hashed.

Rate limiting and lockout

Unlimited attempts make guessing possible. Hard lockout (disable after N failures) is simple but lets an attacker lock out real users on purpose. A balanced design:

  • Progressive delay: each failure doubles the wait.
  • Per-account and per-IP limits, plus a global limit.
  • Challenges (CAPTCHA or MFA step-up) after suspicious patterns.
  • Notify the user of repeated failures.
  • A safe recovery path that does not use the same weak check.
Python Progressive delay limiter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first two failures cost nothing so honest typos are fine; later failures make automation slow and expensive. Combine this with log monitoring so a burst of failures raises an alert.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does regenerating the session ID at login prevent?

    If the ID existed before login, an attacker may know it. A fresh ID after authentication makes the planted one useless.
  2. 2. Which cookie flag stops JavaScript from reading the session cookie?

    HttpOnly hides the cookie from scripts, limiting what an XSS flaw can steal. Secure restricts it to HTTPS.
  3. 3. Which second factor is generally the most resistant to phishing?

    WebAuthn credentials are bound to the real site's origin, so a look-alike phishing page cannot use them.
  4. 4. Why is progressive delay often preferred to permanent lockout?

    Permanent lockout can become a denial-of-service tool. Delays slow automation while keeping accounts usable.

Summary

  • Session IDs must be random, rotated at login, expired and invalidated on the server.
  • Use Secure, HttpOnly and SameSite cookie flags.
  • MFA adds a second proof; passkeys and security keys resist phishing best, SMS least.
  • Limit attempts with progressive delays, per-account and per-IP limits, and watch the logs.

Next: Software and Data Integrity Failures