performing-api-security-testing-with-postman

使用 Postman 构建测试集合,执行结构化 API 安全测试,覆盖 OWASP API 安全 Top 10 漏洞, 包括认证绕过、授权缺陷、注入和数据暴露。测试人员创建包含多个用户角色的环境, 编写自动化安全验证测试脚本,并将 Postman 与 OWASP ZAP 和 Newman 集成以进行 CI/CD 安全测试。 当请求涉及 Postman 安全测试、API 安全集合、自动化 API 测试或使用 Postman 进行 OWASP API 测试时触发。

9 stars

Best use case

performing-api-security-testing-with-postman is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

使用 Postman 构建测试集合,执行结构化 API 安全测试,覆盖 OWASP API 安全 Top 10 漏洞, 包括认证绕过、授权缺陷、注入和数据暴露。测试人员创建包含多个用户角色的环境, 编写自动化安全验证测试脚本,并将 Postman 与 OWASP ZAP 和 Newman 集成以进行 CI/CD 安全测试。 当请求涉及 Postman 安全测试、API 安全集合、自动化 API 测试或使用 Postman 进行 OWASP API 测试时触发。

Teams using performing-api-security-testing-with-postman should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/performing-api-security-testing-with-postman/SKILL.md --create-dirs "https://raw.githubusercontent.com/killvxk/cybersecurity-skills-zh/main/skills/performing-api-security-testing-with-postman/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/performing-api-security-testing-with-postman/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How performing-api-security-testing-with-postman Compares

Feature / Agentperforming-api-security-testing-with-postmanStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

使用 Postman 构建测试集合,执行结构化 API 安全测试,覆盖 OWASP API 安全 Top 10 漏洞, 包括认证绕过、授权缺陷、注入和数据暴露。测试人员创建包含多个用户角色的环境, 编写自动化安全验证测试脚本,并将 Postman 与 OWASP ZAP 和 Newman 集成以进行 CI/CD 安全测试。 当请求涉及 Postman 安全测试、API 安全集合、自动化 API 测试或使用 Postman 进行 OWASP API 测试时触发。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# 使用 Postman 执行 API 安全测试

## 适用场景

- 构建覆盖 OWASP API 安全 Top 10 的可重复 API 安全测试套件
- 创建通过 Newman 在 CI/CD 流水线中运行的自动化安全回归测试
- 系统地跨多个用户角色测试 API 认证和授权
- 将 Postman 与 OWASP ZAP 代理集成,进行手动和自动化安全测试组合
- 在新 API 端点部署前建立基线安全测试集合

**不适用于** 未经授权对生产 API 使用。Postman 安全测试涉及发送潜在恶意载荷。

## 前置条件

- 在活跃工作区中使用 Postman 桌面或 Web 应用
- 目标 API 具有用于集合导入的 OpenAPI/Swagger 规范
- 至少三个角色的测试账户:未认证用户、普通用户、管理员
- 已安装 Newman CLI 用于 CI/CD 集成:`npm install -g newman`
- OWASP ZAP 配置为本地代理(localhost:8080)用于 Postman 代理集成
- 包含基础 URL、令牌和测试数据的 API 环境变量

## 工作流程

### 步骤 1:环境和集合设置

为多角色测试创建 Postman 环境:

```json
// 环境:API 安全测试 - 普通用户
{
    "values": [
        {"key": "base_url", "value": "https://target-api.example.com/api/v1"},
        {"key": "auth_token", "value": ""},
        {"key": "user_email", "value": "regular@test.com"},
        {"key": "user_password", "value": "TestPass123!"},
        {"key": "user_id", "value": ""},
        {"key": "other_user_id", "value": "1002"},
        {"key": "admin_endpoint", "value": "/admin/users"},
        {"key": "test_order_id", "value": ""},
        {"key": "other_user_order_id", "value": "5003"}
    ]
}
```

**用于自动认证的前置请求脚本:**
```javascript
// 集合级前置请求脚本,用于自动登录
if (!pm.environment.get("auth_token") || pm.environment.get("token_expired")) {
    const loginRequest = {
        url: pm.environment.get("base_url") + "/auth/login",
        method: "POST",
        header: {"Content-Type": "application/json"},
        body: {
            mode: "raw",
            raw: JSON.stringify({
                email: pm.environment.get("user_email"),
                password: pm.environment.get("user_password")
            })
        }
    };

    pm.sendRequest(loginRequest, (err, res) => {
        if (!err && res.code === 200) {
            const token = res.json().access_token;
            pm.environment.set("auth_token", token);
            pm.environment.set("user_id", res.json().user.id);
        }
    });
}
```

