Beispiel #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()
Beispiel #2
0
    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()
Beispiel #3
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
Beispiel #4
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))
Beispiel #5
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)
Beispiel #6
0
 def setUp(self):
     """
         Initialize the frame list from the valid pcap file
     """
     self.frames = Capture(self.PCAP_FILE).frames
Beispiel #7
0
    def analyse(
            self,
            filename: str,
            tc_id: str
    ) -> (str, str, list_of(int), str, list_of((str, str)), list_of((type, Exception, is_traceback))):
        """
        Analyse a dump file associated to a test case

        :param filename: The name of the file to analyse
        :param tc_id: The unique id of the test case to confront the given file
        :type filename: str
        :type tc_id: str

        :return: A tuple with the information about the analysis results:
                 - The id of the test case
                 - The verdict as a string
                 - The list of the result important frames
                 - A string with logs
                 - A list of all the partial verdicts
                 - A list of tuples representing the exceptions that occurred
        :rtype: (str, str, [int], str,[(str, str)], [(type, Exception, traceback)])

        :raises FileNotFoundError: If the test env of the tc is not found
        :raises ReaderError: If the capture didn't manage to read and decode
        :raises ObsoleteTestCase: If the test case if obsolete

        .. example::
            ('TD_COAP_CORE_03', 'fail', [21, 22], [('fail', "some comment"),('fail', "some other comment")] , 'verdict description', '')

        .. note::
            Allows multiple occurrences of executions of the testcase, returns as verdict:
                - fail: if at least one on the occurrences failed
                - inconclusive: if all occurrences returned a inconclusive verdict
                - pass: all occurrences are inconclusive or at least one is PASS and
                        the rest is inconclusive
        """

        # Get the test case class
        test_case_class = self.import_test_cases([tc_id])
        assert len(test_case_class) == 1
        test_case_class = test_case_class[0]

        # Disable name resolution for performance improvements
        with Data.disable_name_resolution():
            # Get the capture from the file
            capture = Capture(filename)
            # Initialize the TC with the list of conversations
            test_case = test_case_class(capture)
            verdict, rev_frames, log, partial_verdicts, exceps = test_case.run_test_case()

            # print('##### capture')
            # print(capture)
            # print('#####')
            #
            # # Here we execute the test case and return the result
            #
            # print('##### Verdict given')
            # print(verdict)
            # print('#####')
            # print('##### Review frames')
            # print(rev_frames)
            # print('#####')
            # print('##### Text')
            # print(log, partial_verdicts)
            # print('#####')
            # print('##### Exceptions')
            # print(exceptions)
            # print('#####')

            return tc_id, verdict, rev_frames, log, partial_verdicts, exceps
Beispiel #8
0
 def __get_capture(self, pcap_file, dir_from_test_dumps="coap_observe"):
     pcap_dir_path = os.path.dirname(os.path.abspath(__file__))
     pcap_dir_path = os.path.join(
         pcap_dir_path, os.path.join('../test_dumps/', dir_from_test_dumps))
     return Capture(os.path.join(pcap_dir_path, pcap_file))
Beispiel #9
0
 def setUp(self):
     """
         Initialize the Capture instance
     """
     self.capture = Capture(self.PCAP_FILE)
Beispiel #10
0
    from ttproto.core.dissector import Capture, Dissector

    # filename = '/Users/fsismondi/TD_COAP_CORE_01.pcap'
    # # dissect with no modifs
    # dissector = Dissector(filename)
    #
    # # dissect with filter
    # finterop_tun_profile_filter(filename)
    # #dissector = Dissector('tmp/temp.pcap')
    # print(dissector.summary())
    # print('#####')
    # print(dissector.dissect())

    filename1 = 'tmp/DLT_RAW_1.pcap'
    # dissect with no modifs
    c1 = Capture(filename1)

    # dissect with filter
    remove_first_frames(
        number_of_frames_to_skip=2,
        pcap_filename=filename1,
        new_pcap_filename='tmp/temp.pcap',
    )
    c2 = Capture('tmp/temp.pcap')

    print('#####')
    pprint(c1.summary())
    print('#####')
    pprint(c2.summary())