Implements Gitleaks to detect and prevent hardcoded secrets in git repos via pre-commit hooks, CI/CD integration like GitHub Actions, custom rules, baselines, and remediation workflows.
npx claudepluginhub killvxk/cybersecurity-skills-zhThis skill uses the workspace's default tool permissions.
- 当开发者可能意外将 API 密钥、密码、令牌或私钥提交到仓库时
Implements Gitleaks for secret scanning in git repositories using pre-commit hooks, CI/CD pipelines, custom rules, baseline scans of history, and remediation workflows.
Implements Gitleaks for detecting hardcoded secrets in git repositories via pre-commit hooks, CI/CD integration, custom rules, baseline scans, and remediation workflows.
Integrates gitleaks and trufflehog into CI/CD pipelines to scan git repos for leaked secrets like API keys before deployment. Adds gates to GitHub Actions, GitLab CI, Jenkins.
Share bugs, ideas, or general feedback.
不适用于检测运行中应用或内存中的机密(使用运行时机密检测)、检测后管理机密(使用 Vault 或 AWS Secrets Manager)或扫描容器镜像(使用 Trivy 或 Grype)。
对仓库执行基线扫描,识别 git 历史中所有现有的机密信息。
# 安装 Gitleaks
brew install gitleaks # macOS
# 或从 https://github.com/gitleaks/gitleaks/releases 下载二进制文件
# 扫描整个 git 历史以查找机密
gitleaks detect --source . --report-format json --report-path gitleaks-report.json -v
# 仅扫描暂存变更(用于预提交)
gitleaks protect --staged --report-format json --report-path gitleaks-staged.json
# 扫描特定提交范围
gitleaks detect --source . --log-opts="HEAD~10..HEAD" --report-format json
# 扫描而不查看 git 历史(仅文件系统)
gitleaks detect --source . --no-git --report-format json
将 Gitleaks 设置为预提交钩子以防止机密被提交。
# .pre-commit-config.yaml
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.21.2
hooks:
- id: gitleaks
name: gitleaks
description: Detect hardcoded secrets using Gitleaks
entry: gitleaks protect --staged --verbose --redact
language: golang
pass_filenames: false
# 安装 pre-commit 框架
pip install pre-commit
# 安装 .pre-commit-config.yaml 中定义的钩子
pre-commit install
# 针对所有文件运行(不仅是暂存文件)
pre-commit run gitleaks --all-files
# 使用刻意的机密测试钩子
echo 'AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"' >> test.txt
git add test.txt
git commit -m "test" # 应被 gitleaks 阻止
# .github/workflows/secret-scanning.yml
name: Secret Scanning
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
gitleaks:
name: Gitleaks Secret Scan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # 完整历史以进行全面扫描
- name: Run Gitleaks
uses: gitleaks/gitleaks-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }} # gitleaks-action v2 必需
# 替代方案:直接运行 Gitleaks
- name: Install Gitleaks
run: |
wget -q https://github.com/gitleaks/gitleaks/releases/download/v8.21.2/gitleaks_8.21.2_linux_x64.tar.gz
tar -xzf gitleaks_8.21.2_linux_x64.tar.gz
chmod +x gitleaks
- name: Scan for secrets
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
./gitleaks detect \
--source . \
--log-opts="${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}" \
--report-format sarif \
--report-path gitleaks.sarif \
--exit-code 1
else
./gitleaks detect \
--source . \
--report-format sarif \
--report-path gitleaks.sarif \
--exit-code 1 \
--baseline-path .gitleaks-baseline.json
fi
- name: Upload SARIF
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: gitleaks.sarif
category: gitleaks
为内部机密模式创建组织特定规则。
# .gitleaks.toml
title = "Organization Gitleaks Configuration"
[extend]
useDefault = true # 包含所有默认规则
# 内部 API 令牌的自定义规则
[[rules]]
id = "internal-api-token"
description = "Internal API token for service-to-service auth"
regex = '''(?i)x-internal-token["\s:=]+["\']?([a-zA-Z0-9_\-]{40,})["\']?'''
entropy = 3.5
keywords = ["x-internal-token"]
tags = ["internal", "api"]
[[rules]]
id = "database-connection-string"
description = "Database connection string with embedded credentials"
regex = '''(?i)(postgres|mysql|mongodb|redis)://[^:]+:[^@]+@[^/]+/\w+'''
keywords = ["postgres://", "mysql://", "mongodb://", "redis://"]
tags = ["database", "credentials"]
[[rules]]
id = "jwt-secret"
description = "JWT signing secret"
regex = '''(?i)(jwt[_-]?secret|jwt[_-]?key)["\s:=]+["\']?([a-zA-Z0-9/+_\-]{32,})["\']?'''
entropy = 3.0
keywords = ["jwt_secret", "jwt-secret", "jwt_key", "jwt-key"]
# 测试文件和已知安全模式的允许列表
[allowlist]
description = "Global allowlist"
paths = [
'''(^|/)test(s)?/''',
'''(^|/)spec/''',
'''\.test\.(js|ts|py)$''',
'''\.spec\.(js|ts|py)$''',
'''__mocks__/''',
'''fixtures/''',
'''(^|/)vendor/''',
'''node_modules/'''
]
regexes = [
'''EXAMPLE''',
'''example\.com''',
'''test[-_]?(key|secret|token|password)''',
'''(?i)placeholder''',
'''000000+'''
]
创建已知发现的基线,以避免在历史机密轮换期间阻塞开发。
# 从当前状态生成基线
gitleaks detect --source . --report-format json --report-path .gitleaks-baseline.json
# 后续扫描与基线比较(只有新发现才会触发失败)
gitleaks detect --source . --baseline-path .gitleaks-baseline.json --exit-code 1
# 定期审查基线,随着机密轮换删除条目
cat .gitleaks-baseline.json | python3 -m json.tool | head -50
检测到机密时,遵循轮换和历史清理流程。
# 1. 立即轮换暴露的凭证
# - 在服务提供商处吊销旧 API 密钥/令牌
# - 生成新凭证
# - 将新凭证存储在密钥管理器中
# 2. 使用 git-filter-repo 从 git 历史中删除机密
pip install git-filter-repo
# 创建要删除的机密表达式文件
cat > /tmp/expressions.txt << 'EOF'
regex:AKIA[0-9A-Z]{16}==>REDACTED_AWS_KEY
regex:(?i)password\s*=\s*"[^"]*"==>password="REDACTED"
EOF
git filter-repo --replace-text /tmp/expressions.txt --force
# 3. 强制推送已清理的历史(与团队协调)
# git push --force --all # 警告:需要团队协调
# 4. 将机密模式添加到 .gitleaks.toml 规则中
# 5. 更新基线文件以删除已解决的发现
| 术语 | 定义 |
|---|---|
| 机密 | 不应出现在源代码中的任何凭证、令牌、密钥或敏感字符串 |
| 预提交钩子 | 在提交创建之前运行的 Git 钩子,阻止包含检测到机密的提交 |
| 熵值 | 字符串中随机性的度量;高熵字符串更可能是机密 |
| 基线 | 用于区分新机密与预先存在机密的现有发现快照 |
| 允许列表 | 指定要从检测中排除的路径、模式或提交的配置 |
| SARIF | 静态分析结果交换格式,用于将发现上传到安全仪表板 |
| git-filter-repo | 重写 git 历史以从所有提交中删除敏感数据的工具 |
背景:一个 5 年历史的仓库从未被扫描。团队需要在历史机密轮换期间不阻塞所有开发的情况下启用机密扫描。
方法:
gitleaks detect 并生成基线 JSON 文件注意事项:在未分类的情况下生成基线意味着接受未轮换机密的风险。切勿假设历史机密是非活跃的,除非通过服务提供商验证。在未与团队协调的情况下对共享仓库运行 git-filter-repo 将导致所有团队成员的变基冲突。
Gitleaks 机密扫描报告
=================================
仓库:org/web-application
扫描类型:完整历史
已扫描提交:4,523
日期:2026-02-23
发现:
总计:12
新发现(不在基线中):3
基线(预先存在):9
新发现(阻塞):
[1] AWS 访问密钥 ID
规则:aws-access-key-id
文件:src/config/aws.py:23
提交:a1b2c3d(2026-02-22,dev@company.com)
机密:AKIA...已编辑
熵值:3.8
[2] GitHub 个人访问令牌
规则:github-pat
文件:scripts/deploy.sh:15
提交:d4e5f6g(2026-02-21,ops@company.com)
机密:ghp_...已编辑
熵值:4.2
[3] 内部 API 令牌
规则:internal-api-token
文件:src/services/auth.py:89
提交:h7i8j9k(2026-02-20,dev@company.com)
质量门禁:失败(3 个新发现)
操作:立即轮换暴露的凭证。