TShark网络协议分析器 analysis-tshark

TShark是Wireshark项目的命令行界面工具,用于网络流量分析、安全调查、取证检查和网络故障排除。它支持数据包捕获、过滤、分析,并能提取敏感信息如凭证和文件。

数字取证 0 次安装 0 次浏览 更新于 3/1/2026

TShark Network Protocol Analyzer

Overview

TShark 是 Wireshark 项目的命令行网络协议分析器。它为安全调查、取证分析和网络故障排除提供了强大的数据包捕获和分析功能。这项技能涵盖了授权的安全操作,包括流量分析、凭证提取、恶意软件检测和取证检查。

重要:网络数据包捕获可能会暴露敏感信息,必须在适当的授权下进行。在捕获网络流量之前,请确保合法合规和隐私考虑。

Quick Start

基本的数据包捕获和分析:

# 在接口上捕获数据包
sudo tshark -i eth0

# 捕获100个数据包并保存到文件
sudo tshark -i eth0 -c 100 -w capture.pcap

# 读取并分析捕获文件
tshark -r capture.pcap

# 应用显示过滤器
tshark -r capture.pcap -Y "http.request.method == GET"

# 提取HTTP对象
tshark -r capture.pcap --export-objects http,extracted_files/

Core Workflow

Network Analysis Workflow

进度: [ ] 1. 验证数据包捕获的授权 [ ] 2. 确定目标接口和捕获要求 [ ] 3. 使用适当的过滤器捕获网络流量 [ ] 4. 分析捕获的数据包以查找安全指标 [ ] 5. 提取工件(文件、凭证、会话) [ ] 6. 记录发现和安全影响 [ ] 7. 安全处理和存储捕获文件 [ ] 8. 根据保留政策清理敏感数据

系统地完成每个步骤。完成的项目请打勾。

1. Authorization Verification

关键:在任何数据包捕获之前:

  • 确认网络监控的书面授权
  • 验证法律合规性(窃听法、隐私法规)
  • 了解数据处理和保留要求
  • 文档捕获范围(接口、持续时间、过滤器)
  • 确保捕获数据的安全存储

2. Interface Discovery

识别可用的网络接口:

# 列出所有接口
tshark -D

# 列出带有接口详细信息
sudo tshark -D

# 在特定接口上捕获
sudo tshark -i eth0
sudo tshark -i wlan0

# 在任何接口上捕获
sudo tshark -i any

# 在多个接口上捕获
sudo tshark -i eth0 -i wlan0

接口类型

  • eth0/ens33:以太网接口
  • wlan0:无线接口
  • lo:回环接口
  • any:所有接口(仅限Linux)
  • mon0:监听模式接口(无线)

3. Basic Packet Capture

捕获网络流量:

# 无限期捕获(按Ctrl+C停止)
sudo tshark -i eth0

# 捕获特定数量的数据包
sudo tshark -i eth0 -c 1000

# 捕获特定持续时间(秒)
sudo tshark -i eth0 -a duration:60

# 捕获到文件
sudo tshark -i eth0 -w capture.pcap

# 使用环形缓冲区(旋转文件)
sudo tshark -i eth0 -w capture.pcap -b filesize:100000 -b files:5

捕获选项

  • -c <count>:捕获数据包计数
  • -a duration:<sec>:在持续时间后自动停止
  • -w <file>:写入文件
  • -b filesize:<KB>:在文件大小处旋转
  • -b files:<num>:保持N个环形缓冲文件

4. Capture Filters

在捕获期间应用 BPF(Berkeley Packet Filter)以提高效率:

# 仅捕获HTTP流量
sudo tshark -i eth0 -f "tcp port 80"

# 捕获特定主机
sudo tshark -i eth0 -f "host 192.168.1.100"

# 捕获子网
sudo tshark -i eth0 -f "net 192.168.1.0/24"

# 捕获多个端口
sudo tshark -i eth0 -f "tcp port 80 or tcp port 443"

