Ubuntu Sniffer流量过滤方法
在Ubuntu系统中,常用的流量过滤工具包括tcpdump(命令行)和Wireshark(图形界面),两者均支持通过过滤表达式精准定位目标流量。以下是具体操作指南:
tcpdump是Ubuntu默认安装的网络抓包工具(若未安装,可通过sudo apt install tcpdump安装),适用于快速捕获和过滤命令行环境下的流量。
按协议过滤:捕获指定协议的流量(如HTTP、ICMP)。
示例:捕获eth0接口的HTTP流量(端口80):
sudo tcpdump -i eth0 port 80
捕获所有ICMP流量(ping请求/响应):
sudo tcpdump -i eth0 icmp
按IP地址过滤:捕获与特定IP相关的流量(源/目标)。
示例:捕获与192.168.1.100相关的所有流量:
sudo tcpdump -i eth0 host 192.168.1.100
捕获来自192.168.1.100的流量:
sudo tcpdump -i eth0 src 192.168.1.100
捕获发送到192.168.1.100的流量:
sudo tcpdump -i eth0 dst 192.168.1.100
按端口过滤:捕获指定端口的流量(如SSH端口22)。
示例:捕获eth0接口的SSH流量:
sudo tcpdump -i eth0 port 22
通过and(与)、or(或)、not(非)组合多个条件,精确定位流量。
示例:捕获192.168.1.100主机的HTTPS流量(端口443,TCP协议):
sudo tcpdump -i eth0 'tcp and port 443 and host 192.168.1.100'
示例:捕获来自192.168.1.0/24网段的HTTP流量:
sudo tcpdump -i eth0 'src net 192.168.1.0/24 and port 80'
排除特定流量:忽略不需要的流量(如ICMP ping包)。
示例:捕获所有流量,但排除ICMP:
sudo tcpdump -i eth0 not icmp
按数据包大小过滤:捕获指定大小范围的流量(如100-500字节)。
示例:捕获TCP流量,且有效负载长度在100-500字节之间:
sudo tcpdump -i eth0 'tcp and tcp.len >= 100 and tcp.len <= 500'
保存到文件:将捕获的流量保存为.pcap文件(供后续分析)。
示例:捕获eth0接口的流量并保存到capture.pcap:
sudo tcpdump -i eth0 -w capture.pcap
读取文件:用tcpdump读取.pcap文件并分析。
示例:读取capture.pcap文件:
sudo tcpdump -r capture.pcap
Wireshark是功能强大的图形化协议分析工具(需安装:sudo apt install wireshark),支持实时过滤和深度分析数据包。
在顶部过滤器栏输入过滤条件,按Enter应用(支持语法高亮和实时预览)。
httpip.src == 192.168.1.100ip.dst == 192.168.1.100tcp.port == 80使用and(与)、or(或)、not(非)组合多个条件。
示例:过滤192.168.1.100主机的HTTP POST请求(需匹配TCP端口80和POST方法):
http and tcp.port == 80 and http.request.method == "POST"
示例:过滤10.0.0.0/8网段的UDP流量:
udp and net 10.0.0.0/8
http contains "password"tcp.len > 100not arpsudo usermod -aG wireshark $USER)才能捕获流量。-c 100或保存到文件-w)。通过上述方法,可快速定位Ubuntu系统中的目标流量,有效排查网络问题或进行安全分析。