api-security-testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

API安全测试

API Security Testing

概述

Overview

API安全测试是确保API接口安全性的重要环节。本技能提供API安全测试的方法、工具和最佳实践。
API security testing is a critical step to ensure the security of API interfaces. This skill provides methods, tools, and best practices for API security testing.

测试范围

Testing Scope

1. 认证和授权

1. Authentication and Authorization

测试项目:
  • Token有效性验证
  • Token过期处理
  • 权限控制
  • 角色权限验证
Test Items:
  • Token validity verification
  • Token expiration handling
  • Permission control
  • Role permission verification

2. 输入验证

2. Input Validation

测试项目:
  • 参数类型验证
  • 数据长度限制
  • 特殊字符处理
  • SQL注入防护
  • XSS防护
Test Items:
  • Parameter type verification
  • Data length restrictions
  • Special character handling
  • SQL injection protection
  • XSS protection

3. 业务逻辑

3. Business Logic

测试项目:
  • 工作流验证
  • 状态转换
  • 并发控制
  • 业务规则
Test Items:
  • Workflow verification
  • State transition
  • Concurrency control
  • Business rules

4. 错误处理

4. Error Handling

测试项目:
  • 错误信息泄露
  • 堆栈跟踪
  • 敏感信息暴露
Test Items:
  • Error information leakage
  • Stack trace exposure
  • Sensitive information disclosure

测试方法

Testing Methods

1. API发现

1. API Discovery

识别API端点:
bash
undefined
Identify API Endpoints:
bash
undefined

使用目录扫描

使用目录扫描

gobuster dir -u https://target.com -w api-wordlist.txt
gobuster dir -u https://target.com -w api-wordlist.txt

使用Burp Suite被动扫描

使用Burp Suite被动扫描

浏览应用,观察API调用

浏览应用,观察API调用

分析JavaScript文件

分析JavaScript文件

查找API端点定义

查找API端点定义

undefined
undefined

2. 认证测试

2. Authentication Testing

Token测试:
http
undefined
Token Testing:
http
undefined

测试无效Token

测试无效Token

GET /api/user Authorization: Bearer invalid_token
GET /api/user Authorization: Bearer invalid_token

测试过期Token

测试过期Token

GET /api/user Authorization: Bearer expired_token
GET /api/user Authorization: Bearer expired_token

测试无Token

测试无Token

GET /api/user

**JWT测试:**
```bash
GET /api/user

**JWT Testing:**
```bash

使用jwt_tool

使用jwt_tool

python jwt_tool.py <JWT_TOKEN>
python jwt_tool.py <JWT_TOKEN>

测试算法混淆

测试算法混淆

python jwt_tool.py <JWT_TOKEN> -X a
python jwt_tool.py <JWT_TOKEN> -X a

测试密钥暴力破解

测试密钥暴力破解

python jwt_tool.py <JWT_TOKEN> -C -d wordlist.txt
undefined
python jwt_tool.py <JWT_TOKEN> -C -d wordlist.txt
undefined

3. 授权测试

3. Authorization Testing

水平权限:
http
undefined
Horizontal Privilege:
http
undefined

用户A访问用户B的资源

用户A访问用户B的资源

GET /api/user/123 Authorization: Bearer user_a_token
GET /api/user/123 Authorization: Bearer user_a_token

应该返回403

应该返回403


**垂直权限:**
```http

**Vertical Privilege:**
```http

普通用户访问管理员接口

普通用户访问管理员接口

GET /api/admin/users Authorization: Bearer user_token
GET /api/admin/users Authorization: Bearer user_token

应该返回403

应该返回403

undefined
undefined

4. 输入验证测试

4. Input Validation Testing

SQL注入:
http
POST /api/search
{
  "query": "test' OR '1'='1"
}
命令注入:
http
POST /api/execute
{
  "command": "ping; id"
}
XXE:
http
POST /api/parse
Content-Type: application/xml

<?xml version="1.0"?>
<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
<foo>&xxe;</foo>
SQL Injection:
http
POST /api/search
{
  "query": "test' OR '1'='1"
}
Command Injection:
http
POST /api/execute
{
  "command": "ping; id"
}
XXE:
http
POST /api/parse
Content-Type: application/xml

<?xml version="1.0"?>
<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
<foo>&xxe;</foo>

5. 速率限制测试

5. Rate Limiting Testing

测试速率限制:
python
import requests

for i in range(1000):
    response = requests.get('https://target.com/api/endpoint')
    print(f"Request {i}: {response.status_code}")
Test Rate Limiting:
python
import requests

for i in range(1000):
    response = requests.get('https://target.com/api/endpoint')
    print(f"Request {i}: {response.status_code}")

工具使用

Tool Usage

Postman

Postman

