How to prevent credential stuffing attacks
Short answer
Use several layers, because each one fails alone. Offer or require MFA, refuse passwords that appear in breaches, detect bots on every login endpoint with device and connection fingerprints, rate limit per account as well as per IP address, return the same error for every failed login, and alert users about suspicious activity. Apply the same checks to mobile APIs and password reset.
The OWASP Credential Stuffing Prevention Cheat Sheet assumes that every single defence will be bypassed at some point. It recommends layers, with a volume metric for each one so you can see when one starts failing. The controls below follow it, with a starting configuration for each.
Add multi-factor authentication
MFA is the strongest single control. A correct password from a combo list is not enough to get in. If you cannot require it for everyone, require it for admins and staff, and ask for it on risky logins: a new device, an unusual location, an IP address that has tried many accounts, or a session that looks scripted. MFA has its own weak points, such as real-time phishing and SIM swaps, covered in does MFA stop account takeover.
Refuse passwords that appear in breaches
Credential stuffing only works with passwords that have already leaked. Check new passwords against breach data at signup and at every password change. NIST SP 800-63B-4 requires this check against a blocklist that includes breached passwords (NIST SP 800-63B).
The free Pwned Passwords range API uses k-anonymity: you send only the first five characters of the password's SHA-1 hash, receive every breached hash suffix that starts with them, and compare locally. The password and its full hash never leave your server. This Node.js function (version 18 or later, for the built-in fetch) does the check:
import { createHash } from "node:crypto";
export async function isBreachedPassword(password) {
const hash = createHash("sha1").update(password, "utf8").digest("hex").toUpperCase();
const prefix = hash.slice(0, 5);
const suffix = hash.slice(5);
const response = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`, {
headers: { "Add-Padding": "true" },
});
if (!response.ok) {
throw new Error(`Pwned Passwords returned HTTP ${response.status}`);
}
const body = await response.text();
return body.split("\n").some((line) => {
const [candidate, count] = line.trim().split(":");
return candidate === suffix && Number(count) > 0;
});
}The Add-Padding header adds fake entries with a count of zero, so the response size does not reveal the prefix; the count check discards them. You can also run the check at login and send users with a breached password to a reset.
Detect bots at the login endpoint
Device fingerprinting combines request headers with attributes read in the browser, such as screen size and fonts (browser fingerprinting explains the method). Connection fingerprinting looks at how the client opens the connection: the TLS handshake (JA3 or JA4), HTTP/2 settings and header order. A request whose user agent claims an iPhone while its TLS handshake matches a scripting library is not an iPhone. Both kinds can be spoofed, so use a mismatch to trigger an extra check such as MFA or a challenge.
The cheat sheet adds two cheap measures. Requiring a token generated by JavaScript stops tools that only send raw HTTP. Attackers can answer by running real or headless browsers, and blocking users without JavaScript can hurt accessibility. A multi-step login, where the form first fetches a CSRF token or asks for the username and password on separate steps, breaks tools built for a single POST. Keep CAPTCHAs for high-risk attempts only, since CAPTCHA solving services clear them for a small fee.
Rate limit per account and per IP
A per-IP limit is easy to set up and stops one machine hammering your login. Against credential stuffing it fails, because attackers rotate through thousands of residential proxy addresses and each address makes one or two attempts. A per-IP limit at the edge is still a useful floor:
# in the http block
limit_req_zone $binary_remote_addr zone=login_ip:10m rate=10r/m;
server {
location = /login {
limit_req zone=login_ip burst=5 nodelay;
limit_req_status 429;
proxy_pass http://127.0.0.1:8080;
}
}The limits that hurt stuffing are counted in the application: failures per username, per device fingerprint and per ASN. A starting point is five failures for one username in 15 minutes, then a growing delay or an extra check. A hard lockout does little against stuffing, which tries each account once, and lets attackers lock customers out on purpose. OWASP also advises against relying on a single predictable limit. Watch long windows too, for low and steady traffic from many addresses. What rate limits stop and what they miss goes further.
Return the same error for every failed login
If "unknown email" and "wrong password" produce different messages, status codes or response times, your login tells attackers which emails have accounts. Return one response for both:
HTTP/1.1 401 Unauthorized
Content-Type: application/json
{"error": "Incorrect email or password."}Do the same on password reset ("If an account exists for this email, we have sent a link") and on signup, where you can email the existing account holder instead of saying the address is taken.
Alert users about unusual security events
Do not email users about every failed password. OWASP recommends alerting when a correct password is followed by a failed MFA check, because that means the password is known. Show the last login time and location, and let users see and end their active sessions.
Cover every login surface
Attackers go to whichever endpoint has the fewest checks: the mobile app API, a legacy login path, an OAuth token endpoint, the password reset flow or a partner API. Apply the same limits and bot detection to each of them, as described in how to protect APIs from bots.
Tools that help
StopBots is published by Kairal, which is one of the tools listed. The list is not a ranking.
- Pwned Passwords is free and needs no API key, and the range API above covers breached password checks for sites of any size.
- Cloudflare rate limiting rules set per-IP and other request limits on login paths at the edge, which suits sites already behind Cloudflare.
- AWS WAF Bot Control adds managed rules for automated traffic in front of AWS-hosted applications, and AWS WAF has a separate rule group for account takeover prevention on login pages.
- Castle scores login and signup events through an API to flag bots and account takeover attempts.
- Kairal detects credential stuffing from device, network and behavioural signals, verifies suspicious visitors with invisible challenges, and lets you set protection per endpoint, including login.
Common questions
Does a CAPTCHA stop credential stuffing?
It slows it down. Attackers can pay CAPTCHA solving services to clear the puzzles, so OWASP suggests showing one only for suspicious logins and watching solve rates, since an unusually high solve rate is itself a sign of automated solving.
Can Cloudflare stop credential stuffing?
Cloudflare has several controls that help: rate limiting rules, bot scores on Bot Management plans, and leaked credentials detection, which flags login requests whose password or username and password pair appears in known breaches. Which checks you get depends on the plan, and per-account logic still belongs in your application.
How do you mitigate credential stuffing on a mobile app API?
Apply the same per-account limits and breached password checks on the server, and add app attestation such as Apple App Attest or Google Play Integrity so the API can tell your genuine app from a script that copies its requests.