关于 implementing-api-security-testing-with-42crunch
面向 API 开发与安全团队的 42Crunch API 安全测试指南,介绍 OpenAPI 静态审计、运行中 API 动态扫描及运行时保护。内容还涵盖 IDE 与 CI/CD 集成及发现问题后的修复流程。
主要功能
- 01审计 OpenAPI 定义
- 02动态扫描运行中的 API
- 03集成 IDE 与 CI/CD
- 04通过微网关实施运行时保护
- 05按严重程度处理并复核问题
适用场景
- 01发布前检查 API 规范中的安全缺陷
- 02在 CI/CD 流水线运行 API 安全扫描
- 03验证运行时 API 与 OpenAPI 契约的一致性
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill implementing-api-security-testing-with-42crunch安装后在 Claude Code 中输入/implementing-api-security-testing-with-42crunch确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEreferences/api-reference.mdscripts/agent.py详细介绍
使用 42Crunch 实现 API 安全测试
概述
42Crunch 是一个 API 安全平台,将左移安全测试与右移运行时防护相结合。它提供用于 OpenAPI 定义静态安全分析的 API Audit、用于动态漏洞检测的 API Conformance Scan,以及用于实时威胁防御的 API Protect。该平台可集成到 CI/CD 流水线和 IDE 中,在部署前后识别 OWASP API Security Top 10 漏洞。
何时使用
- 在环境中部署或配置使用 42Crunch 实现 API 安全测试的能力时
- 在建立符合合规要求的安全控制措施时
- 在为该领域构建或改进安全架构时
- 在执行需要此实现的安全评估时
前提条件
- 42Crunch 平台账户(评估可用的免费套餐)
- 目标 API 的 OpenAPI Specification (OAS) v2.0、v3.0 或 v3.1 定义
- 带有 42Crunch 扩展的 IDE(VS Code、IntelliJ 或 Eclipse)
- CI/CD 流水线(Jenkins、GitHub Actions、Azure DevOps 或 GitLab CI)
- 用于动态扫描的运行中 API 实例(一致性扫描)
- 用于 CLI 工具链的 Node.js 或 Python 环境
核心概念
API Audit(静态分析)
API Audit 对 OpenAPI 定义执行静态安全分析,无需运行中的 API。它依据 300+ 项安全检查对规范进行评估,这些检查分为多个类别:
安全评分类别:
- 数据验证:Schema 定义、参数约束、响应验证
- 身份验证:安全方案定义、作用域要求
- 传输安全:服务器 URL 方案、TLS 要求
- 错误处理:错误响应定义、信息泄漏防护
通过 VS Code 扩展运行 API Audit:
- 从 VS Code marketplace 安装 42Crunch 扩展
- 打开 OpenAPI 规范文件(YAML 或 JSON)
- 点击编辑器工具栏中的安全审计图标
- 查看安全评分(0-100)和各项发现
- 使用内联修复指导解决问题
带安全控制的 OpenAPI 定义示例:
openapi: 3.0.3
info:
title: Secure User API
version: 1.0.0
servers:
- url: https://api.example.com/v1
description: Production server (HTTPS only)
security:
- BearerAuth: []
paths:
/users/{userId}:
get:
operationId: getUserById
summary: Retrieve user by ID
parameters:
- name: userId
in: path
required: true
schema:
type: string
format: uuid
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
maxLength: 36
responses:
'200':
description: User details
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'400':
description: Invalid request
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'401':
description: Unauthorized
'404':
description: User not found
components:
securitySchemes:
BearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
schemas:
User:
type: object
required:
- id
- email
properties:
id:
type: string
format: uuid
readOnly: true
email:
type: string
format: email
maxLength: 254
name:
type: string
maxLength: 100
pattern: '^[a-zA-Z\s\-]+$'
additionalProperties: false
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
maxLength: 256
additionalProperties: false
API Conformance Scan(动态测试)
一致性扫描会根据 OpenAPI 契约动态测试运行中的 API,以检测运行时漏洞,包括 OWASP API Security Top 10 问题:
Scan v2 配置:
# 42c-conf.yaml
version: "2.0"
scan:
target:
url: https://api.example.com/v1
authentication:
- type: bearer
token: "${API_TOKEN}"
in: header
name: Authorization
settings:
maxScanTime: 3600
requestsPerSecond: 10
followRedirects: false
tests:
owasp:
- bola
- bfla
- injection
- ssrf
- massAssignment
- excessiveDataExposure
通过 CLI 运行一致性扫描:
# Install the 42Crunch CLI
npm install -g @42crunch/cicd-cli
# Run conformance scan
42crunch-cli scan \
--api-definition ./openapi.yaml \
--target-url https://api.example.com/v1 \
--token $CRUNCH_TOKEN \
--min-score 70 \
--report-format sarif \
--output scan-report.sarif
CI/CD 流水线集成
GitHub Actions 集成:
name: API Security Testing
on:
push:
paths:
- 'api/**'
- 'openapi/**'
jobs:
api-security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: 42Crunch API Audit
uses: 42Crunch/api-security-audit-action@v3
with:
api-token: ${{ secrets.CRUNCH_API_TOKEN }}
collection-name: "my-api-collection"
min-score: 75
upload-to-code-scanning: true
- name: 42Crunch Conformance Scan
if: github.ref == 'refs/heads/main'
uses: 42Crunch/api-conformance-scan@v1
with:
api-token: ${{ secrets.CRUNCH_API_TOKEN }}
target-url: ${{ secrets.STAGING_API_URL }}
scan-config: ./42c-conf.yaml
Jenkins 流水线集成:
pipeline {
agent any
stages {
stage('API Security Audit') {
steps {
script {
def auditResult = sh(
script: '''
42crunch-cli audit \
--api-definition openapi.yaml \
--token ${CRUNCH_TOKEN} \
--min-score 75 \
--report-format json \
--output audit-report.json
''',
returnStatus: true
)
if (auditResult != 0) {
error("API Security Audit failed - score below threshold")
}
}
}
}
stage('Conformance Scan') {
when { branch 'main' }
steps {
sh '''
42crunch-cli scan \
--api-definition openapi.yaml \
--target-url ${STAGING_URL} \
--token ${CRUNCH_TOKEN} \
--scan-config 42c-conf.yaml
'''
}
}
}
post {
always {
archiveArtifacts artifacts: '*-report.*'
publishHTML([
reportDir: '.',
reportFiles: 'audit-report.html',
reportName: 'API Security Report'
])
}
}
}
API Protect(运行时防护)
API Protect 作为微网关部署在 API 端点前方,以在运行时强制执行 OpenAPI 契约:
# api-protect-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: api-protect-config
data:
protection-config.json: |
{
"apiDefinition": "/config/openapi.yaml",
"enforcement": {
"validateRequests": true,
"validateResponses": true,
"blockOnFailure": true,
"logLevel": "warn"
},
"rateLimit": {
"enabled": true,
"requestsPerMinute": 100,
"burstSize": 20
},
"allowlist": {
"contentTypes": ["application/json"],
"methods": ["GET", "POST", "PUT", "DELETE"]
}
}
修复工作流
当 42Crunch 发现问题时,请遵循以下修复流程:
- 分类:查看按严重性(严重、高、中、低)排序的发现
- 分析:了解 OpenAPI 定义中缺少的具体安全控制
- 修复:将建议的更改应用到规范
- 验证:重新运行审计以确认评分提升
- 部署:将更新后的规范推送到 CI/CD 流水线
常见审计发现及修复:
| 发现 | 严重性 | 修复 |
|---|---|---|
| 未定义身份验证 | 严重 | 添加 securitySchemes 和 security 要求 |
| 缺少输入验证 | 高 | 添加 type、format、pattern、maxLength 约束 |
| 服务器 URL 使用 HTTP | 高 | 将服务器 URL 改为 HTTPS |
| 未定义错误响应 | 中 | 添加 4xx 和 5xx 响应定义 |
| 未限制 additionalProperties | 中 | 在对象 schema 上设置 additionalProperties: false |
| 缺少速率限制 | 中 | 添加 x-rateLimit 扩展或使用 API Protect |
关键安全检查
42Crunch 围绕以下关键安全领域评估 API:
- BOLA 防护:验证是否定义了对象级授权模式
- BFLA 防护:检查是否定义了功能级访问控制
- 注入防护:确保输入参数具有正确的类型/格式/模式约束
- 数据暴露:验证响应模式是否限制了返回的属性
- 安全配置错误:检查认证方案、传输安全、CORS 设置
- 批量赋值:验证请求体是否使用了显式的属性允许列表
参考资料
- 42Crunch API Security Platform:https://42crunch.com/api-security-platform/
- 42Crunch 文档:https://docs.42crunch.com/
- Microsoft Defender for Cloud 42Crunch 集成:https://learn.microsoft.com/en-us/azure/defender-for-cloud/onboarding-guide-42crunch
- OWASP API Security Top 10 2023:https://owasp.org/API-Security/editions/2023/en/0x00-header/
- 42Crunch 的 Jenkins 插件:https://plugins.jenkins.io/42crunch-security-audit/