def convert_masscan_report(xml_path, xls_path): DOMTree = xml.dom.minidom.parse(xml_path) data = DOMTree.documentElement nodelist = data.getElementsByTagName('host') ip_info = {} for node in nodelist: scan_endtime = node.getAttribute('endtime') scan_endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(scan_endtime))) address_node = node.getElementsByTagName('address') addrtype = address_node[0].getAttribute('addrtype') addr = address_node[0].getAttribute('addr') port_node = node.getElementsByTagName('port') ip_prefix = addr.split(".")[0] + "." + addr.split(".")[1] + "." + addr.split(".")[2] if ip_prefix not in ip_info: ip_info[ip_prefix] = {} for port in port_node: if addr in ip_info[ip_prefix]: ip_info[ip_prefix][addr][1] = ip_info[ip_prefix][addr][1] + "," + portid continue protocol = port.getAttribute('protocol') portid = port.getAttribute('portid') state_element = port.getElementsByTagName('state') state = state_element[0].getAttribute('state') reason = state_element[0].getAttribute('reason') reason_ttl = state_element[0].getAttribute('reason_ttl') print('[+] | %s | %s | %s | %s | %s | %s | %s | %s |' % ( addr, portid, state, protocol, addrtype, reason, reason_ttl, scan_endtime)) scan_info = [addr, portid, state, protocol, addrtype, reason, reason_ttl, scan_endtime] ip_info[ip_prefix][addr] = scan_info workbook = xlsxwriter.Workbook(xls_path) for sheet_name, sheet_value in ip_info.items(): worksheet = workbook.add_worksheet(sheet_name) worksheet.autofilter("A1:H1") # 设置过滤 worksheet.freeze_panes(1, 0) # 冻结窗格 worksheet.lastrow = 0 summary_header = ["addr", "port", "state", "protocol", "addrtype", "reason", "reason_ttl", "scan_endtime"] for idx, item in enumerate(summary_header): worksheet.write(0, idx, item, workbook.add_format({"bold": True})) worksheet.lastrow += 1 for addr, addr_info in sheet_value.items(): for i in range(0, len(addr_info)): worksheet.write(worksheet.lastrow, i, addr_info[i]) worksheet.lastrow += 1 workbook.close()
时间: 2024-02-29 17:54:57 浏览: 103
xml.rar_java dom4j jar_pbbp1ij6.xml
这段代码的功能是将 Masscan 扫描结果的 XML 格式转换成 Excel 格式。具体实现的过程是:首先解析 XML 文件,然后遍历 XML 中的每个 host 节点,获取其中的 IP 地址,端口号等信息。将 IP 地址按照前三个段进行分组,最终将结果写入到 Excel 文件中,每个 IP 段对应一个 Excel Sheet,每个 Sheet 中包含了该 IP 段下的所有 IP 地址和对应的扫描结果。
阅读全文