Esempio n. 1
0
 def getUDPPacket(self):
     """
     构造UDP数据包
     :param self:
     :return:
     """
     try:
         ip_packet = IP()
         ip_packet.version = int(self.entries[3].get())
         ip_packet.ihl = int(self.entries[4].get())
         ip_packet.tos = int(self.entries[5].get())
         ip_packet.id = int(self.entries[6].get())
         # ip_packet.flags = int(self.entries[7].get())
         ip_packet.frag = int(self.entries[8].get())
         ip_packet.ttl = int(self.entries[9].get())
         # ip_packet.chksum = self.entries[10].get()
         ip_packet.src = self.entries[11].get()
         ip_packet.dst = self.entries[12].get()
         udp_packet = UDP()
         udp_packet.sport = int(self.entries[0].get())
         udp_packet.dport = int(self.entries[1].get())
         # udp_packet.chksum = int(self.entries[2].get())
         # scapy自动计算IP、UDP校验和
         # 获得数据包的二进制值
         pkg_raw = raw(ip_packet / udp_packet)
         udp_packet_raw = pkg_raw[20:]
         # 构造数据包,自动计算校验和
         scapy_chksum_IP = IP(pkg_raw).chksum
         scapy_chksum_udp = UDP(udp_packet_raw).chksum
         print("scapy自动计算的UDP校验和为:%04x" % scapy_chksum_udp)
         # 手动计算UDP校验和
         udp_packet.chksum = 0
         packet = ip_packet / udp_packet
         udp_raw = raw(packet)[20:]
         self_chksum = in4_chksum(socket.IPPROTO_UDP, packet[IP], udp_raw)
         print("手动计算的UDP校验和为:%04x" % self_chksum)
         if self_chksum == scapy_chksum_udp:
             print("UDP验证和正确")
         else:
             print("UDP验证和不正确")
         udp_packet.chksum = scapy_chksum_udp
         self.entries[2].delete(0, END)
         self.entries[2].insert(0, hex(scapy_chksum_udp))
         self.entries[10].delete(0, END)
         self.entries[10].insert(0, hex(scapy_chksum_IP))
         udp_packet.show()
         self.resultText.insert('end', udp_packet.summary() + '\n')
         self.resultText.insert('end', str(udp_packet) + '\n')
         return Ether() / ip_packet / udp_packet
     except Exception as e:
         print(e.with_traceback())
     finally:
         pass
Esempio n. 2
0
    def post_dissect(self, data):
        # uncompress payload
        packet = IPv6()
        packet.version = IPHC_DEFAULT_VERSION
        packet.tc = self.traffic_class
        packet.fl = self.flow_label
        nh_match = {
            1: socket.IPPROTO_UDP,
            2: socket.IPPROTO_ICMP,
            3: socket.IPPROTO_TCP
        }
        if self.nh:
            packet.nh = nh_match.get(self.nh)
        packet.hlim = self.hopLimit

        packet.src = self.decompressSourceAddr()
        packet.dst = self.decompressDestAddr()

        if self.hc2 and self.nh == 1:  # UDP
            udp = UDP()
            udp.sport = self.udpSourcePort
            udp.dport = self.udpDestPort
            udp.len = self.udpLength or None
            udp.chksum = self.udpChecksum
            udp.add_payload(data)
            packet.add_payload(udp)
        else:
            packet.add_payload(data)
        data = raw(packet)
        return Packet.post_dissect(self, data)
Esempio n. 3
0
 def post_dissect(self, data):
     if not self.underlayer or not hasattr(self.underlayer, "_ipv6"):
         return data
     if self.guess_payload_class(data) != IPv6:
         return data
     # Underlayer is LoWPAN_IPHC
     packet = self.underlayer._ipv6
     try:
         ipv6_hdr = next(
             x for x in self.exts if isinstance(x, LoWPAN_NHC_IPv6Ext)
         )
     except StopIteration:
         ipv6_hdr = None
     if ipv6_hdr:
         # XXX todo: implement: append the IPv6 extension
         # packet = packet / ipv6extension
         pass
     try:
         udp_hdr = next(
             x for x in self.exts if isinstance(x, LoWPAN_NHC_UDP)
         )
     except StopIteration:
         udp_hdr = None
     if udp_hdr:
         packet.nh = 0x11  # UDP
         udp = UDP()
         # https://tools.ietf.org/html/rfc6282#section-4.3.3
         if udp_hdr.C == 0:
             udp.chksum = udp_hdr.udpChecksum
         if udp_hdr.P == 0:
             udp.sport = udp_hdr.udpSourcePort
             udp.dport = udp_hdr.udpDestPort
         elif udp_hdr.P == 1:
             udp.sport = udp_hdr.udpSourcePort
             udp.dport = 0xF000 + udp_hdr.udpDestPort
         elif udp_hdr.P == 2:
             udp.sport = 0xF000 + udp_hdr.udpSourcePort
             udp.dport = udp_hdr.udpDestPort
         elif udp_hdr.P == 3:
             udp.sport = 0xF0B0 + udp_hdr.udpSourcePort
             udp.dport = 0xF0B0 + udp_hdr.udpDestPort
         packet.lastlayer().add_payload(udp / data)
     else:
         packet.lastlayer().add_payload(data)
     data = raw(packet)
     return Packet.post_dissect(self, data)
