github.com/n00py/Slackor@v0.0.0-20200610224921-d007fcea1740/impacket/tests/ImpactPacket/test_ICMP6.py (about)

     1  #!/usr/bin/env python
     2  #Impact test version
     3  try:
     4      from impacket import IP6_Address, IP6, ImpactDecoder, ICMP6
     5  except:
     6      pass
     7  
     8  #Standalone test version
     9  try:
    10      import sys
    11      sys.path.insert(0,"../..")
    12      import IP6_Address, IP6, ImpactDecoder, ICMP6
    13  except:
    14      pass
    15  
    16  import unittest
    17  
    18  class TestICMP6(unittest.TestCase):
    19          
    20      def setUp(self):
    21          self.packet_list = self.generate_icmp6_constructed_packets()        
    22          self.message_description_list = [
    23                               "Echo Request",
    24                               "Echo Reply",
    25                               "Parameter problem - Erroneous header field",
    26                               "Parameter problem - Unrecognized Next Header",
    27                               "Parameter problem - Unrecognized IP6 Option",
    28                               "Destination unreachable - No route to destination",
    29                               "Destination unreachable - Administratively prohibited",
    30                               "Destination unreachable - Beyond scope of source address",
    31                               "Destination unreachable - Address unreachable ",
    32                               "Destination unreachable - Port unreachable",
    33                               "Destination unreachable - Src addr failed due to policy",
    34                               "Destination unreachable - Reject route",
    35                               "Time exceeded - Hop limit exceeded in transit",
    36                               "Time exceeded - Fragment reassembly time exceeded",
    37                               "Packet too big"
    38                               ]
    39          self.reference_data_list = [
    40                                 [0x80, 0x00, 0xA2, 0xA6, 0x00, 0x01, 0x00, 0x02, 0xFE, 0x56, 0x88],#Echo Request
    41                                 [0x81, 0x00, 0xA1, 0xA6, 0x00, 0x01, 0x00, 0x02, 0xFE, 0x56, 0x88],#Echo Reply
    42                                 [0x04, 0x00, 0x1E, 0xA8, 0x00, 0x00, 0x00, 0x02, 0xFE, 0x56, 0x88],#Parameter problem
    43                                 [0x04, 0x01, 0x1E, 0xA7, 0x00, 0x00, 0x00, 0x02, 0xFE, 0x56, 0x88],
    44                                 [0x04, 0x02, 0x1E, 0xA6, 0x00, 0x00, 0x00, 0x02, 0xFE, 0x56, 0x88],
    45                                 [0x01, 0x00, 0x21, 0xAA, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x56, 0x88],#Dest. unreachable
    46                                 [0x01, 0x01, 0x21, 0xA9, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x56, 0x88],
    47                                 [0x01, 0x02, 0x21, 0xA8, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x56, 0x88],
    48                                 [0x01, 0x03, 0x21, 0xA7, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x56, 0x88],
    49                                 [0x01, 0x04, 0x21, 0xA6, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x56, 0x88],
    50                                 [0x01, 0x05, 0x21, 0xA5, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x56, 0x88],
    51                                 [0x01, 0x06, 0x21, 0xA4, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x56, 0x88],
    52                                 [0x03, 0x00, 0x1F, 0xAA, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x56, 0x88],#Time exceeded
    53                                 [0x03, 0x01, 0x1F, 0xA9, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x56, 0x88],
    54                                 [0x02, 0x00, 0x1B, 0x96, 0x00, 0x00, 0x05, 0x14, 0xFE, 0x56, 0x88]#Packet too big
    55                                 ]
    56          
    57      def encapsulate_icmp6_packet_in_ip6_packet(self, icmp6_packet):    
    58          #Build IP6 reference packet (which will be used to construct the pseudo-header and checksum)
    59          ip6_packet = IP6.IP6()
    60          ip6_packet.set_traffic_class(0)
    61          ip6_packet.set_flow_label(0)
    62          ip6_packet.set_hop_limit(1)
    63          ip6_packet.set_ip_src("FE80::78F8:89D1:30FF:256B")
    64          ip6_packet.set_ip_dst("FF02::1")
    65      
    66          #Encapsulate ICMP6 packet in IP6 packet, calculate the checksum using the pseudo-header        
    67          ip6_packet.contains(icmp6_packet)
    68          ip6_packet.set_next_header(ip6_packet.child().get_ip_protocol_number())
    69          ip6_packet.set_payload_length(ip6_packet.child().get_size())
    70          icmp6_packet.calculate_checksum()
    71          return ip6_packet
    72          
    73      def compare_icmp6_packet_with_reference_buffer(self, icmp6_packet, reference_buffer, test_fail_message):
    74          #Encapsulate the packet, in order to compute the checksum
    75          ip6_packet = self.encapsulate_icmp6_packet_in_ip6_packet(icmp6_packet)
    76          
    77          #Extract the header and payload bytes
    78          icmp6_header_buffer = ip6_packet.child().get_bytes().tolist()
    79          icmp6_payload_buffer = icmp6_packet.child().get_bytes().tolist()
    80          generated_buffer = icmp6_header_buffer + icmp6_payload_buffer
    81          
    82          self.assertEquals(generated_buffer, reference_buffer, test_fail_message)
    83          
    84      def generate_icmp6_constructed_packets(self):
    85          packet_list = []
    86          
    87          arbitrary_data = [0xFE, 0x56, 0x88]
    88          echo_id = 1
    89          echo_sequence_number = 2
    90          icmp6_packet = ICMP6.ICMP6.Echo_Request(echo_id, echo_sequence_number, arbitrary_data)
    91          packet_list.append(icmp6_packet)                
    92          icmp6_packet = ICMP6.ICMP6.Echo_Reply(echo_id, echo_sequence_number, arbitrary_data)                
    93          packet_list.append(icmp6_packet)        
    94  
    95          originating_packet_data = arbitrary_data        
    96          for code in range(0, 3):
    97              problem_pointer = 2
    98              icmp6_packet = ICMP6.ICMP6.Parameter_Problem(code, problem_pointer, originating_packet_data)                
    99              packet_list.append(icmp6_packet)        
   100  
   101          for code in range(0, 7):
   102              icmp6_packet = ICMP6.ICMP6.Destination_Unreachable(code, originating_packet_data)                
   103              packet_list.append(icmp6_packet)        
   104              
   105          for code in range(0, 2):
   106              icmp6_packet = ICMP6.ICMP6.Time_Exceeded(code, originating_packet_data)                
   107              packet_list.append(icmp6_packet)        
   108          
   109          icmp6_packet = ICMP6.ICMP6.Packet_Too_Big(1300, originating_packet_data)                
   110          packet_list.append(icmp6_packet)        
   111          return packet_list
   112  
   113  
   114          
   115      def test_message_construction(self):
   116          for packet, reference, msg in zip(self.packet_list, self.reference_data_list, self.message_description_list):
   117              self.compare_icmp6_packet_with_reference_buffer(packet, reference, "ICMP6 creation of " + msg + " - Buffer mismatch")
   118              
   119      def test_message_decoding(self):                    
   120          d = ImpactDecoder.ICMP6Decoder()
   121          
   122          msg_types = [
   123                       ICMP6.ICMP6.ECHO_REQUEST,
   124                       ICMP6.ICMP6.ECHO_REPLY,
   125                       ICMP6.ICMP6.PARAMETER_PROBLEM,
   126                       ICMP6.ICMP6.PARAMETER_PROBLEM,
   127                       ICMP6.ICMP6.PARAMETER_PROBLEM,
   128                       ICMP6.ICMP6.DESTINATION_UNREACHABLE,
   129                       ICMP6.ICMP6.DESTINATION_UNREACHABLE,
   130                       ICMP6.ICMP6.DESTINATION_UNREACHABLE,
   131                       ICMP6.ICMP6.DESTINATION_UNREACHABLE,
   132                       ICMP6.ICMP6.DESTINATION_UNREACHABLE,
   133                       ICMP6.ICMP6.DESTINATION_UNREACHABLE,
   134                       ICMP6.ICMP6.DESTINATION_UNREACHABLE,
   135                       ICMP6.ICMP6.TIME_EXCEEDED,
   136                       ICMP6.ICMP6.TIME_EXCEEDED,
   137                       ICMP6.ICMP6.PACKET_TOO_BIG
   138                       ]
   139          
   140          msg_codes = [
   141                      0,
   142                      0,                    
   143                      ICMP6.ICMP6.ERRONEOUS_HEADER_FIELD_ENCOUNTERED,
   144                      ICMP6.ICMP6.UNRECOGNIZED_NEXT_HEADER_TYPE_ENCOUNTERED,
   145                      ICMP6.ICMP6.UNRECOGNIZED_IPV6_OPTION_ENCOUNTERED,
   146                      ICMP6.ICMP6.NO_ROUTE_TO_DESTINATION,
   147                      ICMP6.ICMP6.ADMINISTRATIVELY_PROHIBITED,
   148                      ICMP6.ICMP6.BEYOND_SCOPE_OF_SOURCE_ADDRESS,
   149                      ICMP6.ICMP6.ADDRESS_UNREACHABLE,
   150                      ICMP6.ICMP6.PORT_UNREACHABLE,
   151                      ICMP6.ICMP6.SOURCE_ADDRESS_FAILED_INGRESS_EGRESS_POLICY,
   152                      ICMP6.ICMP6.REJECT_ROUTE_TO_DESTINATION,    
   153                      ICMP6.ICMP6.HOP_LIMIT_EXCEEDED_IN_TRANSIT,
   154                      ICMP6.ICMP6.FRAGMENT_REASSEMBLY_TIME_EXCEEDED,
   155                      0
   156                      ]
   157          
   158          for i in range (0, len(self.reference_data_list)):
   159              p = d.decode(self.reference_data_list[i])
   160              self.assertEquals(p.get_type(), msg_types[i], self.message_description_list[i] + " - Msg type mismatch")
   161              self.assertEquals(p.get_code(), msg_codes[i], self.message_description_list[i] + " - Msg code mismatch")
   162              
   163              if i in range(0, 2):
   164                  self.assertEquals(p.get_echo_id(), 1, self.message_description_list[i] + " - ID mismatch")
   165                  self.assertEquals(p.get_echo_sequence_number(), 2, self.message_description_list[i] + " - Sequence number mismatch")
   166                  self.assertEquals(p.get_echo_arbitrary_data().tolist(), [0xFE, 0x56, 0x88], self.message_description_list[i] + " - Arbitrary data mismatch")
   167              if i in range(2, 5):
   168                  self.assertEquals(p.get_parm_problem_pointer(), 2, self.message_description_list[i] + " - Pointer mismatch")
   169              if i in range(5, 15):
   170                  self.assertEquals(p.get_originating_packet_data().tolist(), [0xFE, 0x56, 0x88], self.message_description_list[i] + " - Originating packet data mismatch")
   171              if i in range(14, 15):
   172                  self.assertEquals(p.get_mtu(), 1300, self.message_description_list[i] + " - MTU mismatch")
   173  
   174  
   175  suite = unittest.TestLoader().loadTestsFromTestCase(TestICMP6)
   176  unittest.TextTestRunner(verbosity=1).run(suite)