learning-it by example

#281 How it works, by example 6.4 min

How Sites Store Passwords: Hash, Salt and Slow Hashes

Two users, one password, the same plain hash. A salt and a slow hash fix it.

1. What should a website keep in its table instead of your actual password?
2. In a Python password-storage demo, two made-up users chose the same password and it was stored as a plain SHA-256 hash. What did the password table show, and how many of 4 users did a small made-up wordlist crack in 8 hashes?
3. Why does a unique random salt for every user make a wordlist attack take more work?
4. In a Python password-storage demo, the code checks that one scrypt guess costs more than a fixed floor of plain SHA-256 guesses. Which floor does the check use?
5. In a Python password-storage demo, the scrypt cost value was picked to make the demo run fast. What is said about copying that value into real code?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Give bob a different password

    Copy the code folder first. In store.py change bob's password from dragon42 to dragon43, then run python run.py. Predict whether bob and dave still share a row in the plain table, and how many users get cracked. You should see different rows for bob and dave, and 2 of 4 cracked in 8 hashes for the plain table. Change bob's password back to dragon42 afterwards, because the next exercise starts from the original file.

    A hint

    The attacker's wordlist in crack.py does not contain dragon43.

  2. Put carol's password on the wordlist

    In crack.py add the text otter-kettle-lantern to the end of the WORDLIST. Predict how many users are cracked and how many hashes each attack uses, then run python run.py. You should see 4 of 4 cracked, with 9 hashes for the plain table and 25 for the salted one.

    A hint

    The plain attack hashes each word once. The salted attack hashes guesses again for every user.

  3. Predict a near-miss login

    In run.py, change the right-password check for carol so the attempt has a capital O at the start, for example Otter-kettle-lantern. Predict what verify prints, then run python run.py slow. The printed label still says right password, so read it as the Otter attempt. Explain what the stored salt is used for in the check. Afterwards change the attempt back to otter-kettle-lantern.

    A hint

    The check hashes the attempt with the stored salt and compares it with the stored hash.

  4. Make scrypt cheap

    In store.py change the cost value in the slow function from n=2**14 (that is 16384) to n=16 (the same as 2**4). Predict what the line about one scrypt guess costing over 1,000 SHA-256 guesses will say, then run python run.py slow. In a test run it printed False, though timings depend on your machine. Why is a cost this low a bad idea outside a demo?

    A hint

    Only the slow hash makes each guess cost more. The timing depends on your machine, so a result may differ.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

All users, passwords and the wordlist are made up; the scrypt cost is a demo value, not a recommendation.

Up next in How it works, by example

More in How it works, by example

All How it works, by example videos

Related videos