Brute Force Protection

Difficulty: Beginner

Question

Why should a login endpoint add delays, rate limits or lockouts after failed attempts, and how would you design brute force protection without locking out real users?

Answer

Without limits on failed attempts, an attacker can try passwords as fast as your server responds, so weak or reused passwords fall in minutes. Delays, rate limits and lockouts cap how many guesses are possible per account and per source, turning millions of attempts into a handful, which makes online guessing impractical. The design challenge is doing this without letting attackers lock real users out on purpose.

The attacks you are defending against

  • Classic brute force: many passwords against one account.
  • Password spraying: one common password (Welcome@123) against many accounts, staying under per-account limits.
  • Credential stuffing: username and password pairs leaked from other breaches, replayed at scale from thousands of IPs. This is the most common real-world attack on Indian consumer apps too, because people reuse passwords.
  • OTP brute force: a 6-digit OTP has only 1,000,000 combinations, so an unthrottled OTP verify endpoint can be guessed.

Layered controls

ControlWhat it limitsWatch out for
Per-account throttlingGuesses against one userAttackers can lock victims out if it is a hard lock
Per-IP / per-device rate limitVolume from one sourceBotnets and residential proxies rotate IPs
Progressive delay (backoff)Speed of guessingMust be enforced server side, not in the UI
CAPTCHA or bot challenge after N failuresAutomated toolsAccessibility, solver services
MFAValue of a correct passwordRate limit OTP verification too
Breached password checksCredential stuffing successCheck at signup and password change

NIST SP 800-63B requires verifiers to rate limit failed attempts and caps it at no more than 100 consecutive failures per account, and recommends CAPTCHA, increasing delays and risk-based checks before that point.

Lockout vs throttling

A hard lockout ("account locked for 24 hours after 5 failures") is easy to abuse: anyone who knows a username can deny service to that user. Better patterns:

  • Soft lockout: exponential delays (1s, 2s, 4s, ... up to a cap) or a 15-minute temporary lock.
  • Step-up: after a few failures, require a CAPTCHA or an email/OTP confirmation instead of blocking.
  • Count failures per (account, IP) and per account globally so one attacker cannot lock everyone, but distributed attacks still trip an alert.
  • Notify the user of suspicious activity and offer a password reset.

Implementation details that matter

  • Keep counters in a shared store like Redis with TTLs, not in process memory, or each server instance has its own limit.
  • Return the same generic message ("Invalid email or password") for unknown users and wrong passwords, and keep response times similar, to avoid account enumeration.
  • Apply limits to every authentication surface: login, OTP verify, password reset, API token endpoints, mobile app APIs. Attackers go for the forgotten one.
  • Slow password hashing (Argon2id, bcrypt) protects against offline cracking after a database leak. It is not a replacement for online rate limiting.
  • Log and alert on spikes in failures; a WAF or bot management layer can block known bad infrastructure upstream.
Escalating response to failed logins. Friction increases with each failure instead of a single hard lockout.
  1. Failures 1-3: normal response: Generic error, counter stored in Redis.
  2. Failures 4-5: add delay: Server enforces exponential backoff.
  3. Failure 6+: challenge: Require CAPTCHA or bot check.
  4. Sustained failures: temporary lock: Short lock plus email alert to the user.
  5. Global spike: alert SOC: Possible credential stuffing campaign.

Code examples

Per-account and per-IP throttling with Redis

import { createClient } from "redis";
const redis = createClient();
await redis.connect();

const WINDOW_SEC = 15 * 60;

async function bump(key) {
  const n = await redis.incr(key);
  if (n === 1) await redis.expire(key, WINDOW_SEC);
  return n;
}

export async function checkLoginAllowed(email, ip) {
  const acct = Number(await redis.get(`fail:acct:${email}`)) || 0;
  const src = Number(await redis.get(`fail:ip:${ip}`)) || 0;
  if (src > 100) return { allowed: false, reason: "rate_limited" };
  if (acct >= 10) return { allowed: false, reason: "temporarily_locked" };
  if (acct >= 5) return { allowed: true, requireCaptcha: true };
  return { allowed: true };
}

export async function recordFailure(email, ip) {
  await bump(`fail:acct:${email}`);
  await bump(`fail:ip:${ip}`);
}

export async function recordSuccess(email) {
  await redis.del(`fail:acct:${email}`);
}

Counters live in Redis so every app instance shares them and they expire automatically. Friction escalates from CAPTCHA to a temporary lock instead of a permanent one.

Key points

  • Online guessing must be limited server side; client-side delays are useless.
  • Defend against brute force, password spraying, credential stuffing and OTP guessing.
  • Prefer throttling, backoff and step-up challenges over hard lockouts that enable account DoS.
  • Use a shared store (Redis) for counters and cover every auth endpoint, including OTP and reset.
  • Slow hashing protects leaked hashes; rate limiting protects the live login.

Common follow-up questions

  • How would you stop credential stuffing that comes from thousands of different IPs?
  • How can account lockout be abused, and how do you prevent it?
  • How would you rate limit OTP verification for a mobile login flow?
  • How do you avoid leaking whether an email is registered?

Interview tips

  • Name credential stuffing and password spraying; it shows you know how attacks happen today.
  • Discuss the lockout-as-DoS trade-off rather than just saying 'lock after 5 attempts'.
  • Mention Redis or another shared store so the limit works across multiple servers.

Concepts covered

Brute Force, Credential Stuffing, Rate Limiting, Account Lockout, Password Spraying, Exponential Backoff

More Web Security (OWASP, JWT, XSS, CSRF) questions