Scapy:我如何获得完整的IP数据包头? [英] Scapy: how do I get the full IP packet header?

查看:727
本文介绍了Scapy:我如何获得完整的IP数据包头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scapy中,我希望 手动 匹配具有相应ICMP超时消息的数据包。



我需要匹配:


  • ICMP中的IP-in-ICMP字段数据包

  • 我的数据包的IP头和前8个字节
    ICMP数据包不成问题:


    对于数据的前8个字节来说,这个数据库中的数据的前8个字节就是数据库中的前8个字节。

  • 数据包,我只需要这样做:

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

    我不知道如何仅 myPacket 。我现在所做的是用前8个字节替换整个数据包中的有效载荷。这个搜索和替换,如果适用于数千个数据包,可能需要很长时间,恐怕: 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头长度位于 ihl (互联网标题长度)。它表示为报头使用的32位字的数量。 (由于标题的'选项'部分,它是可变的)。所以,如果我们将该字段乘以32,然后除以8(或* 4),我们就可以得到头部填充的字节数,不管是否有选项。



我很惊讶没有方法(我可以找到)只返回不包含底层的IP头。



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

In Scapy, I want to manually match packets with their corresponding ICMP time-exceeded messages.

I need to match:

  • IP-in-ICMP field of ICMP packet
  • IP header and first 8 bytes of my data packet The ICMP packet isn't a problem:

    icmpPayload = str(icmpPacket[ICMP].payload)

As for the first 8 bytes of the data packet, I just need to do:

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

I don't know how to get only the IP header of myPacket. All I do now is replace the payload in the whole packet with its first 8 bytes. This search and replace, if applied to thousands of packets, might take too long, I'm afraid:

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

Any faster way that will let me do simply the following?

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

解决方案

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

The IP header length is in the field ihl (Internet Header Length). It is represented as the number of 32bit words the header uses. (it is variable because of the 'options' section of the header). So, if we multiply that field by 32 and then divide by 8 (or * 4) we get the number of bytes the header fills, whether is has options or not.

I am surprised there is no method (that i could find) to return JUST the IP header without the lower layers.

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

这篇关于Scapy:我如何获得完整的IP数据包头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