# 排除特定流量
sudo tshark -i eth0 -f "not port 22"

# 仅捕获SYN数据包
sudo tshark -i eth0 -f "tcp[tcpflags] & tcp-syn != 0"

常见捕获过滤器

  • host <ip>:到/从IP的流量
  • net <cidr>:到/从网络的流量
  • port <port>:特定端口
  • tcp|udp|icmp:协议类型
  • src|dst:方向过滤器
  • and|or|not:逻辑运算符

5. Display Filters

使用 Wireshark 显示过滤器分析捕获的流量:

# 仅HTTP请求
tshark -r capture.pcap -Y "http.request"

# HTTP响应
tshark -r capture.pcap -Y "http.response"

# DNS查询
tshark -r capture.pcap -Y "dns.flags.response == 0"

# TLS握手
tshark -r capture.pcap -Y "tls.handshake.type == 1"

# 可疑流量模式
tshark -r capture.pcap -Y "tcp.flags.syn==1 and tcp.flags.ack==0"

# 失败的连接
tshark -r capture.pcap -Y "tcp.flags.reset==1"

高级显示过滤器

# 带有凭证的HTTP POST请求
tshark -r capture.pcap -Y "http.request.method == POST and (http contains \"password\" or http contains \"username\")"

# SMB文件传输
tshark -r capture.pcap -Y "smb2.cmd == 8 or smb2.cmd == 9"

# 可疑用户代理
tshark -r capture.pcap -Y "http.user_agent contains \"python\" or http.user_agent contains \"curl\""

# 大型数据传输
tshark -r capture.pcap -Y "tcp.len > 1400"

# 信标检测(定期流量)
tshark -r capture.pcap -Y "http" -T fields -e frame.time_relative -e ip.dst

6. Protocol Analysis

分析特定协议:

HTTP/HTTPS分析

# 提取HTTP请求
tshark -r capture.pcap -Y "http.request" -T fields -e ip.src -e http.host -e http.request.uri

# 提取HTTP用户代理
tshark -r capture.pcap -Y "http.user_agent" -T fields -e ip.src -e http.user_agent

# HTTP状态码
tshark -r capture.pcap -Y "http.response" -T fields -e ip.src -e http.response.code

# 提取HTTP cookie
tshark -r capture.pcap -Y "http.cookie" -T fields -e ip.src -e http.cookie

DNS分析

# DNS查询
tshark -r capture.pcap -Y "dns.flags.response == 0" -T fields -e ip.src -e dns.qry.name

# DNS响应
tshark -r capture.pcap -Y "dns.flags.response == 1" -T fields -e dns.qry.name -e dns.a

# DNS隧道检测(长域名)
tshark -r capture.pcap -Y "dns" -T fields -e dns.qry.name | awk 'length > 50'

# DNS查询类型
tshark -r capture.pcap -Y "dns" -T fields -e dns.qry.type -e dns.qry.name

TLS/SSL分析

# TLS握手
tshark -r capture.pcap -Y "tls.handshake.type == 1" -T fields -e ip.src -e ip.dst -e tls.handshake.extensions_server_name

# TLS证书
tshark -r capture.pcap -Y "tls.handshake.certificate" -T fields -e tls.handshake.certificate

# SSL/TLS版本
tshark -r capture.pcap -Y "tls" -T fields -e tls.record.version

# 弱密码套件
tshark -r capture.pcap -Y "tls.handshake.ciphersuite" -T fields -e tls.handshake.ciphersuite

SMB/CIFS分析

# SMB文件访问
tshark -r capture.pcap -Y "smb2" -T fields -e ip.src -e smb2.filename

# SMB认证
tshark -r capture.pcap -Y "ntlmssp" -T fields -e ip.src -e ntlmssp.auth.username

# SMB命令
tshark -r capture.pcap -Y "smb2" -T fields -e smb2.cmd

7. Credential Extraction

从网络流量中提取凭证(仅限授权取证):

HTTP基本认证

