tshark是wireshark的命令行工具,经过shell命令抓取、解析报文。tcpdump是Linux系统下的抓包工具。wireshark和tcpdump都共同使用 libpcap做为其底层抓包的库,tshark也能够抓取报文。html
有时候须要在linux系统或者ARM开发板中进行抓包,使用tcpdump抓包更加方便,在这种场景下,通常使用tcpdump进行抓包,而后在Windows中使用wireshark来分析生成的包文件,在自动化分析或者自动化测试中,可使用tshark来进行包解析。本文介绍使用tcpdump抓取报文后使用tshark进行报文解析。python
wireshark下载安装
wireshark官网:https://www.wireshark.org/download.htmllinux
# linux yum -y install wireshark yum -y install tcpdump
tcpdump官方文档:https://www.tcpdump.org/index.html#documentationshell
抓取eth1网卡数据包,数据写入文件/tmp/packet.pcapjson
tcpdump -i eth1 -w /tmp/packet.pcap >/dev/null 2>&1 &
其中windows
也能够只抓取特定协议的报文,好比过滤tcp报文:bash
tcpdump -i eth1 tcp -w /tmp/packet.pcap >/dev/null 2>&1 &
若是没有限制 tcpdump 抓包的数量(-c 参数),tcpdump 会持续抓包。能够经过 Ctrl+C 来中止抓包,或者杀掉tcpdump进程:tcp
killall -9 tcpdump
杀掉tcpdump进程后会中止抓包。工具
抓包完成后对数据包进行解析,下面介绍使用tshark解析数据包。测试
tshark参考文档:https://www.wireshark.org/docs/man-pages/tshark.html
若是要在windows命令行窗口使用tshark须要将Wireshark安装路径 C:\Program Files\Wireshark 添加到环境变量。
Linux系统和windows系统tshark使用方法同样
经常使用参数:
-T fields
,通常与-e
选项连用。 separator=,
:默认分隔符为缩进(\t)tshark [ -r <infile> ] -T fields [ -e <field> ] -E <field print option> -Y <displaY filter> tshark -r packet.pcap -T fields -e 解析的字段 -E separator=,
须要解析的字段能够经过Wireshark查看:
选择要过滤的内容 -> 右键 -> Apply as Filter -> Selected
例1:过滤具备源IP和目的IP字段的全部报文
tshark -r packet.pcap -T fields -e ip.src -e ip.dst
例2:过滤源地址为fe80::ca3a:35ff:fe09:efa1的DHCPv6 Solicit报文,并读取UDP源端口号和IPv6目的地址。
$ tshark -r packet.pcap -T fields -E separator=, -Y dhcpv6.msgtype==1 -Y ipv6.src==fe80::ca3a:35ff:fe09:efa1 -e udp.srcport -e ipv6.dst ,ff02::2 ,ff02::2 ,ff02::16 546,ff02::1:2 546,ff02::1:2 546,ff02::1:2 546,ff02::1:2 $
过滤完成后进行进一步的分析
好比可使用grep命令进一步提取知足条件的报文
过滤源地址或者目的地址为192.168.5.38的报文
# linux tshark -r packet.pcap -T fields -e ip.src -e ip.dst | grep 192.168.5.38 # windows tshark -r packet.pcap -T fields -e ip.src -e ip.dst | findstr 192.168.5.38
也可使用python、Java等高级语言进行进一步的分析,Python示例以下:
result = os.popen("tshark -r packet.pcap -T fields -e ip.src -e ip.dst“) ret = result.read() # for i, value in enumerate(ret.split("\n")): 处理value值 pass
某些字段可能没法使用tshark过滤,这种状况下,能够先将pcap文件解码,tshark支持以下文件格式:
ek|fields|json|jsonraw|pdml|ps|psml|tabs|text
解码成xml和text格式文件:
tshark -r packet.pcap -V -T pdml > packet.xml tshark -r packet.pcap -V -T text > packet.txt
xml文档可使用python的ElementTree工具解析:
try: import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET
系统:windows10
tshark已加入环境变量中
输入tshark,显示:
C:\Users\DELL>tshark The NPF driver isn't running. You may have trouble capturing or listing interfaces. Capturing on '鏈湴杩炴帴* 9'
WIreshark安装须要安装WinPcap,查看电脑已经安装了WinPcap。
接下来以管理员身份运行命令行串口,输入net start npf
启动NPF,出现以下报错信息:
C:\WINDOWS\system32>net start npf 服务名无效。 请键入 NET HELPMSG 2185 以得到更多的帮助。
卸载WinPcap10,下载安装winpcap4.1.3:https://www.winpcap.org/install/default.htm
从新输入net start npf
启动NPF:
C:\WINDOWS\system32>net start npf 请求的服务已经启动。 请键入 NET HELPMSG 2182 以得到更多的帮助。
启动成功!
tshark命令也能够正常使用了
文章标题:tcpdump抓包及tshark命令解包方法介绍
本文做者:hiyo
本文连接:https://www.cnblogs.com/hiyong/p/14288239.html 欢迎关注公众号:「测试开发小记」及时接收最新技术文章!