def test_plot_high_entropy_file(self):
        temp_dir = tempfile.gettempdir()
        # temp_dir = self.test_file_dir
        bin_src = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-test_plot_high_entropy_file.bin')
        bp_src = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-test_plot_high_entropy_file.pic.png')
        bar_src = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-test_plot_high_entropy_file.bar.png')

        helper.create_random_file(bin_src, 0x800)

        cad = CodescannerAnalysisData(bin_src)
        cad.regions['HighEntropy'] = [(0, 3584)]

        bar_bytes = cad.plot_to_buffer(100, cad.COLOR_MAP)
        bp_bytes = cad.plot_to_buffer(100, cad.BYTE_PLOT)
        cad.plot_to_file(bar_src, 100, cad.COLOR_MAP)
        cad.plot_to_file(bp_src, 100, cad.BYTE_PLOT)

        assert bar_bytes.startswith(helper.MAGIC_PNG_BYTES)
        assert bp_bytes.startswith(helper.MAGIC_PNG_BYTES)
        assert os.path.isfile(bar_src)
        assert os.path.isfile(bp_src)

        os.remove(bin_src)
        os.remove(bar_src)
        os.remove(bp_src)
    def test_plot_to_file_color_map_plot(self):
        dpi = 100
        temp_dir = tempfile.gettempdir()
        # temp_dir = self.test_file_dir
        img_path = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-test_plot_to_file_color_map_plot.png')
        expected_size = (dpi * ColorMap.FIG_SIZE[0],
                         dpi * ColorMap.FIG_SIZE[1])

        cad = CodescannerAnalysisData(self.test_file)
        cad.plot_to_file(str(img_path), dpi, cad.COLOR_MAP)

        assert os.path.isfile(img_path)
        helper.assert_image_size(expected_size, str(img_path))

        os.remove(img_path)
    def test_plot_to_file_byte_plot(self):
        dpi = 100
        temp_dir = tempfile.gettempdir()
        # temp_dir = self.test_file_dir
        img_path = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-test_plot_to_file_byte_plot.png')
        expected_size = (dpi * BytePlot.FIG_SIZE[0],
                         dpi * BytePlot.FIG_SIZE[1])
        p_file = self.test_file

        cad = CodescannerAnalysisData(p_file)
        cad.plot_to_file(str(img_path), dpi, cad.BYTE_PLOT)

        assert os.path.isfile(img_path)
        helper.assert_image_size(expected_size, str(img_path))

        os.remove(img_path)
    def test_plot_to_file_byte_plot_big_file(self):
        dpi = 100
        temp_dir = tempfile.gettempdir()
        # temp_dir = self.test_file_dir
        img_path = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-test_plot_to_file_byte_plot_big_file.png'
        )
        expected_size = (dpi * BytePlot.FIG_SIZE[0],
                         dpi * BytePlot.FIG_SIZE[1])
        big_file = os.path.join(temp_dir, "big.bin")
        helper.create_random_file(big_file, (10 * 1024 * 1024))  # 10 mb

        cad = CodescannerAnalysisData(big_file)
        cad.plot_to_file(str(img_path), dpi, cad.BYTE_PLOT)

        assert os.path.isfile(img_path)
        helper.assert_image_size(expected_size, str(img_path))

        os.remove(big_file)
        os.remove(img_path)
    def test_not_supported_file_plot(self):
        dpi = 100
        temp_dir = tempfile.gettempdir()
        unsupported = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-est_not_supported_file_plot_in.png')

        cad = CodescannerAnalysisData(self.test_file)
        cad.plot_to_file(str(unsupported), dpi, True)

        # img_path2 = self._get_image_src(self._test_file_dir, 'test_not_supported_file_plot.png')
        img_path2 = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-test_not_supported_file_plot_out.png')
        cad = CodescannerAnalysisData(unsupported)
        cad.plot_to_file(img_path2, dpi, True)
        pic_bytes = cad.plot_to_buffer(dpi, True)

        assert pic_bytes.startswith(helper.MAGIC_PNG_BYTES)

        os.remove(unsupported)
        os.remove(img_path2)
    def test_plot_with_offsets(self):
        dpi = 100
        temp_dir = tempfile.gettempdir()
        # temp_dir = self._test_file_dir
        img_path = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-test_plot_with_offsets-dot.png')
        bar_path = os.path.join(
            temp_dir,
            'CodescannerAnalysisDataTest-test_plot_with_offsets-bar.png')

        start = 144640
        end = 184320

        cad = CodescannerAnalysisData(self.test_medium_binary_src, start, end)
        cad.plot_to_file(str(img_path), dpi, cad.BYTE_PLOT)
        cad.plot_to_file(str(bar_path), dpi, cad.COLOR_MAP)

        assert os.path.isfile(img_path)
        assert os.path.isfile(bar_path)

        os.remove(img_path)
        os.remove(bar_path)
Example #7
0
            endsAt = int(sys.argv[3], base=16)
            cad = CAD(finamepath, startAt, endsAt)
    else:
        cad = CAD(finamepath)

    if cad is None:
        print("error: cad object is None!")
        sys.exit()

    print(cad.decision)
    print(cad.file_header)
    print(cad.architecture)

    short_finame = os.path.split(finamepath)[1]

    finame_template = ""
    fi_splitext = os.path.splitext(short_finame)
    if (fi_splitext[1]):
        finame_template = "%s_%s" % (fi_splitext[0], fi_splitext[1][1:]
                                     )  # dot-extension
    else:
        finame_template = fi_splitext[0]  # Elf-Binaries without dot-extension

    pngname1 = "%s_byteplot.png" % (finame_template)
    pngname2 = "%s_colormap.png" % (finame_template)

    cad.plot_to_file(os.path.join(RESULTFOLDER, pngname1), 150, 1)
    cad.plot_to_file(os.path.join(RESULTFOLDER, pngname2), 150, 2)

    sys.exit()