OWASP ZAP API Security Testing
Advanced techniques for testing API security with OWASP ZAP including REST, GraphQL, and SOAP APIs
API Security Testing with OWASP ZAP
Modern applications heavily rely on APIs for communication between components and services. OWASP ZAP provides specialized features for testing the security of various API types, including REST, GraphQL, SOAP, and more. This guide covers advanced techniques for comprehensive API security testing.
API Testing Overview
REST API Testing
ZAP can test REST APIs through:
- OpenAPI/Swagger definition import
- Manual request creation
- API-specific scan rules
- Authentication handling
- Parameter fuzzing
The OpenAPI Support add-on enhances ZAP's capabilities for REST API testing by automatically generating requests based on API specifications.
Setting Up for API Testing
Install Required Add-ons
- Open ZAP
- Go to Marketplace (in the Manage Add-ons dialog)
- Install the following add-ons:
- OpenAPI Support
- GraphQL Support (if testing GraphQL)
- SOAP Scanner (if testing SOAP)
- Import/Export
- Fuzzer
Configure API Options
- Go to Tools > Options
- Configure relevant sections:
- API: Set API configurations
- Network: Configure proxy settings if needed
- Authentication: Set up authentication if required
Prepare API Documentation
Gather API documentation:
- OpenAPI/Swagger specifications for REST APIs
- GraphQL schemas
- WSDL files for SOAP services
These will be imported into ZAP to generate requests automatically.
Testing REST APIs
Importing OpenAPI Definitions
Import OpenAPI Definition
- Go to Import > Import an OpenAPI definition
- Select your OpenAPI file (JSON or YAML) or provide a URL
- Configure import options:
- Target URL (if different from the one in the definition)
- Context name
- Click Import
ZAP will parse the definition and add the endpoints to the Sites tree.
Review Imported Endpoints
- Expand the imported site in the Sites tree
- Review the endpoints and parameters
- Right-click on endpoints to:
- Send to Spider
- Send to Active Scanner
- Open in Request Editor
Generate Requests
The OpenAPI add-on provides two ways to generate requests:
-
Automatic Request Generation:
- Right-click on the imported API
- Select OpenAPI > Generate Full Requests
- ZAP will generate requests with example values
-
Manual Request Creation:
- Right-click on an endpoint
- Select Open/Resend with Request Editor
- Modify parameters as needed
- Send the request
API Scanning Strategies
Passive Scanning
Passive scanning analyzes requests and responses without sending additional requests:
- Generate API requests using the OpenAPI definition
- ZAP automatically performs passive scanning on all traffic
- Review passive scan alerts in the Alerts tab
Passive scanning can detect:
- Information disclosure
- Insecure configurations
- Missing security headers
- Sensitive data exposure
- Content security policy issues
Authentication for REST APIs
API Key Authentication
For API key authentication:
- Use the HTTP Headers tab in the Request Editor
- Add the appropriate header (e.g.,
X-API-Key: your-api-key
) - Alternatively, use the Replacer add-on to automatically add the header to all requests
OAuth 2.0 Authentication
For OAuth 2.0:
- Install the OAuth Support add-on
- Configure OAuth settings:
- Go to Tools > Options > Authentication > OAuth 2.0
- Set client ID, client secret, token endpoint, etc.
- Create a context with OAuth authentication
- Use the context for scanning
JWT Authentication
For JWT-based authentication:
- Install the JWT Support add-on
- Obtain a valid JWT token (via login or other means)
- Configure the Authorization header:
Authorization: Bearer your-jwt-token
- Use the JWT Support add-on to:
- Decode and analyze tokens
- Test for JWT vulnerabilities
- Fuzz JWT claims
Testing GraphQL APIs
Working with GraphQL
Import GraphQL Schema
- Go to Import > Import a GraphQL Schema
- Provide the GraphQL endpoint URL or schema file
- Configure import options:
- Endpoint URL
- Query generation options
- Click Import
ZAP will parse the schema and generate queries for all available operations.
Generate GraphQL Queries
The GraphQL add-on automatically generates:
- Queries: For retrieving data
- Mutations: For modifying data
- Subscriptions: For real-time data (if supported)
You can configure query generation options:
- Maximum query depth
- Arguments generation
- Optional fields inclusion
Send GraphQL Queries
- Navigate to the GraphQL tab
- Select a generated query
- Modify the query if needed
- Click Send to execute the query
- Review the response
All queries and responses are added to the History tab for further analysis.
GraphQL-Specific Vulnerabilities
Introspection Vulnerabilities
GraphQL introspection allows clients to query the schema structure:
-
Test if introspection is enabled:
{ __schema { types { name fields { name } } } }
-
If introspection is enabled in production, it may expose sensitive information about the API structure
-
Use the GraphQL add-on to:
- Automatically detect enabled introspection
- Generate alerts for introspection in production
- Map the API structure using introspection
Testing SOAP/XML Web Services
Working with SOAP Services
Import WSDL Definition
- Go to Import > Import a WSDL file
- Select your WSDL file or provide a URL
- Configure import options
- Click Import
ZAP will parse the WSDL and add the SOAP endpoints to the Sites tree.
Generate SOAP Requests
- Right-click on a SOAP operation in the Sites tree
- Select Open/Resend with Request Editor
- ZAP will generate a template SOAP request
- Modify the request parameters as needed
- Send the request
SOAP-Specific Testing
Test for SOAP-specific vulnerabilities:
-
XML External Entity (XXE):
- Modify XML declarations to include external entities
- Test for file disclosure and SSRF
-
XML Injection:
- Insert malicious XML content
- Test XML parser vulnerabilities
-
SOAP Action Spoofing:
- Modify the SOAPAction header
- Test for unauthorized access to operations
-
WS-Security Issues:
- Test for weak encryption
- Test for signature verification bypasses
- Test for token replay attacks
Advanced API Testing Techniques
API Fuzzing
Set Up API Fuzzing
- Right-click on an API request in the History tab
- Select Attack > Fuzz
- Select the parameter to fuzz
- Add fuzz payloads appropriate for the parameter type
Custom Fuzzing Payloads
Create custom fuzzing payloads for API testing:
- Go to Tools > Options > Fuzzer
- Add custom payload files
Effective API fuzzing payloads include:
- JSON structure manipulation
- GraphQL query manipulation
- XML structure manipulation
- Type conversion attacks (e.g., sending strings to numeric fields)
- Boundary testing values
Fuzzing Strategies
Apply these fuzzing strategies:
-
Parameter Fuzzing:
- Fuzz individual parameters
- Try different data types
- Test boundary conditions
-
Structure Fuzzing:
- Modify JSON/XML structure
- Add unexpected fields
- Remove required fields
-
Protocol Fuzzing:
- Modify content types
- Test with malformed requests
- Test with unexpected HTTP methods
Custom Scan Rules for APIs
Custom Passive Rules
Create passive scan rules for API-specific issues:
- Go to Tools > Scripts
- Select Passive Rules script type
- Create a new script
- Implement the
scan
function to analyze requests and responses
Example passive rules for APIs:
- Detecting sensitive information in responses
- Identifying missing security headers
- Checking for verbose error messages
- Detecting insecure API configurations
API Security Testing Automation
Automation Framework
Use ZAP's Automation Framework for API testing:
env:
contexts:
- name: "API Context"
urls:
- "https://api.example.com"
jobs:
- type: import
parameters:
type: "openapi"
file: "/path/to/openapi.json"
target: "https://api.example.com"
- type: passiveScan-wait
parameters:
maxDuration: 10
- type: activeScan
parameters:
context: "API Context"
policy: "API-Only"
- type: report
parameters:
template: "traditional-json"
reportDir: "/path/to/reports"
reportFile: "api-scan-report"
API Testing Scripts
Create scripts for automated API testing:
// Example standalone script for API testing
function testApi() {
var HttpSender = Java.type("org.parosproxy.paros.network.HttpSender");
var HttpMessage = Java.type("org.parosproxy.paros.network.HttpMessage");
var URI = Java.type("org.apache.commons.httpclient.URI");
// Create a new HTTP sender
var sender = new HttpSender(
HttpSender.MANUAL_REQUEST_INITIATOR);
// Define API endpoints to test
var endpoints = [
{
url: "https://api.example.com/users",
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_TOKEN"
}
},
{
url: "https://api.example.com/data",
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_TOKEN"
},
body: '{"key": "value"}'
}
];
// Test each endpoint
for (var i = 0; i < endpoints.length; i++) {
var endpoint = endpoints[i];
print("Testing endpoint: " + endpoint.url);
// Create message
var msg = new HttpMessage(new URI(endpoint.url, true));
// Set method
msg.getRequestHeader().setMethod(endpoint.method);
// Set headers
for (var header in endpoint.headers) {
msg.getRequestHeader().setHeader(header, endpoint.headers[header]);
}
// Set body if present
if (endpoint.body) {
msg.setRequestBody(endpoint.body);
msg.getRequestHeader().setContentLength(msg.getRequestBody().length());
}
// Send request
sender.sendAndReceive(msg, true);
// Process response
var responseStatus = msg.getResponseHeader().getStatusCode();
var responseBody = msg.getResponseBody().toString();
print("Response status: " + responseStatus);
print("Response body: " + responseBody);
// Add to history
model.getSession().getHistory().addHistory(msg);
}
}
testApi();
CI/CD Integration
Integrate API security testing into CI/CD pipelines:
# Example Jenkins pipeline step
stage('API Security Testing') {
steps {
sh '''
docker run -v ${WORKSPACE}:/zap/wrk/:rw -t owasp/zap2docker-stable \
zap-api-scan.py -t https://api.example.com -f openapi \
-d /zap/wrk/openapi.json -r api-report.html
'''
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: '.',
reportFiles: 'api-report.html',
reportName: 'ZAP API Security Report'
])
}
}
API Security Best Practices
API Testing Approach
Follow these best practices for API security testing:
-
Understand the API:
- Review API documentation
- Identify authentication mechanisms
- Understand data formats and structures
- Map out endpoints and operations
-
Test Methodically:
- Start with passive analysis
- Test authentication and authorization
- Test input validation
- Test business logic
- Test error handling
-
Use Multiple Techniques:
- Automated scanning
- Manual testing
- Fuzzing
- Custom scripts
- Business logic testing
Case Studies
REST API Testing Example
Scenario
Testing a RESTful e-commerce API with endpoints for products, users, and orders.
Approach
- Import the OpenAPI definition
- Configure authentication with API keys
- Generate requests for all endpoints
- Perform passive scanning
- Test authorization between different user roles
- Perform active scanning with API-specific rules
- Fuzz critical parameters
- Test business logic flows
Findings
Common issues found in REST APIs:
- Broken object-level authorization allowing access to other users' data
- Mass assignment vulnerabilities in update endpoints
- Excessive data exposure in responses
- Missing rate limiting on authentication endpoints
- Improper error handling revealing sensitive information
GraphQL API Testing Example
Scenario
Testing a GraphQL API for a social media application.
Approach
- Import the GraphQL schema
- Generate queries for all types and operations
- Test introspection security
- Test query depth and complexity limits
- Test authorization between different user roles
- Fuzz GraphQL variables and arguments
- Test for injection in arguments
Findings
Common issues found in GraphQL APIs:
- Introspection enabled in production
- Missing query depth limits allowing DoS attacks
- Authorization flaws in nested objects
- Information disclosure through detailed error messages
- Injection vulnerabilities in arguments
Next Steps
Now that you understand API security testing with OWASP ZAP, explore these related topics:
- Authentication Techniques - Configure authentication for API testing
- Scripting - Create custom scripts for API testing
- Automation - Automate API security testing
- Best Practices - Best practices for effective and ethical use of ZAP