# 提取HTTP基本认证凭证
tshark -r capture.pcap -Y "http.authbasic" -T fields -e ip.src -e http.authbasic

# 解码Base64凭证
tshark -r capture.pcap -Y "http.authorization" -T fields -e http.authorization | base64 -d

FTP凭证

# 提取FTP用户名
tshark -r capture.pcap -Y "ftp.request.command == USER" -T fields -e ip.src -e ftp.request.arg

# 提取FTP密码
tshark -r capture.pcap -Y "ftp.request.command == PASS" -T fields -e ip.src -e ftp.request.arg

NTLM/Kerberos

# 提取NTLM哈希
tshark -r capture.pcap -Y "ntlmssp.auth.ntlmv2response" -T fields -e ntlmssp.auth.username -e ntlmssp.auth.domain -e ntlmssp.auth.ntlmv2response

# Kerberos票据
tshark -r capture.pcap -Y "kerberos.CNameString" -T fields -e kerberos.CNameString -e kerberos.realm

电子邮件凭证

# SMTP认证
tshark -r capture.pcap -Y "smtp.req.command == AUTH" -T fields -e ip.src

# POP3凭证
tshark -r capture.pcap -Y "pop.request.command == USER or pop.request.command == PASS" -T fields -e pop.request.parameter

# IMAP凭证
tshark -r capture.pcap -Y "imap.request contains \"LOGIN\"" -T fields -e imap.request

8. File Extraction

从数据包捕获中提取文件:

# 导出HTTP对象
tshark -r capture.pcap --export-objects http,extracted_http/

# 导出SMB对象
tshark -r capture.pcap --export-objects smb,extracted_smb/

# 导出DICOM对象
tshark -r capture.pcap --export-objects dicom,extracted_dicom/

# 导出IMF(电子邮件)对象
tshark -r capture.pcap --export-objects imf,extracted_email/

手动文件重建

# 从HTTP响应中提取文件数据
tshark -r capture.pcap -Y "http.response and http.content_type contains \"application/pdf\"" -T fields -e data.data | xxd -r -p > extracted_file.pdf

# 重新组装TCP流
tshark -r capture.pcap -q -z follow,tcp,ascii,<stream-number>

9. Malware Detection

识别恶意网络活动:

# 检测常见的C2信标模式
tshark -r capture.pcap -Y "http" -T fields -e frame.time_relative -e ip.dst -e http.host | sort | uniq -c | sort -rn

# 可疑DNS查询(DGA域名)
tshark -r capture.pcap -Y "dns.qry.name" -T fields -e dns.qry.name | awk -F'.' '{print $(NF-1)"."$NF}' | sort | uniq -c | sort -rn

# 检测端口扫描
tshark -r capture.pcap -Y "tcp.flags.syn==1 and tcp.flags.ack==0" -T fields -e ip.src -e ip.dst -e tcp.dstport | sort | uniq -c | sort -rn

# 检测数据泄露(大上传)
tshark -r capture.pcap -Y "http.request.method == POST" -T fields -e ip.src -e http.content_length | awk '$2 > 1000000'

# 可疑的可执行文件下载
tshark -r capture.pcap -Y "http.response and (http.content_type contains \"application/exe\" or http.content_type contains \"application/x-dosexec\")"

10. Statistics and Reporting

生成流量统计信息:

# 协议层次结构
tshark -r capture.pcap -q -z io,phs

# 对话统计信息
tshark -r capture.pcap -q -z conv,tcp
tshark -r capture.pcap -q -z conv,udp
tshark -r capture.pcap -q -z conv,ip

# HTTP统计信息
tshark -r capture.pcap -q -z http,tree

# DNS统计信息
tshark -r capture.pcap -q -z dns,tree

# 端点
tshark -r capture.pcap -q -z endpoints,tcp
tshark -r capture.pcap -q -z endpoints,udp

# 专家信息(警告/错误)
tshark -r capture.pcap -q -z expert

Security Considerations

