python - Scapy:如何获取完整的 IP 数据包 header ?

标签 python header ip scapy

在 Scapy 中,我想手动 将数据包与其对应的 ICMP 超时消息匹配。

我需要匹配:

  • ICMP数据包的IP-in-ICMP字段
  • 我的数据包的 IP header 和前 8 个字节 ICMP 数据包不是问题:

    icmpPayload = str(icmpPacket[ICMP].payload)

对于数据包的前8个字节,我只需要做:

str(myPacket[IP].payload)[:8]

我不知道如何获取myPacket 的IP header 。我现在所做的就是用它的前 8 个字节替换整个数据包中的有效负载。如果应用于数千个数据包,这种搜索和替换可能会花费太长时间,恐怕:

 strOfMyPacket = str(myPacket[IP])
 strOfMyPacket.replace(str(myPacket[IP].payload),str(myPacket[IP].payload)[:8],1)

有什么更快的方法可以让我简单地执行以下操作?

 partOfPayload = str(myPacket[IP].payload)[:8]
 fullHeader = _______
 stringToCompare = fullHeader + partOfPayload

最佳答案

str(myPacket)[:(myPacket[IP].ihl * 4)]

IP header 长度在字段ihl(Internet header 长度)中。它表示为 header 使用的 32 位字的数量。 (由于标题的“选项”部分,它是可变的)。因此,如果我们将该字段乘以 32,然后除以 8(或 * 4),我们将得到 header 填充的字节数,无论是否有选项。

我很惊讶没有方法(我能找到)只返回没有较低层的 IP header 。

http://en.wikipedia.org/wiki/IPv4_header#Header

关于python - Scapy:如何获取完整的 IP 数据包 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11794602/

相关文章:

python - 在robot framework中申请loop后,报错如下- Keyword name cannot be empty

python - 值错误 : Failed to convert a NumPy array to a Tensor (Unsupported object type list) in Keras/Tensorflow Python

php - python 中最接近 WordPress 而不是 php 的东西是什么?

c++ - C++ 中包含的外部 header

python - Debian 上的端口映射 HTTP 服务器?

android - android 热点上的网络服务器 - 它的 IP 是多少?

python - 清除函数外的数据(列表/字典/元组)

java - Retrofit @Header 中的属性必须是常量

mysql - 左连接select后查询结果表头

c - 0 - 0 的补码