简体   繁体   English

使用 DPKT python 检查数据包是否包含 Ethe.net 层或原始 IP 数据包

[英]Check if packet contains Ethernet layer or a Raw IP packet using DPKT python

I have a some pcap files that I need to extract some information from, those packets are mixed, some are Raw IP and others contains ethe.net frames.我有一些 pcap 文件,我需要从中提取一些信息,这些数据包是混合的,有些是 Raw IP,有些包含 ethe.net 帧。

I need to conditionally check for the type of packet before parsing as the packets with ethe.net frames could be parsed with:我需要在解析之前有条件地检查数据包的类型,因为可以使用以下方法解析带有 ethe.net 帧的数据包:

for ts, buf in pkts:
    if buf contains_ethernet:
        eth = dpkt.ethernet.Ethernet(buf)
        if eth.type == dpkt.ethernet.ETH_TYPE_IP:
            ip = eth.data
        else:
            continue
    else:
        ip = dpkt.ip.IP(buf)

How can I define the contains_ethe.net as a boolean or a condition?如何将contains_ethe.net定义为 boolean 或条件?

The pcap header file defines the link type of the capture (Ethe.net, Raw IP, ...) pcap header 文件定义了捕获的链接类型(Ethe.net,Raw IP,...)

Before processing the packet, you shoud use datalink() of your dpkt.pcap.Reader() object to get the link type of your pcap file.在处理数据包之前,您应该使用 dpkt.pcap.Reader() object 的 datalink() 来获取 pcap 文件的链接类型。 According to your script example:根据您的脚本示例:

if <<dpkt.pcap.Reader>>.datalink() == LINKTYPE_ETHE.NET: ## Process Ethe.net frame elif <<dpkt.pcap.Reader>>.datalink() == LINKTYPE_RAW: ## Processs Raw IP datagram else: ## Other link types

Here is the list of link types: http://www.tcpdump.org/linktypes.html以下是链接类型列表: http://www.tcpdump.org/linktypes.html

With values LINKTYPE_ETHE.NET for Ethe.net and LINKTYPE_RAW for Raw IP Ethe.net 的值为 LINKTYPE_ETHE.NET,Raw 的值为 LINKTYPE_RAW IP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM