Skip to content
Back to CurriculumLesson 27 of 30
resilience-securityIntermediate

Credential Stuffing & Password Spraying: Exponential Backoff, Account Lockout & Adaptive CAPTCHAs

Credential stuffing attacks use automated botnets to test billions of username and password pairs leaked from third-party database breaches against an application. Effective defense requires multi-tiered sliding-window rate limiting (per IP and per account), exponential backoff, leaked credential screening, and adaptive proof-of-work CAPTCHAs.

Interactive Visual Concept ExplainerStep-by-Step Educational Simulation
Visual Step-by-Step Concept Explainerresilience-security
Credential Stuffing & Password Spraying: Exponential Backoff, Account Lockout & Adaptive CAPTCHAs
Steps:
Limiter 1: IP Sliding Window
Rate: 10 requests / minute / IP
Defeats naive single-source brute force scripts.
Limiter 2: Account Window (Anti-Botnet)
Rate: 5 failed attempts / 15m / username
Even when botnet rotates 1,000 IPs, targeted account locks or enforces CAPTCHA!
Step 1 of 6
Auto-stepping (1x)

Step 1: Incoming POST /auth/login request arrives at the edge gateway.

Security Guarantee: Implement dual-tier rate limiting tracking both the client IP address and the targeted account identifier.
Why This Architectural Concept Exists

Over 65% of internet users reuse passwords across multiple websites. When a minor forum is breached, attackers take the leaked credentials and immediately test them against banking, email, and SaaS services at rates of thousands of attempts per second.

Intuitive Mental Model

The Bank ATM PIN Throttler: If someone punches 3 incorrect PINs into an ATM, the machine does not allow 10,000 more attempts in the next minute. It temporarily freezes the card, activates camera surveillance, and demands identity verification at a branch counter.

Architectural System TopologyProtocol Diagram
[ Botnet Cluster (1,000 IPs) ] ──(Millions of Leaked Passwords)──► [ CosmosApp Edge Gateway ]
                                                                       │
                                              ┌────────────────────────┴────────────────────────┐
                                              ▼                                                 ▼
                                  [ Rate Limit by IP (Redis) ]                     [ Rate Limit by Account (Redis) ]
                                  Max 10 req/min per IP                            Max 5 failed attempts per account
                                              │                                                 │
                                              ├── Limit Exceeded? ──► HTTP 429 Too Many Requests
                                              │
                                              └── 3 Failed Attempts?
                                                  Trigger Adaptive Turnstile CAPTCHA challenge!
CosmosApp Production Milestone

CosmosApp detects a coordinated botnet launching 50,000 login attempts per minute across distributed residential proxy IPs. CosmosApp enforces a dual-key rate limiter in Redis: tracking failed attempts both by client IP subnet (/24) and target email address. When an account sees 5 failed attempts, CosmosApp triggers an adaptive Cloudflare Turnstile CAPTCHA and temporarily locks the account.

Step-by-Step Execution Sequence
01

Step 1: Incoming POST /auth/login request arrives at the edge gateway.

02

Step 2: Server checks Redis sliding-window counter for the client IP address; if > 10 attempts/min, returns HTTP 429.

03

Step 3: Server checks account-level failure counter for the submitted email address.

04

Step 4: If account failures exceed threshold (e.g. 5 failures in 15 mins), require an interactive CAPTCHA verification.

05

Step 5: If authentication succeeds, reset account failure counters to zero.

06

Step 6: If authentication fails, increment failure counter and enforce an artificial delay (e.g. 200–500ms) to throttle brute-forcing.

Production Architecture Blueprint
import Redis from 'ioredis';
const redis = new Redis(process.env.REDIS_URL);

// CosmosApp Dual-Key Sliding-Window Rate Limiter:
export async function checkLoginRateLimit(ip: string, email: string) {
  const ipKey = `rate:login:ip:${ip}`;
  const accountKey = `rate:login:account:${email.toLowerCase()}`;

  const [ipAttempts, accountAttempts] = await Promise.all([
    redis.incr(ipKey),
    redis.incr(accountKey),
  ]);

  if (ipAttempts === 1) await redis.expire(ipKey, 60); // 1-minute window
  if (accountAttempts === 1) await redis.expire(accountKey, 900); // 15-minute window

  if (ipAttempts > 10) {
    throw new Error('Too many requests from this IP. Please try again later.');
  }

  if (accountAttempts > 5) {
    return { requireCaptcha: true, locked: accountAttempts > 10 };
  }

  return { requireCaptcha: false, locked: false };
}
Common Architectural Misconceptions
  • ✕Rate limiting by IP address alone stops credential stuffing (Botnets rotate through tens of thousands of residential proxy IPs, easily circumventing IP-only rate limits).
  • ✕Permanently locking accounts after 5 failures is secure (Permanent lockouts allow attackers to launch denial-of-service attacks by intentionally locking out all corporate accounts).

Key Architectural Takeaways

  • ✓Implement dual-tier rate limiting tracking both the client IP address and the targeted account identifier.
  • ✓Use temporary progressive lockouts (15–30 minutes) rather than permanent lockouts to prevent DoS attacks.
  • ✓Check passwords against the HaveIBeenPwned k-Anonymity API upon user registration and password updates.
Common Architectural Pitfall

Relying purely on IP-based rate limiting, which is easily defeated by attackers using distributed residential proxy networks.

Production Best Practice Fix

Track failed authentication attempts against both the target user identifier and the IP subnet simultaneously.

Interactive Knowledge Check

Why is permanent account lockout considered an architectural antipattern in modern authentication systems?