关于 secrets-scan
secrets-scan 是用于查找源代码及 Git 历史中硬编码凭据和敏感字符串的 Agent 技能。它结合已知凭据模式与可选的熵值检测,并支持限定扫描范围和排除规则。
主要功能
- 01检测各类服务的凭据模式
- 02扫描 Git 历史中的密钥
- 03分析高熵字符串
- 04按路径或文件模式限定扫描
- 05排除文件并设置行内忽略
适用场景
- 01提交前检查源代码
- 02查找已从当前文件删除但仍留在历史中的凭据
- 03扫描指定目录或文件类型
- 04配置 CI 扫描前审查发现项
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ jwynia/ agent-skills --skill secrets-scan安装后在 Claude Code 中输入/secrets-scan确认已加载。
详细介绍
密钥扫描
深入检测源代码中硬编码的凭据和敏感数据。
快速开始
/secrets-scan # Scan current directory
/secrets-scan --scope src/ # Scan specific path
/secrets-scan --entropy # Include high-entropy detection
/secrets-scan --git-history # Check git commit history
此技能的检测内容
高置信度模式
误报率极低的模式:
| 类型 | 模式示例 | 提供方 |
|---|---|---|
| AWS 访问密钥 | AKIA...(20 个字符) | AWS |
| AWS 密钥 | 40 位 base64 | AWS |
| GitHub 令牌 | ghp_、gho_、ghu_、ghs_、ghr_ | GitHub |
| GitLab 令牌 | glpat-... | GitLab |
| Slack 令牌 | xoxb-、xoxp-、xoxa- | Slack |
| Stripe 密钥 | sk_live_、rk_live_ | Stripe |
| Twilio | SK...(34 个字符) | Twilio |
| SendGrid | SG. 后接 base64 | SendGrid |
| 私钥 | -----BEGIN (RSA|EC|DSA)?PRIVATE KEY----- | 各类 |
| Google API 密钥 | AIza...(39 个字符) |
中置信度模式
可能需要结合上下文验证:
| 类型 | 模式 | 说明 |
|---|---|---|
| 通用 API 密钥 | api[_-]?key.*=.*['"][a-zA-Z0-9]{16,} | 变量名 |
| 通用密钥 | secret.*=.*['"][^'"]+ | 需要结合上下文 |
| 密码 | password.*=.*['"][^'"]+ | 可能是配置项 |
| 连接字符串 | ://[^:]+:[^@]+@ | 数据库凭据 |
| Bearer 令牌 | Bearer [a-zA-Z0-9_-]+ | 位于标头/代码中 |
高熵检测
通过熵分析查找潜在密钥:
/secrets-scan --entropy
检测可能属于以下类型的高随机性字符串:
- Base64 编码的密钥
- 十六进制编码的令牌
- 自定义 API 密钥格式
检测模式
云服务提供商密钥
# AWS
AKIA[0-9A-Z]{16} # Access Key ID
[A-Za-z0-9/+=]{40} # Secret Access Key (context needed)
# Azure
[a-zA-Z0-9+/=]{88} # Storage Account Key
# GCP
AIza[0-9A-Za-z_-]{35} # API Key
[0-9]+-[a-z0-9]{32}\.apps\.googleusercontent\.com # OAuth Client
版本控制令牌
# GitHub
gh[pousr]_[A-Za-z0-9]{36,} # Personal/OAuth/User/Repo/App
github_pat_[A-Za-z0-9]{22}_[A-Za-z0-9]{59} # Fine-grained PAT
# GitLab
glpat-[A-Za-z0-9-_]{20,} # Personal Access Token
# Bitbucket
[a-zA-Z0-9]{24} # App Password (context needed)
支付与金融
# Stripe
sk_live_[a-zA-Z0-9]{24,} # Secret Key
rk_live_[a-zA-Z0-9]{24,} # Restricted Key
pk_live_[a-zA-Z0-9]{24,} # Publishable Key
# Square
sq0[a-z]{3}-[A-Za-z0-9_-]{22,} # Access Token
# PayPal
access_token\$[a-zA-Z0-9-_.]+ # OAuth Token
通信服务
# Slack
xox[bpas]-[0-9]{10,}-[a-zA-Z0-9]{24,} # Bot/User/App Token
# Twilio
SK[a-f0-9]{32} # API Key SID
[a-f0-9]{32} # Auth Token (context)
# SendGrid
SG\.[a-zA-Z0-9_-]{22}\.[a-zA-Z0-9_-]{43} # API Key
数据库连接字符串
# PostgreSQL/MySQL
(postgres|mysql|mariadb)://[^:]+:[^@]+@[^/]+/\w+
# MongoDB
mongodb(\+srv)?://[^:]+:[^@]+@
# Redis
redis://:[^@]+@
私钥
-----BEGIN (RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----
-----BEGIN PGP PRIVATE KEY BLOCK-----
JWT 与会话
eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+ # JWT
扫描选项
基本扫描
/secrets-scan
仅扫描高置信度模式。
熵分析
/secrets-scan --entropy
添加高熵字符串检测(发现更多内容,也会产生一些误报)。
指定范围
/secrets-scan --scope src/api/
/secrets-scan --scope "*.ts"
Git 历史扫描
/secrets-scan --git-history
/secrets-scan --git-history --since "2024-01-01"
扫描提交历史,查找曾提交后又被移除的密钥。
排除模式
/secrets-scan --exclude "*.test.ts" --exclude "fixtures/"
输出格式
发现报告
SECRETS SCAN RESULTS
====================
High-Confidence Findings: 2
Medium-Confidence Findings: 5
Entropy Findings: 3
[!] CRITICAL: AWS Access Key
File: src/config/aws.ts:15
Pattern: AKIAIOSFODNN7EXAMPLE
Action: Rotate immediately, check CloudTrail
[!] CRITICAL: GitHub Token
File: .env.example:8
Pattern: ghp_xxxx...xxxx (redacted)
Action: Revoke token, remove from history
[H] HIGH: Database Password
File: docker-compose.yml:23
Pattern: password: supersecret
Action: Use environment variable
[M] MEDIUM: Possible API Key
File: src/services/api.ts:44
Pattern: apiKey = "a1b2c3..."
Context: May be test value
汇总统计
Files scanned: 342
Patterns checked: 127
Time elapsed: 2.3s
By Severity:
Critical: 2
High: 5
Medium: 8
By Type:
Cloud credentials: 2
API keys: 4
Passwords: 3
Private keys: 1
Other: 5
误报处理
常见误报
-
示例/占位值
AKIAIOSFODNN7EXAMPLE(AWS 示例值)sk_test_...(Stripe 测试密钥)your-api-key-here
-
测试夹具
- 测试文件中的模拟凭据
- 夹具数据
-
文档
- README 示例
- API 文档
忽略文件
创建 .secrets-scan-ignore:
# Ignore test fixtures
**/fixtures/**
**/__mocks__/**
*.test.ts
*.spec.js
# Ignore documentation
docs/**
*.md
# Ignore specific false positives
src/constants.ts:EXAMPLE_KEY
# Inline ignore comment
# secrets-scan-ignore: test fixture
行内忽略
// secrets-scan-ignore: example value
const EXAMPLE_KEY = "AKIAIOSFODNN7EXAMPLE";
修复步骤
发现密钥时
-
立即采取的措施
- 立即轮换凭据
- 检查访问日志,确认是否存在未授权使用
- 从代码/配置中移除
-
清理 Git 历史
# Remove secret from history git filter-branch --force --index-filter \ 'git rm --cached --ignore-unmatch path/to/file' \ --prune-empty --tag-name-filter cat -- --all # Or use BFG Repo Cleaner bfg --replace-text secrets.txt repo.git -
防止未来提交
- 添加提交前钩子
- 在 CI 中配置密钥扫描
预防措施
# Install pre-commit hook
npx husky add .husky/pre-commit "npx secrets-scan --staged"
集成
CI/CD 流水线
# GitHub Actions
- name: Secrets Scan
run: |
/secrets-scan --fail-on-findings
exit $?
# Exit codes:
# 0 = No findings
# 1 = Findings detected
# 2 = Error during scan
提交前钩子
#!/bin/sh
# .husky/pre-commit
files=$(git diff --cached --name-only)
/secrets-scan --files "$files"
相关技能
/security-scan- 全面安全分析/config-scan- 配置安全/dependency-scan- 软件包漏洞
评论
支持 Markdown