### 步骤 2:BOLA(API1)测试集合

```javascript
// 测试:访问其他用户的个人资料(BOLA)
// 请求:GET {{base_url}}/users/{{other_user_id}}
// 认证:Bearer {{auth_token}}

// 测试脚本:
pm.test("BOLA:不能访问其他用户资料", function() {
    pm.expect(pm.response.code).to.be.oneOf([401, 403]);
});

pm.test("BOLA:拒绝时无用户数据泄露", function() {
    if (pm.response.code === 200) {
        const body = pm.response.json();
        pm.expect(body).to.not.have.property("email");
        pm.expect(body).to.not.have.property("phone");
        pm.expect(body).to.not.have.property("address");
        // 如果返回了完整资料,标记为 BOLA 漏洞
        console.error("BOLA 漏洞:返回了其他用户的完整资料");
    }
});

// 测试:访问其他用户的订单
// 请求:GET {{base_url}}/orders/{{other_user_order_id}}
pm.test("BOLA:不能访问其他用户订单", function() {
    pm.expect(pm.response.code).to.be.oneOf([401, 403, 404]);
});

// 测试:修改其他用户的资源
// 请求:PATCH {{base_url}}/users/{{other_user_id}}
// 请求体:{"name": "Hacked"}
pm.test("BOLA:不能修改其他用户资料", function() {
    pm.expect(pm.response.code).to.be.oneOf([401, 403]);
});
```

### 步骤 3:认证(API2)测试集合

```javascript
// 测试:令牌验证
// 请求:GET {{base_url}}/users/me
// 认证:Bearer invalid_token_value

pm.test("认证:无效令牌被拒绝", function() {
    pm.expect(pm.response.code).to.be.oneOf([401, 403]);
});

// 测试:过期令牌处理
// 请求:GET {{base_url}}/users/me
// 认证:Bearer {{expired_token}}

pm.test("认证:过期令牌被拒绝", function() {
    pm.expect(pm.response.code).to.equal(401);
});

// 测试:缺少认证
// 请求:GET {{base_url}}/users/me
// 认证:无

pm.test("认证:未认证请求被拒绝", function() {
    pm.expect(pm.response.code).to.equal(401);
});

// 测试:登录中的 SQL 注入
// 请求:POST {{base_url}}/auth/login
// 请求体:{"email": "' OR 1=1--", "password": "test"}

pm.test("认证:登录中的 SQL 注入被拒绝", function() {
    pm.expect(pm.response.code).to.not.equal(200);
    pm.expect(pm.response.text()).to.not.include("token");
});

// 测试:账号枚举
// 前置:使用有效邮箱+错误密码发送,然后使用无效邮箱+错误密码发送
pm.test("认证:无账号枚举", function() {
    // 与存储的有效邮箱尝试响应进行比较
    const validEmailResponse = pm.environment.get("valid_email_response");
    const currentResponse = pm.response.text();
    pm.expect(currentResponse).to.equal(validEmailResponse);
});
```

### 步骤 4:数据暴露(API3)和 BFLA(API5)测试

