コード例 #1
0
    def save_proc(self,
                  name,
                  path,
                  file_type,
                  content,
                  index,
                  total,
                  extra_props=None):
        name = str(name)
        file_id = StrUtils.uuid_str()

        # 保存文件参数
        FwFileDO.save_file_item(self.pack_id,
                                file_id,
                                name,
                                file_type,
                                file_path=path,
                                extra_props=extra_props)
        # 保存文件内容
        FwFilesStorage.save(file_id, name, path, file_type, content)

        percent = (index + 1) * 100.0 / total
        MyTask.save_task_percentage(self.task_id, percent)

        return not MyTask.is_task_stopped(self.task_id)
コード例 #2
0
def test_http_get():


    url = "http://" + utils.g_java_service_ip + ":10901/firmware/setdata"

    v1= {}
    v1['1'] = "123"
    v1['2'] = "234"
    v1['3'] = "345"
    data = json.dumps(v1)

    # 定义请求数据,并且对数据进行赋值
    values = {}
    values['task_uuid'] = StrUtils.uuid_str()
    values['datas'] = data

    # # 对请求数据进行编码
    # data = urllib.parse.urlencode(values).encode('utf-8')
    # print(type(data))  # 打印<class 'bytes'>
    # print(data)  # 打印b'status=hq&token=C6AD7DAA24BAA29AE14465DDC0E48ED9'

    # 若为post请求以下方式会报错TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
    # Post的数据必须是bytes或者iterable of bytes,不能是str,如果是str需要进行encode()编码
    data = urllib.parse.urlencode(values)
    print(type(data))  # 打印<class 'str'>
    print(data)  # 打印status=hq&token=C6AD7DAA24BAA29AE14465DDC0E48ED9

    # 将数据与url进行拼接
    req = url + '?' + data
    # 打开请求,获取对象
    response = urllib.request.urlopen(req)

    print("get complate")
コード例 #3
0
def fetch(request):
    firmware_id = req_get_param(request, 'firmware_id')
    if StrUtils.is_blank(firmware_id):
        return sys_app_err('ERROR_INVALID_PARAMETER')
    doc = firmware_db.fetch(firmware_id)
    if doc is None:
        #SysLog.fail('提取漏洞', '没有提取到漏洞信息(ID={})'.format(firmware_id))
        return sys_app_err('ERROR_FWID_NOT_FOUND')
    #SysLog.success('提取漏洞', '成功提取漏洞信息(ID={})'.format(firmware_id))
    return app_ok_p(doc)
コード例 #4
0
def _save_pack_db(fw_download_url, path_file_name, download_info, task_id):
    # todo file_type
    file_type, contents = check_file_type(path_file_name)
    file_type = FileType.PACK
    file_name = os.path.basename(path_file_name)

    # 新建或保存文件记录
    # 新的 pack ID
    pack_id = StrUtils.uuid_str()
    # 新的 pack 文件 UUID
    file_id = StrUtils.uuid_str()
    # 读取包文件内容
    contents = MyFile.read(path_file_name)
    # 保存文件记录
    PackFileDO.save(pack_id, file_id, name=file_name, file_type=file_type)
    # 保存文件内容
    PackFilesStorage.save(file_id, file_name, FileType.PACK, contents)

    # 返回固件包ID,文件ID
    return pack_id, file_id
コード例 #5
0
    def cdg_graph(self):
        project = self.angr_proj.proj
        cfg = CFGAnalyze.emulated_normal(self.angr_proj.proj, self.func_addr)
        cdg = project.analyses.CDG(cfg=cfg, start=self.func_addr)

        # 将控制依赖图生成到一个随机文件名的 png 中
        graph_file = os.path.join(MyPath.temporary(), StrUtils.uuid_str())
        plot_cdg(cfg, cdg, graph_file, pd_edges=True, cg_edges=True)

        # 读取文件内容的b64编码结果并返回
        return MyFile.read_and_b64encode(graph_file + '.png')
