Layered login defence: rate limits, lockout and captcha

Security2 min read

Red padlock resting on a black computer keyboard
Photo: FlyDUnsplash

A login form is the one page on a site that attackers are guaranteed to try. Most attacks are not clever. They are scripts that try common passwords, or passwords leaked from other sites, as fast as the server answers. No single defence stops all of them, so I layer several cheap ones.

1. Rate limit by IP

ASP.NET Core has a built-in rate limiter. A fixed window of around ten attempts per IP per fifteen minutes stops naive scripts without bothering a real person who mistyped twice.

options.AddPolicy("admin-login", context =>
    RateLimitPartition.GetFixedWindowLimiter(
        context.Connection.RemoteIpAddress?.ToString() ?? "unknown",
        _ => new FixedWindowRateLimiterOptions { PermitLimit = 10, Window = TimeSpan.FromMinutes(15) }));

Behind a proxy or CDN, make sure the forwarded headers middleware is configured, or every visitor will share the proxy's address.

2. Lock the account, not just the IP

An attacker with many IP addresses gets around a per-IP limit. A per-account counter catches that: after five failed attempts the account locks for fifteen minutes, whatever IP the attempts come from. A successful login resets the counter.

The lockout has to be short and automatic. A permanent lock turns your defence into a way for anyone to lock you out of your own site.

3. A challenge before the password check

A proof-of-work captcha or a hosted challenge makes each attempt cost the client something. The server verifies the challenge before it touches the password hash, so a flood of attempts without solutions costs almost nothing to reject.

4. Slow, salted hashes

Store passwords with a slow key derivation function. PBKDF2 with SHA-256 and a high iteration count, bcrypt or Argon2 are all reasonable. The point is that even a leaked database is expensive to crack.

Keep the iteration count in the stored hash, for example pbkdf2-sha256$600000$salt$hash. When you raise it later, rehash on the next successful login.

5. Give nothing away

  • Return the same message for an unknown user name and a wrong password.
  • Take the same time for both. If the user does not exist, run a hash verification anyway so the response time does not reveal which names are valid.
  • Do not tell the client how many attempts are left.

6. Treat the session as part of login

A strong login does not help if the cookie is easy to steal or reuse:

  • HttpOnly, Secure and SameSite=Strict on the session cookie.
  • The __Host- prefix, which forces Secure, Path=/ and no Domain.
  • A security stamp stored with the user. Changing the password changes the stamp, and every other session stops working on its next request.

Test it

Each layer should have a test: the eleventh request returns 429, the sixth wrong password returns 423, an unknown user and a wrong password produce the same body, and an old cookie fails after a password change. Defences that are not tested tend to break quietly during a refactor.

Have a platform in mind?

Tell me who will use it and what they need to do. I'll reply with questions and next steps.

Join the conversation

No comments yet. Be the first to share your thoughts.

Leave a comment

Questions, ideas or a different point of view are all welcome. Your comment will appear here once it has been approved.

Your email is never shown publicly. It is only used if you ask to be told about a reply.