Authorization & Legal Compliance

  • 书面授权:获取网络监控的明确权限
  • 隐私法规:遵守窃听和隐私法规(GDPR、CCPA、ECPA)
  • 数据最小化:仅捕获调查所需的流量
  • 凭证处理:将提取的凭证视为高度敏感
  • 保留政策:遵循数据保留和安全删除要求

Operational Security

  • 加密存储:加密静态捕获文件
  • 访问控制:限制对数据包捕获的访问
  • 安全传输:使用加密通道传输捕获文件
  • 匿名化:在共享捕获时删除或编辑PII
  • 监管链:维护法律程序的取证完整性

Audit Logging

记录所有数据包捕获活动:

  • 捕获的开始和结束时间戳
  • 捕获的接口
  • 应用的捕获过滤器
  • 文件名称和存储位置
  • 访问捕获的人员
  • 捕获和调查结果的目的
  • 安全删除的时间戳

Compliance

  • MITRE ATT&CK:T1040(网络嗅探)
  • NIST CSFDE.AE(检测过程 - 异常和事件)
  • PCI-DSS:网络安全监控要求
  • ISO 27001:A.12.4 日志和监控
  • GDPR:数据保护和隐私要求

Common Patterns

Pattern 1: Incident Response Investigation

# 在事件期间捕获流量
sudo tshark -i eth0 -w incident_$(date +%Y%m%d_%H%M%S).pcap -a duration:300

# 分析横向移动
tshark -r incident.pcap -Y "smb2 or rdp or ssh" -T fields -e ip.src -e ip.dst

# 识别C2通信
tshark -r incident.pcap -Y "http or dns" -T fields -e ip.dst -e http.host -e dns.qry.name

# 提取IOCs
tshark -r incident.pcap -Y "ip.dst" -T fields -e ip.dst | sort -u > ioc_ips.txt
tshark -r incident.pcap -Y "dns.qry.name" -T fields -e dns.qry.name | sort -u > ioc_domains.txt

Pattern 2: Malware Traffic Analysis

# 捕获恶意软件沙箱流量
sudo tshark -i eth0 -w malware_traffic.pcap

# 提取C2指标
tshark -r malware_traffic.pcap -Y "http.host" -T fields -e ip.src -e http.host -e http.user_agent

# 识别DNS隧道
tshark -r malware_traffic.pcap -Y "dns" -T fields -e dns.qry.name | awk 'length > 50'

# 提取下载的有效载荷
tshark -r malware_traffic.pcap --export-objects http,malware_artifacts/

# 分析加密/编码
tshark -r malware_traffic.pcap -Y "http.request.method == POST" -T fields -e data.data

Pattern 3: Credential Harvesting Detection

# 监控凭证传输
sudo tshark -i eth0 -Y "(http.authorization or ftp or pop or imap) and not tls" -T fields -e ip.src -e ip.dst

# 提取所有HTTP POST数据
tshark -r capture.pcap -Y "http.request.method == POST" -T fields -e http.file_data > post_data.txt

# 搜索密码关键字
tshark -r capture.pcap -Y "http contains \"password\" or http contains \"passwd\"" -T fields -e ip.src -e http.request.uri

# NTLM哈希提取
tshark -r capture.pcap -Y "ntlmssp.auth.ntlmv2response" -T fields -e ntlmssp.auth.username -e ntlmssp.auth.domain -e ntlmssp.auth.ntlmv2response > ntlm_hashes.txt

Pattern 4: Network Forensics

# 重建HTTP对话
tshark -r capture.pcap -q -z follow,http,ascii,0

# 时间线分析
tshark -r capture.pcap -T fields -e frame.time -e ip.src -e ip.dst -e tcp.dstport

# 识别文件传输
tshark -r capture.pcap -Y "http.content_type contains \"application/\" or ftp-data" -T fields -e frame.number -e http.content_type