コード例 #6
0
def poc_delete(request):
    firmware_id = req_get_param(request, 'firmware_id')
    if StrUtils.is_blank(firmware_id):
        return app_err(Error.INVALID_REQ_PARAM)

    # 删除POC
    if not firmware_pocs.delete(firmware_id):
        #SysLog.fail('删除POC', '删除POC失败(漏洞ID={})'.format(firmware_id))
        return app_err(Error.EDB_POC_NOT_FOUND)

    #SysLog.success('删除POC', '成功删除漏洞的POC(漏洞ID={})'.format(firmware_id))
    return app_ok()
コード例 #7
0
def poc_add(request):
    firmware_id = req_post_param(request, 'firmware_id')
    alias = req_post_param(request, 'alias')
    content = req_post_param(request, 'content')
    # 这三个参数都是POC的基本参数,不能为空
    if StrUtils.is_blank(firmware_id) or StrUtils.is_blank(alias) or StrUtils.is_blank(content):
        return app_err(Error.INVALID_REQ_PARAM)

    # 只有定制漏洞才能添加POC
    if not firmware_db.custom_firmware_id(firmware_id):
        #SysLog.fail('添加POC', '添加POC失败(漏洞ID={})'.format(firmware_id))
        return firmware_db.err_not_custom()

    # firmware_id不存在,表示没有可以添加POC的漏洞信息条目
    if not firmware_db.exist_firmware_id(firmware_id):
        #SysLog.fail('添加POC', '添加POC失败(漏洞ID={})'.format(firmware_id))
        return app_err(Error.firmware_id_NOT_FOUND)

    firmware_pocs.add(firmware_id, alias, content)
    #SysLog.success('添加POC', '成功添加漏洞的POC(漏洞ID={})'.format(firmware_id))
    return app_ok()
コード例 #8
0
    def _save_pack(pack_index, pack_name):
        # 获取 pack 文件的路径
        file_path = LoadDefaultPack._get_fw_file_path(pack_index)
        if file_path is None:
            return None, None

        # 文件类型
        file_type = LoadDefaultPack._get_file_type(pack_index)

        # 新建或保存文件记录
        # 新的 pack ID
        pack_id = StrUtils.uuid_str()
        # 新的 pack 文件 UUID
        file_id = StrUtils.uuid_str()
        # 读取包文件内容
        contents = MyFile.read(file_path)
        # 保存文件记录
        PackFileDO.save(pack_id, file_id, name=pack_name, file_type=file_type)
        # 保存文件内容
        PackFilesStorage.save(file_id, pack_name, file_type, contents)

        return pack_id, file_id
コード例 #9
0
    def add_single_exec(file_name, file_data):
        # 新的 pack ID
        pack_id = StrUtils.uuid_str()
        # 保存虚拟包记录
        PackFileDO.save(pack_id,
                        '',
                        name=file_name,
                        pack_type=PackType.VIRTUAL,
                        file_type=FileType.PACK)

        # 新的 pack 文件 UUID
        exec_file_id = StrUtils.uuid_str()
        # 保存文件记录
        FwFileDO.save_file_item(pack_id, exec_file_id, file_name,
                                FileType.EXEC_FILE)

        # 在存储桶中保存文件数据
        # 保存文件内容
        FwFilesStorage.save(exec_file_id, file_name, file_name,
                            FileType.EXEC_FILE, file_data)

        return pack_id, exec_file_id
コード例 #10
0
    def verify_exec_bin_file(file_path, content=None):
        if file_path is None:
            if content is None:
                return FileType.OTHER_FILE, None
            file_path = MyFile.write('verify_exec_' + StrUtils.uuid_str(), content, folder=MyPath.temporary())

        # 疑似可执行文件,结合 binwalk 和 angr project 检验是否为可执行文件
        file_type, extra_props = FileTypeJudge.scan_file_type(file_path)
        if file_type == FileType.EXEC_FILE:
            arch, endianness = ExecFile.parse_exec_arch(file_path, prefer=extra_props)
            return file_type, {'arch': arch, 'endianness': endianness}

        return file_type, None
