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)
def SYN_UDP_Flood(self, dstIP, dstPort, counter): try: total = 0 print("Packets are sending ...") for x in range(0, int(counter)): s_port = random.randint(1000, 9000) s_eq = random.randint(1000, 9000) w_indow = random.randint(1000, 9000) IP_Packet = IP() IP_Packet.src = ".".join( map(str, (random.randint(0, 255) for _ in range(4)))) IP_Packet.dst = dstIP # Created our own TCP packet UDP_Packet = UDP() UDP_Packet.sport = s_port UDP_Packet.dport = int(dstPort) UDP_Packet.flags = "S" UDP_Packet.seq = s_eq UDP_Packet.window = w_indow send(IP_Packet / UDP_Packet, verbose=0) total += 1 print("\nTotal packets sent: %i\n" % total) print() return 1 except: return 0
def send_twamp_test_query(self): """Send a TWAMP query to a reflector""" print('sid ist', self.monitored_path) # Get the counter for the color of the previuos interval sender_block_number = self.get_prev_color() sender_transmit_counter = self.hwadapter.read_tx_counter( sender_block_number, self.monitored_path['sidlist']) list_rev = list(self.monitored_path['sidlistrev']) mod_sidlist = utils.set_punt(list_rev) ipv6_packet = IPv6() ipv6_packet.src = 'fcff:1::1' # TODO me li da il controller? ipv6_packet.dst = list_rev[0] # TODO me li da il controller? # ipv6_packet.dst = 'fcff:3::1' #TODO me li da il controller? # print('Dest', ipv6_packet.dst) srv6_header = IPv6ExtHdrSegmentRouting() srv6_header.addresses = mod_sidlist # TODO vedere se funziona con NS variabile srv6_header.segleft = len(mod_sidlist) - 1 # TODO vedere se funziona con NS variabile srv6_header.lastentry = len(mod_sidlist) - 1 ipv6_packet_inside = IPv6() # TODO me li da il controller? ipv6_packet_inside.src = 'fd00:0:13::1' # TODO me li da il controller? ipv6_packet_inside.dst = 'fd00:0:83::2' ipv6_packet_inside.src = 'fcff:1::1' ipv6_packet_inside.dst = mod_sidlist[-1] udp_packet = UDP() udp_packet.dport = self.refl_udp_port udp_packet.sport = self.ss_udp_port # in band response TODO gestire out band nel controller sender_control_code = 1 sender_seq_num = self.monitored_path['txSequenceNumber'] twamp_data = twamp.TWAMPTestQuery( SequenceNumber=sender_seq_num, TransmitCounter=sender_transmit_counter, BlockNumber=sender_block_number, SenderControlCode=sender_control_code) pkt = (ipv6_packet / srv6_header / ipv6_packet_inside / udp_packet / twamp_data) print( 'SS - SEND QUERY SL {sl} - SN {sn} - TXC {txc} - C {col}'.format( sl=mod_sidlist, sn=sender_seq_num, txc=sender_transmit_counter, col=sender_block_number)) send(pkt, count=1, verbose=False) # Increase the SN self.monitored_path['txSequenceNumber'] += 1
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)
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
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)
def send_twamp_test_response(self, sid_list, sender_block_color, sender_counter, sender_seq_num): """Send a TWAMP response to the sender""" # pylint: disable=too-many-locals # Read the RX counter FW path nopunt_sid_list = utils.rem_punt(sid_list)[:: -1] # no punt and reversed rf_receive_counter = self.hwadapter.read_rx_counter( sender_block_color, nopunt_sid_list) # Reverse path rf_block_number = self.get_prev_color() rf_transmit_counter = self.hwadapter.read_tx_counter( rf_block_number, self.monitored_path['returnsidlist']) ipv6_packet = IPv6() # ipv6_packet.src = 'fcff:5::1' #TODO me li da il controller? # ipv6_packet.dst = 'fcff:4::1' #TODO me li da il controller? ipv6_packet.src = 'fcff:8::1' ipv6_packet.dst = self.monitored_path['returnsidlist'][0] mod_sidlist = utils.set_punt( list(self.monitored_path['returnsidlistrev'])) srv6_header = IPv6ExtHdrSegmentRouting() srv6_header.addresses = mod_sidlist # TODO vedere se funziona con NS variabile srv6_header.segleft = len(mod_sidlist) - 1 # TODO vedere se funziona con NS variabile srv6_header.lastentry = len(mod_sidlist) - 1 ipv6_packet_inside = IPv6() # ipv6_packet_inside.src = 'fcff:5::1' #TODO me li da il controller? # ipv6_packet_inside.dst = 'fcff:2::1' #TODO me li da il controller? ipv6_packet_inside.src = 'fcff:8::1' ipv6_packet_inside.dst = self.monitored_path['returnsidlist'][-1] udp_packet = UDP() udp_packet.dport = self.ss_udp_port udp_packet.sport = self.refl_udp_port # Response sequence number rf_sequence_number = self.monitored_path['revTxSequenceNumber'] # Response control code rf_receiver_control_code = 0 twamp_data = twamp.TWAMPTestResponse( SequenceNumber=rf_sequence_number, TransmitCounter=rf_transmit_counter, BlockNumber=rf_block_number, ReceiveCounter=rf_receive_counter, SenderCounter=sender_counter, SenderBlockNumber=sender_block_color, SenderSequenceNumber=sender_seq_num, ReceverControlCode=rf_receiver_control_code) pkt = (ipv6_packet / srv6_header / ipv6_packet_inside / udp_packet / twamp_data) send(pkt, count=1, verbose=False) # Increse the SequenceNumber self.monitored_path['revTxSequenceNumber'] += 1 print( 'RF - SEND RESP SL {sl} - SN {sn} - TXC {txc} - C {col} - RC {rc}'. format(sl=mod_sidlist, sn=rf_sequence_number, txc=rf_transmit_counter, col=rf_block_number, rc=rf_receive_counter))
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)