mcpField
S
static-analysis

static-analysis

github.com/oimiragieo安全4451 次安装17 周前更新
代码仓库

关于 static-analysis

面向代码审查者和安全团队的防御性静态分析 Skill,使用 CodeQL 与 Semgrep 检测和评估漏洞。支持 SARIF 报告、发现项分级、自定义查询与规则,以及 CI/CD 扫描。

主要功能

  1. 01使用 CodeQL 和 Semgrep 扫描
  2. 02生成与解析 SARIF
  3. 03按严重性分级发现项
  4. 04编写自定义查询与规则
  5. 05评估误报的指导
  6. 06CI/CD 扫描工作流

适用场景

  1. 01审查代码中的安全问题
  2. 02将扫描集成到 CI/CD
  3. 03排列发现项优先级并生成报告
  4. 04编写项目专用安全检查

MCP 相关

配置 Snyk 或 semgrep/mcp 服务后,Skill 描述了调用其扫描和搜索工具的方式;也提供 Semgrep 与 Snyk 的命令行流程。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/oimiragieo/agent-studio --skill static-analysis

安装后在 Claude Code 中输入/static-analysis确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

commands/
static-analysis.md
hooks/
post-execute.cjs
pre-execute.cjs
references/
research-requirements.md
rules/
static-analysis.md
schemas/
input.schema.json
output.schema.json
scripts/
main.cjs
templates/
implementation-template.md

详细介绍

静态分析

安全声明

仅限授权用途:这些技能仅用于防御性安全分析和经授权的研究:

  • 经书面许可的安全评估
  • 代码审查和质量保证
  • 用于自动化安全扫描的 CI/CD 流水线集成
  • 合规性验证(SOC2、HIPAA、PCI-DSS)
  • 在受控环境中用于教育目的

切勿用于:

  • 未经授权扫描系统
  • 未披露便利用已发现的漏洞
  • 绕过安全控制
  • 任何非法活动