コード例 #11
0
def _save_pack_com_db(path_file_name, download_info, edb_id, title, task_id):
    # todo file_type
    #file_type, contents = check_file_type(path_file_name)
    file_type = FileType.PACK
    file_name = os.path.basename(path_file_name)
    name, ver = file_name.split('-')
    version = ver.split('.tar.gz')[0]


    # 新建或保存文件记录
    # 新的 pack ID
    pack_com_id = StrUtils.uuid_str()
    # 新的 pack 文件 UUID
    file_com_id = StrUtils.uuid_str()
    # 读取包文件内容
    contents = MyFile.read(path_file_name)
    # 保存文件记录
    PackCOMFileDO.save(pack_com_id, file_com_id, edb_id, title, version, path_file_name, name=file_name, file_type=file_type)
    # 保存文件内容
    PackCOMFilesStorage.save(file_com_id, file_name, FileType.PACK, contents)

    # 返回固件包ID,文件ID
    return pack_com_id, file_com_id
コード例 #12
0
    def cg_graph(self, verbose=True):
        cfg = CFGAnalyze.emulated_cg(self.angr_proj.proj, self.func_addr)

        # 将函数调用关系图生成到一个随机文件名的 png 中
        graph_file = os.path.join(MyPath.temporary(), StrUtils.uuid_str())
        # graph_file = StrUtils.uuid_str()
        set_plot_style('kyle')
        plot_cg(self.angr_proj.proj.kb,
                graph_file,
                format="png",
                verbose=verbose)

        # 读取文件内容的b64编码结果并返回
        contents = MyFile.read_and_b64encode(graph_file + '.png')
        return contents
コード例 #13
0
    def file_path_insert_into_antd_tree(tree_obj,
                                        file_path,
                                        file_id,
                                        component=None):
        # 从文件路径获取 folders_list, file_name
        folders_list, file_name = MyFile.file_path_to_folder_list(file_path)

        # if len(folders_list) == 0:
        #     nodes_list = tree_obj
        #     nodes_list.append({'title': file_name, 'key': file_id, 'file_path': file_path, 'children': []})

        nodes_list = tree_obj
        if len(folders_list) > 0:
            # 添加或遍历各级 folder 节点
            for index, folder in enumerate(folders_list):
                found = False
                for node in nodes_list:
                    if node['title'] == folder:
                        # 找到 folder 节点后,结束查找,设置标志
                        found = True
                        break

                # 没有找到 folder 节点,添加新节点
                if not found:
                    # folder 节点不设置 file_path
                    node = {
                        'title': folder,
                        'key': StrUtils.uuid_str(),
                        'file_path': '',
                        'children': []
                    }
                    nodes_list.append(node)

                # 在找到或新建的节点下,准备下一个节点定位,或者添加文件节点
                nodes_list = node['children']

        # 添加文件节点
        node = {
            'title': file_name,
            'key': file_id,
            'file_path': file_path,
            'component': component,
            'children': []
        }
        nodes_list.append(node)
コード例 #14
0
    def cfg_graph(self, verbose=True):
        cfg = CFGAnalyze.emulated_normal(self.angr_proj.proj, self.func_addr)
        # 将控制流程图生成到一个随机文件名的 png 中
        graph_file = os.path.join(MyPath.temporary(), StrUtils.uuid_str())
        set_plot_style('kyle')
        plot_cfg(
            cfg,
            graph_file,
            asminst=True,
            vexinst=False,
            # func_addr={self.func_addr: True},
            debug_info=verbose,
            remove_imports=True,
            remove_path_terminator=True,
            color_depth=True)

        # 读取文件内容的b64编码结果并返回
        return MyFile.read_and_b64encode(graph_file + '.png')