```javascript
// 测试:过度数据暴露检查
// 请求:GET {{base_url}}/users/me

pm.test("数据暴露:响应中无敏感字段", function() {
    const sensitiveFields = [
        "password", "password_hash", "passwordHash",
        "ssn", "social_security", "credit_card",
        "api_key", "secret_key", "mfa_secret",
        "refresh_token", "session_id"
    ];
    const responseText = pm.response.text().toLowerCase();
    sensitiveFields.forEach(field => {
        pm.expect(responseText).to.not.include('"' + field + '"');
    });
});

pm.test("数据暴露:安全响应头存在", function() {
    pm.expect(pm.response.headers.has("X-Content-Type-Options")).to.be.true;
    pm.expect(pm.response.headers.has("X-Frame-Options")).to.be.true;
    pm.expect(pm.response.headers.get("X-Content-Type-Options")).to.equal("nosniff");
});

pm.test("数据暴露:无服务器信息泄露", function() {
    pm.expect(pm.response.headers.has("Server")).to.be.false;
    pm.expect(pm.response.headers.has("X-Powered-By")).to.be.false;
});

// 测试:BFLA - 管理员端点访问
// 请求:GET {{base_url}}{{admin_endpoint}}
// 认证:Bearer {{auth_token}}(普通用户)

pm.test("BFLA:普通用户不能访问管理员端点", function() {
    pm.expect(pm.response.code).to.be.oneOf([401, 403]);
});

// 测试:BFLA - 管理员功能执行
// 请求:DELETE {{base_url}}/users/{{other_user_id}}
// 认证:Bearer {{auth_token}}(普通用户)

pm.test("BFLA:普通用户不能删除其他用户", function() {
    pm.expect(pm.response.code).to.be.oneOf([401, 403]);
});
```

### 步骤 5:批量赋值和限速测试

```javascript
// 测试:通过资料更新进行批量赋值
// 请求:PUT {{base_url}}/users/me
// 请求体:{"name": "Test", "role": "admin", "is_admin": true}

pm.test("批量赋值:角色字段不被接受", function() {
    if (pm.response.code === 200) {
        const user = pm.response.json();
        pm.expect(user.role).to.not.equal("admin");
        pm.expect(user.is_admin).to.not.equal(true);
    }
});

// 测试:限速执行
// 此测试应在高迭代次数的集合运行器中运行

pm.test("限速:超过限制时返回 429", function() {
    // 此测试期望在多次迭代后触发限速
    const iterationCount = pm.info.iteration;
    if (iterationCount > 50) {
        // 50 次迭代后应看到限速
        if (pm.response.code === 429) {
            pm.expect(pm.response.headers.has("Retry-After")).to.be.true;
            console.log("在第 " + iterationCount + " 次迭代时触发限速");
        }
    }
});

// 测试:限速响应头存在
pm.test("限速:限速响应头存在", function() {
    const hasRateHeaders = pm.response.headers.has("X-RateLimit-Limit") ||
                           pm.response.headers.has("X-Rate-Limit-Limit") ||
                           pm.response.headers.has("RateLimit-Limit");
    pm.expect(hasRateHeaders).to.be.true;
});
```

### 步骤 6:Newman CI/CD 集成

```bash
# 通过 Newman CLI 运行安全测试集合
newman run "API-Security-Tests.postman_collection.json" \
    --environment "Security-Test-Environment.postman_environment.json" \
    --reporters cli,htmlextra,junit \
    --reporter-htmlextra-export ./reports/security-test-report.html \
    --reporter-junit-export ./reports/security-test-results.xml \
    --iteration-count 1 \
    --timeout-request 10000 \
    --delay-request 100 \
    --bail

# 使用不同用户角色运行
for role in "regular_user" "admin_user" "unauthenticated"; do
    echo "使用角色测试:$role"
    newman run "API-Security-Tests.postman_collection.json" \
        --environment "Security-Test-${role}.postman_environment.json" \
        --reporters cli,junit \
        --reporter-junit-export "./reports/security-${role}.xml"
done
```

**GitHub Actions 集成:**
```yaml
# .github/workflows/api-security-test.yml
name: API Security Tests
on:
  pull_request:
    paths: ['src/api/**', 'openapi.yaml']

jobs:
  security-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm install -g newman newman-reporter-htmlextra
      - name: Run API Security Tests
        run: |
          newman run tests/postman/api-security.json \
            --environment tests/postman/env-staging.json \
            --reporters cli,htmlextra,junit \
            --reporter-htmlextra-export reports/security.html \
            --reporter-junit-export reports/security.xml
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: security-reports
          path: reports/
```

## 核心概念

| 术语 | 定义 |
|------|------|
| **Postman 集合(Postman Collection)** | 包含测试脚本的有组织 API 请求组,可共享、版本控制和自动执行 |
| **Newman** | Postman 的命令行伴侣工具,可在 CI/CD 流水线中运行集合并生成测试报告 |
| **前置请求脚本(Pre-request Script)** | 在 Postman 请求前执行的 JavaScript 代码,用于动态认证和测试数据设置 |
| **测试脚本(Test Script)** | 在 Postman 响应后执行的 JavaScript 代码,用于针对响应验证安全断言 |
| **集合运行器(Collection Runner)** | Postman 功能,按顺序执行集合中所有请求,可配置迭代次数和延迟 |
| **环境变量(Environment Variables)** | 限定在 Postman 环境范围内的键值对,用于针对不同目标、角色和配置参数化请求 |

