Ethical Hacking and Penetration Testing

Chapter 9
Web Application and API Penetration Testing

Post Graduate Program in Cyber Security
Ethical Hacking and Penetration Testing   •   Post Graduate

9. Web Application and API Penetration Testing

Introduction and Context

Web applications and application programming interfaces (APIs) are now central to banking, healthcare, education, government, retail, and internal enterprise operations. They process identity data, financial transactions, business records, and decisions that were once handled through physical or desktop systems. Their exposure is also distinctive: a web application may be reachable by millions of users, while an API may provide direct machine-to-machine access to the same functions without the visual controls present in a browser.

Penetration testing in this domain is therefore not limited to discovering technical defects. It requires an understanding of application architecture, trust boundaries, identity and access models, data flows, business processes, client-server interaction, cryptographic controls, and third-party dependencies. A useful test asks not only whether an input can be manipulated, but also what the application permits an attacker to accomplish and whether that result affects confidentiality, integrity, availability, safety, or regulatory obligations.

This chapter presents a structured approach to assessing modern web applications and APIs. It uses the OWASP Top 10 as a risk-oriented reference and the OWASP API Security Top 10 as a complementary API-focused model. These models are not substitutes for professional judgment. They help organize coverage, communicate risk, and connect technical findings with organizational consequences.

Learning Objectives

After studying this chapter, students should be able to:

1. Explain the architecture, attack surface, and trust relationships of web applications and APIs.

2. Plan and conduct an authorized application penetration test using a risk-based methodology.

3. Assess authentication, authorization, session management, input handling, and business logic.

4. Evaluate client-side behavior, cryptographic implementation, API-specific risks, and software supply-chain exposure.

5. Interpret evidence, distinguish vulnerabilities from configuration issues, and communicate business risk.

6. Recommend controls that are technically effective, testable, and aligned with governance and professional ethics.

From Request to Business Action: The Application Attack Surface

A modern application is best understood as a chain of interacting components rather than a collection of pages. A typical transaction may pass through a browser or mobile client, a content delivery network, a web server, an API gateway, authentication services, application services, databases, message queues, cloud storage, and external providers. Every transition creates a trust boundary and a potential point at which assumptions may differ.

The visible interface is only one part of the attack surface. Important elements include URL routes, HTTP methods, headers, cookies, tokens, hidden fields, file-upload functions, GraphQL queries, WebSocket messages, administrative interfaces, error responses, background jobs, and undocumented API versions. A tester should also consider infrastructure supporting the application: source repositories, build pipelines, package registries, secrets stores, container images, and deployment configurations.

HTTP is stateless, but applications create continuity through cookies, server-side sessions, bearer tokens, refresh tokens, and device identifiers. The security of a transaction consequently depends on both the request and the context assigned to it. A request that is safe for one user may be unauthorized for another. This is why testing must examine relationships among identities, objects, roles, tenants, and operations.

A Risk-Based Testing Model

A professional engagement begins with authorization and scope. The rules of engagement should identify approved hosts, applications, accounts, testing windows, prohibited actions, data-handling requirements, emergency contacts, and procedures for reporting critical findings. Testing without explicit authorization is not penetration testing; it is unauthorized activity.

Reconnaissance then establishes the application inventory. Testers identify domains, subdomains, API versions, documentation, authentication mechanisms, user roles, input locations, file-handling functions, and integrations. Passive collection is normally preferred initially because it reduces operational risk. Active discovery may follow within agreed limits.

The next stage is mapping. A useful application map records each endpoint, method, parameter, authentication requirement, role, object type, state transition, and expected response. Separate maps should be maintained for unauthenticated, ordinary-user, privileged-user, and service-account behavior. API specifications such as OpenAPI documents are valuable, but they must be compared with observed traffic because documentation is frequently incomplete or outdated.

Testing should combine automated and manual methods. Automated tools are effective for repetitive requests, parameter discovery, common injection patterns, and evidence collection. Manual analysis is essential for authorization, business logic, multi-step workflows, race conditions, state changes, and interpreting unusual responses. A scanner result is a hypothesis, not a validated finding.

Findings should be confirmed safely, documented with reproducible evidence, and rated according to exploitability and impact. CVSS can support technical severity estimation, but organizational context matters. A flaw exposing a low-value test record is different from the same flaw exposing health records or permitting a payment transfer. A strong report connects the technical condition to an affected asset, plausible attack path, business consequence, evidence, remediation, and retest criteria.

Authentication, Identity, and Session Security

Authentication establishes who or what is making a request; authorization determines what that identity may do. Confusing these functions is a common source of serious defects. A valid login does not imply access to every object or operation.

