0

在我的程序中,我使用 scapy 创建/解析数据包,但实际程序是 C++ 中的。由于用户只需要 ICMPv6 的前几个字段,这些字段对于所有 ICMPv6 数据包都是相同的,因此我在 C++ 端创建了一个 ICMPv6 类(使用 ICMPv6Unknown)。

我的问题是,虽然它们可以很好地处理 ICMPv6 的字段,但 IPv6 中的 plen 字段没有根据我放入 ICMPv6 标头的类型正确更新。

我不确定我可以在 IPv6 类中编辑什么以使其根据接下来的 ICMPv6 类型更改字段,现在它可以:

    def post_build(self, p, pay):
    p += pay
    if self.plen is None:
        l = len(p) - 40
        p = p[:4]+struct.pack("!H", l)+p[6:]
    return p

失败是因为 ICMPv6Unknown 返回 len 为 4,因此它不会根据我的类型字段更改大小。我知道以太会根据字段更改类型,但我无法为 ICMPv6 重现此内容

4

2 回答 2

0

为了解决这个问题,我向 IPv6 添加了一个 if 在这种情况下不使用 len(p) 的方法,方法如下:

    def post_build(self, p, pay):
    p += pay
    if self.plen is None:
        if self.nh == 58:
            icmp_type = ord(pay[0])
            l = icmpv6_len(icmp_type)
            print "len is: " + str(l)
    else:
    l = len(p) - 40
        p = p[:4]+struct.pack("!H", l)+p[6:]
    return p

其中 icmpv6_len 静态返回类型的长度。

于 2013-04-07T06:56:19.480 回答
0

我通过重载 build_payload() 解决了这个问题:

def build_payload(self):
        if isinstance(self.payload, ICMPv6Unknown): 
            icmp_type = ord(str(self.payload)[0]) 
            icmp_class = eval(icmp6typescls[icmp_type]) 
            if self.payload.haslayer(Raw): #create actual class from the first 2 fields of the ICMPv6Unknown (type and code, ignoring the checksum) and add the other layers if there are any
                self.payload = icmp_class(str(self.payload[0])[0:2]) / self.payload[1:]
        else:
                self.payload = icmp_class(str(self.payload[0])[0:2])
        return super(IPv6 ,self).build_payload()

这基本上将 ICMPv6Unknown 层中的前两个字段重新解析为我们想要的层。

于 2013-04-08T06:44:30.993 回答