#282 How it works, by example 6.4 min
5 Password Attacks and the Defence for Each
Brute force: 6 of 6 guesses through, then 2 of 6 once the lockout was on.
Spread out the brute force
Copy the code folder first. In attacks.py, in the brute_force function, change site.clock.tick(1) to site.clock.tick(60), then run python run.py. Predict how many of the 6 guesses reach the password check with the lockout on. You should see 6 of 6, so the lockout only helps when guesses come quickly.
A hint
A lockout is a time in seconds. Compare it with the gap between guesses.
Move the weak account first
In server.py reorder USERS so that chloe comes first, then amina, then ben. Run python run.py and look at the spray in section 4. Predict the result with the per-address counter on. You should see ['ok', 'wrong', 'wrong'] even with the defence on, because the weak account is tried before the counter blocks anything. Put USERS back in the original order afterwards, because the next exercise quotes results for the original order.
A hint
The spray tries the accounts in the order they appear in the store.
Change the address limit
In login.py change IP_LIMIT from 2 to 1, and run python run.py. Predict the spray result with the defence on. Then change it to 3 and predict again. You should see ['wrong', 'ip blocked', 'ip blocked'] for 1 and ['wrong', 'wrong', 'ok'] for 3. Which value is a better defence, and what does a very low limit do to honest users?
A hint
The limit is how many failures one address may have before it is blocked.
Put the strong password on the wordlist
In attacks.py add the text river-copper-29 to the end of WORDLIST, then run python run.py. Predict the list of cracked accounts and the count of slow hashes. You should see all three accounts cracked using 15 slow hashes. The printed message still says amina is not found, because that text is fixed in run.py. Fix it so it prints the real result.
A hint
The video says a strong password is simply not in the list, so this exercise shows what changes when it is.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
Every user, password and list is a made-up toy for defence teaching; thresholds and hash cost are toy values, far below real guidance.
Up next in How it works, by example
More in How it works, by example
Related videos
- #90 Stop Leaking API Keys: Build a Secret Scanner in Python AI security and governance
- #281 How Sites Store Passwords: Hash, Salt and Slow Hashes How it works, by example
- #01 Local AI Agent in Python: No API Key (Ollama) AI agents and MCP
- #02 RAG From Scratch in Python: Chunk, Embed, Retrieve (Local) RAG and vector search
