Je1al/network-traffic-analyzer

GitHub: Je1al/network-traffic-analyzer

一款基于Python的离线PCAP网络流量分析器,通过解码底层协议并内置多种威胁检测规则来识别恶意网络行为。

Stars: 0 | Forks: 0

# 网络流量分析器 ## 目录 - [概述](#overview) - [功能](#features) - [安装说明](#installation) - [快速开始](#quick-start) - [CLI 参考](#cli-reference) - [PCAP 分析的工作原理](#how-pcap-analysis-works) - [检测规则详解](#detection-rules-explained) - [端口扫描检测](#1-port-scan-detection) - [SYN Flood 检测](#2-syn-flood-detection) - [ICMP Flood 检测](#3-icmp-flood-detection) - [暴力破解检测](#4-brute-force-detection) - [可疑端口检测](#5-suspicious-port-detection) - [流量峰值检测](#6-traffic-spike-detection) - [IDS 规则引擎](#ids-rule-engine) - [攻击原理说明](#attack-explanations) - [安全局限性](#security-limitations) - [仓库结构](#repository-structure) - [输出示例](#sample-output) ## 概述 `net-analyzer` 读取 PCAP 文件并应用多层威胁检测来识别恶意网络行为。它完全基于离线捕获的数据运行——无需实时捕获数据包。 ``` sample.pcap │ ▼ [packet_loader] ── parse raw PCAP bytes (Ethernet / IPv4 / TCP / UDP / ICMP) │ ▼ [protocol_analyzer] ── extract metadata, track conversations │ ▼ [statistics] ── protocol distribution, top IPs, size stats, time series │ ▼ [detector] ── port scan, SYN flood, ICMP flood, brute force, suspicious ports │ ▼ [report] ── console output + JSON + CSV [visualizer] ── matplotlib charts ``` ## 功能 | 功能 | 描述 | |---|---| | 原生 PCAP 解析器 | 纯 Python 实现——核心分析无需 scapy | | 协议支持 | TCP、UDP、ICMP、IPv4、IPv6 | | 会话跟踪 | 双向流分组 | | 端口扫描检测 | 滑动窗口算法,覆盖 SYN/隐蔽/UDP 扫描 | | SYN Flood 检测 | SYN:ACK 比率分析 | | ICMP Flood 检测 | 基于速率的按源窗口检测 | | 暴力破解检测 | 针对服务端口的重复 SYN 请求 | | 可疑端口 | 10+ 已知恶意软件/后门端口 | | 流量峰值检测 | Z-score 异常检测(3σ 阈值) | | IDS 规则引擎 | 受 Snort 启发的数据包级别匹配 | | 图表 | 5 种 matplotlib 可视化图表 | | 导出格式 | JSON(完整报告)+ CSV(警报) | | CLI 接口 | 带有过滤功能的完整 argparse CLI | | 零依赖* | *matplotlib 为图表的可选依赖 | ## 安装说明 ``` git clone https://github.com/Je1al/network-traffic-analyzer.git cd network-traffic-analyzer # 最小化 (无 charts): pip install scapy # optional — native parser works without it # 完整功能: pip install matplotlib pandas # 或安装所有内容: pip install -r requirements.txt ``` **requirements.txt** ``` scapy>=2.5.0 matplotlib>=3.5.0 pandas>=1.4.0 ``` ## 快速开始 ``` # 1. 生成带有模拟攻击的示例 PCAP python cli.py --generate-sample # 2. 运行完整分析 python cli.py --file examples/sample.pcap --stats --detect # 3. 完整分析(包含 charts 和导出) python cli.py --file examples/sample.pcap --stats --detect --visualize \ --export-json reports/report.json \ --export-csv reports/alerts.csv # 4. 仅分析来自一个 IP 的 TCP 流量 python cli.py --file capture.pcap --filter tcp --filter-ip 192.168.1.200 --detect ``` ## CLI 参考 ``` usage: net-analyzer [-h] [--file PCAP] [--generate-sample] [--stats] [--detect] [--convs] [--visualize] [--filter PROTO] [--filter-ip IP] [--filter-port PORT] [--export-json PATH] [--export-csv PATH] [--output-dir DIR] [--quiet] Options: Input: --file PCAP Path to PCAP file --generate-sample Generate demo PCAP and exit Analysis: --stats, -s Print traffic statistics --detect, -d Run all threat detectors --convs, -c Show top conversations --visualize, -v Generate matplotlib charts Filters: --filter PROTO tcp | udp | icmp --filter-ip IP Match source or destination IP --filter-port PORT Match source or destination port --time-start SEC Unix timestamp lower bound --time-end SEC Unix timestamp upper bound Export: --export-json PATH Full report as JSON --export-csv PATH Alerts as CSV --output-dir DIR Chart/report output directory (default: reports/) ``` ## PCAP 分析的工作原理 ### PCAP 文件格式 PCAP 文件是一系列带有时间戳的原始捕获帧。 ``` ┌──────────────────────────────────────────────────────────┐ │ Global Header (24 bytes) │ │ magic_number | version | snaplen | link_type │ ├──────────────────────────────────────────────────────────┤ │ Packet Record 1 │ │ ts_sec | ts_usec | incl_len | orig_len | frame_bytes │ ├──────────────────────────────────────────────────────────┤ │ Packet Record 2 … │ └──────────────────────────────────────────────────────────┘ ``` ### 解析栈 每个帧通过协议层自下而上进行解析: ``` Ethernet II (14 bytes) └── IPv4 Header (20 bytes) ├── TCP Segment (20+ bytes) │ ├── src_port, dst_port, seq, ack │ └── flags: SYN | ACK | FIN | RST | PSH | URG ├── UDP Datagram (8 bytes) │ └── src_port, dst_port, length └── ICMP Message (4+ bytes) └── type, code (Echo=8, Reply=0, Unreachable=3) ``` 原生解析器(`packet_loader.py`)使用 Python 的 `struct` 模块解码这些标头,展示了对协议二进制格式的深刻理解。 ### 会话跟踪 分析器将数据包分组为双向流: ``` key = (src_ip, dst_ip, src_port, dst_port, protocol) ``` 两个方向被合并为一个会话。这使得以下功能成为可能: - TCP 状态推断(SYN/ACK/FIN 序列) - 每个连接的流量分析 - 识别长连接与短连接 ## 检测规则详解 ### 1. 端口扫描检测 **算法:** 滑动窗口扫描 **阈值:** 同一源 IP 在 60 秒内访问 ≥ 15 个不同的目标端口 ``` # 核心逻辑 distinct_ports_in_window >= PORT_SCAN_THRESHOLD ``` **为何具有恶意性:** 端口扫描器(nmap, masscan)快速连续地探测多个端口,以便在发动针对性漏洞利用之前发现开放的服务。合法主机很少会在一分钟内连接到同一目标上的 15 个以上不同端口。 **覆盖的扫描类型:** - TCP SYN(隐蔽)扫描 — 半开连接 - TCP connect 扫描 — 完整的三次握手 - UDP 端口扫描 — 快速探测 UDP 服务 **误报情况:** - 运行定期评估的漏洞扫描器(Nessus, Qualys) - 执行服务发现的网络监控工具 ### 2. SYN Flood 检测 **算法:** SYN 与 ACK 比率分析 **阈值:** SYN 数量 ≥ 20 且 SYN:ACK 比率 ≥ 5:1 ``` Normal handshake: SYN → SYN/ACK → ACK (ratio 1:1) SYN flood: SYN SYN SYN SYN ... (ratio ∞:1) ``` **为何具有恶意性:** 每个未响应的 SYN 会导致服务器在其半开连接表中分配状态,并等待永远不会到达的 ACK。在每秒数千个 SYN 的情况下,此表会被填满,服务器将无法接受合法连接(拒绝服务)。 **关于非对称路由的说明:** 在仅可见出站流量的捕获中,ACK 数量合理地接近于零。请调整 `SYN_FLOOD_MIN_PACKETS` 以避免误报。 ### 3. ICMP Flood 检测 **算法:** 基于源 IP 的窗口速率计数器 **阈值:** 同一源 IP 在 10 秒内发出 ≥ 50 个 ICMP Echo 请求 **为何具有恶意性:** 高速率的 ICMP Flood 会使其饱和: - 网络带宽(特别是在带有大型 payload 时) - 目标机器上的 CPU 中断处理程序 - 上游链路容量(分布式“ping flood” / Smurf 攻击变种) **误报情况:** - 具有激进 ping 检查的网络监控系统(Nagios, Zabbix) - 基于 ICMP 的 MTU 发现或路径分析工具 ### 4. 暴力破解检测 **算法:** 针对特定服务端口的 SYN 速率 **阈值:** 30 秒内向同一 dst:port 发送 ≥ 10 个 SYN 数据包 **监控端口:** SSH(22)、Telnet(23)、RDP(3389)、MySQL(3306)、PostgreSQL(5432)、Redis(6379)、MongoDB(27017) **为何具有恶意性:** 自动化工具(Hydra, Medusa, Burp Suite Intruder)会使用凭据列表进行重复的身份验证尝试。每次失败的尝试都会生成一个新的 TCP SYN。到身份验证服务的许多短生命周期连接模式是一个强烈的指标。 **误报情况:** - 具有高并发的负载测试工具(JMeter, locust) - 频繁重新连接的连接池 ### 5. 可疑端口检测 **算法:** 与已知恶意端口列表进行精确端口匹配 | 端口 | 关联 | |------|-------------| | 4444 | Metasploit 默认反向 shell | | 31337 | Back Orifice / "elite" 后门 | | 6667 | IRC — 经常被僵尸网络用于 C2 | | 9001 | Tor 中继默认端口 | | 23 | Telnet — 明文传输凭据 | | 5900 | VNC — 通常在无认证的情况下暴露 | | 2323 | Telnet 备用端口 — Mirai IoT 蠕虫默认端口 | **为何具有恶意性:** 这些端口要么是: 1. 在流行的漏洞利用框架中被硬编码(4444, 31337) 2. 与暴露凭据的未加密协议相关联(23, 5900) 3. 被僵尸网络 C2 基础设施使用(6667, IRC) ### 6. 流量峰值检测 **算法:** Z-score 异常检测 **阈值:** z ≥ 3.0(≈ 高于基线平均值 3 个标准差) ``` z = (packets_in_second - mean_pps) / std_dev_pps ``` **为何可疑:** 突然的流量峰值可能表明: - DDoS 攻击开始 - 蠕虫传播(Blaster, Slammer — 通过快速扫描传播) - 大量数据外泄 - 反射/放大攻击 假设正常流量服从高斯分布,z-score 为 3.0 意味着约 0.1% 的误报率。 ## IDS 规则引擎 `net-analyzer` 包含一个受 Snort 启发的规则引擎,用于评估每个数据包的规则。 ``` from detector import RuleEngine, IdsRule, SEVERITY_HIGH engine = RuleEngine() # 添加自定义规则 engine.add_rule(IdsRule( name="DETECT_CLEARTEXT_FTP", protocol="TCP", dst_port=21, severity=SEVERITY_HIGH, message="FTP login — credentials transmitted in plaintext", )) # 添加所有内置规则 engine.add_default_rules() alerts = engine.evaluate(packets) ``` **内置规则包括:** - Telnet 访问(端口 23) - FTP 明文传输(端口 21) - SNMP 访问(UDP 161) - 大型 DNS 响应(>512 字节 — 可能存在放大攻击) - TCP RST 数据包 - Metasploit 端口(4444, 31337) - VNC 暴露(5900) ## 攻击原理说明 ### SYN Flood(DDoS 网络层/传输层) ``` Attacker Server │── SYN (spoofed IP 1) ───▶ │ Allocates: half-open slot 1 │── SYN (spoofed IP 2) ───▶ │ Allocates: half-open slot 2 │── SYN (spoofed IP 3) ───▶ │ Allocates: half-open slot 3 │ … │ Table FULL → drops new SYNs Legitimate user: │── SYN ───────────────────▶ │ DROPPED — server cannot respond ``` **缓解措施:** SYN cookies、速率限制、防火墙状态检测。 ### 端口扫描 → 漏洞利用链 ``` 1. Scanner discovers open ports: 22 (SSH), 80 (HTTP), 3306 (MySQL) 2. Version fingerprinting: SSH 7.4 (CVE-2018-10933 — libssh auth bypass) 3. Exploitation: Authenticate without credentials 4. Privilege escalation + persist ``` ### Metasploit 反向 Shell(端口 4444) ``` Attacker machine Victim │ │ │ exploit delivered ─▶│ │ │ (victim executes payload) │◀── connect to 4444 ─│ │ (attacker receives │ │ shell session) │ ``` ## 安全局限性 | 局限性 | 描述 | |---|---| | 加密流量 | 无法检查 TLS/HTTPS payload — 仅支持基于行为的检测 | | 非对称捕获 | 缺少单向流量会影响基于比率的检测器 | | 阈值敏感性 | 固定阈值可能不适用于所有网络环境 | | IPv6 覆盖范围 | ICMPv6 和 IPv6 扩展标头仅被部分解析 | | 无实时捕获 | 仅支持离线 PCAP 分析 — 无实时模式 | | 无 ML 基线 | 异常检测使用统计 z-score,而非学习到的基线 | **针对生产环境的推荐改进:** - 使用 `pyshark` 或 `scapy` 进行更深入的解析 - 与 Elasticsearch/Kibana 集成以实现 SIEM 关联 - 添加机器学习基线(Isolation Forest, Autoencoder) - 为外部 IP 实现 GeoIP 查找 - 支持 ZEEK/Bro 日志格式作为额外输入 ## 仓库结构 ``` network-traffic-analyzer/ │ ├── cli.py ← Entry point — argparse CLI ├── packet_loader.py ← Native PCAP parser (pure Python) ├── protocol_analyzer.py ← Protocol dissection + conversation tracking ├── statistics.py ← Traffic statistics engine ├── detector.py ← Threat detection + IDS rule engine ├── visualizer.py ← matplotlib chart generation ├── report.py ← Console / JSON / CSV reporters ├── generate_sample_pcap.py ← Synthetic PCAP generator │ ├── examples/ │ └── sample.pcap ← 1045-packet demo capture with 9 attack scenarios │ ├── reports/ ← Generated charts and exports (gitignored) │ └── README.md ``` ## 输出示例 ### 控制台报告 ``` ╔═══════════════════════════════════════════╗ ║ NET-ANALYZER · Traffic Analysis Tool ║ ╚═══════════════════════════════════════════╝ ── CAPTURE SUMMARY ───────────────────────────────────── Duration : 29.8s Total packets : 1,045 Total bytes : 93.2 KB Packets/sec : 35.02 ── THREAT DETECTION — 59 ALERT(S) ────────────────────── 🔴 CRITICAL [SYN_FLOOD] 192.168.1.200 sent 40 SYN packets with only 0 ACKs (ratio 40.0:1) 🔴 CRITICAL [IDS_RULE] Traffic to port 4444 — Metasploit reverse shell port 🟠 HIGH [PORT_SCAN] 192.168.1.200 probed 15 distinct ports within 60s 🟠 HIGH [BRUTE_FORCE] 10.0.0.50 made 15 attempts to 192.168.1.1:22 (SSH) in 30s 🟠 HIGH [ICMP_FLOOD] 172.16.0.5 sent 120 ICMP Echo Requests in 10s (~12.0 pps) ``` *为应用网络安全的教育和作品集目的而构建。* *在捕获或分析网络流量之前,请务必确保您已获得合法授权。*
标签:DFIR, Python, URL发现, 安全检测, 插件系统, 无后门, 红队行动, 网络流量分析, 逆向工具