コード例 #15
0
def taintadd(request):
    # 从请求中取参数:文件 ID
    func_name, hazard, solution = ReqParams.many(request, ['func_name', 'hazard', 'solution'], protocol='POST')

    # find func_name in taint_dict
    if TaintdictDO.search_func_name(func_name) is None:
        print("add func_name")
        # 新的 fun ID
        fun_id = StrUtils.uuid_str()
        TaintdictDO.save(fun_id, func_name, hazard, solution)

        # 保存操作日志
        LogRecords.save('', category='statistics', action='自定义污点函数-增加',
                        desc='污点函数(ID=%s)的信息,' % fun_id)
        return sys_app_ok_p('添加成功')
    else:
        print("already has func_name")
        return sys_app_ok_p(['函数已存在'])
コード例 #16
0
    def save(log_contents, category='debug', action='普通操作', desc='操作日志', user='******'):
        # 根据系统配置,不在配置参数中的日志类型,不做日志记录
        if not SystemConfig.is_log_on(category):
            return

        log_uuid = StrUtils.uuid_str()
        create_time = SysUtils.get_now_time()
        logs_coll.update_one({'uuid': log_uuid},
                             {'$set': {
                                 'uuid': log_uuid,
                                 'category': category,
                                 'action': action,
                                 'description': desc,
                                 'content': log_contents,
                                 'user_account': user,
                                 'create_time': create_time
                             }}, True)
        return
コード例 #17
0
    def __init__(self, exec=None, args=(), extra_info={}):
        # exec 为回调函数,即 Process 的 target
        if exec is None:
            return

        # 设置任务基本参数
        self._exec = exec
        self._task_id = StrUtils.uuid_str()
        self._extra_info = extra_info

        # 增加参数,并创建新进程对象
        args += (self._task_id, )
        self._process = Process(target=exec, args=args)
        # 设置为守护进程。主进程退出后,子进程会跟随其同时退出,不受保护,主进程退出时不考虑子进程的运行状态
        # self._process.daemon = True

        # 初始化任务状态,并启动进程
        self.init_exec_status()
        self._process.start()
コード例 #18
0
    def call_graph(self, func_addr, verbose=True):
        proj = self.angr_proj.proj
        # 初始化状态机
        start_state = proj.factory.blank_state(addr=func_addr)
        start_state.stack_push(0x0)

        # 仿真模式解析代码
        cfg = self.angr_proj.call_cfg(start_addr=[func_addr])

        # 将函数关系图生成到一个随机文件名的 png 中
        graph_file = StrUtils.uuid_str()
        plot_cg(proj.kb, graph_file, format="png", verbose=verbose)

        # 读取文件
        try:
            with open(graph_file + '.png', 'rb') as fp:
                return fp.read()
        except (OSError, IOError):
            return None
コード例 #19
0
def test_angr_plot_graph(request):
    file_id, file_name, func_addr = ReqParams.many(request, ['file_id', 'file_name', 'func_addr.hex'])
    if len(file_id) == 0:
        if len(file_name) == 0:
            return sys_app_err_p('INVALID_REQ_PARAM', 'file_id 或 file_name 必填其一')
        file_path = os.path.join(MyPath.samples(), file_name)
        project = angr.Project(file_path, load_options={'auto_load_libs': False})
        start_state = project.factory.blank_state(addr=func_addr)
        start_state.stack_push(0x0)
        with hook0(project):
            cfg = project.analyses.CFGEmulated(fail_fast=True, starts=[func_addr], initial_state=start_state,
                                         context_sensitivity_level=2, keep_state=True, call_depth=100, normalize=True)
        graph_file = os.path.join(MyPath.temporary(), StrUtils.uuid_str())
        plot_cfg(cfg, graph_file, asminst=True, vexinst=False, func_addr={func_addr: True},
                 debug_info=False, remove_imports=True, remove_path_terminator=True)
    else:
        func_parse = FunctionParse(file_id, func_addr)
        content = func_parse.cfg_graph()

    return sys_app_ok()
