Threat Modeling: How to Think Like an Attacker Before Building an API

Imagine you're developing a food delivery application. Users can register, log in, browse restaurants, place orders, make payments, and track their deliveries.
Everything works perfectly. Your APIs return the expected responses, authentication is implemented, and the application passes functional testing.
But have you ever asked yourself:
- What if one user accesses another user's order?
- What if someone discovers an API that developers forgot to document?
- What if a third-party API exposes sensitive customer information?
- What if an attacker manipulates the business logic to receive something without paying?
Your application works as expected. But is it secure against someone who doesn't intend to use it as expected?
This is where Threat Modeling comes into the picture.
Recently, while learning API security, I came across the concept of Threat Modeling.
I realized that security shouldn't begin only when someone starts penetration testing an application. It should begin much earlier, when we're designing and developing it.
In this article, I'll explain what threat modeling means, why it matters, how it works, and how we can apply it when developing applications that rely on APIs.
1. What Exactly Is Threat Modeling?
Threat Modeling is the process of identifying what needs to be protected, understanding how an attacker could target it, identifying possible weaknesses, evaluating the associated risks, and deciding how to reduce those risks.
Think of it like securing your house.Before installing security cameras, you would probably ask:
- What valuables do I have?
- Where are the possible entry points?
- Which doors and windows are vulnerable?
- How could someone enter?
- What would happen if they got inside?
- What security measures should I implement?
You wouldn't wait for someone to break into your house before thinking about security.The same principle applies to applications.
Before an application goes live, developers and security teams should understand what attackers might target and how the application could be abused.That's the basic idea behind threat modeling.
2. Why Do We Need Threat Modeling?
One common mistake in application development is focusing only on whether a feature works.
For example: A developer creates a login API.
POST /api/login
The API accepts credentials and returns an authentication token when the credentials are correct. From a functional perspective, everything works. But security requires additional questions:
- Can an attacker perform unlimited login attempts?
- Are passwords stored securely?
- Can authentication tokens be stolen or reused?
- Does the API expose sensitive information?
- Can one user access another user's resources?
These questions go beyond functionality. An application can work perfectly while still containing security weaknesses. Threat modeling helps identify potential security problems before they become real vulnerabilities in a deployed application.
It also helps teams prioritize security efforts instead of trying to protect everything without understanding the actual risks.
3. Start With One Important Question: What Do Attackers Want?
Before identifying vulnerabilities, we need to understand what we're protecting. Not every application stores the same type of information, and attackers don't target every application for the same reasons.
Consider some examples:
| Asset | Why might an attacker target it? |
|---|---|
| Personal information (PII) | Identity theft, phishing, data resale |
| Banking information | Financial fraud and theft |
| Customer accounts | Account takeover and impersonation |
| Corporate information | Espionage and intellectual property theft |
| Payment systems | Financial manipulation |
| Critical infrastructure | Disruption and operational damage |
Let's take an e-commerce application. It might contain:
- Customer names and addresses
- Email addresses and phone numbers
- Order history
- Payment-related information
- Account credentials
- Business and inventory information
Now imagine an attacker gains unauthorized access to the customer database. The impact could be significant.
They might obtain personal information, conduct phishing campaigns, access customer accounts, or exploit other systems using the exposed information. This is why identifying valuable assets is one of the first steps in threat modeling.
You cannot properly assess what needs protection until you understand what you have.
4. Understand Your Attack Surface
Now that we know what we're protecting, the next question is: Where could an attacker interact with our application?
This is where the attack surface comes in. An attack surface includes the different entry points, interfaces, components, and workflows that an attacker might interact with. For an API-based application, this includes much more than just the API endpoints.
Consider a simple food delivery application:
Food Delivery Application
|
+------------+------------+
| | |
Users APIs Third Parties
| | |
Login /api/login Payment API
Register /api/orders SMS Service
Profile /api/users Maps API
Orders /api/payment Email Service
Each component introduces potential security considerations.
For example:
User functionality
- Registration
- Authentication
- Password recovery
- Profile management
API functionality
- User APIs
- Order APIs
- Payment APIs
- Administrative APIs
Third-party integrations
- Payment gateways
- Email services
- SMS providers
- External authentication services
Internal services
- Microservices communicating with each other
- Internal APIs
- Database access
- Background processing services
The important point is that attackers don't necessarily have to attack the main application directly. They might target an exposed API, abuse a business workflow, or exploit weaknesses in a third-party integration.
A question worth asking
Do we actually know every API our application is using?
Sometimes organizations have undocumented, forgotten, or unknown APIs. These are often discussed as rogue or zombie APIs. An API might remain accessible even after its original purpose has disappeared or its developers have stopped maintaining it. If that API still exposes sensitive information or functionality, it can become an attractive target.
An API doesn't stop being an attack surface simply because nobody remembers it exists.
5. Understand How APIs Fit Into Your Application
Today, APIs are fundamental to how modern applications communicate. A website might use APIs to retrieve user information, process payments, and display order details. A mobile application might communicate with the same backend APIs. Microservices might use internal APIs to exchange information with one another. Organizations may also expose APIs to business partners, customers, and external developers. This means an application can have several different API communication paths.
For example:
Mobile Application
|
v
Public APIs
|
+---------+---------+
| | |
User API Order API Payment API
| | |
+---------+---------+
|
Backend Services
|
Database
Now consider the security questions:
- Which APIs access sensitive information?
- Which APIs can modify customer data?
- Which APIs communicate with external services?
- Are internal APIs properly protected?
- Can one service access information belonging to another service?
- Are authorization checks performed consistently?
These questions help us understand where APIs fit into the application's overall attack surface. It is not enough to know that an API exists. We need to understand what it does, what information it handles, and what could happen if someone misuses it.
6. Identify Potential Threats and Vulnerabilities
Once we understand our assets and attack surface, the next step is identifying what could go wrong. Let's continue with our food delivery application.
Suppose we have the following endpoint:
GET /api/orders/1001
It returns the details of an order. A legitimate user should only be able to access their own orders.
But what happens if someone changes the order ID?
GET /api/orders/1002
If the API returns another customer's order without checking ownership, we may have an Insecure Direct Object Reference (IDOR) vulnerability, commonly associated with Broken Object Level Authorization (BOLA).
Now consider another example. An application allows users to register and log in. But its password recovery API does not properly restrict repeated requests.
An attacker might abuse the endpoint to generate excessive requests or attempt account-related attacks.
Or consider a payment workflow:
- A user places an order.
- The application sends a payment request.
- The payment provider processes the transaction.
- The application updates the order status.
What if the application trusts a client-controlled parameter instead of verifying the payment result with the payment provider?
An attacker might manipulate the workflow and obtain an order without completing the payment. These examples demonstrate an important idea:
A security problem doesn't always exist in the API endpoint itself. Sometimes the weakness exists in how the application processes the entire business workflow.
Threat modeling encourages us to examine both technical vulnerabilities and business logic weaknesses.
7. Understand Likelihood and Impact
Identifying a possible threat is only part of the process. We also need to understand how likely it is to happen and what the consequences would be. Consider two hypothetical findings.
Scenario A: An application exposes an API that returns publicly available weather information. An attacker discovers the endpoint and accesses it. The impact might be relatively low.
Scenario B: An application exposes an undocumented API that provides access to millions of customer records. An attacker discovers the endpoint and successfully exploits it. The impact could be extremely serious.
Both involve exposed APIs, but their risks are very different. This is why threat modeling considers multiple dimensions. A simplified way to understand risk is:
Risk = Threat × Vulnerability × Likelihood × Impact
Here is what each term means:
| Factor | Meaning |
|---|---|
| Threat | Who or what could cause harm? |
| Vulnerability | What weakness could be exploited? |
| Likelihood | How probable is successful exploitation? |
| Impact | What happens if exploitation succeeds? |
This isn't necessarily a universal numerical formula. Organizations may use different scoring methods and risk matrices. The important part is evaluating the factors together.
A vulnerability with a low likelihood and limited impact may receive a lower priority than a weakness that could expose sensitive customer information across an entire application.
8. Let's Perform a Simple Threat Modeling Exercise
Now let's put everything together. Imagine we're developing an online shopping application.
It contains an API that allows users to view their order history.
GET /api/orders/{order_id}
Let's examine it through a basic threat modeling exercise.
Step 1: Identify the asset
Customer order information, including personal details, delivery addresses, and purchase history.
Step 2: Identify the attack surface
The order retrieval API.
Step 3: Identify a possible threat
An authenticated attacker attempts to access another customer's order.
Step 4: Identify the vulnerability
The API checks whether the user is authenticated but fails to verify whether the requested order belongs to that user.
Step 5: Evaluate likelihood
An attacker may be able to manipulate the order ID and retrieve another user's information. The likelihood depends on factors such as identifier predictability, authorization checks, and exposure.
Step 6: Evaluate impact
Unauthorized disclosure of customer information and purchase history.
Step 7: Determine mitigation
- Implement server-side object-level authorization.
- Verify resource ownership on every request.
- Never rely on client-provided identifiers as proof of authorization.
- Add appropriate security tests.
- Monitor suspicious access patterns.
We can summarize the exercise:
| Element | Assessment |
|---|---|
| Asset | Customer order information |
| Attack surface | Order API |
| Threat | Authenticated malicious user |
| Vulnerability | Missing object-level authorization |
| Likelihood | Depends on endpoint exposure and controls |
| Impact | Unauthorized customer data disclosure |
| Mitigation | Server-side ownership validation |
Notice something? We identified the potential security problem before writing the API implementation. That's the real value of threat modeling.
9. Threat Modeling Should Happen Before and During Development
One thing I want to emphasize is that threat modeling isn't a one-time activity that happens only at the beginning of a project. Applications change. Developers introduce new endpoints, add integrations, modify authentication mechanisms, and introduce new business workflows. Every significant change can introduce new security risks.
For example:
During application design:
- Identify sensitive assets.
- Map application components.
- Understand API communication.
- Identify trust boundaries.
- Think about potential threats.
During development:
- Review authentication and authorization.
- Validate input handling.
- Examine business logic.
- Implement appropriate security controls.
During testing:
- Validate the identified threats.
- Test authorization boundaries.
- Examine API behaviour.
- Verify that security controls work as expected.
After deployment:
- Monitor exposed APIs.
- Review changes in the attack surface.
- Investigate suspicious activity.
- Reassess risks when functionality changes.
Threat modeling and penetration testing complement each other. Threat modeling helps us anticipate what could go wrong, while security testing helps validate whether those weaknesses actually exist.
10. What Should Developers and API Security Testers Take Away?
For developers, threat modeling provides an opportunity to consider security before implementation. Instead of asking only: "Does this API work?" Start asking:
- Who should be allowed to access this API?
- What information does it expose?
- What happens if someone changes the request parameters?
- Can users access resources belonging to others?
- Can the business workflow be manipulated?
- What happens if a third-party service is compromised?
- Are there undocumented APIs that nobody is monitoring?
For penetration testers and API security professionals, this process provides a useful way to approach an application. Instead of randomly testing endpoints, first understand the application's functionality, its sensitive assets, and how its APIs interact. Then identify where an attacker could potentially abuse those interactions. That makes security testing more structured and meaningful.
Final Thoughts
Before learning about threat modeling, I mostly associated application security with finding vulnerabilities during security testing. But this concept helped me understand another important side of cybersecurity:
Security isn't only about finding weaknesses after an application is built. It's also about thinking ahead and identifying how those weaknesses could be introduced in the first place.
When developing an API-based application, understanding the attack surface, business workflows, sensitive information, third-party integrations, likelihood, and potential impact can make a significant difference.
A simple question can change how we approach development:
"If I were an attacker, how could I misuse this functionality?"
Start asking that question before writing the code, not just after deploying the application.
That's the mindset I'm trying to develop as I continue learning API security and penetration testing.
More API security concepts and hands-on learning notes coming soon.
References & Further Learning
About the author
- PJPT-certified penetration tester interested in offensive security, VAPT, and red teaming. Sharing practical insights, security research, lab writeups, and hands-on projects. ShadowSensei | Offensive Security
Comments
Join the discussionNo comments yet. Be the first to comment.



