Beispiel #1
0
	def _calc_sum(self):
		"""Recalculate the TCP-checksum This won't reset changed state."""
		# TCP and underwriting are freaky bitches: we need the IP pseudoheader to calculate their checksum.
		try:
			# we need src/dst for checksum-calculation
			src, dst = self._lower_layer.src, self._lower_layer.dst
			self.sum = 0
			# logger.debug("TCP sum recalc: IP=%d / %s / %s" % (len(src), src, dst))

			tcp_bin = self.header_bytes + self.body_bytes
			# IP-pseudoheader, check if version 4 or 6
			if len(src) == 4:
				s = pack(">4s4sxBH", src, dst, 6, len(tcp_bin))			# 6 = TCP
			else:
				s = pack(">16s16sxBH", src, dst, 6, len(tcp_bin))		# 6 = TCP

			# Get checksum of concatenated pseudoheader+TCP packet
			# logger.debug("pseudoheader: %r" % s)
			# logger.debug("tcp_bin: %r" % tcp_bin)
			# assign via non-shadowed variable to trigger re-packing
			self.sum = checksum.in_cksum(s + tcp_bin)
			# logger.debug(">>> new checksum: %0X" % self._sum)
		except Exception:
			# not an IP packet as lower layer (src, dst not present) or invalid src/dst
			# logger.debug("could not calculate checksum: %r" % e)
			pass
Beispiel #2
0
	def _calc_sum(self):
		"""Recalculate the UDP-checksum."""
		# TCP and underwriting are freaky bitches: we need the IP pseudoheader to calculate their checksum
		# logger.debug("UDP sum recalc: %s/%s/%s" % (src, dst, changed))
		try:
			# we need src/dst for checksum-calculation
			src, dst = self._lower_layer.src, self._lower_layer.dst
			# logger.debug(src + b" / "+ dst)
			self.sum = 0
			udp_bin = self.header_bytes + self.body_bytes

			# IP-pseudoheader, check if version 4 or 6
			if len(src) == 4:
				s = pack_ipv4(src, dst, 17, len(udp_bin))  # 17 = UDP
			else:
				s = pack_ipv6(src, dst, 17, len(udp_bin))  # 17 = UDP

			csum = checksum.in_cksum(s + udp_bin)

			if csum == 0:
				csum = 0xffff    # RFC 768, p2

			# get the checksum of concatenated pseudoheader+TCP packet
			# assign via non-shadowed variable to trigger re-packing
			self.sum = csum
		except (AttributeError, struct.error):
			# not an IP packet as lower layer (src, dst not present) or invalid src/dst
			pass
Beispiel #3
0
	def _update_fields(self):
		if self.sum_au_active and self._changed():
			# logger.debug(">>> IP: calculating sum")
			# reset checksum for recalculation,  mark as changed / clear cache
			self.sum = 0
			# logger.debug(">>> IP: bytes for sum: %s" % self.header_bytes)
			self.sum = checksum.in_cksum(pypacker.Packet.bin(self, update_auto_fields=True))
Beispiel #4
0
 def __calc_sum(self):
     """Recalculate checksum."""
     # logger.debug(">>> IP: calculating sum")
     # reset checksum for recalculation,  mark as changed / clear cache
     self._sum = 0
     # logger.debug(">>> IP: bytes for sum: %s" % self.pack_hdr())
     self._sum = checksum.in_cksum(self.pack_hdr())
Beispiel #5
0
    def _calc_sum(self):
        """Recalculate the TCP-checksum. This won't reset changed state."""
        # TCP and underwriting are freaky bitches: we need the IP pseudoheader
        # to calculate their checksum.
        try:
            # we need src/dst for checksum-calculation
            src, dst = self._lower_layer.src, self._lower_layer.dst
            self.sum = 0
            # logger.debug("TCP sum recalc: IP=%d / %s / %s" % (len(src), src, dst))

            tcp_bin = self.header_bytes + self.body_bytes
            # IP-pseudoheader, check if version 4 or 6
            if len(src) == 4:
                s = pack_ipv4_header(src, dst, 6, len(tcp_bin))  # 6 = TCP
            else:
                s = pack_ipv6_header(src, dst, 6, len(tcp_bin))  # 6 = TCP

            # Get checksum of concatenated pseudoheader+TCP packet
            # logger.debug("pseudoheader: %r" % s)
            # logger.debug("tcp_bin: %r" % tcp_bin)
            # assign via non-shadowed variable to trigger re-packing
            self.sum = checksum.in_cksum(s + tcp_bin)
            # logger.debug(">>> new checksum: %0X" % self._sum)
        except (AttributeError, struct.error):
            # not an IP packet as lower layer (src, dst not present) or invalid src/dst
            # logger.debug("could not calculate checksum: %r" % e)
            pass