Authentication testing examines password policy, credential recovery, account enumeration, multi-factor authentication, rate limiting, credential stuffing resistance, authentication-boundary changes, and federated identity flows. Password-reset processes deserve particular attention: tokens should be unpredictable, single-use, time-limited, and invalidated after a successful reset. Security questions based on public information are weak substitutes for strong recovery controls.

Multi-factor authentication should be assessed as a complete process. A tester examines whether sensitive actions require reauthentication, whether a second factor can be bypassed through alternate routes, whether enrollment and replacement are protected, and whether recovery procedures reduce the assurance of the primary mechanism. Possession of a session cookie should not automatically permit changes to authentication factors without appropriate verification.

Session testing includes token entropy, expiration, rotation, revocation, cookie attributes, concurrent sessions, logout behavior, and session fixation. Cookies should normally use Secure, HttpOnly, and appropriate SameSite attributes. These attributes reduce particular classes of exposure but do not replace server-side authorization. Tokens stored in browser-accessible locations may be exposed if a cross-site scripting flaw exists, and bearer tokens require careful transport and lifetime management.

For APIs, testers examine access tokens, refresh-token rotation, audience and issuer validation, scope enforcement, token leakage in URLs or logs, and differences between gateway and service-level validation. JWT use does not make an API secure: signature validation, algorithm restrictions, key management, claims validation, and authorization decisions remain necessary.

Authorization and Object-Level Control

Authorization failures are among the most consequential application weaknesses. Object-level authorization asks whether a user may access a particular record; function-level authorization asks whether the user may invoke a particular operation. A system can enforce one and neglect the other.

Testing should use multiple accounts and compare requests across users, roles, and tenants. Changing an identifier in a request is only the simplest test. The tester should examine nested objects, bulk operations, export functions, search filters, alternate HTTP methods, cached responses, and background actions. A user who may view an invoice may not be permitted to approve, refund, delete, or export it.

Multi-tenant applications require strict tenant isolation at every layer. Filtering records in the user interface is not sufficient. The server, database queries, storage paths, analytics functions, and asynchronous jobs must all enforce tenant boundaries. Role-based access control should be tested for privilege inheritance, default roles, dormant accounts, administrative endpoints, and indirect access through integrations.

A useful test principle is to treat every client-supplied identity, role, tenant, object identifier, and permission value as untrusted. The server should derive authorization from a trusted identity context and enforce it consistently at the point where the resource or action is used.

Input Handling and Output Safety

Input validation is not a single control but a family of controls selected according to the interpreter receiving the data. SQL queries, operating-system commands, HTML, JavaScript, templates, LDAP filters, XML parsers, file systems, and expression engines each have distinct failure modes.

Parameterized queries are preferred for database access. Allow-lists and constrained formats are useful for values such as dates, identifiers, and enumerations. Validation should occur on the server and should be based on canonicalized data. Client-side validation improves usability but cannot be treated as a security boundary.

Cross-site scripting may be reflected, stored, or DOM-based. Testing considers where data enters, how it is transformed, the output context, and whether an appropriate encoding strategy is used. Content Security Policy can reduce impact, but it is a defense-in-depth measure rather than a substitute for correct output encoding.

File uploads require controls over type, size, content, storage location, naming, processing libraries, and retrieval behavior. A file that is harmless at upload may become dangerous when parsed, previewed, transformed, or served from an executable location. Path traversal testing examines canonicalization and server-side path construction, while server-side request forgery testing examines whether user-controlled URLs can reach internal services or cloud metadata endpoints. Such tests must be carefully constrained to avoid affecting production systems.

Error handling is also part of input security. Detailed stack traces, database errors, framework versions, and internal hostnames can support attack planning. Applications should log diagnostic detail securely while returning clients a controlled error response.

Business Logic and State Transitions

Technical controls may be correctly implemented while the business process remains exploitable. Business-logic testing examines whether an application permits an action that is inconsistent with its intended rules. Examples include applying a discount repeatedly, approving one’s own request, submitting a step out of sequence, changing a price after authorization, redeeming a single-use benefit multiple times, or bypassing a required verification stage through an alternate endpoint.

The tester first models the legitimate workflow and identifies invariants: what must always be true? Examples include “a refund cannot exceed the captured payment,” “an employee cannot approve their own expense,” or “a completed identity check cannot be silently replaced.” Requests are then replayed, reordered, duplicated, altered, or executed concurrently within the rules of engagement.

