def test_simple_hop_by_hop_contained_in_ipv6(self): ipv6_binary_packet = [ 0x64, 0x82, 0x46, 0x05, 0x05, 0xdc, 0x00, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xf8, 0x89, 0xd1, 0x30, 0xff, 0x25, 0x6b, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 ] hop_by_hop_binary_packet = [ 0x3a, 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00 ] binary_packet = ipv6_binary_packet + hop_by_hop_binary_packet ip6_packet = IP6.IP6() ip6_packet.set_traffic_class(72) ip6_packet.set_flow_label(148997) ip6_packet.set_payload_length(1500) ip6_packet.set_next_header(17) ip6_packet.set_hop_limit(1) ip6_packet.set_ip_src("FE80::78F8:89D1:30FF:256B") ip6_packet.set_ip_dst("FF02::1:3") hop_by_hop = IP6_Extension_Headers.Hop_By_Hop() hop_by_hop.set_next_header(58) ip6_packet.contains(hop_by_hop) self.assertEqual( self.string_to_list(ip6_packet.get_packet()), binary_packet, "IP6 Hop By Hop Header contained in IPv6 Header - Buffer mismatch") self.assertEqual( ip6_packet.get_size(), len(binary_packet), "IP6 Hop By Hop Header contained in IPv6 Header - Size mismatch")
def send_ns_packet(self, source_link, send_frequency, target_address, vlan_id=0): ip = IP6.IP6() ip.set_source_address(self.get_source_address()) ip.set_destination_address(self.get_target_address()) ip.set_traffic_class(0) ip.set_flow_label(0) ip.set_hop_limit(255) s = socket(AF_PACKET, SOCK_RAW, IPPROTO_ICMPV6) s.bind((self.network_card, N)) payload = self.create_ns_message(source_link, target_address) print send_frequency for i in range(0, send_frequency): icmp = ICMP6.ICMP6() icmp.set_byte(0, 135) # Put Type? icmp.set_byte(1, 00) # Put Code? payloadObject = ImpactPacket.Data() payloadObject.set_data(payload) icmp.contains(payloadObject) ip.contains(icmp) ip.set_next_header(ip.child().get_ip_protocol_number()) ip.set_payload_length(ip.child().get_size()) eth = ImpactPacket.Ethernet( '\x33\x33\x00\x00\x00\x01\xff\xff\xff\xff\xff\xff\x81\x00') eth.pop_tag() if vlan_id != 0: vlan = ImpactPacket.EthernetTag() vlan.set_vid(vlan_id) eth.push_tag(vlan) icmp.calculate_checksum() eth.contains(ip) s.send(eth.get_packet())
def test_chained_basic_options_inside_ipv6_packet(self): ipv6_binary_packet = [ 0x64, 0x82, 0x46, 0x05, 0x05, 0xdc, 0x00, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xf8, 0x89, 0xd1, 0x30, 0xff, 0x25, 0x6b, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 ] hop_by_hop_binary_packet = [ 0x2b, 0x01, 0x01, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] routing_options_binary_packet = [ 0x3c, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00 ] dest_opts_binary_packet = [ 0x3a, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00 ] binary_packet = ipv6_binary_packet + hop_by_hop_binary_packet + routing_options_binary_packet + dest_opts_binary_packet ip6_packet = IP6.IP6() ip6_packet.set_traffic_class(72) ip6_packet.set_flow_label(148997) ip6_packet.set_payload_length(1500) ip6_packet.set_next_header(17) ip6_packet.set_hop_limit(1) ip6_packet.set_ip_src("FE80::78F8:89D1:30FF:256B") ip6_packet.set_ip_dst("FF02::1:3") hop_by_hop = IP6_Extension_Headers.Hop_By_Hop() hop_by_hop.add_option(IP6_Extension_Headers.Option_PADN(14)) routing_options = IP6_Extension_Headers.Routing_Options() routing_options.set_next_header(58) routing_options.set_routing_type(0) routing_options.set_segments_left(10) dest_opts = IP6_Extension_Headers.Destination_Options() dest_opts.add_option(IP6_Extension_Headers.Option_PAD1()) ip6_packet.contains(hop_by_hop) hop_by_hop.contains(routing_options) routing_options.contains(dest_opts) dest_opts.set_next_header(58) self.assertEqual( self.string_to_list(ip6_packet.get_packet()), binary_packet, "Chained options inside an IPv6 packet - Buffer mismatch") self.assertEqual( ip6_packet.get_size(), len(binary_packet), "Chained options inside an IPv6 packet - Size mismatch")
def decode(self, buffer): ip6_packet = IP6.IP6(buffer) self.set_decoded_protocol(ip6_packet) start_pos = ip6_packet.get_header_size() end_pos = ip6_packet.get_payload_length() + start_pos contained_protocol = ip6_packet.get_next_header() multi_protocol_decoder = IP6MultiProtocolDecoder(contained_protocol) child_packet = multi_protocol_decoder.decode(buffer[start_pos:end_pos]) ip6_packet.contains(child_packet) return ip6_packet
def test_creation(self): '''Test IP6 Packet creation.''' crafted_packet = IP6.IP6() crafted_packet.set_traffic_class(72) crafted_packet.set_flow_label(148997) crafted_packet.set_payload_length(1500) crafted_packet.set_next_header(17) crafted_packet.set_hop_limit(1) crafted_packet.set_ip_src("FE80::78F8:89D1:30FF:256B") crafted_packet.set_ip_dst("FF02::1:3") crafted_buffer = crafted_packet.get_bytes().tolist() self.assertEqual(crafted_buffer, self.binary_packet, "IP6 creation - Buffer mismatch")
def encapsulate_icmp6_packet_in_ip6_packet(self, icmp6_packet): #Build IP6 reference packet (which will be used to construct the pseudo-header and checksum) ip6_packet = IP6.IP6() ip6_packet.set_traffic_class(0) ip6_packet.set_flow_label(0) ip6_packet.set_hop_limit(1) ip6_packet.set_ip_src("FE80::78F8:89D1:30FF:256B") ip6_packet.set_ip_dst("FF02::1") #Encapsulate ICMP6 packet in IP6 packet, calculate the checksum using the pseudo-header ip6_packet.contains(icmp6_packet) ip6_packet.set_next_header(ip6_packet.child().get_ip_protocol_number()) ip6_packet.set_payload_length(ip6_packet.child().get_size()) icmp6_packet.calculate_checksum() return ip6_packet
def test_simple_routing_options_contained_in_ipv6(self): ipv6_binary_packet = [ 0x64, 0x82, 0x46, 0x05, 0x05, 0xdc, 0x2b, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xf8, 0x89, 0xd1, 0x30, 0xff, 0x25, 0x6b, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03] routing_options_binary_packet = [ 0x3a, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00] binary_packet = ipv6_binary_packet + routing_options_binary_packet ip6_packet = IP6.IP6() ip6_packet.set_traffic_class(72) ip6_packet.set_flow_label(148997) ip6_packet.set_payload_length(1500) ip6_packet.set_next_header(17) ip6_packet.set_hop_limit(1) ip6_packet.set_source_address("FE80::78F8:89D1:30FF:256B") ip6_packet.set_destination_address("FF02::1:3") routing_options = IP6_Extension_Headers.Routing_Options() routing_options.set_next_header(58) routing_options.set_routing_type(0) routing_options.set_segments_left(10) ip6_packet.contains(routing_options) self.assertEquals( self.string_to_list(ip6_packet.get_packet()), binary_packet, "IP6 Hop By Hop Header contained in IPv6 Header - Buffer mismatch") self.assertEquals( ip6_packet.get_size(), len(binary_packet), "IP6 Hop By Hop Header contained in IPv6 Header - Size mismatch")
def ping(destination_address): global source_address, raw_socket, payload, sequence_id ip = IP6.IP6() ip.set_ip_src(source_address) ip.set_ip_dst(destination_address) ip.set_traffic_class(0) ip.set_flow_label(0) ip.set_hop_limit(64) sequence_id += 1 sequence_id = sequence_id % 65536 icmp = ICMP6.ICMP6.Echo_Request(1, sequence_id, payload) ip.contains(icmp) ip.set_next_header(ip.child().get_ip_protocol_number()) ip.set_payload_length(ip.child().get_size()) icmp.calculate_checksum() raw_socket.sendto(icmp.get_packet(), (destination_address, 0))
import sys from impacket import ImpactDecoder, IP6, ICMP6, version print(version.BANNER) if len(sys.argv) < 3: print("Use: %s <src ip> <dst ip>" % sys.argv[0]) sys.exit(1) src = sys.argv[1] dst = sys.argv[2] # Create a new IP packet and set its source and destination addresses. ip = IP6.IP6() ip.set_ip_src(src) ip.set_ip_dst(dst) ip.set_traffic_class(0) ip.set_flow_label(0) ip.set_hop_limit(64) # Open a raw socket. Special permissions are usually required. s = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_ICMPV6) payload = "A" * 156 print("PING %s %d data bytes" % (dst, len(payload))) seq_id = 0 while 1: # Give the ICMP packet the next ID in the sequence.