import re import csv def process_text(file_path): service_data = [] vuln_data = [] with open(file_path, 'r', encoding='utf-8') as f: content = f.readlines() for line in content: if '[SERVICE]' in line: url_match = re.search(r'Url=(.*?)(?:,|\s|$)', line) if url_match: url = url_match.group(1).replace(',', '').strip() service_data.append({"Url": url}) if '[VULN]' in line and 'type=weak-password' in line: target_match = re.search(r'目标:(.*?)\s', line) service_match = re.search(r'service=(.*?)\s', line) port_match = re.search(r'port=(.*?)\s', line) username_match = re.search(r'username=(.*?)\s', line) password_match = re.search(r'password=(.*?)(\s|$)', line) if target_match: target = target_match.group(1).replace(',', '').strip() service = service_match.group(1).replace(',', '').strip() if service_match else '' port = port_match.group(1).replace(',', '').strip() if port_match else '' username = username_match.group(1).replace(',', '').strip() if username_match else '' password = password_match.group(1).replace(',', '').strip() if password_match else '' vuln_data.append({ "目标": target, "service": service, "port": port, "username": username, "password": password }) return service_data, vuln_data def export_to_csv(data, filename, headers): with open(filename, 'w', newline='', encoding='utf-8') as f: writer = csv.DictWriter(f, fieldnames=headers) writer.writeheader() writer.writerows(data) def export_to_txt(service_data, vuln_data, filename): with open(filename, 'w', encoding='utf-8') as f: f.write("===== VULN 结果(弱口令)=====\n") for item in vuln_data: f.write( f"目标: {item['目标']}, service: {item['service']}, " f"port: {item['port']}, username: {item['username']}, " f"password: {item['password']}\n" ) f.write("\n===== SERVICE 结果 =====\n") for item in service_data: f.write(f"Url: {item['Url']}\n") if __name__ == "__main__": input_file = "result.txt" service_results, vuln_results = process_text(input_file) if service_results: export_to_csv(service_results, "service_results.csv", ["Url"]) print(f"SERVICE结果已导出到 service_results.csv,共 {len(service_results)} 条记录") else: print("未找到[SERVICE]类型数据") if vuln_results: export_to_csv(vuln_results, "vuln_results.csv", ["目标", "service", "port", "username", "password"]) print(f"VULN结果(弱口令)已导出到 vuln_results.csv,共 {len(vuln_results)} 条记录") else: print("未找到[VULN]类型(弱口令)数据") export_to_txt(service_results, vuln_results, "scan_out.txt") print("所有结果已汇总到 scan_out.txt")