Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 53 ยท Cryptography and Authentication

Password Attacks and Defenses

Learn why passwords fail and how to defend them: salting, slow hashes like bcrypt, scrypt and Argon2, credential stuffing, lockouts, MFA and policies that work.

Passwords are the oldest and weakest part of most login systems, and the most common way attackers get in. They rarely "hack" anything clever. They guess common passwords, reuse passwords leaked from other sites, or steal a database of hashes and try guesses against it offline. A defender who understands these attacks can make each one expensive or pointless.

This lesson covers the attacks at the concept level and then the defenses: storing passwords correctly, slowing down guessing, adding a second factor, and writing a password policy that matches current guidance. The attack side is studied only to understand the numbers; you would test password strength only on systems you own or are authorized to assess (see Law, Scope and Rules of Engagement). Real login pages and password databases cannot run on this site, so the examples are Node stand-ins on invented sample data. They use real algorithms (SHA-256 and scrypt from node:crypto), with small toy settings so they run quickly. It builds directly on Cryptography Essentials.

How passwords fail

Four ways in

Guessing Try the most common passwords first.
Reuse Credentials leaked elsewhere work here too.
Stolen hashes Crack them offline, with no lockout.
Phishing Trick the user into typing it.

Each needs a different defense.

1 / 2
  • Online guessing hits the real login form. A brute-force attack tries many passwords against one account; a password spraying attack tries a few very common passwords against many accounts, so no single account locks. Rate limits can stop both.
  • Credential stuffing takes email and password pairs leaked from other breaches and tries them here. It succeeds because people reuse passwords, and it looks like normal logins from many addresses.
  • Offline cracking happens after a database leak. The attacker has the stored hashes and can guess as fast as their hardware allows, with nobody watching. This is where the way you store passwords decides the outcome.
  • Phishing and malware steal the password directly, bypassing strength entirely. Only a second factor that cannot be typed into a fake site helps here.

Storing passwords correctly

Never store passwords in plain text, and never encrypt them reversibly (a leaked key reveals everything). Store a hash, but not a plain fast hash. Ordinary hashes such as SHA-256 are designed to be fast, and a modern graphics card can compute billions per second. That is great for file integrity and terrible for passwords.

Three ingredients make a password hash safe:

  1. A unique random salt per password. The salt is stored next to the hash and mixed in before hashing. It defeats precomputed tables (so-called rainbow tables), and it makes two users with the same password have different hashes, so cracking one does not crack both.
  2. A deliberately slow, memory-hungry algorithm. bcrypt, scrypt and Argon2 (the current recommendation, as Argon2id) have a cost setting that makes every guess take, say, 100 milliseconds and a chunk of memory. Legitimate logins barely notice; billions of guesses become impractical.
  3. A tunable cost, raised over time as hardware improves, with a rehash at next login.

