nobuo-miura/SecretLens
GitHub: nobuo-miura/SecretLens
一款支持扫描 Git 历史、CI 日志、环境文件和 Docker 镜像层的密钥泄露检测 CLI 工具。
Stars: 0 | Forks: 0
# SecretLens
[English](README.md) | [日本語](README.ja.md)
[](https://github.com/nobuo-miura/SecretLens/actions/workflows/ci.yml)
[](go.mod)
[](https://goreportcard.com/report/github.com/nobuo-miura/SecretLens)
[](LICENSE.md)
## 安装
### go install
```
go install github.com/nobuo-miura/SecretLens/cmd/secretlens@latest
```
### 从源码构建
```
git clone https://github.com/nobuo-miura/SecretLens.git
cd SecretLens
make build # creates bin/secretlens
make install # installs to $GOPATH/bin/secretlens
```
## 快速开始
```
# 扫描 Git 历史和环境文件(默认)
secretlens scan .
# 显式扫描 Git 历史和环境文件
secretlens scan --all .
# 扫描来自 GitHub Actions 的 CI 日志
secretlens scan --source=cilog --repo=owner/repo
# 扫描 Docker 镜像层
secretlens scan --source=docker --image=myapp:latest
# 写入 SARIF 输出
secretlens scan --format=sarif --out=results.sarif .
# 生成 HTML 报告
secretlens scan --format=html --out=report.html .
# 当发现结果达到严重性阈值时返回退出代码 1
secretlens scan --fail-on=HIGH .
```
## 命令参考
### `secretlens scan`
| 标志 | 描述 | 默认值 |
|------|-------------|---------|
| `--all` | 扫描 Git 历史和环境文件 | false |
| `--source` | 扫描来源:`git` `envfile` `cilog` `docker` | git+envfile |
| `--format` | 输出格式:`text` `json` `sarif` `html` `github-pr` | text |
| `--out` | 输出文件路径。省略时写入 stdout | - |
| `--fail-on` | 达到或超过指定严重程度时以代码 1 退出:`CRITICAL` `HIGH` `MEDIUM` `LOW` | - |
| `--rules-dir` | 包含 YAML 规则的目录 | 可执行文件旁的 `rules/` |
| `--baseline` | Baseline 文件路径 | `.secretlens.baseline.json` |
| `--repo` | `owner/repo` 格式的 GitHub 仓库,供 `cilog` 使用 | - |
| `--image` | Docker 镜像名称,供 `docker` 使用 | - |
| `--pr` | 用于发表 PR 评论的 Pull request 编号 | - |
| `--sha` | 用于创建 Check Run 的 Commit SHA | - |
| `--github-token` | GitHub API token。也支持 `GITHUB_TOKEN` | - |
| `--slack-webhook` | Slack webhook URL。也支持 `SLACK_WEBHOOK_URL` | - |
| `--verify` | 对检测到的 secret 运行实时 API 验证(选择启用) | false |
### `secretlens org`
并发扫描 GitHub Organization 中的所有 repository。
```
secretlens org --org=my-company --format=html --out=audit.html
```
| 标志 | 描述 | 默认值 |
|------|-------------|---------|
| `--org` | GitHub Organization 名称(必填) | - |
| `--token` | GitHub API token。也支持 `GITHUB_TOKEN` | - |
| `--concurrency` | 并发扫描数 | 4 |
| `--format` | 输出格式:`text` `json` `html` | text |
| `--out` | 输出文件路径 | - |
### `secretlens baseline`
```
secretlens baseline list # list registered fingerprints
secretlens baseline update # show baseline update guidance
```
### `secretlens rules list`
```
secretlens rules list # list enabled rules with ID, severity, and name
```
## 评分与严重程度
每个发现都会获得一个分数,然后映射到相应的严重程度。
| 条件 | 分数 |
|-----------|-------|
| 基础规则:CRITICAL | +60 |
| 基础规则:HIGH | +40 |
| 基础规则:MEDIUM | +20 |
| 实时验证通过 | +50 |
| 熵 > 4.5 | +20 |
| 敏感文件名(如 `.env` 或 `credentials`) | +15 |
| 测试代码 | -30 |
| 注释行 | -20 |
| 分数 | 严重程度 |
|-------|----------|
| 60 及以上 | CRITICAL |
| 40-59 | HIGH |
| 20-39 | MEDIUM |
| 低于 20 | LOW |
## 自定义规则
在 `rules/` 下添加 YAML 文件以扩展规则集。
```
# rules/my-company.yaml
rules:
- id: myco-internal-token
name: MyCompany Internal Token
severity: CRITICAL
pattern: 'MYCO_[A-Za-z0-9]{32}'
entropy_min: 4.0
context_exclude:
- "**/*_test.go"
- "**/testdata/**"
tags:
- myco
- internal
```
### 规则 Schema
| 字段 | 类型 | 描述 |
|-------|------|-------------|
| `id` | string | 唯一规则 ID(必填) |
| `name` | string | 显示名称(必填) |
| `severity` | string | `CRITICAL` `HIGH` `MEDIUM` `LOW`(必填) |
| `pattern` | string | 使用 Go regexp 语法的正则表达式(必填) |
| `entropy_min` | float | 最小熵阈值(可选) |
| `context_exclude` | []string | 排除的 glob 模式(可选) |
| `tags` | []string | 标签(可选) |
## Baseline 管理
将已知的误报添加到 baseline 文件中,以便将来的扫描可以忽略它们。
```
# 检查 fingerprints
secretlens scan --format=json . | jq -r '.[].fingerprint'
# 手动将 fingerprints 添加到 .secretlens.baseline.json
{
"fingerprints": {
"abc123...": true
}
}
```
## GitHub Actions 集成
```
# .github/workflows/secretlens.yml
name: Secret Scan
on: [push, pull_request]
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # fetch full Git history
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Install SecretLens
run: go install github.com/nobuo-miura/SecretLens/cmd/secretlens@latest
- name: Scan
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
secretlens scan \
--all \
--format=sarif \
--out=results.sarif \
--fail-on=HIGH \
.
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: results.sarif
- name: PR Comment
if: github.event_name == 'pull_request'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
secretlens scan \
--all \
--format=github-pr \
--repo=${{ github.repository }} \
--pr=${{ github.event.pull_request.number }} \
--sha=${{ github.sha }} \
.
```
## 内置规则
| 文件 | 覆盖范围 |
|------|----------|
| `rules/aws.yaml` | AWS Access Key ID / Secret Access Key |
| `rules/gcp.yaml` | GCP Service Account Key / API Key |
| `rules/azure.yaml` | Azure Storage Account Key / Connection String |
| `rules/jwt.yaml` | JWT Token |
| `rules/generic.yaml` | API Key / Password / Token / Private Key / Connection String |
## 开发
```
make test # run tests
make check # run vet, tests, and lint
make run-scan # run a sample scan
make report-html # generate and open an HTML report
make release # cross-compile for major platforms
make help # show available commands
```
### 目录结构
```
secretlens/
|-- cmd/secretlens/ # CLI entrypoint (cobra)
|-- internal/
| |-- scanner/
| | |-- git/ # streaming parser for git log --all -p
| | |-- cilog/ # GitHub Actions / GitLab CI log APIs
| | |-- envfile/ # scan .env / .tfvars / *.yaml files
| | `-- docker/ # scan Docker image layers
| |-- detector/
| | |-- regex/ # YAML rule loading and regex matching
| | |-- entropy/ # Shannon entropy calculation
| | |-- context/ # exclude test code and comments
| | `-- verifier/ # live API verification for AWS / GCP / GitHub
| |-- finding/ # Finding type and scoring
| |-- reporter/
| | |-- sarif/ # SARIF v2.1.0 output
| | |-- github/ # PR comments and Check Run API
| | |-- slack/ # Slack webhook notifications (Block Kit)
| | `-- html/ # interactive HTML reports
| |-- baseline/ # .secretlens.baseline.json management
| `-- org/ # GitHub Organization audit mode
|-- rules/ # built-in YAML rules
`-- testdata/ # sample files for tests
```
## 许可证
MIT 许可证
标签:DevSecOps, EVTX分析, Go, Ruby工具, StruQ, 上游代理, 对抗攻击, 敏感信息检测, 日志审计, 请求拦截