python / dpkt:判断数据包是tcp数据包还是udp数据包,
我有一个 python 脚本,它使用 dpkt 捕获以太网上的数据包,但我如何区分哪些数据包是 tcp,哪些数据包是 udp。
最终我想要一个在时间间隔期间建立的每个 TCP 连接的数据包列表。
我的代码是:
import dpkt
import pcapy
cap=pcap.open_live('eth0',100000,1,0)
(header,payload)=cap.next()
while header:
eth=dpkt.ethernet.Ethernet(str(payload))
ip=eth.data
tcp=ip.data
# i need to know whether it is a tcp or a udp packet here!!!
(header,payload)=cap.next()
I have a python scripts that captures the packets on the ethernet using dpkt, but how do i differentiate between which packets are tcp and which ones are for udp.
Eventually i would like to have a list of packets for each tcp connection that was established during the time interval.
my code is:
import dpkt
import pcapy
cap=pcap.open_live('eth0',100000,1,0)
(header,payload)=cap.next()
while header:
eth=dpkt.ethernet.Ethernet(str(payload))
ip=eth.data
tcp=ip.data
# i need to know whether it is a tcp or a udp packet here!!!
(header,payload)=cap.next()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IP 标头包含字段协议。 dpkt 应该允许您获取此值,并使用它您可以猜测 IP 之上的内容。以下是有效协议编号的列表 http://www.iana.org/assignments/protocol-数字/protocol-numbers.xml。
UDP 等于 17,而 TCP 等于 6。
编辑:
我已经检查过这个问题,正如我提到的,dpkg 提供
p
属性来访问 IP 的协议字段。所以你可以再检查一下。但它也会自动解析数据包并将data
属性设置为代表上层协议(如 UDP 或 TCP)的类的实例。因此,您可以检查data
属性的类型并识别该协议。IP header contains field protocol. dpkt should allow you to obtain this value and using it you can guess what is on top of IP. Here is a list of valid protocols numbers http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml.
UDP is equal to 17 while TCP is 6.
Edit:
I have checked this issue and as I mentioned dpkg provide
p
properties to access protocol field of IP. So you can check agains it. But it also automatically parse packet and setdata
property to instance of class that represent upper protocol like UDP or TCP. So you can check type ofdata
property and you recognize this protocol.使用 dpkt 捕获以太网适配器
eth0
上的数据包的 Python 脚本,以及区分IP的TCP和UDP数据包。A python script that captures the packets on the ethernet adapter
eth0
using dpkt, and differentiates between TCP and UDP packets of the IP.