Beispiel #6
0
	def _calc_sum(self):
		"""Recalculate the UDP-checksum."""
		# TCP and underwriting are freaky bitches: we need the IP pseudoheader to calculate their checksum
		# logger.debug("UDP sum recalc: %s/%s/%s" % (src, dst, changed))
		try:
			# we need src/dst for checksum-calculation
			src, dst = self._lower_layer.src, self._lower_layer.dst
			# logger.debug(src + b" / "+ dst)
			self.sum = 0
			udp_bin = self.header_bytes + self.body_bytes

			# IP-pseudoheader: IP src, dst, \x00, UDP upper proto, length
			# check if version 4 or 6
			if len(src) == 4:
				s = pack(">4s4sBBH", src, dst, 0, 17, len(udp_bin))		# 17 = UDP
			else:
				s = pack(">16s16sxBH", src, dst, 17, len(udp_bin))		# 17 = UDP

			csum = checksum.in_cksum(s + udp_bin)

			if csum == 0:
				csum = 0xffff    # RFC 768, p2

			# get the checksum of concatenated pseudoheader+TCP packet
			# assign via non-shadowed variable to trigger re-packing
			self.sum = csum
		except (AttributeError, struct.error):
			# not an IP packet as lower layer (src, dst not present) or invalid src/dst
			pass
Beispiel #7
0
 def _update_fields(self):
     # logger.debug("sum is: %d" % self.sum)
     if self.sum_au_active and self._changed():
         # logger.debug("sum is: %d" % self.sum)
         # logger.debug("header: %r", self.header_bytes)
         # logger.debug("body: %r", self.body_bytes)
         self.sum = 0
         self.sum = checksum.in_cksum(self.header_bytes + self.body_bytes)
Beispiel #8
0
	def bin(self, update_auto_fields=True):
		# logger.debug("sum is: %d" % self.sum)
		if update_auto_fields and self.sum_au_active and self._changed():
			# logger.debug("sum is: %d" % self.sum)
			self.sum = 0
			self.sum = checksum.in_cksum(self._pack_header() + self.body_bytes)
			# logger.debug("sum is: %d" % self.sum)
		return pypacker.Packet.bin(self, update_auto_fields=update_auto_fields)
Beispiel #9
0
 def bin(self, update_auto_fields=True):
     # logger.debug("sum is: %d" % self.sum)
     if update_auto_fields and self.sum_au_active and self._changed():
         # logger.debug("sum is: %d" % self.sum)
         self.sum = 0
         self.sum = checksum.in_cksum(self._pack_header() + self.body_bytes)
         # logger.debug("sum is: %d" % self.sum)
     return pypacker.Packet.bin(self, update_auto_fields=update_auto_fields)
Beispiel #10
0
	def bin(self, update_auto_fields=True):
		if update_auto_fields and self.sum_au_active and self._changed():
				# logger.debug(">>> IP: calculating sum")
				# reset checksum for recalculation,  mark as changed / clear cache
				self.sum = 0
				# logger.debug(">>> IP: bytes for sum: %s" % self.header_bytes)
				self.sum = checksum.in_cksum(pypacker.Packet.bin(self, update_auto_fields=True))

		return pypacker.Packet.bin(self, update_auto_fields=update_auto_fields)
Beispiel #11
0
    def create_gre_obj(cls, utg_gre):
        # type: (Packet.GRE) -> object
        obj = GRE()
        #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8')
        obj.version_number = utg_gre.version
        #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8')
        if utg_gre.use_checksum is True:
            obj.opts.append(GREOptionChecksum(checksum=0))
            obj.opts.append(GREOptionReserve_1())
            obj.checksum_bit = 1
        #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8')
        if utg_gre.key_field is not None:
            obj.opts.append(
                GREOptionKey(
                    key=Converter.convertstring2int(utg_gre.key_field)))
            obj.key_bit = 1
        #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8')
        if utg_gre.sequence_number is not None:
            obj.opts.append(
                GREOptionSequence(sequence=Converter.convertstring2int(
                    utg_gre.sequence_number)))
            obj.sequence_bit = 1
        #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8')
        if utg_gre.use_checksum is True:
            innerPacketData = obj.bin()
            if utg_gre.l3_proto is TGEnums.L3_PROTO.IPV4:
                obj.protocol_type = ETH_TYPE_IP
                packer_v4 = utg_gre.ipv4.to_packer()
                innerPacketData += packer_v4.bin()
            elif utg_gre.l3_proto is TGEnums.L3_PROTO.IPV6:
                obj.protocol_type = ETH_TYPE_IP6
                v6 = utg_gre.ipv6.to_packer()
                innerPacketData += v6.bin()
            else:
                proto = Converter.remove_non_hexa_sumbols(utg_gre.l3_proto)
                proto = Converter.hexaString2int(proto)
                obj.protocol_type = proto

            if utg_gre.l4_proto is TGEnums.L4_PROTO.UDP:
                packer_udp = utg_gre.udp.to_packer()
                innerPacketData += packer_udp.bin()
            elif utg_gre.l4_proto is TGEnums.L4_PROTO.TCP:
                packer_tcp = utg_gre.tcp.to_packer()
                innerPacketData += packer_tcp.bin()

            s = checksum.in_cksum(innerPacketData)
            obj.opts[0].checksum = s
        #bin_headers = '0x' + binascii.hexlify(obj.bin()).decode('utf-8')
        return obj
Beispiel #12
0
 def _calc_sum(self):
     try:
         # we need src/dst for checksum-calculation
         src, dst = self._lower_layer.src, self._lower_layer.dst
         # logger.debug("TCP sum recalc: IP=%d / %s / %s" % (len(src), src, dst))
         # pseudoheader
         # packet length = length of upper layers
         self.sum = 0
         pkt = self.header_bytes + self.body_bytes
         hdr = pack_ipv6_icmp6(src, dst, len(pkt), 58)
         self.sum = checksum.in_cksum(hdr + pkt)
         #logger.debug(">>> new checksum: %0X" % self._sum)
     except Exception:
         # not an IP packet as lower layer (src, dst not present) or invalid src/dst
         # logger.debug("could not calculate checksum: %r" % e)
         pass
Beispiel #13
0
	def __calc_sum(self):
		"""Recalculate the TCP-checksum This won't reset changed state."""
		self._sum = 0
		tcp_bin = self.pack_hdr() + self.data
		# we need src/dst for checksum-calculation
		src, dst, changed = self._callback("ip_src_dst_changed")
		#logger.debug("TCP sum recalc: IP=%d/%s/%s/%s" % (len(src), src, dst, changed))

		# IP-pseudoheader, check if version 4 or 6
		if len(src) == 4:
			s = pack(">4s4sxBH", src, dst, 6, len(tcp_bin)) # 6 = TCP
		else:
			s = pack(">16s16sxBH", src, dst, 6, len(tcp_bin)) # 6 = TCP

		# Get checksum of concatenated pseudoheader+TCP packet
		self._sum = checksum.in_cksum(s + tcp_bin)
Beispiel #14
0
	def _calc_sum(self):
		try:
			# we need src/dst for checksum-calculation
			src, dst = self._lower_layer.src, self._lower_layer.dst
		except Exception:
			# not an IP packet as lower layer (src, dst not present) or invalid src/dst
			# logger.debug("could not calculate checksum: %r" % e)
			return

		# pseudoheader
		# packet length = length of upper layers
		self.sum = 0
		# logger.debug("TCP sum recalc: IP6= len(src)=%d\n%s\n%s\nhdr=%s\nbody=%s" %
		#			 (len(src), src, dst, self.header_bytes, self.body_bytes))
		pkt = self.header_bytes + self.body_bytes
		hdr = pack_ipv6_icmp6(src, dst, len(pkt), 58)
		# this will set the header status to changes, should be reset by calling bin()
		self.sum = checksum.in_cksum(hdr + pkt)