# 连接的地理位置(需要GeoIP)
tshark -r capture.pcap -T fields -e ip.src -e ip.dst -e ip.geoip.src_country -e ip.geoip.dst_country

Pattern 5: Wireless Security Assessment

# 捕获无线流量(需要监听模式)
sudo tshark -i mon0 -w wireless_capture.pcap

# 识别无线网络
tshark -r wireless_capture.pcap -Y "wlan.fc.type_subtype == 0x08" -T fields -e wlan.ssid -e wlan.bssid

# 检测脱认证攻击
tshark -r wireless_capture.pcap -Y "wlan.fc.type_subtype == 0x0c"

# WPA握手捕获
tshark -r wireless_capture.pcap -Y "eapol"

# 客户端探测活动
tshark -r wireless_capture.pcap -Y "wlan.fc.type_subtype == 0x04" -T fields -e wlan.sa -e wlan.ssid

Integration Points

SIEM Integration

将数据包分析导出到SIEM平台:

# 导出为Splunk/ELK的JSON
tshark -r capture.pcap -T ek > packets.json

# 导出特定字段为JSON
tshark -r capture.pcap -Y "http" -T json -e ip.src -e ip.dst -e http.host -e http.request.uri

# 导出为CSV进行分析
tshark -r capture.pcap -T fields -E separator=, -e frame.time -e ip.src -e ip.dst -e tcp.dstport > packets.csv

Scripting and Automation

#!/bin/bash
# continuous_monitor.sh - 持续网络监控

INTERFACE="eth0"
ALERT_FILTER="http contains \"cmd.exe\" or dns.qry.name contains \".tk\" or dns.qry.name contains \".xyz\""

sudo tshark -i $INTERFACE -Y "$ALERT_FILTER" -T fields -e frame.time -e ip.src -e ip.dst -e http.host -e dns.qry.name | \
while read line; do
  echo "[ALERT] $(date): $line" | tee -a security_alerts.log
  # 触发事件响应工作流程
  echo "$line" | mail -s "Security Alert" soc@company.com
done

Troubleshooting

Issue: “Permission denied” when capturing

解决方案

# 使用sudo运行
sudo tshark -i eth0

# 或将用户添加到wireshark组(Linux)
sudo usermod -a -G wireshark $USER
sudo setcap cap_net_raw,cap_net_admin+eip /usr/bin/tshark

# 注销并登录以使组更改生效

Issue: “No interfaces found”

解决方案

# 验证tshark安装
tshark --version

# 使用sudo列出接口
sudo tshark -D

# 检查接口状态
ip link show
ifconfig -a

Issue: Capture file is huge

解决方案

# 使用捕获过滤器减小大小
sudo tshark -i eth0 -f "not port 22" -w capture.pcap

# 使用环形缓冲区
sudo tshark -i eth0 -w capture.pcap -b filesize:100000 -b files:5

# 限制数据包大小(snaplen)
sudo tshark -i eth0 -s 128 -w capture.pcap

Issue: Cannot decrypt TLS traffic

解决方案

# 提供SSL密钥日志文件(需要SSLKEYLOGFILE环境变量)
tshark -r capture.pcap -o tls.keylog_file:sslkeys.log -Y "http"

# 使用前主密钥
tshark -r capture.pcap -o tls.keys_list:192.168.1.100,443,http,/path/to/server.key

Defensive Considerations

组织应保护免受未经授权的数据包捕获:

  • 网络分割:减少数据包嗅探的暴露
  • 加密:使用TLS/SSL保护传输中的敏感数据
  • 交换机安全:启用端口安全和DHCP嗅探
  • 无线安全:使用WPA3,禁用广播SSID
  • 入侵检测:监控混杂模式接口
  • 物理安全:保护网络基础设施免受窃听设备

检测数据包捕获活动:

  • 监控混杂模式网络接口
  • 检测ARP欺骗和MAC泛洪攻击
  • 审计对网络设备的管理访问
  • 监控不寻常的出站数据传输
  • 部署网络访问控制(802.1X)

References