简体   繁体   English

如何检查 scapy 数据包中是否存在层?

[英]How to check for presence of a layer in a scapy packet?

How do I check for the presence of a particular layer in a scapy packet?如何检查 scapy 数据包中是否存在特定层? For example, I need to check the src/dst fields of an IP header, how do I know that a particular packet actually has an IP header (as opposed to IPv6 for instance).例如,我需要检查 IP 标头的 src/dst 字段,我如何知道特定数据包实际上具有 IP 标头(例如,与 IPv6 相对)。

My problem is that when I go to check for an IP header field, I get an error saying that the IP layer doesn't exist.我的问题是,当我检查 IP 标头字段时,我收到一条错误消息,指出 IP 层不存在。 Instead of an IP header, this particular packet had IPv6.这个特定的数据包没有 IP 标头,而是使用 IPv6。

pkt = Ether(packet_string)
if pkt[IP].dst == something:
  # do this

My error occurs when I try to reference the IP layer.当我尝试引用 IP 层时出现错误。 How do I check for that layers existence before attempting to manipulate it?在尝试操作之前如何检查该层是否存在?

Thanks!谢谢!

You should try the in operator.您应该尝试使用in运算符。 It returns True or False depending if the layer is present or not in the Packet .它返回TrueFalse取决于该层是否存在于Packet中。

root@u1010:~/scapy# scapy
Welcome to Scapy (2.2.0-dev)
>>> load_contrib("ospf")
>>> pkts=rdpcap("rogue_ospf_hello.pcap")
>>> p=pkts[0]
>>> IP in p
True
>>> UDP in p
False
>>>
root@u1010:~/scapy#

For completion I thought I would also mention the haslayer method.为了完成,我想我还会提到haslayer方法。

>>> pkts=rdpcap("rogue_ospf_hello.pcap") 
>>> p=pkts[0]
>>> p.haslayer(UDP)
0
>>> p.haslayer(IP)
1

Hope that helps as well.希望这也有帮助。

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

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