Beispiel #15
0
	def bin(self, update_auto_fields=True):
		if update_auto_fields:
			if self._changed():
				self.len = len(self)

				# length changed so we have to recalculate checksum
				# logger.debug("updating checksum")
				# logger.debug(">>> IP: calculating sum")
				# reset checksum for recalculation,  mark as changed / clear cache
				self.sum = 0
				# Update header length. NOTE: needs to be a multiple of 4 Bytes.
				# logger.debug("updating: %r" % self._packet)
				# options length need to be multiple of 4 Bytes
				self._hl = int(self.header_len / 4) & 0xf
				# logger.debug(">>> IP: bytes for sum: %s" % self.header_bytes)
				self.sum = checksum.in_cksum(self._pack_header())
				# logger.debug("IP: new hl: %d / %d" % (self._packet.hdr_len, hdr_len_off))
				# logger.debug("new sum: %0X" % self.sum)

		return pypacker.Packet.bin(self, update_auto_fields=update_auto_fields)
Beispiel #16
0
	def __calc_sum(self):
		"""Recalculate the UDP-checksum."""
		# mark as achanged
		self._sum = 0
		udp_bin = self.pack_hdr() + self.data
		src, dst, changed = self._callback("ip_src_dst_changed")

		#logger.debug("UDP sum recalc: %s/%s/%s" % (src, dst, changed))

		# IP-pseudoheader, check if version 4 or 6
		if len(src) == 4:
			s = pack(">4s4sxBH", src, dst, 17, len(udp_bin)) # 17 = UDP
		else:
			s = pack(">16s16sxBH", src, dst, 17, len(udp_bin)) # 17 = UDP

		sum = checksum.in_cksum(s + udp_bin)
		if sum == 0:
			sum = 0xffff    # RFC 768, p2

		# get the checksum of concatenated pseudoheader+TCP packet
		self._sum = sum
Beispiel #17
0
 def _calc_checksum(self, payload):
     """
     Using a Pseudoheader of:
         - Source address
         - Destination address
         - L4 protocol type (UDP)
         - UDP header, excluding checksum
     """
     assert isinstance(self._src, SCIONAddr), type(self._src)
     assert isinstance(self._dst, SCIONAddr), type(self._dst)
     pseudo_header = b"".join([
         self._dst.isd_as.pack(),
         self._src.isd_as.pack(),
         self._dst.host.pack(),
         self._src.host.pack(),
         b"\x00",
         struct.pack("!B", L4Proto.UDP),
         self.pack(payload, checksum=bytes(2)),
         payload,
     ])
     chk_int = checksum.in_cksum(pseudo_header)
     return struct.pack("!H", chk_int)
Beispiel #18
0
	def __calc_sum(self):
		self._sum = 0
		object.__setattr__(self, "_sum", checksum.in_cksum(pypacker.Packet.bin()) )
Beispiel #19
0
	def __calc_sum(self):
		# mark as changed
		self._sum = 0
		self._sum = checksum.in_cksum( pypacker.Packet.bin(self) )
Beispiel #20
0
 def bin(self, update_auto_fields=True):
     if update_auto_fields and self.sum_au_active and self._changed():
         self.sum = 0
         self.sum = checksum.in_cksum(pypacker.Packet.bin(self))
     return pypacker.Packet.bin(self, update_auto_fields=update_auto_fields)
Beispiel #21
0
 def _update_fields(self):
     if self.sum_au_active and self._changed():
         self.sum = 0
         self.sum = checksum.in_cksum(pypacker.Packet.bin(self))
Beispiel #22
0
	def __str__(self):
		data = "".join(map(str, self.data))
		if not self.sum:
			self.sum = checksum.in_cksum(self.pack_hdr() + data)
		return self.pack_hdr() + data
Beispiel #23
0
	def __calc_sum(self):
		self._sum = 0
		self._sum = checksum.in_cksum(pypacker.Packet.bin(self))
Beispiel #24
0
 def bin(self, update_auto_fields=True):
     if update_auto_fields and self._changed():
         self.sum = 0
         self.sum = checksum.in_cksum(pypacker.Packet.bin(self))
     return pypacker.Packet.bin(self, update_auto_fields=update_auto_fields)
Beispiel #25
0
	def __calc_sum(self):
		# mark as changed / clear cache
		self._sum = 0
		self._sum = checksum.in_cksum(self.pack_hdr() + self.data)