コード例 #20
0
def save_make_files(pack_com_id, buildpath, arch):
    print(buildpath)

    # 遍历生成目录
    # 遍历目录 读取文件内容保存到DB
    itotal_files = 0
    for root, dirs, files in os.walk(buildpath):

        # root 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # files 表示该文件夹下的文件list

        # 遍历文件
        for f in files:
            itotal_files += 1
            print(os.path.join(root, f))
            path_file_name = os.path.join(root, f)
            file_type = FileType.MAKE_FILE
            file_name = os.path.basename(path_file_name)
            mode = os.stat(path_file_name).st_mode

            # 新建或保存文件记录
            # 新的 pack ID
            # pack_com_id = StrUtils.uuid_str()
            # 新的 pack 文件 UUID
            file_com_id = StrUtils.uuid_str()
            # 读取包文件内容
            contents = MyFile.read(path_file_name)
            # 保存文件记录
            MakeCOMFileDO.save_file_item(pack_com_id, file_com_id, file_name,
                                         file_type, arch, mode, path_file_name,
                                         None)
            # 保存文件内容
            MakeCOMFilesStorage.save(file_com_id, file_name, path_file_name,
                                     file_type, contents)

        # print(root)
        # print(dir)
        print(files)

    print(itotal_files)
コード例 #21
0
    def _save_image(file_index, pack_id):
        # 获取 文件的路径和文件类型
        file_path = LoadDefaultPack._get_fw_file_path(file_index)
        if file_path is None:
            return None

        # 文件类型
        file_type = LoadDefaultPack._get_file_type(file_index)

        # 新的文件 UUID
        file_id = StrUtils.uuid_str()
        # 读取文件内容
        contents = MyFile.read(file_path)
        # 保存文件记录
        FwFileDO.save_file_item(pack_id, file_id, os.path.basename(file_path),
                                file_type)
        # 保存文件内容
        FwFilesStorage.save(file_id, os.path.basename(file_path),
                            os.path.basename(file_path), file_type, contents)

        return file_id
コード例 #22
0
def _save_file_db(path_file_name, pack_id):
    # 获取 文件的路径和文件类型
    # file_path = _get_fw_file_path(virtual_id)
    # if file_path is None:
    #     return None
    #
    print(path_file_name)
    file_path, file_name = os.path.split(path_file_name)
    # 新的文件 UUID
    file_id = StrUtils.uuid_str()
    # 获取文件类型 内容
    file_type, contents = check_file_type(path_file_name)
    # file_type = _get_file_type(file_name)
    if contents is None:  # linux 系统下 BINWALK会提取出空目录,读目录文件为None
        return None
    # 保存文件记录
    FwFileDO.save_file_item(pack_id, file_id, file_name, file_type, file_path)
    # 保存文件内容
    FwFilesStorage.save(file_id, file_name, file_path, file_type, contents)

    # 返回文件ID
    return file_id
コード例 #23
0
def _save_source_code_file_db(path_file_name, pack_com_id, task_id):
    # 遍历目录 读取文件内容保存到DB
    itotal_files=0
    for root, dirs, files in os.walk(path_file_name):

        # root 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # files 表示该文件夹下的文件list

        # 遍历文件
        for f in files:
            itotal_files+=1
            print(os.path.join(root, f))
            path_file_name = os.path.join(root, f)
            #contents = MyFile.read(path_file_name)
            mode = os.stat(path_file_name).st_mode

            # todo file_type
            # file_type, contents = check_file_type(path_file_name)
            file_type = FileType.OTHER_FILE
            file_name = os.path.basename(path_file_name)

            # 新建或保存文件记录
            # 新的 pack ID
            # pack_com_id = StrUtils.uuid_str()
            # 新的 pack 文件 UUID
            file_com_id = StrUtils.uuid_str()
            # 读取包文件内容
            contents = MyFile.read(path_file_name)
            # 保存文件记录
            SourceCodeFileDO.save_file_item(pack_com_id, file_com_id, file_name, file_type, mode, path_file_name, None)
            # 保存文件内容
            SourceCodeFilesStorage.save(file_com_id, file_name, path_file_name, file_type, contents)


        # 遍历所有的文件夹
        for d in dirs:
            print(os.path.join(root, d))
