Ejemplo n.º 1
0
    def test_decoding_extension_header_from_string(self):
        hop_by_hop_binary_packet = '\x2b\x01\x01\x0C\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

        d = ImpactDecoder.HopByHopDecoder()
        parsed_packet = d.decode(hop_by_hop_binary_packet)

        next_header = parsed_packet.get_next_header()
        header_extension_length = parsed_packet.get_header_extension_length()
        options = parsed_packet.get_options()

        self.assertEquals(
            1, len(options),
            "Simple Hop By Hop Parsing - Wrong Quantity of Options")

        padn_option = options[0]
        padn_option_type = padn_option.get_option_type()
        padn_option_length = padn_option.get_option_length()

        self.assertEquals(parsed_packet.get_header_type(), 0,
                          "Simple Hop By Hop Parsing - Incorrect packet")
        self.assertEquals(
            next_header, 43,
            "Simple Hop By Hop Parsing - Incorrect next header value")
        self.assertEquals(header_extension_length, 1,
                          "Simple Hop By Hop Parsing - Incorrect size")
        self.assertEquals(padn_option_type, 1,
                          "Simple Hop By Hop Parsing - Incorrect option type")
        self.assertEquals(padn_option_length, 12,
                          "Simple Hop By Hop Parsing - Incorrect option size")
Ejemplo n.º 2
0
    def test_decoding(self):
        '''Test IP6 Packet decoding.'''

        d = ImpactDecoder.IP6Decoder()
        parsed_packet = d.decode(self.binary_packet)

        protocol_version = parsed_packet.get_protocol_version()
        traffic_class = parsed_packet.get_traffic_class()
        flow_label = parsed_packet.get_flow_label()
        payload_length = parsed_packet.get_payload_length()
        next_header = parsed_packet.get_next_header()
        hop_limit = parsed_packet.get_hop_limit()
        source_address = parsed_packet.get_source_address()
        destination_address = parsed_packet.get_destination_address()

        self.assertEquals(protocol_version, 6,
                          "IP6 parsing - Incorrect protocol version")
        self.assertEquals(traffic_class, 72,
                          "IP6 parsing - Incorrect traffic class")
        self.assertEquals(flow_label, 148997,
                          "IP6 parsing - Incorrect flow label")
        self.assertEquals(payload_length, 1500,
                          "IP6 parsing - Incorrect payload length")
        self.assertEquals(next_header, 17,
                          "IP6 parsing - Incorrect next header")
        self.assertEquals(hop_limit, 1, "IP6 parsing - Incorrect hop limit")
        self.assertEquals(source_address.as_string(),
                          "FE80::78F8:89D1:30FF:256B",
                          "IP6 parsing - Incorrect source address")
        self.assertEquals(destination_address.as_string(), "FF02::1:3",
                          "IP6 parsing - Incorrect destination address")
Ejemplo n.º 3
0
    def test_decoding_simple_routing_options(self):
        routing_options_binary_packet = [
            0x3a, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00
        ]

        d = ImpactDecoder.RoutingOptionsDecoder()
        parsed_packet = d.decode(routing_options_binary_packet)

        next_header = parsed_packet.get_next_header()
        header_extension_length = parsed_packet.get_header_extension_length()
        routing_type = parsed_packet.get_routing_type()
        segments_left = parsed_packet.get_segments_left()
        options = parsed_packet.get_options()

        self.assertEquals(parsed_packet.get_header_type(), 43,
                          "Simple Routing Options Parsing - Incorrect packet")
        self.assertEquals(
            next_header, 58,
            "Simple Routing Options Parsing - Incorrect next header value")
        self.assertEquals(header_extension_length, 0,
                          "Simple Routing Options Parsing - Incorrect size")
        self.assertEquals(
            routing_type, 0,
            "Simple Routing Options Parsing - Incorrect routing type")
        self.assertEquals(
            segments_left, 10,
            "Simple Routing Options Parsing - Incorrect quantity of segments left size"
        )
        self.assertEquals(
            0, len(options),
            "Simple Routing Options Parsing - Wrong Quantity of Options")
