Common attacks: stolen passwords, injection, broken access control
45 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Attackers rarely break clever code. They reuse leaked passwords, type SQL into a search box, change a number in a URL or trick a browser. Each of these mistakes has a known defence you can build in.
Stolen and reused passwords
Credential stuffing takes email and password pairs from one leaked database and tries them on other sites, because many people reuse one password everywhere.
A leaked password is tried everywhere. Here a made-up password is checked against public leak lists, sending only five characters of its fingerprint.
Example 1 · Is this made-up password already leaked?
printf 'password123' | openssl dgst -sha1curl -s https://api.pwnedpasswords.com/range/CBFDA | grep C6008F9CAB4083784CBD1874F76618D2A97printf 'campus123' | openssl dgst -sha1curl -s https://api.pwnedpasswords.com/range/52D28 | grep F22B2721275448680CA7FBB8D0D82324B7FSHA1(stdin)= cbfdac6008f9cab4083784cbd1874f76618d2a97 C6008F9CAB4083784CBD1874F76618D2A97:2266543 SHA1(stdin)= 52d28f22b2721275448680ca7fbb8d0d82324b7f F22B2721275448680CA7FBB8D0D82324B7F:6676
What this code does
openssl dgst -sha1turns the password into a fixed-length fingerprint called a hash (the next lesson explains hashes). Only its first five characters,CBFDA, are sent to the public Pwned Passwords service, which returns every leaked fingerprint starting with them.greplooks for the rest of our fingerprint in that list.password123appeared in leaks over 2.2 million times, and evencampus1236,676 times. Attackers try such lists first, which is why servers should refuse them at sign-up.
A leaked database is a copy of a site's user table stolen in a break-in and shared online. Brute force tries many guesses against one account, starting with the most common passwords.
The defences sit on both sides. Users need a different password for every site and a second factor such as an OTP. Servers must store passwords so that a stolen table is useless (the next lesson), refuse very common passwords at sign-up, and slow down repeated failed logins (the Common Security Mistakes module builds this for ShopLite).
Injection: when input becomes part of a command
Injection happens when text from a user becomes part of a command or query that the server runs, instead of staying plain data. SQL injection, the best-known kind, can make a login query skip the password check.
The fix is a parameterised query: the query text is fixed, with a ? slot for each value, and the values travel separately. Even a name with a quote mark stays plain data.
Example 2 · A parameterised query keeps input as data
CREATE TABLE reviews ( id INT PRIMARY KEY AUTO_INCREMENT, customer_name VARCHAR(60) NOT NULL, body VARCHAR(200) NOT NULL);
-- The query text is fixed. Each ? is a slot for one value.PREPARE add_review FROM 'INSERT INTO reviews (customer_name, body) VALUES (?, ?)';PREPARE find_reviews FROM 'SELECT id, customer_name, body FROM reviews WHERE customer_name = ?';
-- The values arrive separately, exactly as the user typed them.SET @name = 'Ryan D''Souza', @body = 'Kettle works fine; boils water in 3 minutes';EXECUTE add_review USING @name, @body;EXECUTE find_reviews USING @name;+----+---------------+---------------------------------------------+ | id | customer_name | body | +----+---------------+---------------------------------------------+ | 1 | Ryan D'Souza | Kettle works fine; boils water in 3 minutes | +----+---------------+---------------------------------------------+
What this code does
PREPAREfixes the SQL text first, with?slots.EXECUTE ... USINGsends the values afterwards, so they can never change the shape of the query. Application code does the same with its database library's prepared statements.- The name contains a quote mark and the review contains a semicolon, both of which mean something in SQL. Here they are stored and found as ordinary characters. In a query built by joining strings, the same quote mark would have broken the query, and that broken spot is exactly where injection begins.
The danger comes from a program that builds its SQL by joining fixed text with whatever the user typed. If the text contains characters that mean something in SQL, such as quote marks, the database reads part of the input as SQL, so a login query can skip the password check and a search can return or change whole tables.
Spot the risk in code reviews by one pattern: any query, shell command or file path built by joining strings with user input. Three more defences add safety: allow-lists for things that cannot be parameters (a sort column must be one of a fixed list), a database user with only the permissions the app needs, and error messages that never show SQL.
Find the bug
Find the injection bug
Each snippet is risky. Tap the wrong line, then check the fix.
Find a customer by email.
Round 1 of 3Broken access control: changing an id in the URL
IDOR (insecure direct object reference) is a broken access control mistake: the URL or body contains an id, and the server returns that record without checking whom it belongs to.
Asha asks for orders 1 to 5, then for order 14, from an API that checks only that her token is valid.
Example 3 · Changing the id: an API without an owner check
Runs in: a demo of the ShopLite API with the owner check switched off on purpose
TOKEN=7f3a9c2e5b8d41f0a6c3e9b2d5f8a1c4 # Asha, customer 1for id in 1 2 3 4 5; do curl -s -o /dev/null -w "order $id: %{http_code}\n" http://localhost:8080/api/v1/orders/$id -H "Authorization: Bearer $TOKEN"; donecurl -i http://localhost:8080/api/v1/orders/14 -H "Authorization: Bearer $TOKEN"order 1: 200
order 2: 200
order 3: 200
order 4: 200
order 5: 200
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
Content-Length: 515
{
"id": 14,
"customerId": 2,
"addressId": 3,
"status": "SHIPPED",
"orderedAt": "2026-09-21T21:05:00+05:30",
"items": [
{
"productId": 1,
"sku": "MOB-RN13",
"productName": "Redmi Note 13 5G",
"quantity": 1,
"unitPrice": 17999.00,
"lineTotal": 17999.00
},
{
"productId": 6,
"sku": "ACC-AD141",
"productName": "boAt Airdopes 141",
"quantity": 1,
"unitPrice": 1299.00,
"lineTotal": 1299.00
}
],
"total": 19298.00
}What this code does
- Asha owns orders 1, 3 and 10, yet every id answers 200. The server checks only that the token is valid, so counting upwards walks through every customer's orders.
- Order 14 is Ravi's (
customerId: 2): his purchases, amounts and address id are now visible to Asha. Nothing was "hacked": she changed one number in a URL.
Example 4 · The same requests with an owner check
Runs in: a demo of the secured ShopLite API on localhost:8080 (you build the real one later in this section)
for id in 1 2 3 4 5; do curl -s -o /dev/null -w "order $id: %{http_code}\n" http://localhost:8080/api/v1/orders/$id -H "Authorization: Bearer $TOKEN"; donecurl -i http://localhost:8080/api/v1/orders/14 -H "Authorization: Bearer $TOKEN"order 1: 200
order 2: 404
order 3: 200
order 4: 404
order 5: 404
HTTP/1.1 404 Not Found
Connection: close
Content-Type: application/problem+json
Content-Length: 184
{
"type": "https://shoplite.example/problems/not-found",
"title": "Resource not found",
"status": 404,
"detail": "Order 14 was not found.",
"instance": "/api/v1/orders/14"
}What this code does
- Now only Asha's own orders 1 and 3 answer 200. Orders 2, 4 and 5 belong to other customers, and the answer is the same 404 a missing order gets, so she learns nothing about them.
- An admin token still gets 200 for any order. The Roles & Permissions module writes this owner check into ShopLite's order service.
Login worked perfectly here; authorisation is what is missing. Broken access control is the general name for a user reaching data or actions that should not be theirs, and IDOR is its most common form.
The fix is an owner check on every request that names a record: load the order only if it belongs to the caller (or the caller is an admin), otherwise answer 404. Even better, give customers URLs without ids, like /api/v1/me/orders, where the server takes the customer from the token.
Two attacks through the browser: XSS and CSRF
Cross-site scripting (XSS) happens when text from one user is shown in another user's browser and the browser runs it as code. The fix is output encoding: the browser shows the characters instead of running them.
A customer's product review contains HTML tags. Output encoding turns them into harmless text before the page shows them.
Example 5 · Output encoding: showing a review as text
Review typed by a customer:<b>Best</b> earphones under ₹1500 & "worth it"The same text after HTML output encoding:<b>Best</b> earphones under ₹1500 & "worth it"<b>Best</b> earphones under ₹1500 & "worth it"
What this code does
- Each special character is replaced by its HTML code:
<becomes<,>becomes>,&becomes&and"becomes". The browser turns the codes back into characters for display, but never treats them as tags. - So the review shows the angle brackets literally instead of making "Best" bold. A review containing a script tag is shown the same way: as harmless text, not as code that runs.
A review that contains an HTML <script> tag, shown as raw HTML, would run inside every reader's page, where it can read their data or act as them. Output encoding replaces <, >, &, " and ' with their HTML codes before untrusted text goes into a page. Modern page frameworks do this by default; trouble starts when code switches it off.
Cross-site request forgery (CSRF) abuses the fact that browsers attach cookies automatically. If a shop keeps your login in a cookie, a page on another site can make your browser send a request to the shop, and the cookie rides along. In steps:
- Asha logs in to a shop that keeps her login in a cookie.
- In another tab she opens a page offering "free fest passes".
- That page quietly submits a hidden form to the shop, for example "change my email".
- Her browser attaches the shop's cookie to that request by itself.
- The shop sees a valid login cookie and makes the change.
Thinking like an attacker
A threat is anything that could go wrong on purpose. The simplest way to find threats is to list every input of an endpoint and ask of each one: what if this is hostile?
Inputs are more than form fields: the URL path, query parameters, headers, cookies, the body and even uploaded file names all come from the client. For POST /api/v1/orders, the inputs and questions look like this:
customerIdin the body: what if it is someone else's id? Take the customer from the token instead.addressId: what if it is another customer's address? Check it belongs to the caller (ShopLite answers 422).quantity: what if it is 0, negative or 1,000,000? Validate 1–10 (400).- The
Authorizationheader: what if it is missing, expired or changed? 401 before anything else runs.
Next step
Check your understanding
12 quick questions