Esempio n. 1
0
    def init_exec_status(self):
        # 初始化缓存的任务信息
        task_info = {
            'task_id': self._task_id,
            'start_time': SysUtils.get_now_time_str(),
            'task_status': TaskStatus.RUNNING,
            'percentage': 0.0,
            'progress_history': [],
            'result': {}
        }
        task_info = dict(task_info, **self._extra_info)
        # 缓存该任务信息
        MyRedis.set(self._task_id, task_info, category=task_cat)

        # 在数据库中保存任务记录
        TasksDAO.save(task_info)

        # 保存任务详情日志
        LogRecords.save(task_info,
                        category='task',
                        action='任务启动',
                        desc='记录任务启动时状态及任务信息')

        # 返回该任务信息
        return task_info
Esempio n. 2
0
    def save_exec_info(task_id, percent, result=None, notify=True):
        # 从缓存或数据库中读取该任务的记录
        task_info = MyTask.fetch_exec_info(task_id)

        # 没有该任务信息记录时,返回失败
        if task_info is None:
            return False

        # 设置百分比和运行状态
        task_info['percentage'] = percent

        # 结果集不为空时,用新的结果集替换
        if result is not None:
            task_info['result'] = result

        # 计算并记录执行时间(预计剩余时间)
        task_info['remain_time'] = MyTask._calc_exec_time(task_info)
        # 设置当前记录时间
        task_info['record_time'] = SysUtils.get_now_time_str()

        # 保存处理进程的历史记录
        MyTask._save_progress_history(task_info)

        # 调用 websocket task_feedback
        task_feedback(task_id, task_info)

        # 运行100%时,设置任务完成状态,并更新数据库,清理缓存
        if percent == 100.0:
            task_info['task_status'] = TaskStatus.COMPLETE
            TasksDAO.save(task_info)
            MyRedis.delete(task_id, category=task_cat)

            # 保存任务详情日志
            LogRecords.save(task_info,
                            category='task',
                            action='任务完成',
                            desc='任务执行100%,记录任务执行结果信息')
        else:
            # 缓存该任务的记录
            MyRedis.set(task_id, task_info, category=task_cat)

        # 发送执行状态的更新消息
        if notify:
            MyTask.notify_task_exec_info(task_info)

        return True
Esempio n. 3
0
    def stop_task(task_id):
        # 从缓存中取出任务信息,同时删除这条缓存
        task_info = MyTask.fetch_exec_info(task_id, remove=True)

        # 只允许终止正在运行的任务
        if task_info['task_status'] != TaskStatus.RUNNING:
            return task_info

        # 设置停止状态和 stop_time
        task_info['task_status'] = TaskStatus.STOPPED
        task_info['stop_time'] = SysUtils.get_now_time_str()

        # 在数据库中保存任务信息
        TasksDAO.save(task_info)

        # 保存任务详情日志
        LogRecords.save(task_info,
                        category='task',
                        action='任务终止',
                        desc='任务被终止')

        return task_info
def websocket_callback(task_id, process, percent=50):
    print(task_id)
    # 从缓存或数据库中读取该任务的记录
    task_info = MyTask.fetch_exec_info(task_id)

    # 没有该任务信息记录时,返回失败
    if task_info is None:
        return False

    # 计算并记录执行时间(预计剩余时间)
    task_info['remain_time'] = MyTask._calc_exec_time(task_info)
    # 设置当前记录时间
    task_info['record_time'] = SysUtils.get_now_time_str()
    # 设置百分比和运行状态
    task_info['percentage'] = percent

    value_list = process.split('\n')
    for result in value_list:
        # 结果集不为空时,用新的结果集替换
        if result is not None:
            task_info['result'] = result

        # 调用 websocket task_feedback
        task_feedback(task_id, task_info)
    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()