SE·00
Start Here: Thinking Like a Defender
For readers who have built web apps but never attacked or defended one: why security is a property you design in, three real vulnerabilities in one small app shown with the attack and the fix side by side (SQL injection, broken access control, weak password storage), the CIA triad and defence in depth, the vocabulary of the volume, and how to study it.
Start here: how to use this volume
Two reading paths
This volume is about building software that holds up against people trying to misuse it: the principles (CIA, least privilege, defence in depth, zero trust), cryptography you can use correctly, the common web and API vulnerabilities with their fixes, identity and access, container and cloud security, the software supply chain, and threat modelling. Every chapter has the same shape: a plain-language Start here section with small, verified examples; a How deep to go ladder; the full-depth sections; a Lab of real measurements; and a Revision sheet.
- New to security? Read this chapter, then the chapters in order. The examples use free tools you already have: Python, Flask, OpenSSL, bcrypt and argon2. Everything runs on your own machine against your own code.
- Experienced? Skip this chapter. In each chapter read the revision sheet first, take its quiz, then use How deep to go to choose the sections and lab experiments to study.
A note on intent: everything here is for defending systems you own or are authorised to test. The attacks are shown so you can recognise and prevent them; run them only against the practice code in these labs or systems you have written permission to test.
Mastery levels
Every chapter uses the same six levels:
| Level | Name | You can… | Example for web security |
|---|---|---|---|
| L1 | Recognition | define it and spot it | "that string-built query is SQL injection" |
| L2 | Working | apply the standard fix | use parameterised queries everywhere |
| L3 | Internals | explain why it works | explain how the parameter is sent separately from the query |
| L4 | Debugging | find it in real code | spot injection, IDOR and weak hashing in a code review |
| L5 | Design | choose defences for a system | design authn/authz, crypto and input handling for a service |
| L6 | Production | run it at scale | set security standards, review and testing for an organisation |
Why security is designed in, not added on
Three holes in one small app
Here is the tiffin centre's first customer portal: log in, then read your orders. It was written the obvious way, and it has three of the most common and most serious flaws in web software. The lab runs each attack against it, then against a fixed version. The differences are one or two lines of code each.
1. SQL injection: the login that lets anyone in
The vulnerable login builds a SQL query by pasting the email straight into a string:
row = db.execute(f"SELECT id FROM users WHERE email = '{e}' AND pw = '{h}'").fetchone()
Send the email user1@example.com' -- and the -- turns the password check into a comment. The measured result:
SQL injection login as user1 with password 'anything': vuln → HTTP 200 {'ok': True, 'user_id': 1}
fixed → HTTP 401 {'error': 'bad login'}
The attacker logged in as any user without knowing the password. The fix is to never build queries by string concatenation; pass the value as a parameter so the database treats it as data, not SQL:
r = db.execute("SELECT id, pw FROM users WHERE email = ?", (e,)).fetchone()
2. Broken access control: reading other people's orders
The order endpoint looked up an order by its ID and returned it. Logged in as user2 (who owns 10 orders), an attacker simply asked for IDs 1 to 50:
logged in as user2, requested orders 1..50: vuln → could read 50; fixed → could read 10 (only their own)
Nothing was "hacked"; the app just never checked who owned the order. This is IDOR (insecure direct object reference), part of the broadest category of web flaws. The fix is to scope every lookup to the current user:
db.execute("SELECT * FROM orders WHERE id = ? AND user_id = ?", (oid, session["uid"]))
3. Weak password storage: a stolen database
Databases get stolen. What matters then is how the passwords were stored. The first version used MD5; the fix used bcrypt. Running a small guessing list against the stolen table:
stolen database, 3,180-word list:
MD5 → cracked 3 of 5 passwords; 1,637,857 guesses/second
bcrypt → cracked 0 of 5 passwords; 4 guesses/second
MD5 is a fast, general-purpose hash, so an attacker can try millions of guesses a second. bcrypt (and argon2, scrypt) is deliberately slow and salted, so the same guessing runs about 400,000 times slower and the weak-but-not-trivial passwords survived the test window. Chapter 107 goes into exactly why.
The CIA triad and defence in depth
What we're protecting
Security is usually framed as protecting three properties, the CIA triad:
- Confidentiality: only authorised parties can read the data (the stolen-database lab is a confidentiality failure made worse by weak hashing).
- Integrity: data and systems can't be changed by unauthorised parties, or changes are detected (signatures, chapter 107).
- Availability: the system is usable when needed (denial of service, rate limiting; overlaps with Volume 9's reliability).
Two more are often added: authenticity (you are who you claim) and non-repudiation (you can't deny an action you took).
Defence in depth
No single control is perfect, so you layer them. In the portal, parameterised queries stop injection, but you also run the database as a low-privilege user, validate input, log access, and alert on anomalies, so that one mistake isn't a breach. The rest of the volume is largely a catalogue of layers: cryptography, identity, input handling, network and container controls, supply-chain checks, and the threat modelling that tells you which layers you actually need.
How to study security
A routine that works
- Build the vulnerable version and attack it yourself, as the lab does, so you see the failure, not just read about it.
- Fix it and re-run the attack to confirm the fix and feel how small it usually is.
- Learn the fix as a default, not a special case: parameterise every query, authorise every request, hash every password with a slow function.
- Use tools: a linter or static analyser (bandit, semgrep) catches many of these in code review.
Warm-up exercises
- In a web app you've written, find one database query. Is the user input parameterised?
- Find one endpoint that takes an ID. Does it check that the caller owns the object?
- Find where passwords (or API tokens) are stored. What function hashes them?
- Run
openssl rand -base64 32and read chapter 107 on why that's the right way to make a key.
Glossary
- Vulnerability / exploit / threat: a weakness / a way to use it / who might.
- SQL injection: making a database run attacker-controlled query syntax.
- Access control / IDOR: deciding who may do what / reading objects you don't own by guessing IDs.
- Hash vs password hash: a one-way digest / a deliberately slow, salted one for passwords.
- Salt: random per-record value mixed into a password hash.
- CIA triad: confidentiality, integrity, availability.
- Defence in depth: multiple independent layers of control.
- Least privilege: give each part only the access it needs.