コード例 #1
0
    def delete_job_by_uuid(self, task_uuid=None):
        req = Xcache.get_module_task_by_uuid(task_uuid=task_uuid)
        Xcache.del_module_task_by_uuid(task_uuid=task_uuid)  # 清理缓存信息

        # 删除后台任务
        try:
            self.ModuleJobsScheduler.remove_job(task_uuid)
        except Exception as E:
            logger.error(E)

        try:
            module_common_instance = req.get("module")
        except Exception as E:
            logger.error(E)
            return False

        # 存储已经生成的结果
        try:
            module_common_instance.log_status("用户手动删除任务")
            module_common_instance._store_result_in_history()
        except Exception as E:
            logger.error("删除多模块实例异常:{} 异常信息: {}".format(module_common_instance.NAME, E))
            Notice.send_exception("模块: {} 执行异常,异常信息: {}".format(module_common_instance.NAME, E))
            logger.error(E)
            return False

        # 发送通知
        Notice.send_info(
            "模块: {} {} 手动删除".format(module_common_instance.NAME, module_common_instance.target_str))
        logger.warning("多模块实例手动删除:{}".format(module_common_instance.NAME))
        return True
コード例 #2
0
ファイル: job.py プロジェクト: j5s/viperpython
    def list_jobs():
        """获取后台任务列表,包括msf任务及本地多模块任务"""

        msf_jobs_dict = Job.list_msfrpc_jobs_no_cache()
        if msf_jobs_dict is None:  # msfrpc临时异常
            uncheck = True  # 跳过任务检查
            msf_jobs_dict = {}
        else:
            uncheck = False

        reqs = Xcache.list_module_tasks()
        reqs_temp = []
        for req in reqs:
            # post python module
            if req.get("job_id") is None:
                req["moduleinfo"] = PostModuleSerializer(req.get("module"),
                                                         many=False).data
                req["moduleinfo"]['_custom_param'] = Job._deal_dynamic_param(
                    req["moduleinfo"]['_custom_param'])
                req.pop("module")  # 弹出module实例
                reqs_temp.append(req)
                continue

            # post msf module
            # 跳过任务检查
            if uncheck:
                req["moduleinfo"] = PostModuleSerializer(req.get("module"),
                                                         many=False).data
                req.pop("module")  # 弹出module实例
                req["moduleinfo"]['_custom_param'] = Job._deal_dynamic_param(
                    req["moduleinfo"]['_custom_param'])
                reqs_temp.append(req)
                continue
            elif msf_jobs_dict.get(str(req.get("job_id"))) is not None:
                req["moduleinfo"] = PostModuleSerializer(req.get("module"),
                                                         many=False).data
                req["moduleinfo"]['_custom_param'] = Job._deal_dynamic_param(
                    req["moduleinfo"]['_custom_param'])
                req.pop("module")  # 弹出module实例
                reqs_temp.append(req)
                continue
            else:
                # 清除失效的任务
                if int(time.time()) - req.get("time") >= 30:
                    logger.error(f"清除失效的任务: {req.get('module').NAME}")
                    logger.error(req)
                    Xcache.del_module_task_by_uuid(req.get("uuid"))
                else:
                    # 如果创建时间不足30秒,则等待callback处理数据
                    req["moduleinfo"] = PostModuleSerializer(req.get("module"),
                                                             many=False).data
                    req["moduleinfo"][
                        '_custom_param'] = Job._deal_dynamic_param(
                            req["moduleinfo"]['_custom_param'])
                    req.pop("module")
                    reqs_temp.append(req)
                    continue
        return reqs_temp
コード例 #3
0
    def store_result_from_sub(message=None):
        # 回调报文数据格式
        # {
        # 'job_id': None,
        # 'uuid': '1b1a1ac0-95db-0137-5103-000c2966078a',
        # 'status': True,
        # 'message': None,
        # 'data': {'WHOAMI': 'nt authority\\system', 'IS_SYSTEM': True, }
        # }
        body = message.get('data')
        # 解析报文
        try:
            msf_module_return_dict = json.loads(body)
        except Exception as E:
            logger.error(E)
            return False

        # 获取对应模块实例
        try:
            req = Xcache.get_module_task_by_uuid(task_uuid=msf_module_return_dict.get("uuid"))
        except Exception as E:
            logger.error(E)
            return False

        if req is None:
            logger.error("未找到请求模块实例")
            logger.error(msf_module_return_dict)
            return False

        module_intent = req.get('module')
        if module_intent is None:
            logger.error("获取模块失败,body: {}".format(msf_module_return_dict))
            return False

        # 调用回调函数
        try:
            logger.warning(f"模块回调:{module_intent.NAME} "
                           f"job_id: {msf_module_return_dict.get('job_id')} "
                           f"uuid: {msf_module_return_dict.get('uuid')}")
            module_intent._clean_log()  # 清理历史结果
        except Exception as E:
            logger.error(E)
            return False

        try:
            module_intent.callback(status=msf_module_return_dict.get("status"),
                                   message=msf_module_return_dict.get("message"),
                                   data=msf_module_return_dict.get("data"))
        except Exception as E:
            Notice.send_error("模块 {} 的回调函数callhack运行异常".format(module_intent.NAME))
            logger.error(E)
        try:
            module_intent._store_result_in_history()  # 存储到历史记录
        except Exception as E:
            logger.error(E)

        Xcache.del_module_task_by_uuid(task_uuid=msf_module_return_dict.get("uuid"))  # 清理缓存信息
        Notice.send_success("模块: {} {} 执行完成".format(module_intent.NAME, module_intent._target_str))