Esempio n. 4
0
    def post_dissect(self, data):
        """dissect the IPv6 package compressed into this IPHC packet.

        The packet payload needs to be decompressed and depending on the
        arguments, several conversions should be done.
        """

        # uncompress payload
        packet = IPv6()
        packet.version = IPHC_DEFAULT_VERSION
        packet.tc, packet.fl = self._getTrafficClassAndFlowLabel()
        if not self.nh:
            packet.nh = self._nhField
        # HLIM: Hop Limit
        if self.hlim == 0:
            packet.hlim = self._hopLimit
        elif self.hlim == 0x1:
            packet.hlim = 1
        elif self.hlim == 0x2:
            packet.hlim = 64
        else:
            packet.hlim = 255
        # TODO: Payload length can be inferred from lower layers from either the  # noqa: E501
        # 6LoWPAN Fragmentation header or the IEEE802.15.4 header

        packet.src = self.decompressSourceAddr(packet)
        packet.dst = self.decompressDestinyAddr(packet)

        if self.nh == 1:
            # The Next Header field is compressed and the next header is
            # encoded using LOWPAN_NHC

            udp = UDP()
            if self.header_compression and \
               self.header_compression & 0x4 == 0x0:
                udp.chksum = self.udpChecksum

            s, d = nhc_port(self)
            if s == 16:
                udp.sport = self.udpSourcePort
            elif s == 8:
                udp.sport = 0xF000 + s
            elif s == 4:
                udp.sport = 0xF0B0 + s
            if d == 16:
                udp.dport = self.udpDestinyPort
            elif d == 8:
                udp.dport = 0xF000 + d
            elif d == 4:
                udp.dport = 0xF0B0 + d

            packet.payload = udp / data
            data = raw(packet)
        # else self.nh == 0 not necessary
        elif self._nhField & 0xE0 == 0xE0:  # IPv6 Extension Header Decompression  # noqa: E501
            warning('Unimplemented: IPv6 Extension Header decompression'
                    )  # noqa: E501
            packet.payload = conf.raw_layer(data)
            data = raw(packet)
        else:
            packet.payload = conf.raw_layer(data)
            data = raw(packet)

        return Packet.post_dissect(self, data)
Esempio n. 5
0
    def post_dissect(self, data):
        """dissect the IPv6 package compressed into this IPHC packet.

        The packet payload needs to be decompressed and depending on the
        arguments, several conversions should be done.
        """

        # uncompress payload
        packet = IPv6()
        packet.version = IPHC_DEFAULT_VERSION
        packet.tc, packet.fl = self._getTrafficClassAndFlowLabel()
        if not self.nh:
            packet.nh = self._nhField
        # HLIM: Hop Limit
        if self.hlim == 0:
            packet.hlim = self._hopLimit
        elif self.hlim == 0x1:
            packet.hlim = 1
        elif self.hlim == 0x2:
            packet.hlim = 64
        else:
            packet.hlim = 255
        # TODO: Payload length can be inferred from lower layers from either the  # noqa: E501
        # 6LoWPAN Fragmentation header or the IEEE802.15.4 header

        packet.src = self.decompressSourceAddr(packet)
        packet.dst = self.decompressDestinyAddr(packet)

        if self.nh == 1:
            # The Next Header field is compressed and the next header is
            # encoded using LOWPAN_NHC

            udp = UDP()
            if self.header_compression and \
               self.header_compression & 0x4 == 0x0:
                udp.chksum = self.udpChecksum

            s, d = nhc_port(self)
            if s == 16:
                udp.sport = self.udpSourcePort
            elif s == 8:
                udp.sport = 0xF000 + s
            elif s == 4:
                udp.sport = 0xF0B0 + s
            if d == 16:
                udp.dport = self.udpDestinyPort
            elif d == 8:
                udp.dport = 0xF000 + d
            elif d == 4:
                udp.dport = 0xF0B0 + d

            packet.payload = udp / data
            data = raw(packet)
        # else self.nh == 0 not necessary
        elif self._nhField & 0xE0 == 0xE0:  # IPv6 Extension Header Decompression  # noqa: E501
            warning('Unimplemented: IPv6 Extension Header decompression')  # noqa: E501
            packet.payload = conf.raw_layer(data)
            data = raw(packet)
        else:
            packet.payload = conf.raw_layer(data)
            data = raw(packet)

        return Packet.post_dissect(self, data)