## 工具与系统

- **Postman**:用于构建、测试和记录 API 的平台,内置脚本和集合管理功能
- **Newman**:支持多种报告格式(HTML、JUnit、JSON)用于 CI/CD 集成的 Postman 集合 CLI 运行器
- **OWASP ZAP**:可配置为 Postman 代理的开源安全代理,用于被动扫描所有请求
- **newman-reporter-htmlextra**:Newman 的增强 HTML 报告器,生成包含请求/响应数据的详细测试报告
- **Postman Flows**:用于链接复杂安全测试序列和条件逻辑的可视化工作流构建器

## 常见场景

### 场景:CI/CD 的 API 安全回归套件

**场景背景**:一个开发团队每两周发布 API 更新。他们需要一个自动化安全测试套件,在每次拉取请求时运行,以在合并前发现授权和认证回归问题。

**方法**:
1. 将 OpenAPI 规范导入 Postman 生成包含所有端点的基础集合
2. 创建三个环境:未认证、普通用户、管理员,各有相应凭据
3. 为每个请求添加安全测试脚本:BOLA 检查、认证验证、数据暴露扫描、请求头安全
4. 创建专用"安全测试"文件夹,包含注入载荷、批量赋值测试和限速检查
5. 将集合和环境导出到代码库
6. 在 GitHub Actions 中配置 Newman,在每个影响 API 代码的 PR 时运行
7. 设置流水线在任何安全测试失败时中止,阻止合并

**常见陷阱**:
- 在集合中硬编码认证令牌,而不是使用前置请求脚本动态生成令牌
- 未使用所有用户角色测试——只测试认证与未认证会遗漏基于角色的授权问题
- 针对生产环境而非预发布环境运行安全测试
- 添加新端点时未更新集合,导致覆盖存在空白
- 在 CI/CD 中忽略 Newman 退出码,允许失败的安全测试静默通过

## 输出格式

```
## API 安全测试报告 - Postman/Newman

**集合**:API 安全测试 v2.3
**环境**:预发布 - 普通用户
**日期**:2024-12-15
**总请求数**:85
**总测试数**:234
**通过**:219
**失败**:15

### 失败测试摘要

| # | 请求 | 测试名称 | 严重性 |
|---|---------|-----------|----------|
| 1 | GET /users/1002 | BOLA:不能访问其他用户资料 | 严重 |
| 2 | GET /orders/5003 | BOLA:不能访问其他用户订单 | 严重 |
| 3 | GET /admin/users | BFLA:普通用户不能访问管理员端点 | 严重 |
| 4 | PUT /users/me | 批量赋值:角色字段不被接受 | 高 |
| 5 | GET /users/me | 数据暴露:响应中无敏感字段 | 高 |
| 6 | POST /auth/login | 认证:无账号枚举 | 中 |
| ... | ... | ... | ... |

### 建议
1. 修复 /users/{id} 和 /orders/{id} 的 BOLA——添加对象级授权检查
2. 修复 /admin/users 的 BFLA——执行基于角色的访问控制中间件
3. 修复 PUT /users/me 的批量赋值——实现字段允许列表
4. 从用户序列化中删除 password_hash 和 mfa_secret
5. 标准化登录错误消息以防止账号枚举
```

Related Skills

triaging-security-incident

9
from killvxk/cybersecurity-skills-zh

使用 NIST SP 800-61r3 和 SANS PICERL 框架对安全事件进行初始分类,确定严重性、范围和所需响应行动。 按类型对事件分类,根据业务影响分配优先级,并路由到相应的响应团队。适用于事件分类、 安全告警分类、严重性评估、事件优先级排序或初始事件分析等请求场景。

triaging-security-incident-with-ir-playbook

9
from killvxk/cybersecurity-skills-zh

使用结构化 IR Playbook 对安全事件进行分类和优先排序,确定严重性、分配响应团队并启动适当的响应程序。

triaging-security-alerts-in-splunk

