Labs / Cryptography at Work / Hashes, passwords and obfuscation

Lab 2 of 5 · Hashes, passwords and obfuscation

Salted passwords: why a leaked hash table still gives up its passwords

Read a leaked password table, crack it with a precomputed lookup, then watch a per-user salt and a slow hash take the attack apart.

Start this lab 6 steps

The idea

Silas keeps the member database for Lindow Fitness, the gym on the high street, and this morning a copy of its password table turned up on a forum. The table holds no plaintext, only a hash of each password, so the owner wants to tell members their passwords are safe. Silas wants to check before anyone sends that email.

Read the leak the way an attacker would, crack it, and then rebuild the store the way it should have been: a per-user salt, and a hash slow enough that each guess costs real time.

Equal digests give reuse away
Equal digests give reuse away
One hashed guess checks every row
One hashed guess checks every row
The hashing happened before the leak
The hashing happened before the leak
Why a salt makes the table worthless
Why a salt makes the table worthless
The salt is not a secret
The salt is not a secret
The same cost, paid once or per guess
The same cost, paid once or per guess

Steps

  1. The member table that leaked

    A password store should never hold plaintext. Silas's holds a SHA-256 of each member's password, one row per account, so a leaked copy doesn't hand the passwords over directly. You'd have to reverse the hash, and you can't.

    That's the theory. Read the leaked table and look at the digests themselves before you try to break anything.

    host:~# cat /srv/members.csv
  2. The same password always hashes the same

    Because the hash is deterministic, an attacker doesn't have to attack each row. They can hash a guess once and see whether its digest shows up in the leak. That single digest matches every account that used that password.

    Try it with one common guess. Hash the word sunshine and compare its digest to the pair you saw.

    host:~# openssl dgst -sha256 -r /srv/guess
  3. A precomputed table reads the passwords out

    An attacker doesn't guess one word at a time. Ahead of the breach they hash a whole wordlist of common passwords once and save the digest → password pairs: a lookup table (its space-saving cousin is a rainbow table). After a leak they match the stolen digests against it and read the answers off, with no work per account.

    The table is already built at /srv/lookup.txt, and the leaked digests pulled out at /srv/leaked.txt. Match one against the other.

    host:~# grep -Ff /srv/leaked.txt /srv/lookup.txt
  4. A salt per account breaks the table

    Silas rebuilds the store with a salt: a short random value, different for every account and stored in clear beside the hash. Instead of sha256(password) the store now holds sha256(salt + password).

    The salt isn't a secret: it sits right there in the table. Its job is to make every account hash differently even when the passwords are the same. Point the same precomputed table at the new store and read the rebuilt rows.

    host:~# cat /srv/members-salted.csv
    host:~# grep -Ff /srv/leaked-salted.txt /srv/lookup.txt
  5. Salted but fast: one account still cracks

    The salt stopped precomputation, not guessing. Since the salt is stored in clear, an attacker who targets one account reads that account's salt from the leak, then hashes the wordlist with it until a digest matches. SHA-256 is fast: a laptop's graphics card can run it hundreds of millions of times a second, so a common password still falls at once.

    Here's that attack as a short script. Read what it does, then run it against silas.

    host:~# python3 /srv/crack-fast.py
  6. Stretch the hash so each guess costs

    The answer is key stretching: hash passwords with a function that is slow on purpose, such as bcrypt, scrypt, argon2 or PBKDF2, instead of a bare SHA-256. It runs the password through many thousands of rounds, set by a cost factor. Raise the cost and every hash takes longer. The honest login checks one password and never notices. The attacker, who must try millions, has their guess rate cut by the same factor.

    Measure it. This script counts how many bare SHA-256 hashes and how many PBKDF2 hashes at 200,000 rounds this machine does in a second.

    host:~# python3 /srv/stretch-cost.py

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab