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
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))
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
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
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
def store_monitor_from_sub(message=None): body = message.get('data') try: msf_module_return_dict = json.loads(body) 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 try: module_intent = req.get('module') if module_intent is None: logger.error("获取模块失败,body: {}".format(msf_module_return_dict)) return False logger.warning("模块回调:{} job_id: {} uuid: {}".format( module_intent.NAME, msf_module_return_dict.get("job_id"), 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) Notice.send_info("模块: {} 回调执行完成".format(module_intent.NAME)) module_intent._store_result_in_history() # 存储到历史记录