创建测试集合:
  1. 导入API文档
  2. 设置认证
  3. 创建测试用例
  4. 运行自动化测试
Create Test Collections:
  1. Import API documentation
  2. Configure authentication
  3. Create test cases
  4. Run automated tests

Burp Suite

Burp Suite

API扫描:
  1. 配置API端点
  2. 设置认证
  3. 运行主动扫描
  4. 分析结果
API Scanning:
  1. Configure API endpoints
  2. Set up authentication
  3. Run active scan
  4. Analyze results

OWASP ZAP

OWASP ZAP

bash
undefined
bash
undefined

API扫描

API扫描

zap-cli quick-scan --self-contained
--start-options '-config api.disablekey=true'
http://target.com/api
undefined
zap-cli quick-scan --self-contained
--start-options '-config api.disablekey=true'
http://target.com/api
undefined

REST-Attacker

REST-Attacker

bash
undefined
bash
undefined

扫描OpenAPI规范

扫描OpenAPI规范

rest-attacker scan openapi.yaml
undefined
rest-attacker scan openapi.yaml
undefined

常见漏洞

Common Vulnerabilities

1. 认证绕过

1. Authentication Bypass

Token验证缺陷:
  • 弱Token生成
  • Token可预测
  • Token不验证签名
Token Verification Flaws:
  • Weak token generation
  • Predictable tokens
  • Token signature not verified

2. 权限提升

2. Privilege Escalation

IDOR:
  • 直接对象引用
  • 未验证资源所有权
IDOR:
  • Insecure Direct Object References
  • Unverified resource ownership

3. 信息泄露

3. Information Disclosure

错误信息:
  • 详细错误信息
  • 堆栈跟踪
  • 敏感数据
Error Messages:
  • Detailed error messages
  • Stack traces
  • Sensitive data exposure

4. 注入漏洞

4. Injection Vulnerabilities

常见注入:
  • SQL注入
  • NoSQL注入
  • 命令注入
  • XXE
Common Injections:
  • SQL injection
  • NoSQL injection
  • Command injection
  • XXE

5. 业务逻辑

5. Business Logic

逻辑缺陷:
  • 价格操作
  • 数量限制绕过
  • 状态修改
Logic Flaws:
  • Price manipulation
  • Quantity restriction bypass
  • State modification

测试清单

Test Checklist

认证测试

Authentication Testing

  • Token有效性验证
  • Token过期处理
  • 弱Token检测
  • Token重放攻击
  • Token validity verification
  • Token expiration handling
  • Weak token detection
  • Token replay attack

授权测试

Authorization Testing

  • 水平权限测试
  • 垂直权限测试
  • 角色权限验证
  • 资源访问控制
  • Horizontal privilege testing
  • Vertical privilege testing
  • Role permission verification
  • Resource access control

输入验证

Input Validation

  • SQL注入测试
  • XSS测试
  • 命令注入测试
  • XXE测试
  • 参数污染
  • SQL injection testing
  • XSS testing
  • Command injection testing
  • XXE testing
  • Parameter pollution

业务逻辑

Business Logic

  • 工作流验证
  • 状态转换
  • 并发控制
  • 业务规则
  • Workflow verification
  • State transition
  • Concurrency control
  • Business rules

错误处理

Error Handling

  • 错误信息泄露
  • 堆栈跟踪
  • 敏感信息暴露
  • Error information leakage
  • Stack trace exposure
  • Sensitive information disclosure

防护措施

Protection Measures

推荐方案

Recommended Solutions

  1. 认证
    • 使用强Token
    • 实现Token刷新
    • 验证Token签名
  2. 授权
    • 基于角色的访问控制
    • 资源所有权验证
    • 最小权限原则
  3. 输入验证
    • 参数类型验证
    • 数据长度限制
    • 白名单验证
  4. 错误处理
    • 统一错误响应
    • 不泄露详细信息
    • 记录错误日志
  5. 速率限制
    • 实现API限流
    • 防止暴力破解
    • 监控异常请求
  1. Authentication
    • Use strong tokens
    • Implement token refresh
    • Verify token signatures
  2. Authorization
    • Role-based access control
    • Resource ownership verification
    • Principle of least privilege
  3. Input Validation
    • Parameter type verification
    • Data length restrictions
    • Whitelist validation
  4. Error Handling
    • Unified error responses
    • Do not disclose detailed information
    • Log error details
  5. Rate Limiting
    • Implement API rate limiting
    • Prevent brute-force attacks
    • Monitor abnormal requests

注意事项

Notes

  • 仅在授权测试环境中进行
  • 避免对API造成影响
  • 注意不同API版本的差异
  • 测试时注意请求频率
  • Only conduct in authorized testing environments
  • Avoid impacting API services
  • Note differences between different API versions
  • Pay attention to request frequency during testing