defacesh/LogHunter

GitHub: defacesh/LogHunter

一款轻量级高性能安全日志异常分析器,通过正则模式匹配从多种日志中自动检测 Web 攻击和暴力破解行为,并以富终端界面和 JSON 格式输出结构化威胁报告。

Stars: 0 | Forks: 0

# 🔍 LogHunter **高性能安全日志异常分析器** ## ✨ 功能 | 功能 | 详情 | | ---------------------- | ------------------------------------------------------------------------------------------ | | **攻击检测** | SQL Injection, XSS, Path Traversal, Command Injection, Brute-Force, Scanner 指纹识别 | | **日志类型** | SSH 认证日志, Apache/Nginx 访问日志, 通用 syslog | | **内存效率** | 基于生成器的行流处理 — 以恒定 RAM 处理多 GB 文件 | | **富终端界面** | 颜色编码的严重级别表,攻击分类,Top-Offenders 排名 | | **暴力破解引擎** | 按 IP 统计的失败认证计数器,阈值可配置 | | **JSON 导出** | 完整的结构化报告,用于 SIEM/pipeline 集成 | | **OOP + 类型提示** | 整洁、带类型、模块化的架构 — 适合作为代码作品集展示 | ## 🏗️ 架构 ``` loghunter.py ├── LogEntry (dataclass) — normalised single-line detection ├── AttackSummary (dataclass) — aggregated run statistics ├── LogAnalyzer (class) — generator streaming + pattern matching ├── Reporter (class) — Rich terminal rendering └── JsonExporter (class) — structured JSON output ``` **设计原则:** - **关注点分离** — 分析和展示完全解耦 - **生成器管道** — `_stream_lines()` 从不将整个文件加载到内存中 - **类型化接口** — 每个公共方法都带有完整的 PEP 484 类型提示 - **Google 风格 docstrings** — 全面记录 Args / Returns / Raises ## 🚀 快速入门 ### 1 — 克隆并安装 ``` git clone https://github.com/defacesh/LogHunter.git cd LogHunter pip install -r requirements.txt ``` ### 2 — 针对日志文件运行 ``` # SSH 暴力破解检测 python loghunter.py -f /var/log/auth.log -t auth # 带有 JSON 导出的 Web 攻击检测 python loghunter.py -f /var/log/nginx/access.log -t access -o report.json # 自定义暴力破解阈值(3 次失败 = 标记) python loghunter.py -f /var/log/syslog -t syslog --threshold 3 ``` ## ⚙️ CLI 参考 ``` usage: loghunter [-h] -f PATH -t TYPE [-o FILE] [--threshold N] [--no-banner] options: -f, --file PATH Log file to analyse (required) -t, --type TYPE Log type: auth | access | syslog (required) -o, --output FILE Export findings to JSON --threshold N Failed-auth count per IP for brute-force flag (default: 5) --no-banner Suppress ASCII art banner ``` ## 🎯 检测到的攻击 | 攻击类型 | 严重级别 | 示例模式 | | ----------------- | ----------- | -------------------------------------------------- | ---------------------------- | | SQL Injection | 💀 危急 (CRITICAL) | `UNION SELECT`, `1=1`, `DROP TABLE`, `xp_cmdshell` | | XSS | 💀 危急 (CRITICAL) | ` HTTP/1.1" 200 128 172.16.0.200 - - [12/Mar/2026:09:16:22 +0000] "GET /../../../../etc/passwd HTTP/1.1" 403 64 192.168.1.105 - - [12/Mar/2026:09:16:55 +0000] "GET /admin HTTP/1.1" 401 0 192.168.1.105 - - [12/Mar/2026:09:16:56 +0000] "GET /admin HTTP/1.1" 401 0 192.168.1.105 - - [12/Mar/2026:09:16:57 +0000] "GET /admin HTTP/1.1" 401 0 192.168.1.105 - - [12/Mar/2026:09:16:58 +0000] "GET /admin HTTP/1.1" 401 0 192.168.1.105 - - [12/Mar/2026:09:16:59 +0000] "GET /admin HTTP/1.1" 401 0 EOF python loghunter.py -f test_access.log -t access --threshold 3 ``` ## 📁 项目结构 ``` loghunter/ ├── loghunter.py # Main tool — all logic in one portable file ├── requirements.txt # Single runtime dependency (rich) └── README.md # This file ``` ## 🔧 扩展 LogHunter **添加新的攻击模式** — 编辑 `loghunter.py` 中的 `ATTACK_PATTERNS` 字典: ``` "MY_ATTACK": { "pattern": re.compile(r"my_regex_here", re.IGNORECASE), "severity": "HIGH", # CRITICAL | HIGH | WARNING | INFO "label": "My Attack Name", }, ``` **添加新的日志类型** — 在 `build_argument_parser()` 中扩展 `--type` 选项,并在 `LogAnalyzer._classify_line()` 中添加任何特定类型的提取逻辑。 ## 📋 依赖要求 | 依赖要求 | 版本 | | ----------- | -------- | | Python | ≥ 3.10 | | rich | ≥ 13.7.0 | ## 📜 许可证 MIT License — 可免费使用、修改和分发。 _专为需要从原始日志数据中获取快速、可靠且视觉清晰的威胁情报的网络安全分析师而构建。_
标签:CISA项目, DOE合作, JSON导出, OOP, PFX证书, Python, Rich库, SQL注入检测, Web安全, XSS检测, 免杀技术, 内存优化, 命令注入检测, 异常检测, 攻击检测, 攻击路径可视化, 数据统计, 无后门, 暴力破解检测, 生成器, 端口扫描, 类型提示, 系统运维, 网络安全, 蓝队分析, 路径遍历检测, 逆向工具, 隐私保护, 黑客工具