Lappy000/keyleak
GitHub: Lappy000/keyleak
keyleak 是一个命令行 API 密钥泄露验证器,能自动识别密钥所属服务并通过安全的只读调用验证其是否仍然有效,帮助安全团队快速评估泄露凭证的真实风险。
Stars: 0 | Forks: 0
# 🔑 keyleak
[](https://www.python.org/downloads/)
[](https://opensource.org/licenses/MIT)
[](https://github.com/psf/black)
[](#supported-services)
**API 密钥泄露验证器** — 从命令行检测并验证 10 多种服务的泄露凭证。
`keyleak` 接收 API 密钥(来自 stdin、文件或直接输入),自动识别其所属服务,并通过发起安全的只读 API 调用来验证其是否仍然有效。
## 🚀 安装说明
```
# 克隆仓库
git clone https://github.com/Lappy000/keyleak.git
cd keyleak
# 以开发模式安装
pip install -e .
# 或直接安装依赖
pip install -r requirements.txt
```
## 📖 用法
### 从 stdin 管道输入密钥
```
# 单个 key
echo 'sk-abc123xyz456...' | keyleak
# 多个 key(每行一个)
cat leaked_keys.txt | keyleak
# 从剪贴板读取 (macOS)
pbpaste | keyleak
```
### 扫描文件中的密钥
```
# 扫描单个文件
keyleak scan secrets.txt
# 扫描多个文件
keyleak scan .env config.yml dump.txt
# 使用 glob 模式扫描
keyleak scan *.env
```
### 直接检查单个密钥
```
# 自动检测服务类型
keyleak check 'ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# 强制指定服务
keyleak check --service stripe 'sk_live_xxxxxx'
```
### 批处理模式
```
# 从 stdin 逐行处理 key
cat keys.txt | keyleak batch
# 输出 JSON
cat keys.txt | keyleak --json batch > results.json
```
### 输出格式
```
# Rich 表格(默认)
keyleak scan dump.txt
# JSON 输出
keyleak --json scan dump.txt
# 紧凑单行格式
keyleak --compact scan dump.txt
# 带进度条的详细输出
keyleak -v scan dump.txt
```
### 按服务筛选
```
# 仅检查 GitHub 和 AWS key
keyleak -s github -s aws scan dump.txt
```
### 列出支持的服务
```
keyleak services
```
## 📋 支持的服务
| 服务 | 密钥前缀 | 验证方法 | Endpoint |
|---------|-----------|-------------------|----------|
| **AWS** | `AKIA...` | STS GetCallerIdentity | `sts.amazonaws.com` |
| **OpenAI** | `sk-...` | 列出模型 | `GET /v1/models` |
| **Anthropic** | `sk-ant-...` | 发送最小化消息 | `POST /v1/messages` |
| **GitHub** | `ghp_` / `gho_` / `github_pat_` | 获取用户 | `GET /user` |
| **Stripe** | `sk_live_` / `sk_test_` | 列出 charges | `GET /v1/charges?limit=1` |
| **Slack** | `xoxb-` / `xoxp-` | 身份验证测试 | `POST auth.test` |
| **SendGrid** | `SG.xxx.xxx` | 获取 profile | `GET /v3/user/profile` |
| **Twilio** | `SK...` (34 chars) | 列出账户 | `GET /2010-04-01/Accounts` |
| **Telegram** | `:` | 获取 bot 信息 | `GET /bot/getMe` |
| **DigitalOcean** | `dop_v1_...` | 获取账户 | `GET /v2/account` |
## 🔧 添加新的验证器
1. 在 `keyleak/validators/` 目录下创建一个新文件:
```
# keyleak/validators/myservice_key.py
"""MyService key validator."""
import time
import httpx
from keyleak.validators import KeyStatus, ValidationResult
def validate(key: str) -> ValidationResult:
"""Validate a MyService API key."""
start_time = time.time()
# Make a safe API call
headers = {"Authorization": f"Bearer {key}"}
try:
with httpx.Client(timeout=15.0) as client:
response = client.get("https://api.myservice.com/verify", headers=headers)
elapsed = (time.time() - start_time) * 1000
if response.status_code == 200:
return ValidationResult(
key_value=key,
service="myservice",
status=KeyStatus.VALID,
message="Key is valid!",
http_status=200,
response_time_ms=round(elapsed, 2),
)
# ... handle other status codes
except Exception as exc:
elapsed = (time.time() - start_time) * 1000
return ValidationResult(
key_value=key,
service="myservice",
status=KeyStatus.ERROR,
message=f"Error: {exc}",
response_time_ms=round(elapsed, 2),
)
```
2. 在 `keyleak/detector.py` 中添加匹配模式:
```
KeyPattern(
service="myservice",
pattern=re.compile(r"ms_[A-Za-z0-9]{32}"),
description="MyService API Key",
validator_module="keyleak.validators.myservice_key",
prefix_hint="ms_",
min_length=35,
max_length=35,
),
```
3. 在 `keyleak/validators/__init__.py` 中注册:
```
_VALIDATOR_MODULES = {
...
"myservice": "keyleak.validators.myservice_key",
}
```
## ⚠️ 免责声明
此工具专为**安全专业人员**和**授权的**泄露验证而设计。请勿使用此工具来利用泄露的凭证。如果您发现有效的密钥,请负责任地向密钥所有者报告。
## 🏗️ 项目结构
```
keyleak/
├── keyleak/
│ ├── __init__.py # Package metadata
│ ├── cli.py # Click-based CLI with subcommands
│ ├── detector.py # Regex-based key type detection
│ ├── output.py # Rich table + JSON formatters
│ └── validators/
│ ├── __init__.py # Base classes + dynamic loader
│ ├── aws.py # AWS STS validation
│ ├── openai_key.py # OpenAI /v1/models
│ ├── anthropic_key.py # Anthropic /v1/messages
│ ├── github_key.py # GitHub /user
│ ├── stripe_key.py # Stripe /v1/charges
│ ├── slack_key.py # Slack auth.test
│ ├── sendgrid_key.py # SendGrid /v3/user/profile
│ ├── twilio_key.py # Twilio /2010-04-01/Accounts
│ ├── telegram_key.py # Telegram /getMe
│ └── digitalocean_key.py # DO /v2/account
├── README.md
├── pyproject.toml
├── requirements.txt
├── .gitignore
└── LICENSE
```
## 📄 许可证
MIT — 详见 [LICENSE](LICENSE)。
标签:API安全, API密钥审计, ESC4, JSON输出, meg, OSINT, Python, StruQ, 信息安全, 凭证验证, 密钥安全, 密钥泄露检测, 开发安全, 无后门, 无服务器架构, 网络安全, 自动化安全检测, 运行时操纵, 逆向工具, 隐私保护