Пример #1
0
    def test_dissection_with_wrong_pcap_file(self):

        with self.assertRaises(ReaderError):
            dis_wrong_file = Capture(self.NOT_A_PCAP_FILE)
            dis = dis_wrong_file.get_dissection()
        with self.assertRaises(ReaderError):
            dis_empty_file = Capture(self.EMPTY_PCAP_FILE)
            dis = dis_empty_file.get_dissection()
Пример #2
0
def dissect_capture(filename,
                    proto_filter=None,
                    output_filename=None,
                    number_of_frames_to_skip=None):
    """
    Dissects (decodes and converts to string representation) network traces (.pcap file).
    """
    assert filename

    if os.path.isfile(filename) is False and os.path.isfile(
            os.path.join(TMPDIR, filename)):
        filename = os.path.join(TMPDIR, filename)

    logger.info("PCAP file dissection starts. Filename: %s" % filename)

    proto_matched = None

    if proto_filter:
        # In function of the protocol asked
        proto_matched = get_protocol(proto_filter)
        if proto_matched is None:
            raise Exception('Unknown protocol %s' % proto_filter)

    if number_of_frames_to_skip:
        filename_pcap_filtered = os.path.join(
            TMPDIR, 'pcap_without_some_skipped_frames.pcap')
        remove_first_frames(pcap_filename=filename,
                            new_pcap_filename=filename_pcap_filtered,
                            number_of_frames_to_skip=number_of_frames_to_skip)
        filename = filename_pcap_filtered

    cap = Capture(filename)

    if proto_matched and len(proto_matched) == 1:
        print(proto_matched)
        proto = eval(proto_matched[0]['name'])
        dissection_as_dicts = cap.get_dissection(proto)
        dissection_as_text = cap.get_dissection_simple_format(proto)
        frames_summary = cap.frames
    else:
        dissection_as_dicts = cap.get_dissection()
        dissection_as_text = cap.get_dissection_simple_format()
        frames_summary = cap.frames

    if frames_summary:
        logger.info(
            'PCAP file dissected (filename: %s). Frames summary:\n%s' %
            (filename, json.dumps(
                ([repr(c) for c in frames_summary]), indent=4)))
    else:
        logger.info('PCAP file dissected (filename: %s). No frames found.')

    if output_filename and type(output_filename) is str:
        # save dissection response
        _dump_json_to_file(json.dumps(dissection_as_dicts), output_filename)

    return dissection_as_dicts, dissection_as_text
Пример #3
0
    def test_dissected_pcaps_returned_values_are_not_empty(self):
        """
        this test that the decoders dont raise any errors
        :return:
        """

        logging.info(
            "[dissector unittests] loaded %s .pcap files for dissection tests"
            % len(self.pcap_for_test))

        for p_file in self.pcap_for_test:
            logging.info('[dissector unittests] dissecting %s' % p_file)
            c = Capture(p_file)
            d = c.get_dissection(self.PROTO_CLASS_FILTER)
            if len(d) == 0:
                self.fail('got empty dissection for %s layer for .pcap %s' %
                          (self.PROTO_CLASS_FILTER, p_file))
            logging.debug('frame dissection: %s' % json.dumps(d, indent=4))
Пример #4
0
    def test_that_it_can_dissect_all_pcaps(self):
        """
        this basicallyt test that the decoders dont raise any errors
        :return:
        """

        logging.info(
            "[dissector unittests] loaded %s .pcap files for dissection tests"
            % len(self.pcap_for_test))

        for p_file in self.pcap_for_test:
            logging.info('[dissector unittests] dissecting %s' % p_file)
            c = Capture(p_file)
            d = c.get_dissection()
            try:
                logging.debug('frame dissection: %s' % json.dumps(d, indent=4))
            except:
                logging.debug('frame dissection: %s' % d)
