Thinking Like an Attacker
Chapters and transcript
Thinking Like an Attacker Most security bugs are not clever tricks. They are normal code, written for normal users. Here a customer fills in a form, and the server answers. But an attacker sends something nobody planned for. In this lesson, you will learn to see your own code the way an attacker sees it: untrusted input, trust boundaries, the OWASP Top 10 and a quick threat model.
Why developers need this When you build a feature, you usually think about the happy path. The user fills in the form correctly, clicks the button and gets a result. An attacker thinks about every other path. What if the field is empty? What if it holds ten thousand characters, or code? What if the user changes the ID in the address bar? Fixing a bug while you write the code costs minutes. Fixing it after a breach can cost data, money and trust.
Never trust input Input means any data that comes into your program from outside your own code. It is more than form fields. It includes the URL path and query string, form fields and JSON bodies, HTTP headers and cookies, and uploaded files, even their file names. Data from other systems counts too, such as payment webhooks, third-party APIs and messages from a mobile app.
Never trust input Here is why. This checkout page has a hidden field that holds the price. But the browser belongs to the user, not to you. With the browser DevTools, anyone can change that field before the request is sent. Here the price becomes one rupee, and a careless server accepts it. Hidden fields, drop-down lists and values set by your own JavaScript can all be changed.
Never trust input An attacker does not even need your page. With a command-line tool such as curl, anyone can build a request from scratch. The shop form only allows a quantity from one to ninety-nine, checked in JavaScript. This request skips the page and sends minus five. The server never checked the value again, so the cart total goes below zero.
Never trust input So here is the golden rule. All input can be controlled by an attacker until your server has checked it. Client-side validation, the checks in the browser, is only for user comfort. It gives quick feedback, and that is good. But every important check must happen again on the server, where the user cannot change your code.
Trust boundaries A trust boundary is a place where data moves from something you do not control into something you do. Data crosses from the browser into your API server. Your server sends queries to the database, and commands to the operating system shell. A payment provider sends a webhook to your order system. Draw your app as boxes and arrows. The arrows that cross a boundary are where most bugs live.
The OWASP Top 10 OWASP, the Open Worldwide Application Security Project, is a non-profit community that publishes free security guidance. Its best-known document is the OWASP Top 10, a list of the most important risks to web applications. It is updated every few years. In recent editions, broken access control has been number one. Treat it as a map of where real apps go wrong, not a checklist to memorise.
The OWASP Top 10 This path follows the same map. Injection, including SQL, commands and cross-site scripting, is lesson two. Authentication, sessions and password storage are lesson three. Broken access control is lesson four. Misconfiguration, outdated components, leaked secrets and poor logging are lesson five. When you are unsure how to do something safely, the free OWASP Cheat Sheet Series gives short, practical advice.
Principles A few principles guide almost every decision. Least privilege: every user, service and database account gets only the access it needs. Defence in depth: use several layers, so if one fails, another protects you. Secure defaults: access is denied unless a rule allows it. Fail securely: an error in a permission check must never mean let them in. And keep it simple: use well-tested libraries for passwords, sessions and encryption.
Principles See two principles working together. An attacker finds a bug in the web app and tries to delete the orders table. But the app connects with a limited database account. It can only read and write orders, so the database refuses. That is least privilege. One layer failed, and another still protected the data. That is defence in depth.
Threat modelling Threat modelling means thinking about what could go wrong before you write the code. A useful version takes ten minutes and four questions. What are we working on? Draw the feature as boxes and arrows. What can go wrong? Walk along each arrow and ask how it could be abused. What are we going to do about it? Choose a fix for each real risk. Did we do a good job? Check the fixes in code review and testing.
Threat modelling For step two, the STRIDE model gives six kinds of threat. Spoofing: can someone pretend to be another user? Tampering: can someone change data, such as a price? Repudiation: can someone deny an action because we keep no record? Information disclosure: can someone read data that is not theirs? Denial of service: can someone make the feature unavailable? Elevation of privilege: can a normal user gain admin rights?
Threat modelling Let us try it. At chaicorp.example, a customer pays with a mobile wallet, such as JazzCash or Easypaisa. The wallet sends a webhook saying payment received. Spoofing: could anyone send a fake webhook? The task is to verify the webhook signature. Tampering: could the customer change the amount? The task is to read the price from our database, never from the request. Repudiation: we log every payment event.
Quick check Quick check. Your team is threat modelling a password reset page. Someone asks: could one person send thousands of requests and make the page unusable for everyone? Which STRIDE threat is that? Choose one.
Key takeaways Let us put it together. All input is untrusted until your server checks it. Browser checks are for comfort, so repeat them on the server. Check data wherever it crosses a trust boundary. Use the OWASP Top 10 as a map. And before you build, ask the four questions and STRIDE. Try it on a feature you have built, then read the lesson below and take the quiz.
Most security bugs are not clever tricks. They are normal code written by developers who only imagined normal users. In this path you will learn to write code that still behaves safely when someone is actively trying to break it, starting with the right way of thinking.
Why developers need this
When you build a feature, you usually think about the happy path: the user fills in the form correctly, clicks the button and gets a result. An attacker thinks about every other path. What if the field is empty? What if it contains 10,000 characters? What if it contains code? What if the user changes the ID in the address bar?
If you have studied the Web Hacking path on this site, you have seen these attacks from the attacker's side. This path is the other half. Here you learn how to prevent those bugs in your own code, and how to make them less harmful when one slips through.
Fixing a bug while you write the code costs a few minutes. Fixing it after a breach can cost customer data, money, and trust that is very hard to win back. A small company in Lahore or Karachi that loses its customers' phone numbers and CNIC numbers may not get a second chance.
Never trust input
Input is any data that comes into your program from outside your own code. Beginners usually think only of form fields, but input includes much more:
- URL paths and query strings, such as
/orders?id=55 - Form fields and JSON bodies in API requests
- HTTP headers, such as
User-AgentandReferer - Cookies
- Uploaded files, including their file names
- Data from other systems: payment callbacks (often called webhooks), third-party APIs, and messages from a mobile app
The golden rule is simple: all input can be controlled by an attacker until your server has checked it. This is true even for hidden form fields, drop-down lists and values set by your own JavaScript. The browser belongs to the user, not to you. Tools like the browser's DevTools let anyone change a request before it is sent, or build a request from scratch.
This means client-side validation (checks in the browser) is only for user comfort. It gives quick feedback, which is good. But every important check must happen again on the server, where the user cannot change your code.
Trust boundaries
A trust boundary is a place where data moves from something you do not control into something you do. Every time data crosses a boundary, you should check it.
Common trust boundaries in a web application:
| From (less trusted) | To (more trusted) |
|---|---|
| The user's browser or mobile app | Your API server |
| Your API server | Your database |
| Your server | The operating system shell |
| A payment provider's webhook | Your order system |
| An uploaded file | Your file storage |
A useful habit is to draw your application as a few boxes with arrows between them, then mark every arrow that crosses a boundary. Those arrows are where most bugs live. For example, the arrow from your server to the database is where SQL injection happens if you build queries by joining strings. You will fix that in the next lesson.
The OWASP Top 10 as a map
OWASP (the Open Worldwide Application Security Project) is a non-profit community that publishes free security guidance. Its best-known document is the OWASP Top 10, a list of the most important risks to web applications. It is updated every few years, and the exact names change a little between editions. Broken Access Control has been at number one in recent editions.
Do not treat the Top 10 as a checklist to memorise. Treat it as a map that shows where real applications most often go wrong. This path follows the same map:
| Risk area | Where you learn to prevent it |
|---|---|
| Injection (SQL, commands, cross-site scripting) | Lesson 2 |
| Authentication and session failures, weak password storage | Lesson 3 |
| Broken access control | Lesson 4 |
| Security misconfiguration, outdated components, leaked secrets, poor logging | Lesson 5 |
OWASP also publishes the free Cheat Sheet Series, which gives short, practical advice for each topic. It is a good place to look when you are unsure how to do something safely.
Principles that prevent whole classes of bugs
A few general principles will guide almost every decision in this path:
- Least privilege. Every user, service and database account gets only the access it needs. If your web app only reads and writes orders, its database account should not be able to drop tables.
- Defence in depth. Use several layers. If one layer fails, another still protects you. For example, use parameterised queries and a limited database account.
- Secure defaults. New accounts, new features and new settings should start in the safe state. Access is denied unless a rule allows it.
- Fail securely. When something goes wrong, the program should block the action, not allow it. An error in a permission check must never mean "let them in".
- Keep it simple. Complex security code is hard to review. Use well-tested libraries for passwords, sessions and encryption instead of writing your own.
Threat modelling in 10 minutes
Threat modelling means thinking about what could go wrong before you write the code. It sounds formal, but a useful version takes ten minutes with a whiteboard or a notebook. A popular approach uses four questions:
- What are we working on? Draw the feature: users, servers, databases and the arrows between them.
- What can go wrong? Walk along each arrow and ask how it could be abused.
- What are we going to do about it? Choose a fix for each real risk.
- Did we do a good job? Check the fixes in code review and testing.
For step 2, the STRIDE model gives you six kinds of threat to ask about:
| Letter | Threat | Example question |
|---|---|---|
| S | Spoofing | Can someone pretend to be another user? |
| T | Tampering | Can someone change data they should not, such as a price? |
| R | Repudiation | Can someone deny an action because we keep no record of it? |
| I | Information disclosure | Can someone read data that is not theirs? |
| D | Denial of service | Can someone make the feature unavailable? |
| E | Elevation of privilege | Can a normal user gain admin rights? |
Imagine a feature at chaicorp.example where customers pay through a mobile wallet such as JazzCash or Easypaisa, and the wallet sends a webhook to say "payment received". Spoofing: can anyone send a fake webhook? Tampering: can the customer change the amount in the request? Repudiation: do we log every payment event? Each answer becomes a task, such as "verify the webhook signature" or "read the price from our database, never from the request".
Try it yourself
Pick one feature in a project you have built, even a small practice app. Then:
- Draw it as boxes and arrows on paper.
- Circle every arrow that crosses a trust boundary.
- For each circled arrow, list every piece of input that travels along it.
- Ask the six STRIDE questions and write down at least three possible problems.
- For each problem, write one line saying how you would prevent it.
Keep this page. As you finish each lesson in this path, come back and add the fixes you have learned.
Check your understanding
Answer every question correctly to complete this lesson. If you miss one, try the quiz again.
1. Your sign-up form checks in JavaScript that the age field is a number between 18 and 100. Is that enough?
2. What is a trust boundary?
3. In the STRIDE model, what does the letter E stand for?
Locked: answer every quiz question above correctly to complete this lesson.
Spotted a mistake or something unclear? Report a mistake