|
@ -35,7 +35,7 @@ class TaskManager: |
|
|
self.batch_num = 0 #一个批次的指令数量 |
|
|
self.batch_num = 0 #一个批次的指令数量 |
|
|
self.long_instr_num = 0 #耗时指令数量 |
|
|
self.long_instr_num = 0 #耗时指令数量 |
|
|
self.long_time_instr = ['nikto'] #耗时操作不计入批量执行的数量,不加不减 |
|
|
self.long_time_instr = ['nikto'] #耗时操作不计入批量执行的数量,不加不减 |
|
|
self.node_queue = [] # |
|
|
self.node_queue = [] #2025-3-19修改为list,兼容pickle反序列化 |
|
|
|
|
|
|
|
|
self.lock = threading.Lock() #线程锁 |
|
|
self.lock = threading.Lock() #线程锁 |
|
|
self.node_num = 0 #在处理Node线程的处理 |
|
|
self.node_num = 0 #在处理Node线程的处理 |
|
@ -72,6 +72,7 @@ class TaskManager: |
|
|
if self.node_queue: |
|
|
if self.node_queue: |
|
|
# 获取测试节点 |
|
|
# 获取测试节点 |
|
|
work_node = self.node_queue.pop(0) |
|
|
work_node = self.node_queue.pop(0) |
|
|
|
|
|
#测试时使用 |
|
|
self.node_num += 1 |
|
|
self.node_num += 1 |
|
|
|
|
|
|
|
|
if work_node: |
|
|
if work_node: |
|
@ -82,35 +83,23 @@ class TaskManager: |
|
|
end_time = get_local_timestr() # 指令执行结束时间 |
|
|
end_time = get_local_timestr() # 指令执行结束时间 |
|
|
self.res_in_quere(bres, instr, reslut, start_time, end_time, th_DBM, source_result, |
|
|
self.res_in_quere(bres, instr, reslut, start_time, end_time, th_DBM, source_result, |
|
|
ext_params,work_node) # 执行结果入队列 |
|
|
ext_params,work_node) # 执行结果入队列 |
|
|
#保存记录--测试使用 |
|
|
|
|
|
|
|
|
# #针对一个节点的指令执行完成后,提交LLM规划下一步操作 |
|
|
|
|
|
# node_list = self.CCM.get_llm_instruction(work_node) |
|
|
|
|
|
# if not node_list:#该节点测试完成----没有返回新指令 |
|
|
|
|
|
# continue #取下一个节点 |
|
|
|
|
|
# for node in node_list: #新指令入队列 |
|
|
|
|
|
# self.node_queue.put(node) |
|
|
|
|
|
|
|
|
|
|
|
# 保存记录--测试时使用--后期增加人为停止测试时可以使用 |
|
|
with self.lock: |
|
|
with self.lock: |
|
|
self.node_num -= 1 |
|
|
self.node_num -= 1 |
|
|
if self.node_num == 0 and len(self.node_queue) == 0: |
|
|
if self.node_num == 0 and len(self.node_queue) == 0: # |
|
|
with open("attack_tree", 'wb') as f: |
|
|
with open("attack_tree", 'wb') as f: |
|
|
pickle.dump(TM.CCM.attack_tree, f) |
|
|
pickle.dump(TM.CCM.attack_tree, f) |
|
|
#针对一个节点的指令执行完成后,提交LLM规划下一步操作 |
|
|
else: #没有带处理的Node |
|
|
# node_list = self.CCM.get_llm_instruction(work_node) |
|
|
|
|
|
# if not node_list:#该节点测试完成 |
|
|
|
|
|
# continue |
|
|
|
|
|
# for node in node_list: |
|
|
|
|
|
# self.node_queue.put(node) |
|
|
|
|
|
else: |
|
|
|
|
|
time.sleep(10) |
|
|
time.sleep(10) |
|
|
# try: |
|
|
|
|
|
# instruction = self.instr_queue.get(block=False) #quere线程安全,block=false非阻塞get |
|
|
|
|
|
# self.doing_instr.put(instruction) #入执行队列--忘了入执行队列的作用 |
|
|
|
|
|
# #执行中会对指令进行微调,并有可能不执行直接返回空结果 |
|
|
|
|
|
# start_time = get_local_timestr() #指令执行开始时间 |
|
|
|
|
|
# bres,instr,reslut,source_result,ext_params = self.InstrM.execute_instruction(instruction) |
|
|
|
|
|
# end_time = get_local_timestr() #指令执行结束时间 |
|
|
|
|
|
# self.res_in_quere(bres,instr,reslut,start_time,end_time,th_DBM,source_result,ext_params) #执行结果入队列 |
|
|
|
|
|
# self.done_instr.put(instruction) #执行完成队列 |
|
|
|
|
|
# #执行情况是否需要用户确认 |
|
|
|
|
|
# if ext_params["is_user"]: |
|
|
|
|
|
# pass |
|
|
|
|
|
# #print("该指令执行需要用户确认") |
|
|
|
|
|
# except queue.Empty: |
|
|
|
|
|
# time.sleep(10) |
|
|
|
|
|
#函数结束,局部变量自动释放 |
|
|
#函数结束,局部变量自动释放 |
|
|
|
|
|
|
|
|
def start_task(self,target_name,target_in): |
|
|
def start_task(self,target_name,target_in): |
|
@ -156,12 +145,12 @@ if __name__ == "__main__": |
|
|
current_path = os.path.dirname(os.path.realpath(__file__)) |
|
|
current_path = os.path.dirname(os.path.realpath(__file__)) |
|
|
strMsg = FM.read_file("test",1) |
|
|
strMsg = FM.read_file("test",1) |
|
|
|
|
|
|
|
|
test_type = 3 |
|
|
test_type = 2 |
|
|
if test_type == 1: |
|
|
if test_type == 1: |
|
|
#测试执行指令 |
|
|
#测试执行指令 |
|
|
with open("attack_tree", "rb") as f: |
|
|
with open("attack_tree", "rb") as f: |
|
|
TM.CCM.attack_tree = pickle.load(f) |
|
|
TM.CCM.attack_tree = pickle.load(f) |
|
|
# 遍历node,查看有res的数据 |
|
|
# 遍历node,查看有instr的ndoe |
|
|
nodes = TM.CCM.attack_tree.traverse_bfs() |
|
|
nodes = TM.CCM.attack_tree.traverse_bfs() |
|
|
for node in nodes: |
|
|
for node in nodes: |
|
|
if node.instr_queue: # list |
|
|
if node.instr_queue: # list |
|
@ -182,17 +171,12 @@ if __name__ == "__main__": |
|
|
#遍历node,查看有res的数据 |
|
|
#遍历node,查看有res的数据 |
|
|
nodes = TM.CCM.attack_tree.traverse_bfs() |
|
|
nodes = TM.CCM.attack_tree.traverse_bfs() |
|
|
for node in nodes: |
|
|
for node in nodes: |
|
|
if node.res_quere: #list |
|
|
if node.res_quere: #有结果需要提交LLM |
|
|
node_list = TM.CCM.get_llm_instruction(node) |
|
|
node_list = TM.CCM.get_llm_instruction(node) #包括对节点操作 |
|
|
if not node_list:#该节点测试完成 |
|
|
|
|
|
continue |
|
|
|
|
|
with TM.lock: |
|
|
|
|
|
for do_node in node_list: |
|
|
|
|
|
TM.node_queue.append(do_node) |
|
|
|
|
|
# 暂存状态-- |
|
|
# 暂存状态-- |
|
|
with open("attack_tree", 'wb') as f: |
|
|
with open("attack_tree", 'wb') as f: |
|
|
pickle.dump(TM.CCM.attack_tree, f) |
|
|
pickle.dump(TM.CCM.attack_tree, f) |
|
|
elif test_type == 3: #新目标测试 |
|
|
elif test_type == 0: #新目标测试 |
|
|
# 启动--初始化指令 |
|
|
# 启动--初始化指令 |
|
|
node_list = TM.CCM.start_do("192.168.204.137", 0) |
|
|
node_list = TM.CCM.start_do("192.168.204.137", 0) |
|
|
with TM.lock: |
|
|
with TM.lock: |
|
@ -202,7 +186,11 @@ if __name__ == "__main__": |
|
|
with open("attack_tree",'wb') as f: |
|
|
with open("attack_tree",'wb') as f: |
|
|
pickle.dump(TM.CCM.attack_tree,f) |
|
|
pickle.dump(TM.CCM.attack_tree,f) |
|
|
elif test_type == 4: #读取messages |
|
|
elif test_type == 4: #读取messages |
|
|
pass |
|
|
with open("attack_tree", "rb") as f: |
|
|
|
|
|
TM.CCM.attack_tree = pickle.load(f) |
|
|
|
|
|
# 遍历node,查看有instr的ndoe |
|
|
|
|
|
nodes = TM.CCM.attack_tree.traverse_bfs() |
|
|
|
|
|
print(nodes[0].messages) |
|
|
else: |
|
|
else: |
|
|
#完整过程测试---要设定终止条件 |
|
|
#完整过程测试---要设定终止条件 |
|
|
pass |
|
|
pass |
|
|