Пример #5
0
class CaptureAndDissectionsTestCase(unittest.TestCase):
    """
    Test dissections. Unit testing methods
    """

    # #################### Tests parameters #########################

    # File path
    TEST_FILE_DIR = 'tests/test_dumps'

    # dissect CoAP pcap with other UDP messages:
    PCAP_FILE = path.join(TEST_FILE_DIR, 'coap', 'CoAP_plus_random_UDP_messages.pcap')

    # pcaps that MUST throw exceptions
    WRONG_TEST_FILE_DIR_NAME = 'others'
    EMPTY_PCAP_FILE = path.join(TEST_FILE_DIR, WRONG_TEST_FILE_DIR_NAME, 'empty_pcap.pcap')
    NOT_A_PCAP_FILE = path.join(TEST_FILE_DIR, WRONG_TEST_FILE_DIR_NAME, 'not_a_pcap_file.dia')

    # Create a struct checker object
    struct_validator = StructureValidator()

    # #################### Init and deinit functions #########################
    def setUp(self):
        """
            Initialize the dissector instance
        """
        self.capture = Capture(self.PCAP_FILE)

    # #################### Utilities functions #########################

    def check_summary(self, summary):
        self.assertTrue(type(summary), tuple)
        self.assertEqual(len(summary), 2)
        self.assertTrue(type(summary[0]), int)
        self.assertGreater(summary[0], 0)
        self.assertTrue(type(summary[1]), str)
        self.assertGreater(len(summary[1]), 0)

    # #################### Tests functions #########################

    # ##### get_dissectable_protocols
    def test_get_dissectable_protocols(self):

        # Get implemented protocols and check their values
        implemented_protocols = get_dissectable_protocols()
        self.assertEqual(type(implemented_protocols), list)
        self.assertGreater(len(implemented_protocols), 0)
        for prot in implemented_protocols:
            self.assertTrue(issubclass(prot, PacketValue))

    # ##### summary
    def test_summary_without_filtering(self):

        # Get and check the summary
        summary = self.capture.summary()
        self.assertTrue(type(summary), list)
        self.assertTrue(len(summary), 5)

        i = 1
        for f_sum in summary:
            self.check_summary(f_sum)
            self.assertEqual(f_sum[0], i)
            i += 1

        # Try to get another summary with None provided
        summary_with_none = self.capture.summary(None)
        self.assertEqual(summary, summary_with_none)

    def test_summary_with_filtering_on_coap(self):

        # Get and check the summary
        summary = self.capture.summary(CoAP)
        self.assertTrue(type(summary), list)
        self.assertTrue(len(summary), 2)

        i = 4  # CoAP frames are n°4 and 5
        for f_sum in summary:
            self.check_summary(f_sum)
            self.assertEqual(f_sum[0], i)
            i += 1

    def test_summary_with_filtering_on_protocols(self):

        # For every implemented protocols
        for prots in get_dissectable_protocols():

            # Get and check the summary
            summary = self.capture.summary(prots)
            self.assertTrue(type(summary), list)
            for f_sum in summary:
                self.check_summary(f_sum)

    def test_summary_with_filtering_on_none_type(self):

        # Get and check the summary
        with self.assertRaises(InputParameterError):
            summary = self.capture.summary(type(None))

    def test_summary_with_filtering_on_not_a_protocol(self):

        # Get and check the summary
        with self.assertRaises(InputParameterError):
            summary = self.capture.summary(Frame)

    def test_summary_with_wrong_pcap_file(self):

        # Create two wrong dissect instances, assert an exeption is raised
        with self.assertRaises(ReaderError):
            dis_wrong_file = Capture(self.NOT_A_PCAP_FILE)
            dis = dis_wrong_file.summary()
        with self.assertRaises(ReaderError):
            dis_empty_file = Capture(self.EMPTY_PCAP_FILE)
            dis = dis_empty_file.summary()

    # ##### dissect
    def test_dissection_without_filtering(self):

        # Get and check the dissect
        dissect = self.capture.get_dissection()
        self.assertTrue(type(dissect), list)
        self.assertTrue(len(dissect), 5)

        i = 1
        for frame in dissect:
            self.struct_validator.check_frame(frame)
            self.assertEqual(frame['id'], i)
            i += 1

        # Try to get another dissect with None provided
        dissect_with_none = self.capture.get_dissection(None)
        self.assertEqual(dissect, dissect_with_none)

    def test_dissection_with_filtering_on_coap(self):

        # Get and check the dissect
        dissect = self.capture.get_dissection(CoAP)
        self.assertTrue(type(dissect), list)
        self.assertTrue(len(dissect), 2)

        i = 4  # CoAP frames are n°4 and 5
        for frame in dissect:
            self.struct_validator.check_frame(frame)
            self.assertEqual(frame['id'], i)
            i += 1

    def test_dissection_with_filtering_on_protocols(self):

        # For every implemented protocols
        for prots in get_dissectable_protocols():

            # Get and check the dissect
            dissect = self.capture.get_dissection(prots)
            self.assertTrue(type(dissect), list)
            for frame in dissect:
                self.struct_validator.check_frame(frame)

    def test_dissection_with_filtering_on_none_type(self):

        # Get and check the dissect
        with self.assertRaises(InputParameterError):
            dissect = self.capture.get_dissection(type(None))

    def test_dissection_with_filtering_on_not_a_protocol(self):

        # Get and check the dissect
        with self.assertRaises(InputParameterError):
            dissect = self.capture.get_dissection(Frame)

    def test_dissection_with_wrong_pcap_file(self):

        with self.assertRaises(ReaderError):
            dis_wrong_file = Capture(self.NOT_A_PCAP_FILE)
            dis = dis_wrong_file.get_dissection()
        with self.assertRaises(ReaderError):
            dis_empty_file = Capture(self.EMPTY_PCAP_FILE)
            dis = dis_empty_file.get_dissection()