Ejemplo n.º 4
0
    def test_decoding_multi_option_destination_options(self):
        destination_options_binary_packet = [
            0x3a, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00
        ]

        d = ImpactDecoder.DestinationOptionsDecoder()
        parsed_packet = d.decode(destination_options_binary_packet)

        next_header = parsed_packet.get_next_header()
        header_extension_length = parsed_packet.get_header_extension_length()
        options = parsed_packet.get_options()

        self.assertEquals(
            2, len(options),
            "Destination Options with multiple options parsing - Wrong Quantity of Options"
        )

        pad1_option = options[0]
        pad1_option_type = pad1_option.get_option_type()

        padn_option = options[1]
        padn_option_type = padn_option.get_option_type()
        padn_option_length = padn_option.get_option_length()

        self.assertEquals(
            parsed_packet.get_header_type(), 60,
            "Destination Options with multiple options parsing - Incorrect packet"
        )
        self.assertEquals(
            next_header, 58,
            "Destination Options with multiple options parsing - Incorrect next header value"
        )
        self.assertEquals(
            header_extension_length, 0,
            "Destination Options with multiple options parsing - Incorrect size"
        )
        self.assertEquals(
            pad1_option_type, 0,
            "Destination Options with multiple options parsing - Incorrect option type"
        )
        self.assertEquals(
            padn_option_type, 1,
            "Destination Options with multiple options parsing - Incorrect option type"
        )
        self.assertEquals(
            padn_option_length, 3,
            "Destination Options with multiple options parsing - Incorrect option size"
        )
Ejemplo n.º 5
0
def main():
    import sys

    f_in = open(sys.argv[1],'rb')
    try:
       f_out = open(sys.argv[2],'wb')
       f_out.write(str(PCapFileHeader()))
    except:
       f_out = None

    hdr = PCapFileHeader()
    hdr.fromString(f_in.read(len(hdr)))

    #hdr.dump()

    decoder = ImpactDecoder.EthDecoder()
    while 1:
       pkt = PCapFilePacket()
       try:
          pkt.fromString(f_in.read(len(pkt)))
       except:
          break
       pkt['data'] = f_in.read(pkt['savedLength'])
       hdr['packets'].append(pkt)
       p = pkt['data']
       try:    in_onion = [decoder.decode(p[1])]
       except: in_onion = [decoder.decode(p[0])]
       try:
          while 1: in_onion.append(in_onion[-1].child())
       except:
          pass

       process(in_onion)
       pkt.dump()
       #print "%r" % str(pkt)

       if f_out:
          #print eth

          pkt_out = PCapFilePacket()
          pkt_out['data'] = str(eth.get_packet())

          #pkt_out.dump()

          f_out.write(str(pkt_out))
Ejemplo n.º 6
0
    def test_decoding_simple_destination_options(self):
        destination_options_binary_packet = [
            0x2b, 0x01, 0x01, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00
        ]

        d = ImpactDecoder.DestinationOptionsDecoder()
        parsed_packet = d.decode(destination_options_binary_packet)

        next_header = parsed_packet.get_next_header()
        header_extension_length = parsed_packet.get_header_extension_length()
        options = parsed_packet.get_options()

        self.assertEquals(
            1, len(options),
            "Simple Destination Options Parsing - Wrong Quantity of Options")

        padn_option = options[0]
        padn_option_type = padn_option.get_option_type()
        padn_option_length = padn_option.get_option_length()

        self.assertEquals(
            parsed_packet.get_header_type(), 60,
            "Simple Destination Options Parsing - Incorrect packet")
        self.assertEquals(
            next_header, 43,
            "Simple Destination Options Parsing - Incorrect next header value")
        self.assertEquals(
            header_extension_length, 1,
            "Simple Destination Options Parsing - Incorrect size")
        self.assertEquals(
            padn_option_type, 1,
            "Simple Destination Options Parsing - Incorrect option type")
        self.assertEquals(
            padn_option_length, 12,
            "Simple Destination Options Parsing - Incorrect option size")
