antwnakys/log-sentinel
GitHub: antwnakys/log-sentinel
一款结合本地启发式检测与 Claude AI 解释的 SSH 日志异常分析工具,帮助快速分诊暴力破解等安全事件。
Stars: 0 | Forks: 0
# 🛰️ log-sentinel
一个 **基于 AI 的日志异常解释器**。它会扫描 SSH 认证日志,通过快速的本地启发式算法标记
可疑活动,然后使用 **Claude** 用通俗易懂的语言解释
每起事件——包括严重等级、置信度,以及
具体的防御措施。
把它想象成一个不知疲倦的初级 SOC 分析师:它执行初步筛查,这样
人类就不必手动阅读成千上万行日志了。
```
[1] SUCCESSFUL-BRUTE-FORCE (critical)
Source IP 198.51.100.23. 6 failed logins, 1 successful login...
→ Attack : SSH brute-force resulting in account compromise
→ Severity : critical (confidence: high)
→ Analysis : Six failed root logins followed by a success from the same
IP strongly indicates a brute-force attack that succeeded.
The attacker likely now has root access to this host.
→ Actions :
- Immediately isolate web01 and rotate all credentials
- Block 198.51.100.23 at the firewall
- Disable password authentication; require SSH keys
- Audit for persistence (new users, cron jobs, SSH keys)
```
## 为什么采用这种设计?
真正的安全管道不会将原始日志发送给昂贵的模型。它们会**先进行低成本过滤,然后再进行高成本推理**:
1. **检测(本地、免费、即时)。** 确定性规则会解析日志并
按源 IP 对事件进行分组,标记出暴力破解、用户枚举和
暴力破解成功等模式。这可以在处理海量日志时无需网络且
零成本。
2. **解释(AI,仅针对已标记的内容)。** *只有少量被标记的集群*
会被发送给 Claude——绝不会发送整个日志。模型会返回 **结构化的
JSON** 分析结果(攻击类型、严重程度、置信度、解释、措施),因此
输出始终是机器可读的,而不是自由格式的文本。
这使得它成本低廉、速度快且注重隐私,同时仍能为人类提供
一个清晰、按优先级排列的事件全貌。
## 检测内容
| 模式 | 含义 | 启发式严重程度 |
| --- | --- | --- |
| **successful-brute-force** | 一个 IP 在多次失败后成功登录——可能已被入侵 | 🟣 critical |
| **ssh-brute-force** | 来自一个 IP 的多次失败登录 | 🔴 high |
| **user-enumeration** | 来自一个 IP 的多次尝试不同/不存在的用户名(侦察) | 🟡 medium |
启发式严重程度只是一个初步猜测;Claude 会根据
实际证据**细化**攻击类型和
严重程度。
## 用法
### 安装
```
pip install -r requirements.txt # installs the anthropic SDK
export ANTHROPIC_API_KEY="sk-ant-..." # your key; never commit this
```
### 运行
```
# 分析 log file,附带 AI 解释:
python -m logsentinel /var/log/auth.log
# 在 bundled sample 上立即试用:
python -m logsentinel --sample
# 仅使用 Heuristics —— 完全离线,无需 API key 或网络:
python -m logsentinel --no-explain --sample
```
如果未设置 `ANTHROPIC_API_KEY`,该工具会自动回退到
仅启发式模式并提示你——检测器本身也始终具有实用价值。
当发现 high/critical 级别的活动时,退出代码为**非零**,因此你可以将其
接入 cron job 或 CI 检查:
```
python -m logsentinel /var/log/auth.log || echo "Investigate now!"
```
## AI 调用的工作原理
解释器使用带有 **structured outputs** 的官方 Anthropic Python SDK,
因此 Claude 的回复始终符合固定的 schema:
```
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=2000,
system=SYSTEM_PROMPT, # "You are a senior SOC analyst..."
messages=[{"role": "user", "content": prompt}],
output_config={"format": {"type": "json_schema", "schema": SCHEMA}},
)
```
API key 从环境中读取,从不硬编码或记录在日志中。
## 运行测试
```
pip install pytest
pytest
```
测试套件**完全离线运行**:检测器测试使用纯日志字符串,
而 AI 解释器测试注入了一个伪造的 client,因此无需
API key 或网络即可验证逻辑。
## 项目结构
```
log-sentinel/
├── logsentinel/
│ ├── detect.py # heuristic detection (pure stdlib, no network)
│ ├── explain.py # AI explanation via Claude (structured output)
│ └── cli.py # command-line interface
├── tests/ # pytest suite (API mocked)
└── samples/auth.log # example log with brute-force + enumeration
```
## 限制与说明
- 专注于 **OpenSSH 认证日志**;其他日志格式需要在
`detect.py` 中添加新的解析器。两阶段设计使得添加它们变得非常简单。
- 启发式阈值简单且可调——生产系统还会
考虑时间窗口和已知的可信 IP 白名单。
- 仅适用于**你自己的日志**。这是一个防御性分析工具。
## License
[MIT](LICENSE) — 可免费使用、学习和在此基础上进行开发。
标签:AI辅助分析, DLL 劫持, SSH, 大语言模型, 安全规则引擎, 安全运营, 扫描框架, 文档结构分析, 逆向工具