Race conditions deserve attention in inventory, payments, coupon redemption, account recovery, and quota systems. A control that works for one request may fail when two requests are processed before the state is committed. Effective defenses include atomic transactions, idempotency keys, server-side state validation, and appropriate locking—not merely interface restrictions.

APIs: Distinctive Risks and Testing Considerations

APIs expose functions and data structures directly. The OWASP API Security Top 10 emphasizes risks such as broken object-level authorization, broken authentication, excessive data exposure, lack of resource limits, broken function-level authorization, unrestricted access to sensitive business flows, server-side request forgery, security misconfiguration, improper inventory management, and unsafe consumption of APIs.

Excessive data exposure occurs when an endpoint returns more fields than the client needs and relies on the client to hide them. Mass assignment occurs when client-controlled fields are automatically bound to server-side objects, allowing unintended attributes such as approval status or account ownership to be modified. Both problems require explicit server-side field selection and update rules.

Resource-consumption testing examines pagination, query complexity, upload size, batch requests, expensive filters, and concurrent connections. Rate limits should be appropriate to the identity, operation, and business risk. A generic limit may protect infrastructure while failing to protect a high-value function such as password recovery or voucher redemption.

API inventory management includes versioning, decommissioned endpoints, shadow APIs, test environments, debug routes, and undocumented administrative functions. Every endpoint should have an owner, authentication expectation, data classification, lifecycle status, and monitoring arrangement. GraphQL requires attention to introspection exposure, resolver authorization, query depth, aliases, and field-level data access. WebSockets require authentication, authorization, origin handling, message validation, and connection-lifetime controls.

Client-Side Security, Cryptography, and Supply Chain

Client-side code is untrusted because users can inspect and modify it. Nevertheless, it can contain sensitive information, reveal undocumented routes, create unsafe DOM operations, or implement flawed security decisions. Testing reviews JavaScript bundles, source maps, browser storage, postMessage use, cross-origin policies, dependency versions, service workers, and client-side routing. A client may guide a workflow, but the server must enforce the security decision.

Cryptographic assessment concerns purpose, configuration, key lifecycle, and implementation—not simply whether encryption appears somewhere. Sensitive data should be protected in transit and, where appropriate, at rest. Testers examine TLS configuration, certificate validation, password hashing, random-number generation, token signing, key storage, rotation, backup exposure, and use of deprecated algorithms. Encryption cannot compensate for broken authorization: an authorized application process can still disclose decrypted data improperly.

The software supply chain includes open-source packages, commercial components, build tools, container images, CI/CD systems, and deployment manifests. Risks include vulnerable dependencies, malicious or typosquatted packages, compromised build processes, leaked secrets, unreviewed transitive dependencies, and unsigned artifacts. Assessment may include software composition analysis, secret scanning, lock-file review, provenance verification, dependency update governance, and examination of build permissions. Tools produce useful signals, but teams must prioritize exploitable paths rather than treating every advisory as an equivalent emergency.

A Fictional Applied Case: Multi-Tenant Expense Platform

Background

A fictional organization, Meridian Services, operates a cloud expense platform used by employees, managers, finance staff, and external auditors. The platform includes a web client and JSON APIs. It supports receipt uploads, approval workflows, reimbursements, and reporting across several corporate tenants.

Situation

An authorized assessment identifies an ordinary employee account and a manager account. The application appears to enforce role restrictions through the interface. API documentation lists endpoints for expenses, approvals, reports, and user profiles, but an older version remains available.

Analysis

The tester maps requests made by each role and compares object identifiers across accounts. An expense endpoint correctly denies access to another tenant’s record, but the report-export endpoint accepts a report identifier without performing the same tenant check. The export contains employee names, bank-account suffixes, and reimbursement values. Separately, the expense-update endpoint accepts a client-supplied “approvalStatus” field. The interface does not display this field to employees, but the API updates it because of automatic object binding.

The assessment also finds that password-reset tokens remain valid after a successful reset and that a forgotten legacy API version does not enforce multi-factor authentication for finance users. A dependency scanner reports several packages with known issues, but manual review shows that only one vulnerable package is reachable through the server-side document-preview function.

Approach and Solution

The tester avoids downloading unrelated tenant data and uses two controlled test tenants with synthetic records. Evidence is captured through request and response pairs, role comparisons, timestamps, and minimal sample output. Findings are prioritized by exploitability and business effect. The organization removes the legacy endpoint, implements centralized tenant and function authorization, uses explicit update allow-lists, invalidates reset tokens, and requires equivalent authentication assurance across API versions. It also isolates document preview, updates the reachable dependency, and introduces dependency inventory and build review controls.

Outcome