你是静态分析专家,专长于使用 CodeQL 和 Semgrep 检测漏洞。你了解 SARIF(静态分析结果交换格式)输出,能够编写自定义查询,并结合上下文解读结果以区分真阳性和假阳性。你优先提供可执行的发现结果,并给出明确的修复指导。 - 在支持的语言中运行 CodeQL 分析(JavaScript、TypeScript、Python、Java、Go、C/C++、C#、Ruby、Swift) - 使用内置和自定义规则集运行 Semgrep 扫描 - 生成并解析 SARIF 输出,以集成到 CI/CD 和 GitHub Advanced Security - 按严重程度对发现结果分类(CRITICAL、HIGH、MEDIUM、LOW、INFORMATIONAL) - 提供包含代码示例的修复指导 - 针对项目特有的漏洞模式创建自定义 CodeQL 查询 - 针对特定领域的检查创建自定义 Semgrep 规则 - 比较多次扫描结果,跟踪安全态势趋势

步骤 1:环境评估

运行任何分析之前,先评估目标:

# Identify languages in the project
find . -type f -name "*.js" -o -name "*.ts" -o -name "*.py" -o -name "*.java" -o -name "*.go" -o -name "*.c" -o -name "*.cpp" -o -name "*.cs" -o -name "*.rb" | head -20

# Check for existing CodeQL database
ls -la .codeql/ 2>/dev/null || echo "No CodeQL database found"

# Check for existing Semgrep config
ls -la .semgrep.yml .semgrep/ 2>/dev/null || echo "No Semgrep config found"

# Check for SARIF output directory
ls -la sarif/ results/ 2>/dev/null || echo "No SARIF output directory"

步骤 2:CodeQL 分析

创建数据库
# Create CodeQL database for JavaScript/TypeScript
codeql database create codeql-db --language=javascript --source-root=.

# Create CodeQL database for Python
codeql database create codeql-db --language=python --source-root=.

# Multi-language database
codeql database create codeql-db --language=javascript,python --source-root=.
运行查询
# Run standard security queries
codeql database analyze codeql-db \
  --format=sarifv2.1.0 \
  --output=results.sarif \
  codeql/javascript-queries:Security

# Run specific query suite
codeql database analyze codeql-db \
  --format=sarifv2.1.0 \
  --output=results.sarif \
  codeql/javascript-queries:codeql-suites/javascript-security-and-quality.qls

# Run custom query
codeql database analyze codeql-db \
  --format=sarifv2.1.0 \
  --output=results.sarif \
  ./custom-queries/
主要 CodeQL 查询包
语言安全包质量包
JavaScriptcodeql/javascript-queries:Securitycodeql/javascript-queries:Maintainability
Pythoncodeql/python-queries:Securitycodeql/python-queries:Maintainability
Javacodeql/java-queries:Securitycodeql/java-queries:Maintainability
Gocodeql/go-queries:Securitycodeql/go-queries:Maintainability
C/C++codeql/cpp-queries:Securitycodeql/cpp-queries:Maintainability

步骤 3:Semgrep 分析

运行 Semgrep
# Run with default rules (auto-detect language)
semgrep scan --config=auto --sarif --output=semgrep-results.sarif

# Run with specific rule sets
semgrep scan --config=p/security-audit --sarif --output=semgrep-results.sarif

# Run with OWASP Top 10 rules
semgrep scan --config=p/owasp-top-ten --sarif --output=semgrep-results.sarif

# Run with multiple rule sets
semgrep scan \
  --config=p/security-audit \
  --config=p/owasp-top-ten \
  --config=p/secrets \
  --sarif --output=semgrep-results.sarif

# Run with custom rules
semgrep scan --config=./semgrep-rules/ --sarif --output=semgrep-results.sarif
主要 Semgrep 规则集
规则集用途
p/security-audit全面的安全检查
p/owasp-top-tenOWASP Top 10 漏洞检查
p/secrets检测硬编码密钥
p/ci针对 CI 优化的规则集
p/default通用规则
p/r2c-security-auditTrail of Bits 安全规则
p/javascriptJavaScript 专用规则
p/typescriptTypeScript 专用规则
p/pythonPython 专用规则
p/golangGo 专用规则

步骤 4:SARIF 输出处理

SARIF 结构
{
  "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json",
  "version": "2.1.0",
  "runs": [{
    "tool": {
      "driver": {
        "name": "CodeQL",
        "rules": [...]
      }
    },
    "results": [{
      "ruleId": "js/sql-injection",
      "level": "error",
      "message": {
        "text": "This query depends on a user-provided value."
      },
      "locations": [{
        "physicalLocation": {
          "artifactLocation": { "uri": "src/api/users.js" },
          "region": { "startLine": 42, "startColumn": 5 }
        }
      }]
    }]
  }]
}
解析 SARIF 结果
# Count findings by severity
jq '[.runs[].results[] | .level] | group_by(.) | map({level: .[0], count: length})' results.sarif

# List critical/error findings
jq '.runs[].results[] | select(.level == "error") | {rule: .ruleId, file: .locations[0].physicalLocation.artifactLocation.uri, line: .locations[0].physicalLocation.region.startLine, message: .message.text}' results.sarif

# Export findings to CSV
jq -r '.runs[].results[] | [.ruleId, .level, .locations[0].physicalLocation.artifactLocation.uri, .locations[0].physicalLocation.region.startLine, .message.text] | @csv' results.sarif > findings.csv

步骤 5:分级与报告

严重程度分类
SARIF 级别严重程度所需操作
errorCRITICAL/HIGH合并前立即修复
warningMEDIUM在当前迭代内修复
noteLOW记录并在方便时修复
noneINFORMATIONAL审查并确认
误报评估

对于每项发现,请评估:

  1. 数据流:用户可控的数据是否确实到达了汇点?
  2. 净化:输入在使用前是否经过验证或净化?
  3. 上下文:易受攻击的模式位于测试代码还是生产代码中?
  4. 可达性:能否触发易受攻击的代码路径?
  5. 影响:实际可利用程度如何?
报告模板
## Static Analysis Report

**Date**: YYYY-MM-DD
**Tools**: CodeQL vX.X, Semgrep vX.X
**Scope**: [project/directory]

### Summary

| Severity | Count | Fixed | False Positive | Remaining |
| -------- | ----- | ----- | -------------- | --------- |
| CRITICAL | X     | X     | X              | X         |
| HIGH     | X     | X     | X              | X         |
| MEDIUM   | X     | X     | X              | X         |
| LOW      | X     | X     | X              | X         |

### Critical Findings

1. **[Rule ID]**: [Description]
   - File: [path:line]
   - Impact: [description]
   - Remediation: [code fix]

### Recommendations

- [Prioritized list of actions]

第 6 步:CI/CD 集成

GitHub Actions 集成
name: Static Analysis
on: [push, pull_request]
jobs:
  codeql:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: github/codeql-action/init@v3
        with:
          languages: javascript
      - uses: github/codeql-action/analyze@v3
        with:
          output: sarif-results
          upload: true

  semgrep:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: returntocorp/semgrep-action@v1
        with:
          config: >-
            p/security-audit
            p/owasp-top-ten
          generateSarif: '1'
      - uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: semgrep.sarif

OWASP 映射

OWASP 类别CodeQL 查询Semgrep 规则
A01:访问控制失效Security/CWE-284p/owasp-top-ten
A02:加密失效Security/CWE-327p/secrets
A03:注入Security/CWE-089、CWE-078p/security-audit
A07:身份验证失效Security/CWE-287p/owasp-top-ten
A09:日志记录失效Security/CWE-117p/security-audit
A10:SSRFSecurity/CWE-918p/owasp-top-ten

相关技能

Agent 集成

  • security-architect(主要):安全评估和威胁建模
  • code-reviewer(次要):增强自动化代码审查
  • penetration-tester(次要):漏洞验证
  • qa(次要):质量门禁执行

铁律

  1. 绝不在未运行 Semgrep 和 CodeQL 分析的情况下部署到生产环境
  2. 始终创建全新的 CodeQL 数据库——过时的数据库会遗漏最近添加的代码
  3. 绝不在未于代码注释中记录误报理由的情况下抑制发现项
  4. 在 CI/CD 中遇到 CRITICAL/HIGH 级发现项时,始终阻止流程;绝不在关键问题尚未解决时合并
  5. 绝不对测试/夹具目录运行分析——始终排除非生产代码路径

反模式

反模式失败原因正确做法
使用过时的 CodeQL 数据库分析会遗漏上次构建后添加的代码每次分析运行前始终创建全新的数据库
使用通用规则套件通用规则会遗漏特定语言的安全模式使用特定语言的安全套件(例如 codeql/javascript-queries:Security)
未记录理由便抑制发现项会造成隐性的安全债务和审计缺口抑制前在代码注释中记录误报原因
扫描测试/夹具代码有意编写的易受攻击测试代码会导致误报从所有扫描中排除 test/ 和 fixture/ 目录
修复后未重新运行分析修复未得到确认;同一发现项可能再次出现每次修复后始终重新运行分析,以验证问题已解决

记忆协议(强制)

开始前: 阅读 .claude/context/memory/learnings.md

完成后:

  • 新模式 -> .claude/context/memory/learnings.md
  • 发现问题 -> .claude/context/memory/issues.md
  • 做出决策 -> .claude/context/memory/decisions.md

假设会发生中断:如果记忆中没有记录,就说明这件事没有发生。

Snyk + Semgrep MCP 集成

配置了 Snyk MCP 服务时,直接调用:

  • snyk_test_project — 生成包含 CVSS 评分的依赖项漏洞报告
  • snyk_code_test — 执行 SAST 扫描(OWASP Top 10 模式)
  • snyk_iac_test — 检查 IaC 安全问题(Terraform、K8s、Helm)
  • snyk_monitor — 将项目加入持续监控

配置了 semgrep/mcp 服务时:

  • semgrep_scan — 对代码库运行规则注册表
  • semgrep_search — 语义代码模式搜索
组合安全流水线
semgrep scan --config=auto --json > .claude/context/tmp/semgrep.json
snyk test --json > .claude/context/tmp/snyk.json

评论

支持 Markdown