cyb3rb4t/netwatch
GitHub: cyb3rb4t/netwatch
NetWatch:实时网络流量分析器与威胁检测器
Stars: 1 | Forks: 0
# NetWatch — 网络流量分析器与威胁检测器




## 它能做什么
NetWatch 实时捕获网络数据包(或读取离线 PCAP 文件),通过模块化检测引擎分析每个数据包,根据严重程度等级发出警报,将每个警报映射到 MITRE ATT&CK 技术上,并为每个会话生成 HTML 报告 + JSON 日志。
**这不是 Wireshark 或 Snort 的包装器。** 每个检测规则都是从头开始用 Python 编写的,所以我完全了解每个规则的作用和原因。
## 检测覆盖范围
| 威胁 | 严重程度 | MITRE 技术 | 如何检测 |
|-----------------------|-----------|------------------------|------------------------------------------------|
| SYN 端口扫描 | CRITICAL | T1046 | 10秒内超过15个不同的目标端口 |
| NULL 扫描 | HIGH | T1046 | TCP 标志 = 0x00 |
| XMAS 扫描 | HIGH | T1046 | FIN+PSH+URG 标志设置 (0x29) |
| FIN 扫描 | MEDIUM | T1046 | 仅 FIN 标志,没有之前的握手 |
| ICMP 洪水 | HIGH | T1498 | 5秒内来自单个源点的 >50 个 echo-requests |
| 超大 ICMP | MEDIUM | T1498 | ICMP 有效负载 >1024 字节 |
| DNS 洞穿 | HIGH | T1071.004 | 标签长度 >40 个字符或 Shannon 熵 >3.8 |
| DNS 信标 | HIGH | T1071.004 | 每分钟来自单个源点的 >100 个 DNS 查询 |
| 大数据包 | LOW | T1030 | 单个数据包 >8000 字节 |
| 数据泄露 | MEDIUM | T1030 | 60秒内向外部 IP 发送 >10MB |
## 架构
```
netwatch.py ← CLI entry point, session management
│
├── core/
│ ├── capture.py ← Scapy sniff() + rdpcap() wrapper
│ ├── analyser.py ← Packet routing, stats tracking
│ └── alert_manager.py ← Thread-safe alert store, deduplication, terminal output
│
├── detectors/
│ ├── port_scan.py ← SYN / NULL / XMAS scan detection
│ ├── icmp_flood.py ← ICMP flood + oversized payload
│ ├── dns_tunnel.py ← DNS tunnelling (length + entropy heuristics)
│ ├── null_xmas.py ← TCP flag anomaly detection
│ └── large_payload.py ← Exfiltration volume detection
│
├── reports/
│ └── reporter.py ← HTML dashboard + Splunk-ready JSON
│
├── tests/
│ └── test_detectors.py ← Unit tests (no root needed)
│
└── samples/ ← Sample PCAP files for offline demo
```
每个检测器都是一个包含单个 `analyse(pkt)` 方法的自包含类。添加新的检测规则意味着在 `analyser.py` 中添加一个文件和一行代码。
## 要求
- Windows 10 / 11
- [Npcap](https://npcap.com) — 选择“WinPcap API 兼容模式”进行安装
- Python 3.10+
- 以 **管理员** 身份运行的 PowerShell
## 设置
```
# Install dependencies (PowerShell as Administrator)
pip install -r requirements.txt
# Verify Scapy can see your interfaces
python netwatch.py --list-ifaces
# Run the test suite (no Admin needed for tests)
python -m pytest tests/ -v
```
## 使用方法
```
# List available interfaces first (do this once to find your interface name)
python netwatch.py --list-ifaces
# Live capture on Ethernet (Administrator required)
python netwatch.py --iface "Ethernet"
# Live capture on Wi-Fi for 60 seconds then generate report
python netwatch.py --iface "Wi-Fi" --duration 60
# Analyse an offline PCAP file (no Administrator needed)
python netwatch.py --pcap samples\portscan_demo.pcap
# Live capture with verbose per-packet output
python netwatch.py --iface "Ethernet" --verbose
# Custom output directory
python netwatch.py --iface "Ethernet" --output C:\Users\Prashant\reports\
```
## 无管理员权限测试
完整的测试套件使用精心制作的 Scapy 数据包在无管理员权限的情况下运行:
```
python -m pytest tests/ -v
```
预期输出:
```
tests/test_detectors.py::TestPortScanDetector::test_syn_scan_triggers_after_threshold PASSED
tests/test_detectors.py::TestPortScanDetector::test_syn_scan_below_threshold_no_alert PASSED
tests/test_detectors.py::TestPortScanDetector::test_null_scan_detected PASSED
...
```
## 模拟针对您的 Docker 实验室攻击
在您的 Windows 机器上运行 NetWatch,然后从 WSL2 或 PowerShell 生成测试流量:
```
# Find your Docker lab subnet IP first
docker inspect dvwa | findstr "IPAddress"
# Simulate a SYN port scan from WSL2 (triggers PORT_SCAN alert)
# In WSL2/Kali terminal:
nmap -sS 172.20.0.2
# Simulate a NULL scan (triggers NULL_SCAN alert)
nmap -sN 172.20.0.2
# Simulate a XMAS scan (triggers XMAS_SCAN alert)
nmap -sX 172.20.0.2
# Simulate an ICMP flood from PowerShell (triggers ICMP_FLOOD alert)
for ($i=0; $i -lt 100; $i++) { ping -n 1 172.20.0.2 }
```
所有目标都在您的本地 Docker 实验室网络内 — 完全合法进行扫描。
## 示例报告
每个会话生成的 HTML 报告包括:
- 会话摘要:分析的数据包、唯一 IP、每秒数据包数
- 协议分解,带有可视化条形图(TCP / UDP / ICMP)
- 完整警报日志,包括严重程度、源/目标 IP、描述、MITRE 技术ID
- 可点击的 MITRE ATT&CK 链接,针对每个检测到的技术
## 构建此工具所学到的
- **字节级别的数据包结构** — IP 头部、TCP 标志、ICMP 类型、DNS 查询格式
- **Shannon 熵** 作为检测编码/随机有效负载的启发式方法
- **线程安全的数据结构** 用于并发数据包捕获和分析
- **滑动窗口算法** 用于基于速率的异常检测
- **MITRE ATT&CK 框架** — 将实际可观察的行为映射到技术ID
## 未来改进
- [ ] 为源 IP 添加 GeoIP 查找(MaxMind GeoLite2)
- [ ] Splunk HEC 集成 — 实时推送警报
- [ ] 基于基线流量配置文件使用机器学习进行异常检测
- [ ] Web 仪表板(Flask)用于实时警报流
- [ ] YAML 配置文件可配置的检测阈值
## 作者
**Prashant Jadhav** — 信息安全 | 网络分析 | Python
[LinkedIn](https://linkedin.com/in/prashant-jadhav-53962a25b) · [GitHub](https://github.com/prashant-jadhav)
标签:AMSI绕过, Cloudflare, DNS信标, DNS隧道, HTML报告, ICMP洪水攻击, JSON日志, MITRE ATT&CK, PCAP文件, Python开发, Scapy库, SYN扫描, 域名解析, 大载荷攻击, 威胁情报, 威胁检测, 安全事件响应, 开发者工具, 插件系统, 数据统计, 模块化检测, 端口扫描, 网络安全, 网络流量分析, 自定义规则, 逆向工具, 速率限制, 隐私保护