简体   繁体   中英

Making IPv6 update plen field according to ICMPv6 type on Scapy

In my program I use scapy to create/parse packets, but the actual program is in C++. Since the users only needed the first few fields of ICMPv6 which are the same for all ICMPv6 packets, I created a single ICMPv6 class (using ICMPv6Unknown) on the C++ side.

My problem is that although they can work fine with the fields of ICMPv6, the plen field in IPv6 does not update correctly according to the type I put in the ICMPv6 header.

I am not sure what can i edit in the IPv6 class to make it change the field according to what ICMPv6 type is next, right now it does:

    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

Which fails because ICMPv6Unknown returns len of 4, so it doesn't change size according to my type field. I know that Ether changes type according to fields but i couldn't reproduce this for ICMPv6

To solve this I added an if to IPv6 that does not use len(p) in this case, in the following way:

    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

where icmpv6_len statically returns the length of the type.

I solved this by overloading build_payload() thus:

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()

This basically re-parses the first two fields in the ICMPv6Unknown layer as the layer that we want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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