Ejemplo n.º 7
0
    def test_decoding_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

        d = ImpactDecoder.IP6Decoder()
        parsed_ipv6_packet = d.decode(binary_packet)

        # IPv6 Parsing
        ipv6_protocol_version = parsed_ipv6_packet.get_protocol_version()
        ipv6_traffic_class = parsed_ipv6_packet.get_traffic_class()
        ipv6_flow_label = parsed_ipv6_packet.get_flow_label()
        ipv6_payload_length = parsed_ipv6_packet.get_payload_length()
        ipv6_next_header = parsed_ipv6_packet.get_next_header()
        ipv6_hop_limit = parsed_ipv6_packet.get_hop_limit()
        ipv6_source_address = parsed_ipv6_packet.get_source_address()
        ipv6_destination_address = parsed_ipv6_packet.get_destination_address()

        # Hop By Hop Parsing
        hop_by_hop_parsed_packet = parsed_ipv6_packet.child()
        hop_by_hop_next_header = hop_by_hop_parsed_packet.get_next_header()
        hop_by_hop_header_extension_length = hop_by_hop_parsed_packet.get_header_extension_length(
        )
        hop_by_hop_options = hop_by_hop_parsed_packet.get_options()
        self.assertEquals(1, len(hop_by_hop_options),
                          "Hop By Hop Parsing - Wrong Quantity of Options")
        hop_by_hop_padn_option = hop_by_hop_options[0]
        hop_by_hop_padn_option_type = hop_by_hop_padn_option.get_option_type()
        hop_by_hop_padn_option_length = hop_by_hop_padn_option.get_option_length(
        )

        # Routing Options Tests
        routing_options_parsed_packet = hop_by_hop_parsed_packet.child()
        routing_options_next_header = routing_options_parsed_packet.get_next_header(
        )
        routing_options_header_extension_length = routing_options_parsed_packet.get_header_extension_length(
        )
        routing_options_routing_type = routing_options_parsed_packet.get_routing_type(
        )
        routing_options_segments_left = routing_options_parsed_packet.get_segments_left(
        )
        routing_options_options = routing_options_parsed_packet.get_options()

        # Destination Options Parsing
        destination_options_parsed_packet = routing_options_parsed_packet.child(
        )
        destination_options_next_header = destination_options_parsed_packet.get_next_header(
        )
        destination_options_header_extension_length = destination_options_parsed_packet.get_header_extension_length(
        )
        destination_options_options = destination_options_parsed_packet.get_options(
        )
        self.assertEquals(
            2, len(destination_options_options),
            "Destination Options Parsing - Wrong Quantity of Options")
        destination_options_pad1_option = destination_options_options[0]
        destination_options_pad1_option_type = destination_options_pad1_option.get_option_type(
        )
        destination_options_padn_option = destination_options_options[1]
        destination_options_padn_option_type = destination_options_padn_option.get_option_type(
        )
        destination_options_padn_option_length = destination_options_padn_option.get_option_length(
        )

        self.assertEquals(ipv6_protocol_version, 6,
                          "IP6 parsing - Incorrect protocol version")
        self.assertEquals(ipv6_traffic_class, 72,
                          "IP6 parsing - Incorrect traffic class")
        self.assertEquals(ipv6_flow_label, 148997,
                          "IP6 parsing - Incorrect flow label")
        self.assertEquals(ipv6_payload_length, 1500,
                          "IP6 parsing - Incorrect payload length")
        self.assertEquals(ipv6_next_header, 0,
                          "IP6 parsing - Incorrect next header")
        self.assertEquals(ipv6_hop_limit, 1,
                          "IP6 parsing - Incorrect hop limit")
        self.assertEquals(ipv6_source_address.as_string(),
                          "FE80::78F8:89D1:30FF:256B",
                          "IP6 parsing - Incorrect source address")
        self.assertEquals(ipv6_destination_address.as_string(), "FF02::1:3",
                          "IP6 parsing - Incorrect destination address")
        self.assertEquals(hop_by_hop_parsed_packet.get_header_type(), 0,
                          "Hop By Hop Parsing - Incorrect packet")
        self.assertEquals(hop_by_hop_next_header, 43,
                          "Hop By Hop Parsing - Incorrect next header value")
        self.assertEquals(hop_by_hop_header_extension_length, 1,
                          "Hop By Hop Parsing - Incorrect size")
        self.assertEquals(hop_by_hop_padn_option_type, 1,
                          "Hop By Hop Parsing - Incorrect option type")
        self.assertEquals(hop_by_hop_padn_option_length, 12,
                          "Hop By Hop Parsing - Incorrect option size")
        self.assertEquals(routing_options_parsed_packet.get_header_type(), 43,
                          "Routing Options Parsing - Incorrect packet")
        self.assertEquals(
            routing_options_next_header, 60,
            "Routing Options Parsing - Incorrect next header value")
        self.assertEquals(routing_options_header_extension_length, 0,
                          "Routing Options Parsing - Incorrect size")
        self.assertEquals(routing_options_routing_type, 0,
                          "Routing Options Parsing - Incorrect routing type")
        self.assertEquals(
            routing_options_segments_left, 10,
            "Routing Options Parsing - Incorrect quantity of segments left size"
        )
        self.assertEquals(
            0, len(routing_options_options),
            "Routing Options Parsing - Wrong Quantity of Options")
        self.assertEquals(destination_options_parsed_packet.get_header_type(),
                          60, "Destination Options Parsing - Incorrect packet")
        self.assertEquals(
            destination_options_next_header, 58,
            "Destination Options Parsing - Incorrect next header value")
        self.assertEquals(destination_options_header_extension_length, 0,
                          "Destination Options Parsing - Incorrect size")
        self.assertEquals(
            destination_options_pad1_option_type, 0,
            "Destination Options Parsing - Incorrect option type")
        self.assertEquals(
            destination_options_padn_option_type, 1,
            "Destination Options Parsing - Incorrect option type")
        self.assertEquals(
            destination_options_padn_option_length, 3,
            "Destination Options Parsing - Incorrect option size")
