$npx -y skills add killvxk/cybersecurity-skills-zh --skill analyzing-network-traffic-with-wireshark使用 Wireshark 和 tshark 捕获并分析网络数据包,识别恶意流量模式、诊断协议问题、提取工件, 并支持对授权网络分段进行事件响应调查。
| 1 | # 使用 Wireshark 分析网络流量 |
| 2 | |
| 3 | ## 适用场景 |
| 4 | |
| 5 | - 通过检查命令与控制(command-and-control)流量、数据外泄或横向移动(lateral movement)的数据包级证据,调查疑似网络入侵 |
| 6 | - 诊断网络性能问题,如重传、分片或 DNS 解析失败 |
| 7 | - 通过捕获沙盒或隔离主机的流量,分析恶意软件的通信模式 |
| 8 | - 通过确认实际穿越网络分段的流量,验证防火墙(firewall)和入侵检测系统(IDS)规则 |
| 9 | - 从捕获的网络会话中提取文件、凭据或失陷指标(IOC) |
| 10 | |
| 11 | **不适用于**:在未经授权的网络上捕获流量、在没有法律授权的情况下拦截私人通信,或作为生产监控中全功能 SIEM 平台的替代品。 |
| 12 | |
| 13 | ## 前置条件 |
| 14 | |
| 15 | - 已安装 Wireshark 4.0+ 和 tshark 命令行工具 |
| 16 | - 进行实时数据包捕获需要 root/sudo 权限或隶属于 `wireshark` 组 |
| 17 | - 访问受监控网段的网络接口(物理网卡、SPAN 端口或网络分路器) |
| 18 | - 足够的磁盘空间用于数据包捕获文件(繁忙的千兆链路估计每分钟 1 GB) |
| 19 | - 熟悉 TCP/IP 协议、HTTP、DNS、TLS 和 SMB 的数据包层级细节 |
| 20 | |
| 21 | ## 工作流程 |
| 22 | |
| 23 | ### 步骤 1:配置捕获环境 |
| 24 | |
| 25 | 设置捕获接口和过滤器,锁定相关流量: |
| 26 | |
| 27 | ```bash |
| 28 | # 列出可用接口 |
| 29 | tshark -D |
| 30 | |
| 31 | # 在 eth0 上启动捕获,使用捕获过滤器限制范围 |
| 32 | tshark -i eth0 -f "host 10.10.5.23 and (port 80 or port 443 or port 445)" -w /tmp/capture.pcapng |
| 33 | |
| 34 | # 使用环形缓冲区捕获以管理磁盘占用(10 个文件,每个 100MB) |
| 35 | tshark -i eth0 -b filesize:102400 -b files:10 -w /tmp/rolling_capture.pcapng |
| 36 | |
| 37 | # 同时在多个接口上捕获 |
| 38 | tshark -i eth0 -i eth1 -w /tmp/multi_interface.pcapng |
| 39 | ``` |
| 40 | |
| 41 | 对于 Wireshark GUI,在开始捕获前在"Capture Options"对话框中设置捕获过滤器。 |
| 42 | |
| 43 | ### 步骤 2:应用显示过滤器进行针对性分析 |
| 44 | |
| 45 | ```bash |
| 46 | # 过滤包含可疑 User-Agent 的 HTTP 流量 |
| 47 | tshark -r capture.pcapng -Y "http.user_agent contains \"curl\" or http.user_agent contains \"Wget\"" |
| 48 | |
| 49 | # 查找指向可疑顶级域名的 DNS 查询 |
| 50 | tshark -r capture.pcapng -Y "dns.qry.name contains \".xyz\" or dns.qry.name contains \".top\" or dns.qry.name contains \".tk\"" |
| 51 | |
| 52 | # 识别表明网络问题的 TCP 重传 |
| 53 | tshark -r capture.pcapng -Y "tcp.analysis.retransmission" |
| 54 | |
| 55 | # 过滤用于横向移动检测的 SMB 流量 |
| 56 | tshark -r capture.pcapng -Y "smb2.cmd == 5 or smb2.cmd == 3" -T fields -e ip.src -e ip.dst -e smb2.filename |
| 57 | |
| 58 | # 查找明文凭据传输 |
| 59 | tshark -r capture.pcapng -Y "ftp.request.command == \"PASS\" or http.authbasic" |
| 60 | |
| 61 | # 检测信标模式(规律间隔的连接) |
| 62 | tshark -r capture.pcapng -Y "ip.dst == 203.0.113.50" -T fields -e frame.time_relative -e ip.src -e tcp.dstport |
| 63 | ``` |
| 64 | |
| 65 | ### 步骤 3:协议专项深度分析 |
| 66 | |
| 67 | ```bash |
| 68 | # 跟踪 TCP 流以重建对话 |
| 69 | tshark -r capture.pcapng -q -z follow,tcp,ascii,0 |
| 70 | |
| 71 | # 分析 HTTP 请求/响应对 |
| 72 | tshark -r capture.pcapng -Y "http" -T fields -e frame.time -e ip.src -e ip.dst -e http.request.method -e http.request.uri -e http.response.code |
| 73 | |
| 74 | # 提取 DNS 查询/响应统计 |
| 75 | tshark -r capture.pcapng -q -z dns,tree |
| 76 | |
| 77 | # 分析 TLS 握手以检测弱密码套件 |
| 78 | tshark -r capture.pcapng -Y "tls.handshake.type == 2" -T fields -e ip.src -e ip.dst -e tls.handshake.ciphersuite |
| 79 | |
| 80 | # SMB 文件访问枚举 |
| 81 | tshark -r capture.pcapng -Y "smb2" -T fields -e frame.time -e ip.src -e ip.dst -e smb2.filename -e smb2.cmd |
| 82 | ``` |
| 83 | |
| 84 | ### 步骤 4:提取工件和 IOC |
| 85 | |
| 86 | ```bash |
| 87 | # 导出 HTTP 对象(通过 HTTP 传输的文件) |
| 88 | tshark -r capture.pcapng --export-objects http,/tmp/http_objects/ |
| 89 | |
| 90 | # 导出 SMB 对象(通过 SMB 传输的文件) |
| 91 | tshark -r capture.pcapng --export-objects smb,/tmp/smb_objects/ |
| 92 | |
| 93 | # 提取所有唯一目标 IP 用于威胁情报查询 |
| 94 | tshark -r capture.pcapng -T fields -e ip.dst | sort -u > unique_dest_ips.txt |
| 95 | |
| 96 | # 提取 SSL/TLS 证书信息 |
| 97 | tshark -r capture.pcapng -Y "tls.handshake.type == 11" -T fields -e x509sat.uTF8String -e x509ce.dNSName |
| 98 | |
| 99 | # 提取所有访问的 URL |
| 100 | tshark -r capture.pcapng -Y "http.request" -T fields -e http.host -e http.request.uri | sort -u > urls.txt |
| 101 | |
| 102 | # 对提取的文件进行哈希,用于 IOC 匹配 |
| 103 | find /tmp/http_objects/ -type f -exec sha256sum {} \; > extracted_file_hashes.txt |
| 104 | ``` |
| 105 | |
| 106 | ### 步骤 5:统计分析与异常检测 |
| 107 | |
| 108 | ```bash |
| 109 | # 协议层级统计 |
| 110 | tshark -r capture.pcapng -q -z io,phs |
| 111 | |
| 112 | # 按字节数排序的对话统计 |
| 113 | tshark -r capture.pcapng -q -z conv,tcp -z conv,udp |
| 114 | |
| 115 | # 识别最活跃主机 |
| 116 | tshark -r capture.pcapng -q -z endpoints,ip |
| 117 | |
| 118 | # IO 图数据(每秒数据包数) |
| 119 | tshark -r capture.pcapng -q -z io,stat,1,"COUNT(frame) frame" |
| 120 | |
| 121 | # 检测端口扫描模式 |
| 122 | tshark -r capture.pcapng -Y "tcp.flags.syn == 1 and tcp.flags.ack == 0" -T fields -e ip.src -e tcp.dstport | sort | uniq -c | sort -rn | head -20 |
| 123 | ``` |
| 124 | |
| 125 | ### 步骤 6:生成报告和导出证据 |
| 126 | |
| 127 | ```bash |
| 128 | # 将过滤后的数据包导出到新 PCAP 用于证据保全 |
| 129 | tshark -r capture.pcapng -Y "ip.addr == 10.10.5.23 and tcp.port == 4444" -w evidence_c2_traffic.pcapng |
| 130 | |
| 131 | # 以 CSV 格式生成数据包摘要 |
| 132 | tshark -r capture.pcapng -T fields -E header=y -E separator=, -e frame.number -e frame.time -e ip.src -e ip.dst -e ip.proto -e tcp.srcport -e tcp.dstport -e frame.len > traffic_summary.csv |
| 133 | |
| 134 | # 创建 PDML(XML)输出用于程序化分析 |
| 135 | tshark -r capture.pcapng -T pdml > capture_analysis.xml |
| 136 | |
| 137 | # 计算捕获文件哈希值用于证据监管链 |
| 138 | sha256sum capture.pcapng > capture_hash.txt |
| 139 | ``` |
| 140 | |
| 141 | ## 核心概念 |
| 142 | |
| 143 | | 术语 | 定义 | |
| 144 | |------|------------| |
| 145 | | **捕获过滤器(BPF)** | 在捕获时应用的伯克利数据包过滤器(Berkeley Packet Filter)语法,用于限制记录的数据包,减小文件大小并提升性能 | |
| 146 | | **显示过滤器** | Wireshark 特有的过滤器语法,应用于已捕获的数据包以进行聚焦分析,不修改捕获文件 | |
| 147 | | **PCAPNG** | 下一代数据包捕获格式,支持在单个文件中存储多个接口、名称解析、注释和元数据 | |
| 148 | | **TCP 流** | 重组后的 TCP 分段序列,代表两个端点之间完整的双向会话 | |
| 149 | | **协议解析器** | Wireshark 模块,用于解码特定协议的字段和结构,实现对数据包内容的深度检查 | |
| 150 | | **IO 图** | 捕获期间数据包或字节速率的时序可视化,用于识别流量峰值或信标行为 | |
| 151 | |
| 152 | ## 工具与系统 |
| 153 | |
| 154 | - **Wireshark 4.0+**:基于 GUI 的数据包分析器,支持 3000+ 协议的解析器、流重组和导出功能 |
| 155 | - **tshark**:Wireshark 的命令行版本,用于无界面捕获、批量处理和脚本化分析流程 |
| 156 | - **tcpdump**:轻量级数据包捕获工具,适合在无 GUI 依赖的远程系统上快速捕获 |
| 157 | - **mergecap**:Wireshark 工具,用于将多个捕获文件合并为单个 PCAP 进行统一分析 |
| 158 | - **editcap**:Wireshark 工具,用于拆分、过滤和转换捕获文件格式 |
| 159 | |
| 160 | ## 常见场景 |
| 161 | |
| 162 | ### 场景:调查疑似通过 DNS 隧道的数据外泄 |
| 163 | |
| 164 | **场景背景**:安全运营中心(SOC)团队检测到一台工作站(10.10.3.45)向外部域名发出异常高频的 DNS |