|
@ -28,7 +28,7 @@ class TaskManager: |
|
|
self.CCM = ControlCenter(self.DBM,self) |
|
|
self.CCM = ControlCenter(self.DBM,self) |
|
|
self.InstrM = InstructionManager(self) # 类对象渗透,要约束只读取信息 |
|
|
self.InstrM = InstructionManager(self) # 类对象渗透,要约束只读取信息 |
|
|
# 控制最大并发指令数量 |
|
|
# 控制最大并发指令数量 |
|
|
self.max_thread_num = 2 |
|
|
self.max_thread_num = 6 |
|
|
self.task_id = 0 #任务id -- |
|
|
self.task_id = 0 #任务id -- |
|
|
self.workth_list = [] #线程句柄list |
|
|
self.workth_list = [] #线程句柄list |
|
|
# self.long_instr_num = 0 #耗时指令数量 |
|
|
# self.long_instr_num = 0 #耗时指令数量 |
|
@ -38,6 +38,7 @@ class TaskManager: |
|
|
self.lock = threading.Lock() #线程锁 |
|
|
self.lock = threading.Lock() #线程锁 |
|
|
self.node_num = 0 #在处理Node线程的处理 |
|
|
self.node_num = 0 #在处理Node线程的处理 |
|
|
self.brun = True |
|
|
self.brun = True |
|
|
|
|
|
self.cookie = "" #cookie参数 |
|
|
|
|
|
|
|
|
def res_in_quere(self,bres,instr,reslut,start_time,end_time,th_DBM,source_result,ext_params,work_node): |
|
|
def res_in_quere(self,bres,instr,reslut,start_time,end_time,th_DBM,source_result,ext_params,work_node): |
|
|
''' |
|
|
''' |
|
@ -57,8 +58,9 @@ class TaskManager: |
|
|
|
|
|
|
|
|
#结果入队列---2025-3-18所有的指令均需返回给LLM便于节点状态的更新,所以bres作用要调整。 |
|
|
#结果入队列---2025-3-18所有的指令均需返回给LLM便于节点状态的更新,所以bres作用要调整。 |
|
|
res = {'执行指令':instr,'结果':reslut} |
|
|
res = {'执行指令':instr,'结果':reslut} |
|
|
|
|
|
str_res = json.dumps(res,ensure_ascii=False) #直接字符串组合也可以-待验证 |
|
|
work_node.llm_type = 1 |
|
|
work_node.llm_type = 1 |
|
|
work_node.add_res(res) #入节点结果队列 |
|
|
work_node.add_res(str_res) #入节点结果队列 |
|
|
|
|
|
|
|
|
def do_worker_th(self): |
|
|
def do_worker_th(self): |
|
|
#线程的dbm需要一个线程一个 |
|
|
#线程的dbm需要一个线程一个 |
|
@ -87,14 +89,21 @@ class TaskManager: |
|
|
with self.lock: |
|
|
with self.lock: |
|
|
self.node_num -= 1 |
|
|
self.node_num -= 1 |
|
|
if self.node_num == 0 and self.node_queue.empty(): # |
|
|
if self.node_num == 0 and self.node_queue.empty(): # |
|
|
|
|
|
self.logger.debug("此批次指令执行完成!") |
|
|
with open("attack_tree", 'wb') as f: |
|
|
with open("attack_tree", 'wb') as f: |
|
|
pickle.dump(TM.CCM.attack_tree, f) |
|
|
pickle.dump(self.CCM.attack_tree, f) |
|
|
|
|
|
|
|
|
except queue.Empty: |
|
|
except queue.Empty: |
|
|
self.logger.debug("暂无需要执行指令的节点!") |
|
|
self.logger.debug("暂无需要执行指令的节点!") |
|
|
time.sleep(20) |
|
|
time.sleep(20) |
|
|
|
|
|
|
|
|
def start_task(self,target_name,target_in): |
|
|
def start_task(self,target_name,target_in): |
|
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
|
|
:param target_name: 任务目标名字 |
|
|
|
|
|
:param target_in: 任务目标访问地址 |
|
|
|
|
|
:return: |
|
|
|
|
|
''' |
|
|
#判断目标合法性 |
|
|
#判断目标合法性 |
|
|
bok,target,type = self.TargetM.validate_and_extract(target_in) |
|
|
bok,target,type = self.TargetM.validate_and_extract(target_in) |
|
|
if bok: |
|
|
if bok: |
|
@ -134,8 +143,10 @@ 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 = 5 |
|
|
test_type = 1 |
|
|
iput_index = 6 # 0是根节点 |
|
|
instr_index = 19 |
|
|
|
|
|
iput_index = -1 # 0是根节点 |
|
|
|
|
|
indexs = [] |
|
|
if test_type == 0: #新目标测试 |
|
|
if 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) |
|
@ -147,10 +158,16 @@ if __name__ == "__main__": |
|
|
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,查看有instr的ndoe |
|
|
# 遍历node,查看有instr的ndoe |
|
|
nodes = TM.CCM.attack_tree.traverse_bfs() |
|
|
nodes = TM.CCM.attack_tree.traverse_dfs() |
|
|
for node in nodes: |
|
|
if indexs: |
|
|
|
|
|
for index in indexs: |
|
|
|
|
|
node = nodes[index] |
|
|
if node.instr_queue: # list |
|
|
if node.instr_queue: # list |
|
|
TM.node_queue.put(node) |
|
|
TM.node_queue.put(node) |
|
|
|
|
|
else: |
|
|
|
|
|
for node in nodes: |
|
|
|
|
|
if node.instr_queue: |
|
|
|
|
|
TM.node_queue.put(node) |
|
|
|
|
|
|
|
|
#创建线程执行指令 |
|
|
#创建线程执行指令 |
|
|
for i in range(TM.max_thread_num): |
|
|
for i in range(TM.max_thread_num): |
|
@ -165,21 +182,18 @@ if __name__ == "__main__": |
|
|
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,查看有res的数据 |
|
|
iput_max_num = 1 |
|
|
iput_max_num = 0 |
|
|
iput_num = 0 |
|
|
iput_num = 0 |
|
|
nodes = TM.CCM.attack_tree.traverse_bfs() |
|
|
nodes = TM.CCM.attack_tree.traverse_dfs() |
|
|
if iput_index != -1:#index 不为-1就是指定节点返回,人为保障不越界 |
|
|
if indexs: |
|
|
node = nodes[iput_index] |
|
|
for index in indexs: |
|
|
|
|
|
node = nodes[index] |
|
|
if node.res_quere: |
|
|
if node.res_quere: |
|
|
TM.CCM.llm_quere.put(node) |
|
|
TM.CCM.llm_quere.put(node) |
|
|
else: |
|
|
else: |
|
|
for node in nodes: |
|
|
for node in nodes: |
|
|
if node.res_quere: #有结果需要提交LLM |
|
|
if node.res_quere: |
|
|
TM.CCM.llm_quere.put(node) |
|
|
TM.CCM.llm_quere.put(node) |
|
|
iput_num += 1 |
|
|
|
|
|
if iput_max_num > 0: #0是有多少提交多少 |
|
|
|
|
|
if iput_num == iput_max_num: |
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
#创建llm工作线程 |
|
|
#创建llm工作线程 |
|
|
TM.CCM.brun = True |
|
|
TM.CCM.brun = True |
|
@ -191,25 +205,27 @@ if __name__ == "__main__": |
|
|
for t in TM.CCM.llmth_list: |
|
|
for t in TM.CCM.llmth_list: |
|
|
t.join() |
|
|
t.join() |
|
|
elif test_type ==3: #执行指定指令 |
|
|
elif test_type ==3: #执行指定指令 |
|
|
instrlist=[ |
|
|
with open("attack_tree", "rb") as f: |
|
|
"msfconsole -q -x \"use auxiliary/scanner/smb/smb_version; set RHOSTS 192.168.204.137; run; exit\""] |
|
|
TM.CCM.attack_tree = pickle.load(f) |
|
|
|
|
|
# 遍历node,查看有instr的ndoe |
|
|
|
|
|
nodes = TM.CCM.attack_tree.traverse_dfs() |
|
|
|
|
|
instrlist = nodes[instr_index].instr_queue |
|
|
|
|
|
# instrlist = [''' |
|
|
|
|
|
# '''] |
|
|
for instr in instrlist: |
|
|
for instr in instrlist: |
|
|
start_time = get_local_timestr() # 指令执行开始时间 |
|
|
start_time = get_local_timestr() # 指令执行开始时间 |
|
|
bres, instr, reslut, source_result, ext_params = TM.InstrM.execute_instruction(instr) |
|
|
bres, instr, reslut, source_result, ext_params = TM.InstrM.execute_instruction(instr) |
|
|
end_time = get_local_timestr() # 指令执行结束时间 |
|
|
end_time = get_local_timestr() # 指令执行结束时间 |
|
|
# 入数据库 -- bres True和False 都入数据库2025-3-10---加node_path(2025-3-18)#? |
|
|
|
|
|
if TM.DBM.ok: |
|
|
res = {'执行结果': reslut} |
|
|
TM.DBM.insetr_result(0, instr, reslut, 0, start_time, end_time, source_result, |
|
|
str_res = json.dumps(res,ensure_ascii=False) # 直接字符串组合也可以-待验证 |
|
|
ext_params, "独立命令执行") |
|
|
print(str_res) |
|
|
else: |
|
|
|
|
|
TM.logger.error("数据库连接失败!!") |
|
|
|
|
|
elif test_type == 4: #修改Message |
|
|
elif test_type == 4: #修改Message |
|
|
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) |
|
|
#创建一个新的节点 |
|
|
#创建一个新的节点 |
|
|
from mycode.AttackMap import TreeNode |
|
|
from mycode.AttackMap import TreeNode |
|
|
testnode = TreeNode("test",0) |
|
|
testnode = TreeNode("test",0,0) |
|
|
TM.CCM.LLM.build_initial_prompt(testnode)#新的Message |
|
|
TM.CCM.LLM.build_initial_prompt(testnode)#新的Message |
|
|
systems = testnode.messages[0]["content"] |
|
|
systems = testnode.messages[0]["content"] |
|
|
#print(systems) |
|
|
#print(systems) |
|
@ -222,9 +238,76 @@ if __name__ == "__main__": |
|
|
elif test_type ==5: #显示指令和结果list |
|
|
elif test_type ==5: #显示指令和结果list |
|
|
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) |
|
|
nodes = TM.CCM.attack_tree.traverse_bfs() |
|
|
nodes = TM.CCM.attack_tree.traverse_dfs() |
|
|
|
|
|
if iput_index == -1: |
|
|
|
|
|
for node in nodes: |
|
|
|
|
|
print(f"----{node.path}-{node.status}----\n****instr_quere") |
|
|
|
|
|
print(f"{','.join(node.instr_queue)}\n****res_quere") |
|
|
|
|
|
try: |
|
|
|
|
|
print(f"{','.join(node.res_quere)}") |
|
|
|
|
|
except: |
|
|
|
|
|
print(f"{json.dumps(node.res_quere)}") |
|
|
|
|
|
elif iput_index == -2:#只输出有instruction的数据 |
|
|
|
|
|
index = 0 |
|
|
|
|
|
for node in nodes: |
|
|
|
|
|
if node.instr_queue: |
|
|
|
|
|
print(f"----{index}--{node.path}--{node.status}----") |
|
|
|
|
|
print(f"{','.join(node.instr_queue)}") |
|
|
|
|
|
index += 1 |
|
|
|
|
|
else: |
|
|
print(f"********\n{','.join(nodes[iput_index].instr_queue)}\n********") |
|
|
print(f"********\n{','.join(nodes[iput_index].instr_queue)}\n********") |
|
|
print(f"&&&&&&&&\n{','.join(nodes[iput_index].res_quere)}\n&&&&&&&&") |
|
|
print(f"&&&&&&&&\n{','.join(nodes[iput_index].res_quere)}\n&&&&&&&&") |
|
|
|
|
|
elif test_type == 6: #给指定节点添加测试指令 |
|
|
|
|
|
with open("attack_tree", "rb") as f: |
|
|
|
|
|
TM.CCM.attack_tree = pickle.load(f) |
|
|
|
|
|
nodes = TM.CCM.attack_tree.traverse_dfs() |
|
|
|
|
|
str_instr = "nmap -sV -p- 192.168.204.137 -T4 -oN nmap_full_scan.txt" |
|
|
|
|
|
index = 9 |
|
|
|
|
|
nodes[index].instr_queue.append(str_instr) |
|
|
|
|
|
nodes[index].res_quere = [] |
|
|
|
|
|
with open("attack_tree", 'wb') as f: |
|
|
|
|
|
pickle.dump(TM.CCM.attack_tree, f) |
|
|
|
|
|
elif test_type == 7: #给指定节点修改指令的执行结果 |
|
|
|
|
|
with open("attack_tree", "rb") as f: |
|
|
|
|
|
TM.CCM.attack_tree = pickle.load(f) |
|
|
|
|
|
nodes = TM.CCM.attack_tree.traverse_dfs() |
|
|
|
|
|
str_instr = "psql -h 192.168.204.137 -U postgres -c '\l'" |
|
|
|
|
|
start_time = get_local_timestr() # 指令执行开始时间 |
|
|
|
|
|
bres, instr, reslut, source_result, ext_params = TM.InstrM.execute_instruction(str_instr) |
|
|
|
|
|
end_time = get_local_timestr() # 指令执行结束时间 |
|
|
|
|
|
# 入数据库 -- bres True和False 都入数据库2025-3-10---加node_path(2025-3-18)#? |
|
|
|
|
|
if TM.DBM.ok: |
|
|
|
|
|
TM.DBM.insetr_result(0, instr, reslut, 0, start_time, end_time, source_result, |
|
|
|
|
|
ext_params, "独立命令执行") |
|
|
|
|
|
index = 9 |
|
|
|
|
|
nodes[index].res_quere.clear() |
|
|
|
|
|
nodes[index].res_quere.append(reslut) |
|
|
|
|
|
|
|
|
|
|
|
with open("attack_tree", 'wb') as f: |
|
|
|
|
|
pickle.dump(TM.CCM.attack_tree, f) |
|
|
|
|
|
elif test_type == 9: #处理自定义llm回复内容 |
|
|
|
|
|
with open("attack_tree", "rb") as f: |
|
|
|
|
|
TM.CCM.attack_tree = pickle.load(f) |
|
|
|
|
|
nodes = TM.CCM.attack_tree.traverse_dfs() |
|
|
|
|
|
node = nodes[5] |
|
|
|
|
|
strconent = ''' |
|
|
|
|
|
{'role': 'assistant', 'content': '{"action":"update_status", "node": "25端口", "status": "已完成", "vulnerability": {"name":"SMTP用户枚举漏洞","risk":"中危","info":"VRFY命令可验证有效用户"}}\n\n```bash-[目标系统->192.168.204.137->25端口]\nsmtp-user-enum -M VRFY -U /usr/share/wordlists/metasploit/unix_users.txt -t 192.168.204.137\n```\n\n```bash-[目标系统->192.168.204.137->25端口]\nnc -nv 192.168.204.137 25 << EOF\nEXPN root\nMAIL FROM: attacker@example.com\nRCPT TO: external@example.com\nDATA\nTest open relay\n.\nQUIT\nEOF\n```'} |
|
|
|
|
|
''' |
|
|
|
|
|
strjson = json.loads(strconent) |
|
|
|
|
|
node_cmds,commands = TM.CCM.LLM.fetch_instruction(strjson["content"]) |
|
|
|
|
|
TM.CCM.tree_manager(node_cmds) |
|
|
|
|
|
|
|
|
|
|
|
# 更新tree |
|
|
|
|
|
bok, new_commands = TM.CCM.tree_manager(node_cmds, node, commands, TM.DBM) |
|
|
|
|
|
# 分析指令入对应节点 |
|
|
|
|
|
if bok: # 节点指令若存在错误,测试指令都不处理,需要LLM重新生成 |
|
|
|
|
|
node_list = TM.CCM.instr_in_node(new_commands, node) |
|
|
|
|
|
|
|
|
|
|
|
#报不保存待定-- |
|
|
|
|
|
with open("attack_tree", 'wb') as f: |
|
|
|
|
|
pickle.dump(TM.CCM.attack_tree, f) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
else: |
|
|
#完整过程测试---要设定终止条件 |
|
|
#完整过程测试---要设定终止条件 |
|
|
pass |
|
|
pass |
|
|