NikoloziKhachiashvili/nids

GitHub: NikoloziKhachiashvili/nids

基于Python的轻量级网络入侵检测系统,通过Scapy实时捕获流量并结合YAML规则引擎检测常见攻击,配备SOC风格终端仪表盘。

Stars: 1 | Forks: 0

# NIDS — 网络入侵检测系统 一个基于 Python CLI 的网络入侵检测系统,支持实时数据包捕获、YAML 规则引擎以及实时 Rich 终端仪表盘。 ![Python](https://img.shields.io/badge/python-3.10%2B-blue) ![License](https://img.shields.io/badge/license-MIT-green) ## 功能特性 - **实时数据包捕获**:通过 Scapy 实现(Windows 上需要管理员权限/Npcap,Linux/macOS 上需要 `sudo`) - **YAML 规则引擎**:无需修改 Python 代码即可编写和加载自定义检测规则 - **11 条内置规则**:SYN flood、端口扫描、ICMP flood、DNS 隧道、明文传输的 HTTP Basic Auth、可疑 User-Agent(sqlmap、nikto、masscan 等)、NULL 扫描、XMAS 扫描、FIN 扫描、Telnet、SMB - **TCP 会话跟踪器**:通过 `(src_ip, dst_ip, src_port, dst_port)` 重组流 - **威胁评分**:每个警报都包含基于触发信号数量的置信度百分比 - **SQLite 持久化**:所有警报保存到 `alerts.db` 并附带完整元数据 - **GeoIP 查询**:通过 ip2location 进行国家解析(可选,见下文) - **SOC 风格终端仪表盘**:实时自动刷新的 Rich UI,包含警报流、Top Talkers、严重程度条、PPS 计数器 - **退出时生成报告**:Markdown 摘要 + JSON 警报转储保存到 `reports/` - **pcap 重放模式**:重放 `.pcap` 文件而非实时捕获,用于离线分析 ## 环境要求 - Python 3.10+ - **Windows**:[Npcap](https://npcap.com/) + 以管理员身份运行 - **Linux/macOS**:使用 `sudo` 运行 ## 安装说明 ``` git clone https://github.com/NikoloziKhachiashvili/nids.git cd nids pip install -e . ``` ### 可选:GeoIP 数据库 从 https://lite.ip2location.com/ 下载免费的 **IP2Location LITE DB1** 二进制文件并将其放置于: ``` data/IP2LOCATION-LITE-DB1.BIN ``` 若未提供,国家查询将回退为 `"Unknown"` —— 其他功能一切正常。 ## 使用说明 ``` # 在特定接口上进行 Live capture python -m nids --interface eth0 # 重放 pcap 文件 (无需 admin) python -m nids --pcap capture.pcap # 自定义 rules 文件、database 和 BPF filter python -m nids --interface eth0 \ --rules rules/default.yml \ --db alerts.db \ --filter "tcp" # All options python -m nids --help ``` | 标志 | 默认值 | 描述 | |------|---------|-------------| | `-i / --interface` | 系统默认 | 用于捕获的网络接口 | | `-r / --rules` | `rules/default.yml` | YAML 规则文件路径 | | `-d / --db` | `alerts.db` | SQLite 数据库路径 | | `-p / --pcap` | — | 重放 `.pcap` 文件 | | `-f / --filter` | — | BPF 过滤表达式 | | `--reports` | `reports/` | 报告输出目录 | 按 **Ctrl+C** 停止捕获 —— 系统将自动保存 Markdown 报告和 JSON 警报转储。 ## 仪表盘 ``` ╔══════════════════════════════════════════════════════════════════╗ ║ NIDS · Network Intrusion Detection System ║ ║ Interface eth0 Uptime 00:02:14 Packets 18,432 PPS 142.3 ║ ║ Alerts 7 Started 14:21:05 ⬤ LIVE ║ ╚══════════════════════════════════════════════════════════════════╝ ┌─ ⚡ Live Alert Feed ────────────────┐ ┌─ 🌐 Top Talkers ─────────┐ │ 14:22:31 ● CRIT SYN Flood │ │ 1 192.168.1.105 ██░░ │ │ 1.2.3.4 → :80 ████ 95% │ │ 2 10.0.0.42 █░░░ │ │ 14:22:28 ● HIGH Port Scan │ ├─ 📊 Severity Overview ───┤ │ ... │ │ CRITICAL ████████ 3 │ └────────────────────────────────────┘ │ HIGH █████ 4 │ └──────────────────────────┘ CRITICAL: 3 HIGH: 4 MEDIUM: 0 LOW: 0 │ Packets: 18,432 PPS: 142.3 ``` ## 编写自定义规则 规则位于 `rules/default.yml`。每条规则支持: ``` rules: - name: My Custom Rule protocol: tcp # tcp | udp | icmp | any severity: high # critical | high | medium | low description: "What this detects" enabled: true # set false to disable without deleting conditions: dst_port: 4444 payload_regex: "cmd.exe|powershell" flags: S # TCP flags (S=SYN, A=ACK, F=FIN, R=RST, P=PSH, U=URG) rate_threshold: 100 # packets per rate_window seconds (for flood rules) rate_window: 10 payload_min_length: 512 ``` 规则中的所有条件必须同时匹配(AND 逻辑)。 ## 项目结构 ``` src/nids/ ├── cli.py CLI entry point (Click) ├── capture.py Scapy packet capture + pcap replay ├── rules.py YAML rule loader ├── engine.py Detection engine ├── session.py TCP session tracking + rate counters ├── database.py SQLite persistence ├── geoip.py GeoIP country lookup ├── dashboard.py Rich terminal dashboard ├── reporter.py Markdown + JSON report generation └── models.py Shared dataclasses (Alert, Rule, PacketInfo, …) rules/ └── default.yml 11 built-in detection rules tests/ pytest suite (118 tests, 87% coverage) data/ Place IP2Location DB here (optional) reports/ Auto-generated reports saved here ``` ## 运行测试 ``` pip install pytest pytest-cov pytest # all tests + coverage report pytest tests/test_engine.py # single module pytest -k "syn_flood" # single test by name pytest --no-cov # skip coverage ``` ## 许可证 MIT
标签:AMSI绕过, Beacon Object File, CISA项目, DNS隧道, ICMP Flood, IP 地址批量处理, NIDS, pcap解析, PE 加载器, Python, Scapy, SOC仪表盘, SQLite, SYN Flood, YAML规则引擎, 入侵检测系统, 威胁检测, 安全数据湖, 安全规则引擎, 安全运营, 容器化, 开源安全工具, 恶意流量识别, 扫描框架, 插件系统, 数据统计, 无后门, 流量监控, 端口扫描, 网络安全, 逆向工具, 逆向工程平台, 隐私保护