wdymjedai/logsentry
GitHub: wdymjedai/logsentry
一个采用 Sigma 风格 YAML 规则的安全日志检测引擎,通过时间窗口关联分析 auth/web 日志以发现暴力破解、扫描和注入等威胁活动。
Stars: 0 | Forks: 0
# logsentry
[](https://github.com/wdymjedai/logsentry/actions/workflows/ci.yml)
[](https://www.python.org/)
[](LICENSE)
**面向安全日志的检测即代码(Detection-as-code)。** 编写人类易读的 YAML 检测
规则,并在您的 auth/web 日志上运行它们,以揭示暴力破解、扫描、
注入和权限提升活动 —— 借助**时间窗口关联**,
而不仅仅是单行 grep。
该规则格式特意采用 [Sigma](https://sigmahq.io/) 风格设计,因此
这些概念可以直接迁移到真实的 SIEM 工作流中。
## 为什么它比 grep 更强大
- **关联窗口。** “60秒内来自同一 IP 的 5 次 SSH 登录失败”
可以作为单条规则,并针对每个组使用滑动窗口进行评估。
- **可插拔解析器。** Linux `sshd`/`sudo` auth 日志、Apache/Nginx 组合
access 日志以及换行符分隔的 JSON 都会被标准化为通用的事件结构。
- **规则即数据。** 检测规则存在于可版本控制的 YAML 中,而不是代码中 ——
只需在 `rules/` 目录下放入一个文件即可添加规则。
- **对 CI/监控友好。** 支持 `--json` 输出和 `--fail-on-alert` 退出码。
## 安装
```
git clone https://github.com/wdymjedai/logsentry.git
cd logsentry
pip install -e . # installs the `logsentry` command (pulls in PyYAML)
```
## 快速开始(使用内置示例)
```
# 使用所有已发布的规则搜寻样本 auth log
logsentry samples/auth.log --rules rules/ --source auth
# 搜寻样本 access log
logsentry samples/access.log --rules rules/ --source access
# 用于 dashboard 的 JSON 输出,并在触发任何内容时使进程失败
logsentry samples/auth.log -r rules/ -s auth --json --fail-on-alert
```
### 示例
```
3 alert(s) HIGH:1 MEDIUM:2
[HIGH] SSH brute-force attempt (ssh_bruteforce)
when : 2026-06-18T09:15:11+00:00
why : 6 events in window for src_ip=192.168.1.50 (threshold 5/60s). ...
log : Jun 18 09:15:11 web01 sshd[2016]: Failed password for root from 192.168.1.50 ...
```
## 编写规则
在您的规则目录中放入一个 `.yml` 文件:
```
id: ssh_bruteforce
title: SSH brute-force attempt
severity: high
description: Many failed SSH logins from one source IP.
logsource: auth # auth | access | json
selection: # all keys AND together
event: # a list means OR
- ssh_failed_login
- ssh_invalid_user
threshold: # optional time-window correlation
group_by: [src_ip]
count: 5
window_seconds: 60
tags: [t1110]
```
操作符的写法为 `字段|操作符`:
| 选择键 | 匹配条件… |
|----------------------|--------------------------------------------|
| `status: "404"` | 字段等于该值(精确匹配) |
| `path|contains: "../"` | 字段包含该子串 |
| `ua|re: "(?i)sqlmap"` | 字段匹配该正则表达式 |
| `method: [GET, POST]` | 字段等于列表中的任意一项(OR) |
如果没有设置 `threshold`,每个匹配的事件都会触发警报。如果设置了,
引擎会根据 `group_by` 对匹配项进行分组,并且仅当 `count` 个事件落在
`window_seconds` 时间窗口内时才触发警报。
## 内置规则
| 规则 | 来源 | 捕获内容 |
|------|--------|-----------------|
| `ssh_bruteforce` | auth | 60秒内每个 IP 出现 ≥5 次 SSH 登录失败 |
| `ssh_invalid_user` | auth | 针对不存在账户的登录尝试 |
| `sudo_brute` | auth | 120秒内每个用户出现 ≥3 次 sudo 认证失败 |
| `web_dir_scanning` | access | 60秒内每个 IP 出现 ≥15 次 `404` |
| `web_sqli_attempt` | access | 请求中包含 SQL 注入特征 |
| `web_path_traversal` | access | `../` 目录遍历 |
| `web_suspicious_useragent` | access | 已知的扫描器 user-agents |
## 项目结构
```
logsentry/
├── logsentry/
│ ├── models.py # Event, Rule, Threshold, Alert, Severity
│ ├── parsers.py # auth / access / json -> normalized Events
│ ├── rules.py # YAML loading + validation
│ ├── engine.py # matching + sliding-window correlation
│ ├── report.py # text / JSON output
│ └── cli.py # argparse entrypoint
├── rules/ # bundled detection rules (YAML)
├── samples/ # example logs to run against
└── tests/ # unit + end-to-end tests
```
## 开发
```
pip install -e ".[dev]"
ruff check .
pytest
```
## 许可证
[MIT](LICENSE) © 2026 wdymjedai
标签:CSV导出, LNA, Python, 动态分析, 安全, 安全规则引擎, 恶意代码分类, 插件系统, 无后门, 检测引擎, 超时处理, 逆向工具