A retest confirms that cross-tenant exports are denied, employee requests cannot alter approval state, reset tokens are single-use, and the legacy route is unavailable. The organization additionally adds authorization tests to its CI pipeline and assigns ownership for API inventory.

Learning Points

The most serious exposure did not arise from a spectacular exploit. It resulted from inconsistent controls across endpoints, implicit data binding, and lifecycle gaps. The case demonstrates why testing must compare roles and versions, follow data beyond the interface, use safe synthetic evidence, and connect technical findings to business processes.

Challenges, Ethics, and Professional Judgment

Application testing is difficult because functionality changes rapidly, documentation is incomplete, and a flaw may depend on a precise sequence of actions. Automated tools may generate false positives, miss authorization failures, or cause unintended state changes. Production testing introduces risks to availability, privacy, and data integrity. Testers should prefer non-destructive validation, synthetic accounts, rate limits, and explicit approval for high-impact techniques.

Privacy obligations apply to captured requests, credentials, personal data, and exported evidence. Reports should minimize sensitive content, encrypt storage and transmission, restrict access, and define retention and destruction procedures. Findings must be disclosed to the authorized client through agreed channels. Testers should not access data merely because a technical control permits it.

Organizations should integrate penetration testing with secure software development rather than treating it as a one-time compliance event. Threat modeling, secure design review, code review, automated testing, dependency governance, logging, incident response, and periodic retesting address different stages of risk. AI-assisted tools may help summarize traffic, identify unusual parameter relationships, generate test cases, or prioritize dependencies, but their output requires human validation. Sensitive source code and production data should not be submitted to external systems without authorization and appropriate safeguards.

Emerging Directions and Best Practices

API-first development, microservices, serverless functions, mobile clients, and single-page applications are increasing the number of interfaces that must be governed. Identity-aware proxies and zero-trust architectures can improve control, but they do not remove the need for application-level authorization. Machine-readable API inventories, continuous discovery, and policy-as-code are becoming important for maintaining visibility.

Effective programs commonly adopt the following practices:

  • define scope, authorization, data-handling rules, and safety limits before testing;
  • maintain an endpoint and dependency inventory with owners and lifecycle status;
  • test authenticated behavior using representative roles and tenants;
  • centralize authorization while enforcing it at the resource and action boundary;
  • use parameterized queries, contextual output encoding, secure file handling, and explicit data-binding rules;
  • protect tokens and keys through sound lifecycle management;
  • design rate limits, idempotency, and state validation around business risk;
  • integrate security tests into development and deployment pipelines;
  • record evidence sufficient for reproduction without unnecessary data exposure; and
  • retest remediation and measure recurring defect patterns, not merely individual fixes.

Chapter Summary

Web application and API penetration testing is an evidence-based assessment of exposed functionality, identity, data, state, and business processes. A sound methodology begins with authorization and scope, maps the application and its dependencies, combines automation with manual reasoning, validates findings safely, and reports risk in business terms. Core assessment areas include authentication, session management, object- and function-level authorization, input handling, output encoding, file processing, business logic, API resource limits, client-side behavior, cryptography, and software supply chains. OWASP models provide useful categories, but effective testing requires application-specific threat modeling and comparison across users, tenants, roles, versions, and workflows. Ethical practice, privacy protection, lifecycle governance, and retesting are essential to professional quality.

Key Terms

Authentication: The process of establishing the identity of a user, device, or service.

Authorization: The decision process that determines which resources or actions an authenticated identity may use.

Session fixation: An attack in which an attacker causes a victim to use a session identifier known to the attacker.

Object-level authorization: Control over whether a user may access or modify a particular data object.

Function-level authorization: Control over whether a user may invoke a particular operation or endpoint.

Multi-tenancy: An architecture in which one application serves multiple logically separated organizations or groups.

Input validation: Checking data against acceptable format, type, range, or content rules.

Output encoding: Transforming data so that it is interpreted safely in its destination context.

Cross-site scripting: Injection of attacker-controlled script into a browser-executed context.

Server-side request forgery: Abuse of a server function to make requests to attacker-selected destinations.

Mass assignment: Unintended modification of server-side object attributes through automatic input binding.

Business logic flaw: A weakness in the implementation of intended business rules or workflow constraints.

Idempotency: A property whereby repeating an operation produces the same intended effect as performing it once.

JSON Web Token: A signed or integrity-protected token carrying claims between parties.

API inventory: A maintained record of API endpoints, versions, owners, controls, and lifecycle status.

Software supply chain: The people, processes, tools, components, and artifacts used to build and deliver software.

Threat modeling: Structured analysis of assets, actors, trust boundaries, threats, and mitigations.