Ejemplo n.º 8
0
    def test_message_decoding(self):
        d = ImpactDecoder.ICMP6Decoder()

        msg_types = [
            ICMP6.ICMP6.ECHO_REQUEST, ICMP6.ICMP6.ECHO_REPLY,
            ICMP6.ICMP6.PARAMETER_PROBLEM, ICMP6.ICMP6.PARAMETER_PROBLEM,
            ICMP6.ICMP6.PARAMETER_PROBLEM, ICMP6.ICMP6.DESTINATION_UNREACHABLE,
            ICMP6.ICMP6.DESTINATION_UNREACHABLE,
            ICMP6.ICMP6.DESTINATION_UNREACHABLE,
            ICMP6.ICMP6.DESTINATION_UNREACHABLE,
            ICMP6.ICMP6.DESTINATION_UNREACHABLE,
            ICMP6.ICMP6.DESTINATION_UNREACHABLE,
            ICMP6.ICMP6.DESTINATION_UNREACHABLE, ICMP6.ICMP6.TIME_EXCEEDED,
            ICMP6.ICMP6.TIME_EXCEEDED, ICMP6.ICMP6.PACKET_TOO_BIG
        ]

        msg_codes = [
            0, 0, ICMP6.ICMP6.ERRONEOUS_HEADER_FIELD_ENCOUNTERED,
            ICMP6.ICMP6.UNRECOGNIZED_NEXT_HEADER_TYPE_ENCOUNTERED,
            ICMP6.ICMP6.UNRECOGNIZED_IPV6_OPTION_ENCOUNTERED,
            ICMP6.ICMP6.NO_ROUTE_TO_DESTINATION,
            ICMP6.ICMP6.ADMINISTRATIVELY_PROHIBITED,
            ICMP6.ICMP6.BEYOND_SCOPE_OF_SOURCE_ADDRESS,
            ICMP6.ICMP6.ADDRESS_UNREACHABLE, ICMP6.ICMP6.PORT_UNREACHABLE,
            ICMP6.ICMP6.SOURCE_ADDRESS_FAILED_INGRESS_EGRESS_POLICY,
            ICMP6.ICMP6.REJECT_ROUTE_TO_DESTINATION,
            ICMP6.ICMP6.HOP_LIMIT_EXCEEDED_IN_TRANSIT,
            ICMP6.ICMP6.FRAGMENT_REASSEMBLY_TIME_EXCEEDED, 0
        ]

        for i in range(0, len(self.reference_data_list)):
            p = d.decode(self.reference_data_list[i])
            self.assertEquals(
                p.get_type(), msg_types[i],
                self.message_description_list[i] + " - Msg type mismatch")
            self.assertEquals(
                p.get_code(), msg_codes[i],
                self.message_description_list[i] + " - Msg code mismatch")

            if i in range(0, 2):
                self.assertEquals(
                    p.get_echo_id(), 1,
                    self.message_description_list[i] + " - ID mismatch")
                self.assertEquals(
                    p.get_echo_sequence_number(), 2,
                    self.message_description_list[i] +
                    " - Sequence number mismatch")
                self.assertEquals(
                    p.get_echo_arbitrary_data().tolist(), [0xFE, 0x56, 0x88],
                    self.message_description_list[i] +
                    " - Arbitrary data mismatch")
            if i in range(2, 5):
                self.assertEquals(
                    p.get_parm_problem_pointer(), 2,
                    self.message_description_list[i] + " - Pointer mismatch")
            if i in range(5, 15):
                self.assertEquals(
                    p.get_originating_packet_data().tolist(),
                    [0xFE, 0x56, 0x88], self.message_description_list[i] +
                    " - Originating packet data mismatch")
            if i in range(14, 15):
                self.assertEquals(
                    p.get_mtu(), 1300,
                    self.message_description_list[i] + " - MTU mismatch")
Ejemplo n.º 9
0
 def initialize(self):
     self.pcap = pcapy.open_live(pcapy.lookupdev(), -1, 1, 1)
     self.pcap.setfilter("port 67", 1, 0xffffff00)
     self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     self.sock.connect(('192.168.1.1',67))
     self.decoder = ImpactDecoder.EthDecoder()