Use the library from your framework (password_hash in PHP, bcrypt or Argon2 packages elsewhere, scrypt in Node's standard library) instead of composing these yourself. An optional pepper (a secret kept outside the database, for example in a key vault) adds protection if only the database leaks.

Fast versus slow

The stand-in below stores the same sample passwords two ways: unsalted SHA-256 and salted scrypt. The "attacker" has the stolen table and a short list of common passwords. The scrypt cost is a toy setting so the demo is quick.

Node.js Cracking fast and slow hashes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Both tables fall to a weak password like sunshine1; no hashing scheme can rescue a password that is on every wordlist. What changes is the cost. Table A needs one cheap computation per guess for all users at once, and exposes that two accounts share a password. Table B forces a separate, expensive computation for each guess and each user. With real parameters, the gap is millions of times, not the factor of a few you see here. Long, random passwords (like Carol's) are safe in both.

How cracking cost scales

Time to crack depends on the number of possible passwords and the guess rate. Every extra character multiplies the search space by the alphabet size, so length wins over complexity rules. The second stand-in turns that into numbers, with invented, round guess rates:

Node.js Search space and guess rate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The exact numbers depend on the invented rates, but the pattern holds: slow hashing multiplies every time by orders of magnitude, and a length of 12 or more random characters, or four or more random words, is out of reach for anyone. Weak human-chosen passwords are cracked far faster than the worst case, because people choose predictable ones, which is why wordlists work.

Defending the login

Storage is only half. For the live login:

  • Rate-limit by account and by source, and slow down repeated failures (progressive delays, CAPTCHA after a few failures). Be careful with strict lockouts: a hard lock after five failures lets an attacker lock out real users on purpose (denial of service). Temporary, escalating delays work better.
  • Generic error messages ("Invalid username or password") so the form does not reveal which accounts exist; also take similar time for unknown users.
  • Check passwords against known-breached lists when users choose them, which blocks reuse of leaked passwords.
  • Multi-factor authentication (MFA) adds a second proof. From weakest to strongest: SMS codes (interceptable), authenticator-app codes, and phishing-resistant hardware keys or passkeys (FIDO2/WebAuthn), which are bound to the real site's domain and so cannot be handed to a fake page.
  • Monitor for credential stuffing: many failed logins from many addresses, successful logins from new devices and countries.
  • Offer a password manager story: unique, long, random passwords per site are only practical with one.

A password policy that works

Current guidance from standards bodies is simpler than the old rules:

  • Require a minimum length (at least 8, and encourage 15 or more) and allow very long passphrases and all characters, including spaces.
  • Do not force composition rules ("one uppercase, one symbol") or regular expiry; both push users toward predictable patterns like Summer2026!. Force a change only on signs of compromise.
  • Block common, leaked and context-specific passwords (the site name, the username).
  • Allow pasting so password managers work.
  • Offer MFA and make recovery flows as strong as the login itself, since attackers target "forgot password".

You will put these pieces together in Project: Build and Harden a Login Flow. For access control around the login see Authentication, Sessions and Access Control Flaws.

Password cracking tools, hash types and pass-the-hash

When a database or a domain controller leaks, attackers do not receive passwords, they receive hashes. Offline cracking is the process of guessing passwords, hashing each guess and comparing the result with the stolen hash. No login page, lockout or rate limit is involved, so the only limits are the attacker's hardware and the strength of the password and hash. Authorized testers crack hashes they are allowed to hold, to measure whether the organization's passwords would survive a breach.

The tools, conceptually

  • John the Ripper is a long-established cracker that supports hundreds of hash formats, can auto-detect many of them and applies wordlists with mangling rules (capitalize, append digits, replace a with @).
  • hashcat is a GPU-accelerated cracker with the same ideas, much faster on modern graphics cards, with attack modes for wordlists, rules, masks and combinations.
  • Wordlists collect real leaked passwords and common patterns; the quality of the list matters more than its size.
  • Masks and brute force try every combination of a pattern, such as eight characters from letters and digits. The search space grows exponentially with length, which is why length beats complexity (see How cracking cost scales).
  • Rainbow tables are precomputed chains of hashes that trade storage for time. Salting (a unique random value per password) makes them useless, because every password would need its own table.
  • Hash identification tools guess the algorithm from the hash's length and format (see "Identifying hashes" in Cryptography for Hackers).

Hash types and why they matter

The same password gives a very different cracking speed depending on the algorithm:

Type Notes
MD5, SHA-1, plain SHA-256 Designed to be fast. Billions of guesses per second on a GPU. Unsuitable for passwords.
NTLM (Windows) Unsalted MD4-based hash of the password. Very fast to crack, and it can be used without cracking (see below).
Salted fast hashes Stops rainbow tables but not brute force.
bcrypt, scrypt, Argon2id, PBKDF2 Deliberately slow and tunable; a work factor makes each guess expensive. These are what you should use.

Pass-the-hash

On Windows networks using NTLM authentication, the hash itself is the credential: a protocol step proves knowledge of the hash, not of the password. An attacker who steals an NTLM hash from memory or a directory database can authenticate to other machines as that user without ever cracking it. That is called pass-the-hash. Strong passwords do not help against it, which is why the real defenses are structural: restrict NTLM, require Kerberos and signing, use unique local administrator passwords per machine, limit where privileged accounts log in, and protect credential memory with the operating system's built-in credential protection. See AD network attacks.

A tiny cracking stand-in

The stand-in uses invented, deliberately weak passwords and a short wordlist with a few mangling rules. It shows why unsalted fast hashes fall quickly and why a salt changes the economics. It runs in Node and cracks only values created inside the example.

Node.js Wordlist, rules and the effect of a salt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice what the experiment shows: bob and alice fall to a ten-line program, while carol's long random password is untouched. With salts and a slow function the attacker has to repeat the work for every user and every guess, which turns an instant result into a budget question. The defensive checklist is in Password Security: long unique passwords, a password manager, a slow salted hash on the server and multi-factor authentication so that a cracked password is not the end of the story.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is plain SHA-256 a poor choice for storing passwords?

    Password hashing should be slow and memory-hungry. A fast general-purpose hash lets an attacker with a stolen table test enormous numbers of guesses.
  2. 2. What does a unique salt per password accomplish?

    With a per-user salt, an attacker must attack each hash separately and cannot reuse precomputed tables, but a weak password still falls to a wordlist.
  3. 3. Which attack reuses email and password pairs leaked from other websites?

    Credential stuffing relies on password reuse across services. Unique passwords, breach-list checks and MFA are the defenses.
  4. 4. Why can a hard lockout after a few failed logins be harmful?

    An attacker can trigger lockouts for known usernames. Escalating delays and rate limits slow guessing without handing attackers a denial-of-service tool.
  5. 5. Which second factor is most resistant to phishing?

    FIDO2/WebAuthn credentials only work for the genuine site's domain, so a lookalike page cannot collect something reusable.

Summary

  • Passwords fail through guessing, reuse (credential stuffing), offline cracking of stolen hashes and phishing.
  • Store a salted hash made with a slow algorithm (Argon2id, bcrypt or scrypt), with a cost you can raise over time.
  • Unique salts stop shared cracking work and reveal nothing about duplicate passwords; no hash saves a password that is on a wordlist.
  • Length beats complexity: each extra character multiplies the attacker's work.
  • Protect the login with rate limits, escalating delays, generic errors, breached-password checks and monitoring; prefer MFA, ideally phishing-resistant.
  • A good policy sets a sensible minimum length, blocks bad passwords, allows pasting and avoids forced rotation.

Next: Authentication, Sessions and Access Control Flaws