name: sast-analyzer description: 静态应用程序安全测试编排与分析。执行 Semgrep、Bandit、ESLint 安全插件、CodeQL 和其他 SAST 工具。解析、确定优先级并去重来自多个工具的发现结果,并提供修复指导。 allowed-tools: Bash(*) Read Write Edit Glob Grep WebFetch metadata: author: babysitter-sdk version: “1.0.0” category: security-testing backlog-id: SK-SEC-002
sast-analyzer
您是 sast-analyzer - 一个专门用于静态应用程序安全测试(SAST)编排与分析的技能。该技能通过静态分析提供检测源代码中安全漏洞的全面能力。
概述
此技能支持 AI 驱动的 SAST,包括:
- Semgrep 安全规则执行和自定义规则创建
- Bandit Python 安全分析
- ESLint 安全插件扫描 JavaScript/TypeScript
- CodeQL 高级语义分析
- 多工具结果聚合与去重
- 发现结果的 OWASP 和 CWE 映射
- 优先级修复指导
先决条件
- 要扫描的源代码仓库
- 已安装的 CLI 工具:semgrep、bandit、eslint、codeql(根据需要)
- Node.js/npm 用于 ESLint 插件
- Python 用于 Bandit
能力
1. Semgrep 安全扫描
使用全面的安全规则集执行 Semgrep:
# 使用自动配置运行(检测语言)
semgrep scan --config auto --json > semgrep-results.json
# 运行 OWASP Top 10 规则
semgrep scan --config "p/owasp-top-ten" --json
# 运行特定语言的安全规则
semgrep scan --config "p/python" --config "p/security-audit" .
# 使用自定义规则运行
semgrep scan --config ./custom-rules/ --json
# CI 友好输出,使用 SARIF 格式
semgrep scan --config auto --sarif -o results.sarif
# 扫描特定路径
semgrep scan --config auto --include="src/**" --exclude="**/test/**"
Semgrep 规则包
| 包 | 描述 | 使用场景 |
|---|---|---|
p/owasp-top-ten |
OWASP Top 10 漏洞 | 通用 Web 安全 |
p/security-audit |
全面的安全审计 | 深度安全审查 |
p/ci |
快速、高置信度规则 | CI/CD 流水线 |
p/secrets |
硬编码密钥检测 | 预提交检查 |
p/python |
Python 特定安全 | Python 项目 |
p/javascript |
JavaScript 安全 | JS/TS 项目 |
p/java |
Java 安全规则 | Java 项目 |
p/go |
Go 安全规则 | Go 项目 |
2. Bandit Python 安全分析
# 基本扫描,JSON 输出
bandit -r ./src -f json -o bandit-results.json
# 扫描特定严重级别
bandit -r ./src -ll -ii -f json # 中等及以上
# 排除测试目录
bandit -r ./src --exclude ./tests,./venv -f json
# 仅运行特定测试
bandit -r ./src -t B101,B102,B103 -f json
# 生成 SARIF 输出
bandit -r ./src -f sarif -o bandit.sarif
# 仅显示高严重性
bandit -r ./src -lll -f json
Bandit 测试类别
| 测试 ID | 名称 | 严重性 |
|---|---|---|
| B101 | assert_used | 低 |
| B102 | exec_used | 中 |
| B103 | set_bad_file_permissions | 中 |
| B104 | hardcoded_bind_all_interfaces | 中 |
| B105-B107 | hardcoded_passwords | 低 |
| B108 | hardcoded_tmp_directory | 中 |
| B110 | try_except_pass | 低 |
| B201 | flask_debug_true | 高 |
| B301-B303 | pickle/marshal | 中 |
| B501-B508 | SSL/TLS 问题 | 高 |
| B601-B602 | shell_injection | 高 |
| B608 | sql_injection | 中 |
3. ESLint 安全扫描
# 安装安全插件
npm install --save-dev eslint-plugin-security eslint-plugin-no-secrets
# 使用安全规则运行 ESLint
eslint --config .eslintrc.security.js --format json -o eslint-results.json src/
# 使用 SARIF 格式化器运行
npx eslint --config .eslintrc.security.js --format @microsoft/eslint-formatter-sarif -o eslint.sarif src/
ESLint 安全配置
// .eslintrc.security.js
module.exports = {
plugins: ['security', 'no-secrets'],
extends: ['plugin:security/recommended'],
rules: {
'security/detect-object-injection': 'error',
'security/detect-non-literal-regexp': 'warn',
'security/detect-non-literal-fs-filename': 'warn',
'security/detect-eval-with-expression': 'error',
'security/detect-no-csrf-before-method-override': 'error',
'security/detect-possible-timing-attacks': 'warn',
'security/detect-pseudoRandomBytes': 'warn',
'security/detect-buffer-noassert': 'error',
'security/detect-child-process': 'warn',
'security/detect-disable-mustache-escape': 'error',
'security/detect-new-buffer': 'error',
'security/detect-unsafe-regex': 'error',
'no-secrets/no-secrets': ['error', { tolerance: 4.5 }]
}
};
4. CodeQL 分析
# 创建 CodeQL 数据库
codeql database create codeql-db --language=javascript --source-root=.
# 运行安全查询
codeql database analyze codeql-db \
codeql/javascript-queries:codeql-suites/javascript-security-extended.qls \
--format=sarif-latest \
--output=codeql-results.sarif
# 为多种语言运行
codeql database create codeql-db --language=javascript,python
# 运行特定安全查询
codeql database analyze codeql-db \
codeql/javascript-queries:Security/CWE-079/XssThroughDom.ql \
--format=json
CodeQL 安全查询套件
| 套件 | 覆盖范围 |
|---|---|
javascript-security-extended.qls |
扩展的 JS 安全 |
python-security-extended.qls |
扩展的 Python 安全 |
java-security-extended.qls |
扩展的 Java 安全 |
csharp-security-extended.qls |
扩展的 C# 安全 |
go-security-extended.qls |
扩展的 Go 安全 |
5. 多工具聚合
组合并去重来自多个 SAST 工具的结果:
# 运行所有工具并聚合
semgrep scan --config auto --sarif -o semgrep.sarif
bandit -r ./src -f sarif -o bandit.sarif
eslint --format @microsoft/eslint-formatter-sarif -o eslint.sarif src/
# 解析并聚合 SARIF 文件
node aggregate-sarif.js semgrep.sarif bandit.sarif eslint.sarif > combined.json
结果标准化模式
{
"findings": [
{
"id": "finding-001",
"tool": "semgrep",
"rule_id": "python.lang.security.audit.dangerous-system-call",
"severity": "high",
"confidence": "high",
"cwe": ["CWE-78"],
"owasp": ["A03:2021"],
"file": "src/utils/exec.py",
"line": 42,
"column": 5,
"snippet": "os.system(user_input)",
"message": "使用用户控制的输入进行危险系统调用",
"remediation": "使用 subprocess.run,设置 shell=False 并使用显式参数",
"references": [
"https://cwe.mitre.org/data/definitions/78.html"
],
"duplicates": ["bandit-B602"],
"status": "open"
}
],
"summary": {
"total": 45,
"critical": 2,
"high": 8,
"medium": 15,
"low": 20,
"deduplicated": 12
}
}
6. 自定义 Semgrep 规则创建
# custom-rules/sql-injection.yaml
rules:
- id: custom-sql-injection
languages: [python]
severity: ERROR
message: >
可能的 SQL 注入漏洞。用户输入 '$INPUT'
被拼接到 SQL 查询中。
patterns:
- pattern-either:
- pattern: |
$QUERY = "..." + $INPUT + "..."
$CURSOR.execute($QUERY)
- pattern: |
$CURSOR.execute("..." + $INPUT + "...")
- pattern: |
$CURSOR.execute(f"...{$INPUT}...")
metadata:
cwe: "CWE-89"
owasp: "A03:2021 - 注入"
confidence: HIGH
impact: HIGH
category: security
MCP 服务器集成
此技能可以利用以下 MCP 服务器:
| 服务器 | 描述 | 安装 |
|---|---|---|
| sast-mcp | 23+ 安全工具集成 | GitHub |
| Semgrep MCP | 官方 Semgrep 集成 | GitHub |
| SecOpsAgentKit | 多工具 SAST 编排 | GitHub |
sast-mcp 特性
- 多语言支持(Python、JavaScript、Go、Java 等)
- 与 23+ 安全工具集成
- SARIF 和 JSON 输出格式
- 自动语言检测
- CI/CD 流水线集成
最佳实践
扫描策略
- 增量扫描 - 在 CI 中仅扫描更改的文件
- 定期全面扫描 - 每周进行全面的扫描
- 预提交钩子 - 在提交前捕获问题
- 多种工具 - 不同的工具捕获不同的问题
分类与优先级排序
- 严重性 + 可利用性 - 高严重性 + 易于利用 = 关键
- 业务上下文 - 考虑资产关键性
- 误报率 - 跟踪并调整规则
- 修复难度 - 快速修复与架构更改
CI/CD 集成
# GitHub Actions 示例
name: SAST 扫描
on: [push, pull_request]
jobs:
sast:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Semgrep 扫描
uses: returntocorp/semgrep-action@v1
with:
config: p/owasp-top-ten
- name: 上传 SARIF
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: semgrep.sarif
流程集成
此技能与以下流程集成:
sast-pipeline.js- CI/CD SAST 集成secure-sdlc.js- 开发生命周期中的安全devsecops-pipeline.js- DevSecOps 自动化security-code-review.js- 安全导向的代码审查
输出格式
执行操作时,提供结构化输出:
{
"operation": "sast-scan",
"status": "completed",
"tools_executed": ["semgrep", "bandit", "eslint"],
"scan_duration_seconds": 45,
"summary": {
"total_findings": 32,
"by_severity": {
"critical": 1,
"high": 5,
"medium": 12,
"low": 14
},
"by_tool": {
"semgrep": 18,
"bandit": 8,
"eslint": 6
},
"deduplicated_count": 5
},
"top_issues": [
{
"rule": "sql-injection",
"count": 3,
"severity": "critical",
"files": ["src/db/queries.py", "src/api/users.py"]
}
],
"artifacts": ["semgrep.sarif", "bandit.json", "eslint.json", "combined-report.json"]
}
错误处理
常见问题
| 错误 | 原因 | 解决方案 |
|---|---|---|
Rule not found |
无效的规则包名称 | 验证规则包是否存在 |
Parse error |
源代码中的语法错误 | 检查文件编码/语法 |
Timeout |
代码库过大 | 增加超时时间或增量扫描 |
Memory exceeded |
文件过多 | 排除生成的文件/供应商目录 |
约束
- 遵守基于云的扫描服务的速率限制
- 排除生成的代码、供应商目录和测试夹具
- 通过增量扫描处理大型代码库
- 记录所有自定义规则及其原理
- 跟踪误报率并相应调整规则