Back to 20 Concepts
resilience-securityIntermediate

Brute-Force & Credential Stuffing Defenses: Account Lockout & CAPTCHA

Credential stuffing uses billions of leaked username/password combinations to breach accounts. Defenses combine Exponential IP Rate Limiting, Account Lockout with email unlock tokens, and Adaptive CAPTCHAs.

Intuitive Mental Model

The Bank ATM Pin Lock: If you enter the wrong ATM pin 3 times, the machine freezes your card and sends an SMS alert to your phone to stop automated key guessing.

Architecture Blueprint & CodeProduction Standard
// Redis Sliding Window Failed Login Limiter:
async function recordFailedLogin(email: string, ip: string): Promise<boolean> {
  const emailKey = `failed:email:${email}`;
  const ipKey = `failed:ip:${ip}`;
  
  const attempts = await redis.incr(emailKey);
  if (attempts === 1) await redis.expire(emailKey, 900); // 15 min window
  
  if (attempts >= 5) {
    // Lock account & require password reset:
    await lockAccountAndSendEmail(email);
    return true; // Account Locked!
  }
  return false;
}

Key Architectural Takeaways

  • Dual-Axis Rate Limiting: Limit both by originating IP (stops single-attacker floods) and by target account email (stops distributed botnets).
  • HaveIBeenPwned API: Check new passwords against known breach databases during user registration.
Common Architectural Pitfall

Locking accounts permanently based solely on IP address, allowing attackers to DoS entire corporate VPN offices.

Production Best Practice

Use adaptive CAPTCHA challenges and email verification links rather than global IP lockouts.