コード例 #4
0
ファイル: job.py プロジェクト: j5s/viperpython
    def destroy_adv_job(task_uuid=None, job_id=None, broker=None):
        try:

            if broker == BROKER.post_python_job:
                flag = aps_module.delete_job_by_uuid(task_uuid)
                if flag is not True:
                    context = data_return(304, Job_MSG.get(304), {})
                    return context
                else:
                    context = data_return(204, Job_MSG.get(204), {
                        "uuid": task_uuid,
                        "job_id": job_id
                    })
                    return context
            elif broker == BROKER.post_msf_job:
                req = Xcache.get_module_task_by_uuid(task_uuid=task_uuid)
                common_module_instance = req.get("module")
                Xcache.del_module_task_by_uuid(task_uuid)
                params = [job_id]
                result = RpcClient.call(Method.JobStop, params)
                if result is None:
                    context = data_return(305, Job_MSG.get(305), {})
                    return context
                if result.get('result') == 'success':
                    # 发送通知
                    Notice.send_info("模块: {} {} 手动删除完成".format(
                        common_module_instance.NAME,
                        common_module_instance.target_str))
                    context = data_return(204, Job_MSG.get(204), {
                        "uuid": task_uuid,
                        "job_id": job_id
                    })
                    return context
                else:
                    context = data_return(304, Job_MSG.get(304), {})
                    return context
            elif broker == BROKER.bot_msf_job:
                flag = Xcache.del_bot_wait_by_group_uuid(task_uuid)
                if flag is not True:
                    context = data_return(304, Job_MSG.get(304), {})
                    return context
                else:
                    context = data_return(204, Job_MSG.get(204),
                                          {"uuid": task_uuid})
                    return context
            else:
                context = data_return(304, Job_MSG.get(304), {})
                return context

        except Exception as E:
            logger.error(E)
            context = data_return(500, CODE_MSG.get(500), {})
            return context
コード例 #5
0
    def store_error_result(task_uuid=None, exception=None):
        req = Xcache.get_module_task_by_uuid(task_uuid=task_uuid)
        Xcache.del_module_task_by_uuid(task_uuid=task_uuid)  # 清理缓存信息
        module_common_instance = req.get("module")

        # 存储运行结果
        try:
            module_common_instance.log_except(exception)
            module_common_instance._store_result_in_history()
            logger.error("多模块实例执行异常:{} 异常信息: {}".format(module_common_instance.NAME, exception))
            Notice.send_exception("模块: {} 执行异常,异常信息: {}".format(module_common_instance.NAME, exception))
            return True
        except Exception as E:
            logger.error("多模块实例执行异常:{} 异常信息: {}".format(module_common_instance.NAME, E))
            Notice.send_exception("模块: {} 执行异常,异常信息: {}".format(module_common_instance.NAME, E))
            logger.error(E)
            return False
コード例 #6
0
    def store_executed_result(task_uuid=None):
        req = Xcache.get_module_task_by_uuid(task_uuid=task_uuid)
        if req is None:
            logger.warning("缓存中无对应实例,可能已经模块已经中途退出")
            return False
        module_common_instance = req.get("module")

        # 存储运行结果
        try:
            module_common_instance._store_result_in_history()
            Notice.send_success(
                "模块: {} {} 执行完成".format(module_common_instance.NAME, module_common_instance.target_str))
            logger.warning("多模块实例执行完成:{}".format(module_common_instance.NAME))
            Xcache.del_module_task_by_uuid(task_uuid=task_uuid)  # 清理缓存信息
            return True
        except Exception as E:
            Xcache.del_module_task_by_uuid(task_uuid=task_uuid)  # 清理缓存信息
            logger.error("多模块实例执行异常:{} 异常信息: {}".format(module_common_instance.NAME, E))
            Notice.send_exception("模块: {} 执行异常,异常信息: {}".format(module_common_instance.NAME, E))
            logger.error(E)
            return False