Penetration test: An authorized security assessment involving controlled attempts to demonstrate exploitable weaknesses.

Review Questions

Short Answer Questions

1. Distinguish authentication from authorization.

2. Why must API testing compare multiple roles and tenants?

3. What security properties should a password-reset token have?

4. Define mass assignment and state one preventive control.

5. Why is a scanner result not automatically a confirmed vulnerability?

Long Answer Questions

1. Explain a risk-based workflow for web application and API penetration testing, from authorization through retesting.

2. Discuss object-level and function-level authorization, using examples of how each can fail.

3. Explain how business-logic testing differs from conventional input validation testing.

4. Analyze the security implications of client-side JavaScript, cryptographic controls, and software dependencies.

5. Describe the principal OWASP API risks and propose a governance approach for API inventory management.

Analytical / Application Questions

1. An employee can view an invoice but can also change its approval status by adding an undocumented JSON field. Identify the vulnerability, explain why interface restrictions are insufficient, and recommend remediation.

2. A report endpoint denies direct access to another tenant’s record but permits a user to export it through a saved-report identifier. Design a safe test plan and explain the likely control failure.

3. A password-reset flow requires a valid token but does not invalidate it after use. Analyze possible consequences, evidence you would collect, and controls you would recommend.

22. Multiple Choice Questions with Answers

Question 1

Which test most directly assesses object-level authorization?

A. Changing an expense identifier to determine whether another user's record is returned

B. Checking whether the login page uses TLS

C. Measuring the size of a JavaScript bundle

D. Reviewing the application's password length policy

Correct Answer: A. Changing an expense identifier to determine whether another user's record is returned

Question 2

Why is client-side validation insufficient as a security control?

A. It always prevents malformed requests

B. A user can modify or bypass client-side code and send requests directly

C. It cannot improve usability

D. It operates only on encrypted data

Correct Answer: B. A user can modify or bypass client-side code and send requests directly

Question 3

Which control most directly reduces mass-assignment risk?

A. Increasing session duration

B. Disabling browser caching

C. Using explicit allow-lists for fields that may be updated

D. Adding more fields to API responses

Correct Answer: C. Using explicit allow-lists for fields that may be updated

Question 4

A tester finds that a normal user can invoke an administrative export endpoint, although the interface hides the export button. This is primarily a failure of:

A. Output encoding

B. Function-level authorization

C. TLS certificate validation

D. Dependency management

Correct Answer: B. Function-level authorization

Question 5

Which property is most important for a password-reset token?

A. It should be predictable so support staff can reproduce it

B. It should remain valid indefinitely

C. It should be single-use, time-limited, and difficult to guess

D. It should be placed in a URL that is logged permanently

Correct Answer: C. It should be single-use, time-limited, and difficult to guess

Question 6

Why should a penetration tester compare documented API behavior with observed traffic?

A. Documentation always contains more endpoints than the application

B. Documentation may omit legacy, shadow, or undocumented endpoints

C. Observed traffic cannot reveal authentication requirements

D. API documentation replaces authorization testing

Correct Answer: B. Documentation may omit legacy, shadow, or undocumented endpoints

Question 7

Which scenario best represents a business-logic flaw?

A. A server returns a generic error message

B. A user can redeem a single-use voucher repeatedly by replaying the workflow

C. A cookie has the HttpOnly attribute

D. A dependency is listed in a lock file

Correct Answer: B. A user can redeem a single-use voucher repeatedly by replaying the workflow

Question 8

What is the principal security purpose of an idempotency key in a payment operation?

A. To ensure repeated requests do not create unintended duplicate effects

B. To reveal the user's password to the payment provider

C. To disable authorization checks for retries

D. To make all API responses public

Correct Answer: A. To ensure repeated requests do not create unintended duplicate effects

Question 9

Which statement about JWTs is correct?

A. Using JWTs automatically enforces authorization

B. JWTs eliminate the need to validate claims and signatures

C. JWT security still depends on correct signature, claim, key, and authorization validation

D. JWTs cannot expire

Correct Answer: C. JWT security still depends on correct signature, claim, key, and authorization validation

Question 10

During a test, a dependency scanner reports a vulnerable package. What is the strongest next step?

A. Treat every reported package as equally exploitable without verification

B. Delete all third-party packages immediately

C. Determine whether the vulnerable component is reachable and relevant to the assessed function

D. Ignore the result because dependencies are outside application security

Correct Answer: C. Determine whether the vulnerable component is reachable and relevant to the assessed function

Ethical Hacking and Penetration Testing
Chapter 9 of 15