9
from killvxk/cybersecurity-skills-zh

在 Splunk Enterprise Security 中对安全告警进行分类,通过 SPL 查询和事件审查(Incident Review) 仪表板对重要事件进行严重性分类、调查、关联相关遥测并做出升级或关闭决策。 适用于 SOC 分析师需要处理关联搜索产生的告警队列、确定调查优先级, 或需要为交接给二/三级分析师记录分类决策时。

testing-websocket-api-security

9
from killvxk/cybersecurity-skills-zh

测试 WebSocket API 实现中的安全漏洞,包括 WebSocket 升级时缺少身份认证、跨站 WebSocket 劫持(Cross-Site WebSocket Hijacking,CSWSH)、通过 WebSocket 消息进行的注入攻击、输入校验不足、通过消息泛洪实施拒绝服务,以及通过 WebSocket 帧造成的信息泄露。测试人员使用 Burp Suite 拦截 WebSocket 握手和消息,构造恶意 payload,并测试 WebSocket 通道上的授权绕过。适用于 WebSocket 安全测试、WS 渗透测试、CSWSH 攻击或实时 API 安全评估相关请求。

testing-oauth2-implementation-flaws

9
from killvxk/cybersecurity-skills-zh

测试 OAuth 2.0 和 OpenID Connect 实现中的安全缺陷,包括授权码拦截、重定向 URI 操控、OAuth 流程中的 CSRF、令牌泄露、权限范围(scope)提升以及 PKCE 绕过。测试人员对授权服务器、客户端应用及令牌处理进行评估,发现可导致账户接管或未授权访问的常见错误配置。适用于 OAuth 安全测试、OIDC 漏洞评估、OAuth2 重定向绕过或授权码流程测试相关请求。

testing-mobile-api-authentication

9
from killvxk/cybersecurity-skills-zh

测试移动应用 API 的认证与授权机制,识别认证失效、不安全的令牌管理、会话固定、 权限提升和 IDOR 漏洞。适用于对移动应用后端进行 API 安全评估、测试 JWT 实现、 评估 OAuth 流程或评估会话管理的场景。适合涉及移动 API 认证测试、令牌安全评估、 OAuth 移动端流程测试或 API 授权绕过的相关请求。

testing-jwt-token-security

9
from killvxk/cybersecurity-skills-zh

在安全测试活动中,评估 JSON Web Token(JWT)实现中的密码学弱点、算法混淆攻击和授权绕过漏洞。

testing-for-xxe-injection-vulnerabilities

9
from killvxk/cybersecurity-skills-zh

在授权的渗透测试中发现和利用 XML 外部实体(XXE)注入漏洞,以读取服务器文件、执行 SSRF 并外泄数据。

testing-for-xss-vulnerabilities

9
from killvxk/cybersecurity-skills-zh

通过向反射型、存储型和 DOM 型上下文注入 JavaScript 载荷,测试 Web 应用程序的跨站脚本(XSS)漏洞, 演示客户端代码执行、会话劫持和用户冒充。测试人员识别所有注入点和输出上下文,构造适合上下文的载荷, 并绕过净化和 CSP 保护。适用于 XSS 测试、跨站脚本评估、客户端注入测试或 JavaScript 注入漏洞测试等请求场景。

testing-for-xss-vulnerabilities-with-burpsuite

9
from killvxk/cybersecurity-skills-zh

在授权的安全评估过程中,使用 Burp Suite 的扫描器、Intruder 和 Repeater 工具识别和验证跨站脚本(XSS)漏洞。适用于 Web 应用渗透测试中检测反射型、存储型和 DOM 型 XSS,验证自动化扫描器报告的 XSS 发现,以及评估 CSP 和 XSS 过滤器的有效性时使用。

testing-for-xml-injection-vulnerabilities

9
from killvxk/cybersecurity-skills-zh

测试 Web 应用程序中的 XML 注入漏洞,包括 XXE(XML 外部实体注入)、XPath 注入和 XML 实体攻击,以识别数据泄露和服务器端请求伪造(SSRF)风险。

testing-for-sensitive-data-exposure

9
from killvxk/cybersecurity-skills-zh

在安全评估中识别敏感数据暴露漏洞,包括 API 密钥泄露、响应中的 PII、不安全存储以及未受保护的数据传输。