Implementing secrets scanning in ci cd

From killvxk/cybersecurity-skills-zh · View interactive version

Implementing secrets scanning in ci cd is an AI agent skill from the GitHub repository killvxk/cybersecurity-skills-zh. 将 gitleaks 和 trufflehog 集成到 CI/CD 管道中,在部署前检测泄露的机密信息

How to use this skill

Skill file

概述

本技能涵盖使用 gitleaks 和 trufflehog 在 CI/CD 管道中实施自动化机密扫描。它使安全团队能够检测意外提交到源代码仓库的 API 密钥、令牌、密码和其他凭证,提供阻止包含高严重性发现的部署的 CI 门禁。

Gitleaks 使用正则表达式模式和熵分析扫描 git 仓库和目录中的硬编码机密。TruffleHog 对文件系统和 git 历史进行扫描,并可选择对实时服务进行机密验证。两者共同为机密检测提供全面覆盖。

前置条件

步骤

  1. 安装扫描工具:通过包管理器或二进制下载安装 gitleaks。通过 brew install trufflehog 或从 GitHub releases 下载安装 trufflehog。
  1. 配置 gitleaks:在仓库根目录创建 .gitleaks.toml 配置文件,以定义自定义规则、允许列表和路径排除。使用 --config 标志指向自定义配置。
  1. 运行 gitleaks 目录扫描:执行 gitleaks dir --source . --report-format json --report-path gitleaks-report.json 以扫描工作目录并生成 JSON 报告。
  1. 运行 trufflehog 文件系统扫描:执行 trufflehog filesystem /path/to/repo --json > trufflehog-report.json 以扫描文件并将 JSON 发现输出到报告文件。
  1. 解析和过滤发现:使用代理脚本解析两个 JSON 报告,按严重性(严重、高危、中危、低危)过滤发现,并确定 CI 管道是否应通过或失败。
  1. 集成到 CI 管道:将扫描步骤添加到 GitHub Actions 工作流、GitLab CI 配置或 Jenkins 管道中,作为预部署门禁。使用 gitleaks 中的 --exit-code 标志控制管道行为。
  1. 配置预提交钩子:使用 gitleaks protect --staged 将 gitleaks 设置为预提交钩子,以在提交之前捕获机密。
  1. 审查和分类发现:检查 JSON 输出中的误报,将合法条目添加到 .gitleaksignore,并立即轮换任何已确认泄露的凭证。

预期输出

代理脚本生成包含以下内容的 JSON 报告:

{
  "scan_summary": {
    "tool": "both",
    "total_findings": 3,
    "critical": 1,
    "high": 1,
    "medium": 1,
    "low": 0,
    "ci_gate": "FAIL",
    "fail_reason": "Found 1 critical and 1 high severity findings"
  },
  "findings": [...]
}
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.