def ip_ok(self, tp=False): try: ip = self.ip() except: return False # IP header checksum s = dpkt.in_cksum(ip.pack_hdr() + ip.opts) if s != 0: return False if not tp: return True else: pass # will compute transport protocol checksum # do we have all the data? if len(str(ip)) != ip.len: return True # get payload try: if self.is_tcp(): p = str(self.tcp()) else: p = str(self.udp()) except: return True s = dpkt.struct.pack('>4s4sxBH', ip.src, ip.dst, ip.p, len(p)) s = dpkt.in_cksum(s + p) if s != 0: return False return True
def ip_ok(self, tp=False): try: ip = self.ip() except: return False # IP header checksum s = dpkt.in_cksum(ip.pack_hdr() + ip.opts) if s != 0: return False if not tp: return True else: pass # will compute transport protocol checksum # do we have all the data? if len(str(ip)) != ip.len: return True # get payload try: if self.is_tcp(): p = str(self.tcp()) else: p = str(self.udp()) except: return True s = dpkt.struct.pack('>4s4sxBH', ip.src, ip.dst, ip.p, len(p)) s = dpkt.in_cksum(s+p) if s != 0: return False return True
def compare_checksum(cnt): s = 'A' * 80 start = time.time() for i in range(cnt): dpkt.in_cksum(s) print('dpkt.in_cksum:', cnt / (time.time() - start), 'pps') start = time.time() for i in range(cnt): dnet.ip_cksum_carry(dnet.ip_cksum_add(s, 0)) print('dnet.ip_cksum_add/carry:', cnt / (time.time() - start), 'pps')
def parseOspfMsg(msg, verbose=1, level=0): iph = parseIpHdr(msg[:IP_HDR_LEN], verbose, level) if not iph: return None msg = msg[IP_HDR_LEN:] if dpkt.in_cksum(msg): LOG.error('[ERROR] OSPF header checksum error.') return None ospfh = parseOspfHdr(msg[:OSPF_HDR_LEN], verbose, level+1) rv = { "T": ospfh["TYPE"], "L": ospfh["LEN"], "H": iph, "V": ospfh, } if MSG_TYPES[ospfh["TYPE"]] == "HELLO": rv["V"]["V"] = parseOspfHello(msg[OSPF_HDR_LEN:ospfh["LEN"]], verbose, level+2) elif MSG_TYPES[ospfh["TYPE"]] == "DBDESC": rv["V"]["V"] = parseOspfDesc(msg[OSPF_HDR_LEN:ospfh["LEN"]], verbose, level+2) elif MSG_TYPES[ospfh["TYPE"]] == "LSREQ": rv["V"]["V"] = parseOspfLsReq(msg[OSPF_HDR_LEN:], verbose, level+1) elif MSG_TYPES[ospfh["TYPE"]] == "LSUPD": rv["V"]["V"] = parseOspfLsUpd(msg[OSPF_HDR_LEN:], verbose, level+2) elif MSG_TYPES[ospfh["TYPE"]] == "LSACK": rv["V"]["V"] = parseOspfLsAck(msg[OSPF_HDR_LEN:], verbose, level+2) return rv
def on_vrrp_packet (header, data): ethernet_frame = ethernet.Ethernet(data) ip_packet = ethernet_frame.data vrrp_packet = ip_packet.vrrp # skip over IPv6 right now, IPv4 needs fixing first if vrrp_packet.version == 3: return print "" print ip_packet.unpack print vrrp_packet.unpack print vrrp_packet.checksum vrrp_packet.checksum = 0 print dpkt.in_cksum(vrrp_packet.pack_hdr() + vrrp_packet.addrs[0]) version = vrrp_packet.version for i in vrrp_packet.addrs: print socket.inet_ntop(ip_version_map[version], i)
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
def dpkt_fix_checksums(pkt): try: eth = dpkt.ethernet.Ethernet(pkt) ip = eth.data ip.sum = 0 if type(ip.data) in (dpkt.tcp.TCP, dpkt.udp.UDP): psdhdr = ip.src + ip.dst + struct.pack('!HH', ip.p, ip.len - 20) ip.data.sum = 0 ip.data.sum = dpkt.in_cksum(psdhdr + str(ip.data)) padding = pkt[len(eth):] pkt = str(eth) + padding except: logging.exception('') return pkt
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)
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)
def unpack(self, buf): # Extract and remove the checksum from the end self.csum = struct.unpack('!H', buf[-2:])[0] buf = buf[:-2] # Check the checksum - someone else can complain if it's wrong self.buf_csum = dpkt.in_cksum(buf) # Unpack the header dpkt.Packet.unpack(self, buf) # Now process the TLV's buf = self.data l = [] while buf: tlv = TLV(buf) l.append(tlv) buf = buf[len(tlv):] self.data = l
def __str__(self): data = ''.join(self.addrs) + self.auth if not self.sum: self.sum = dpkt.in_cksum(self.pack_hdr() + data) return self.pack_hdr() + data
def __str__(self): if not self.sum: self.sum = dpkt.in_cksum(dpkt.Packet.__str__(self)) return dpkt.Packet.__str__(self)
def __str__(self): data = ''.join(map(str, self.data)) if not self.sum: self.sum = dpkt.in_cksum(self.pack_hdr() + data) return self.pack_hdr() + data
def __str__(self): data = ''.join(map(str, self.data)) csum = dpkt.in_cksum(self.pack_hdr() + data) return self.pack_hdr() + data + struct.pack('!H', csum)