예제 #1
0
def calculate_and_set_checksum(ip):
    """
    This function calculates tcp, udp and ip checksums and sets the corresponding packet field to this value

    :param ip: ip objects, loaded using dpkt library
    :return: checksum, packet buffer with set checksum
    """
    if ip.v == 4:
        IP_MF = 0x2000
        IP_OFFMASK = 0x1fff
        # set checksum field to zero
        ip.data.sum = 0

        if (ip.p == 6 or ip.p == 17) and (ip.off & (IP_MF | IP_OFFMASK)) == 0 and \
                isinstance(ip.data, dpkt.Packet) and ip.data.sum == 0:
            # Set zeroed TCP and UDP checksums for non-fragments.
            p = bytes(ip.data)
            s = dpkt.struct.pack('>4s4sxBH', ip.src, ip.dst, ip.p, len(p))
            s = dpkt.in_cksum_add(0, s)
            s = dpkt.in_cksum_add(s, p)
            # ip.data.sum = dpkt.in_cksum_done(s)
            sum = dpkt.in_cksum_done(s)
            if ip.p == 17 and sum == 0:
                sum = 0xffff  # RFC 768
                # XXX - skdada transports which don't need the pseudoheader
            return sum, p
    elif ip.v == 6:
        # XXX - set TCP, UDP, and ICMPv6 checksums
        # if (ip.p == 6 or ip.p == 17 or ip.p == 58) and ip.data.sum == None:
        if (ip.p == 6 or ip.p == 17):  # 6: TCP, 7: UDP
            # ip.data.sum = None
            # set checksum field to zero
            ip.data = bytearray(bytes(ip.data))
            if ip.p == 6:
                ip.data[16] = 0
                ip.data[17] = 0
            elif ip.p == 17:
                ip.data[6] = 0
                ip.data[7] = 0
            p = bytes(ip.data)
            s = dpkt.struct.pack('>16s16sxBH', ip.src, ip.dst, ip.nxt, len(p))
            s = dpkt.in_cksum_add(0, s)
            s = dpkt.in_cksum_add(s, p)
            try:
                return dpkt.in_cksum_done(s), p
            except AttributeError:
                return None
    else:
        print('no IP?')
        return None
예제 #2
0
 def __str__(self):
     if (self.nxt == 6 or self.nxt == 17 or self.nxt == 58) and not self.data.sum:
         # XXX - set TCP, UDP, and ICMPv6 checksums
         p = str(self.data)
         s = dpkt.in_cksum_add(0, self.src + self.dst)
         s = dpkt.in_cksum_add(s, p)
         try:
             self.data.sum = dpkt.in_cksum_done(s + self.nxt + len(p))
         except AttributeError:
             pass
     return dpkt.Packet.__str__(self)
예제 #3
0
 def __str__(self):
     if (self.nxt == 6 or self.nxt == 17 or self.nxt == 58) and \
            not self.data.sum:
         # XXX - set TCP, UDP, and ICMPv6 checksums
         p = str(self.data)
         s = dpkt.in_cksum_add(0, self.src + self.dst)
         s = dpkt.in_cksum_add(s, p)
         try:
             self.data.sum = dpkt.in_cksum_done(s + self.nxt + len(p))
         except AttributeError:
             pass
     return dpkt.Packet.__str__(self)
예제 #4
0
 def __str__(self):
     if (self.nxt == 6 or self.nxt == 17 or self.nxt == 58) and not self.data.sum:
         # XXX - set TCP, UDP, and ICMPv6 checksums
         p = str(self.data)
         s = dpkt.struct.pack('>16s16sxBH', self.src, self.dst, self.nxt, len(p))
         s = dpkt.in_cksum_add(0, s)
         s = dpkt.in_cksum_add(s, p)
         try:
             self.data.sum = dpkt.in_cksum_done(s)
         except AttributeError:
             pass
     return self.pack_hdr() + self.headers_str() + str(self.data)
예제 #5
0
 def __str__(self):
     if (self.p == 6 or self.p == 17 or self.p == 58) and not self.data.sum:
         # XXX - set TCP, UDP, and ICMPv6 checksums
         p = str(self.data)
         s = dpkt.struct.pack('>16s16sxBH', self.src, self.dst, self.nxt,
                              len(p))
         s = dpkt.in_cksum_add(0, s)
         s = dpkt.in_cksum_add(s, p)
         try:
             self.data.sum = dpkt.in_cksum_done(s)
         except AttributeError:
             pass
     return self.pack_hdr() + self.headers_str() + str(self.data)
예제 #6
0
    def __verify_checksums(cls, ippacket):
        if dpkt.in_cksum(ippacket.pack_hdr() + str(ippacket.opts)) != 0:
            return False

        if (ippacket.off & (dpkt.ip.IP_MF | dpkt.ip.IP_OFFMASK)) != 0:
            return True

        p = str(ippacket.data)
        s = dpkt.struct.pack('>4s4sxBH', ippacket.src, ippacket.dst,
                             ippacket.p, len(p))
        s = dpkt.in_cksum_add(0, s)
        s = dpkt.in_cksum_add(s, p)
        return dpkt.in_cksum_done(s) == 0
예제 #7
0
파일: ip.py 프로젝트: birnbap/dpkt
 def __str__(self):
     if self.sum == 0:
         self.sum = dpkt.in_cksum(self.pack_hdr() + str(self.opts))
         if (self.p == 6 or self.p == 17) and (self.off & (IP_MF | IP_OFFMASK)) == 0 and \
                 isinstance(self.data, dpkt.Packet) and self.data.sum == 0:
             # Set zeroed TCP and UDP checksums for non-fragments.
             p = str(self.data)
             s = dpkt.struct.pack('>4s4sxBH', self.src, self.dst,
                                  self.p, len(p))
             s = dpkt.in_cksum_add(0, s)
             s = dpkt.in_cksum_add(s, p)
             self.data.sum = dpkt.in_cksum_done(s)
             if self.p == 17 and self.data.sum == 0:
                 self.data.sum = 0xffff  # RFC 768
                 # XXX - skip transports which don't need the pseudoheader
     return self.pack_hdr() + str(self.opts) + str(self.data)
예제 #8
0
 def __str__(self):
     if self.sum == 0:
         self.sum = dpkt.in_cksum(self.pack_hdr() + self.opts)
         if (self.p == 6 or self.p == 17) and (self.off & (IP_MF | IP_OFFMASK)) == 0 and \
                 isinstance(self.data, dpkt.Packet) and self.data.sum == 0:
             # Set zeroed TCP and UDP checksums for non-fragments.
             p = str(self.data)
             s = dpkt.struct.pack('>4s4sxBH', self.src, self.dst,
                                  self.p, len(p))
             s = dpkt.in_cksum_add(0, s)
             s = dpkt.in_cksum_add(s, p)
             self.data.sum = dpkt.in_cksum_done(s)
             if self.p == 17 and self.data.sum == 0:
                 self.data.sum = 0xffff  # RFC 768
                 # XXX - skip transports which don't need the pseudoheader
     return self.pack_hdr() + self.opts + str(self.data)