コード例 #24
0
def test_generate_uuid(request):
    return sys_app_ok_p(StrUtils.uuid_str())
コード例 #25
0
    def save(self, pack_id):

        title_name = '固件分析报告'

        result_pack = pack_files_col.find({'pack_id': pack_id})

        pack_list = list(result_pack)

        firmware_name = ''
        firmware_file_num = 0
        execute_file_num = 0
        fw_file_lists = ''

        firmware_md5 = ''
        firmware_size = ''

        if pack_list is not None and len(pack_list) > 0:
            pack_info = pack_list[0]
            firmware_name = pack_info.get('name')
            pack_id = pack_info.get('pack_id')
            pack_file_id = pack_info.get('file_id')

            result_files = fw_files_col.find({'pack_id': pack_id})
            fw_file_lists = list(result_files)
            if fw_file_lists is not None or len(fw_file_lists) > 0:
                firmware_file_num = len(fw_file_lists)

                for file_info in fw_file_lists:
                    fw_file_type = file_info.get('file_type')

                    if fw_file_type == 4:
                        execute_file_num += 1

            item = PackFilesStorage.fetch(pack_file_id)

            firmware_md5 = item.get('md5')
            length_b = item.get('length')
            length_kb = length_b / 1024
            length_mb = length_kb / 1024
            if length_kb < 1:
                firmware_size = str('%.2f' % length_b) + ' B'
            elif length_mb < 1:
                firmware_size = str('%.2f' % length_kb) + ' KB'
            else:
                firmware_size = str('%.2f' % length_mb) + ' MB'
        else:
            return sys_app_err('ERROR_INVALID_PARAMETER')

        # firmware_inst = 'MIPS'
        # firmware_decomp_size = '7.2M'

        content = list()

        report_time = SysUtils.get_now_time_str()

        self.draw_con(content, title_name,
                      self.text_type(20, 30, colors.black, 1))
        self.draw_con(content, '报告生成时间:' + report_time,
                      self.text_type(11, 20, colors.black, 2))
        content.append(Spacer(300, 20))  # 添加空白,长度300,宽20

        # 1 固件分析综述
        self.summary_info(content, firmware_name, firmware_md5, firmware_size,
                          pack_id, firmware_file_num, execute_file_num)

        ct = self.text_type(10, 15, colors.black, 1)
        # 设置自动换行
        ct.wordWrap = 'CJK'

        # 2 组件关联的漏洞
        self.relation_loophole(content, fw_file_lists, firmware_name, ct)

        # 3 可执行文件详情
        self.fw_file_table(content, fw_file_lists, ct)

        # 4 特征码
        self.file_inverted_table(content, fw_file_lists, ct)

        self.draw_con(content, '报告结束', self.text_type(11, 20, colors.black, 1))

        time_stamp = SysUtils.parse_time_stamp_str()
        inde = firmware_name.index('.')

        if inde > -1:
            firmware_name = firmware_name[0:inde]

        pdf_name = firmware_name + title_name + time_stamp + '.pdf'

        path = './firmware_analyze_serv_report/'

        if not os.path.exists(path):
            os.mkdir(path)

        pdf_path = path + pdf_name
        # 生成pdf文件
        doc = SimpleDocTemplate(pdf_path, pagesize=letter)
        doc.build(content)

        report_id = StrUtils.uuid_str()
        report_record_info = {
            'report_id': report_id,
            'pack_id': pack_id,
            'pack_name': firmware_name,
            'pdf_path': pdf_path,
            'pdf_name': pdf_name,
            'create_time': report_time
        }

        report_record_col.save(report_record_info)

        return sys_app_ok()