import requests import shlex import re import json from bs4 import BeautifulSoup from tools.ToolBase import ToolBase class CurlTool(ToolBase): # def __init__(self): # super.__init__() # self.headers = {} # self.url = None # self.verify_ssl = True #解析指令到requests def parse_curl_to_requests(self,curl_command): # Split command preserving quoted strings parts = shlex.split(curl_command) if parts[0] != 'curl': raise ValueError("Command must start with 'curl'") # Parse curl flags and arguments i = 1 while i < len(parts): arg = parts[i] if arg == '-k' or arg == '--insecure': self.verify_ssl = False i += 1 elif arg == '-s' or arg == '--silent': # Silencing isn't needed for requests, just skip i += 1 elif arg == '-H' or arg == '--header': if i + 1 >= len(parts): raise ValueError("Missing header value after -H") header_str = parts[i + 1] header_name, header_value = header_str.split(':', 1) self.headers[header_name.strip()] = header_value.strip() i += 2 elif not arg.startswith('-'): if self.url is None: self.url = arg i += 1 else: i += 1 if self.url is None: raise ValueError("No URL found in curl command") #return url, headers, verify_ssl def validate_instruction(self, instruction_old): #instruction = instruction_old #指令过滤 timeout = 0 #添加-i 返回信息头 parts = instruction_old.split() if 'base64 -d' in instruction_old: return instruction_old if '-i' not in parts and '--include' not in parts: url_index = next((i for i, p in enumerate(parts) if p.startswith(('http://', 'https://'))), None) if url_index is not None: # 在URL前插入 -i 参数:ml-citation{ref="1" data="citationList"} parts.insert(url_index, '-i') else: # 无URL时直接在末尾添加 parts.append('-i') return ' '.join(parts),timeout # def execute_instruction(self, instruction_old): # ''' # 执行指令:验证合法性 -> 执行 -> 分析结果 # :param instruction_old: # :return: # bool:true-正常返回给大模型,false-结果不返回给大模型 # str:执行的指令 # str:执行指令的结果 # ''' # # # 第一步:验证指令合法性 # instruction = self.validate_instruction(instruction_old) # if not instruction: # return False,instruction_old,"该指令暂不执行!" # # # 第二步:执行指令 --- 基于request使用 # #print(f"执行指令:{instruction}") # output = "" # # # 第三步:分析执行结果 # analysis = self.analyze_result(output,instruction) # #指令和结果入数据库 # #? # if not analysis: #analysis为“” 不提交LLM # return False,instruction,analysis # return True,instruction, analysis def get_ssl_info(self,stderr,stdout): # -------------------------- # 解释信息的安全意义: # # - 如果证书的 Common Name 与请求的 IP 不匹配(如这里的 'crnn.f3322.net'), # 则可能表明服务隐藏了真实身份或存在配置错误,这在后续攻击中可以作为信息收集的一部分。 # # - TLS 连接信息(如 TLS1.3 和加密套件)有助于判断是否存在弱加密或旧版协议问题。 # # - HTTP 状态和 Content-Type 帮助确认返回的是一个合法的 Web 服务, # 而 HTML Title 暗示了实际运行的是 SoftEther VPN Server,可能存在默认配置或已知漏洞。 # # 这些信息可以作为进一步探测、漏洞验证和渗透测试的依据。 # -------------------------- # 从 stderr 中提取证书及 TLS 信息 # 提取 Common Name(CN) cn_match = re.search(r"common name:\s*([^\s]+)", stderr, re.IGNORECASE) cert_cn = cn_match.group(1) if cn_match else "N/A" # 提取 TLS 连接信息(例如 TLS1.3 及加密套件) tls_match = re.search(r"SSL connection using\s+([^\n]+)", stderr, re.IGNORECASE) tls_info = tls_match.group(1).strip() if tls_match else "N/A" # 提取 Issuer 信息 issuer_match = re.search(r"issuer:\s*(.+)", stderr, re.IGNORECASE) issuer_info = issuer_match.group(1).strip() if issuer_match else "N/A" # 从 stdout 中提取 HTTP 响应头和 HTML 标题 # 分离 HTTP 头部和 body(假设头部与 body 用两个换行符分隔) parts = stdout.split("\n\n", 1) headers_part = parts[0] body_part = parts[1] if len(parts) > 1 else "" # 从头部中提取状态行和部分常见头部信息 lines = headers_part.splitlines() http_status = lines[0] if lines else "N/A" content_type_match = re.search(r"Content-Type:\s*(.*)", headers_part, re.IGNORECASE) content_type = content_type_match.group(1).strip() if content_type_match else "N/A" # 使用 BeautifulSoup 提取 HTML