美文网首页
Nmap输出过滤-xml转excel

Nmap输出过滤-xml转excel

作者: ninefighter | 来源:发表于2019-05-29 11:27 被阅读0次

一个简单的python脚本,将nmap输出的xml格式提取为excel

一、Nmap扫描

精准打击
    在信息收集阶段,一个重要的环节就是端口扫描,如果是一两个目标直接干就完了;但是如果是一堆IP地址段怎么办,这时扫描结果呼啦一大片,随便挑几个深入,这样显得不够专业吧


nmap的几个命令
1. nmap -sS 192.168.1.1 -T4 --open -n
2. nmap -sV 192.168.1.1 -T4 --open -p 22,80,3389 -n -O

注明:nmap的参数有很多,不列举了,sS 使用SYN的快速扫描(速度快),sV 探测端口服务,-O 探测目标操作系统

二、结果输出

    添加-oX参数指定输出为xml格式

1. nmap -sS 192.168.1.1 -T4 --open -n -O -oX test.xml

三、xml结果过滤

1. 关于xml文件解析

    python中解析xml有四种方法,其实对于我们这些偶尔拿来吃鸡的来说,随便一个就行了
常用的xml.dom.minidom和xml.etree.ElementTree,其中xml.etree.ElementTree有一个C语言的实现,即xml.etree.cElementTree,听说速度会快一点。(python3.3+版本后,ElemenTree模块会自动优先使用C加速器,如果不存在C实现,则会使用Python实现)

2. 代码实现
#coding:utf-8

import xml.etree.ElementTree as ET   #解析xml,python3已经默认使用cElementTree
import xlwt     #写excel
import argparse    #运行前参数
import sys

#判断运行时python版本是否小于3.x
if sys.version_info.major < 3:
    print("I need python3.x")

def parsexml(xml,sheet):
    tree = ET.parse(xml)
    root = tree.getroot()     #获取根节点
    hosts = root.findall("host")
    i = 0   #写入excel的计数器
    for host in hosts:
        i += 1
        portTcpRes = ""
        portOtherRes = ""
        ip = host.find("address")
        ipaddress = ip.attrib.get('addr')
        # print("ip地址:"+ ipaddress)
        ports = host.find("ports").findall("port")
        #获取系统版本的扫描结果
        os = host.find("os")
        try:
            osmatch = os.find("osmatch")
            osname = osmatch.attrib.get("name")
            accuracy = osmatch.attrib.get("accuracy")
        except Exception as e:
            osname = ""
            accuracy = ""
        # print(ports)
        for portT in list(ports):
            service = portT.find("service")
            protocol = portT.attrib.get('protocol')
            #获取nmap结果中service信息
            serviceName = service.attrib.get('name')
            product = service.attrib.get('product')
            if product:
                serviceName = serviceName + ':' + product   #将SERVICE和VERSION组合一起
            #获取端口号
            portNum = portT.attrib.get('portid')
            if protocol == 'tcp':
                portTcpRes += portNum + '(' + serviceName + '),'
            else:           #其他协议
                portOtherRes += portNum + '(' + serviceName + '),'
        output(sheet,ipaddress,portTcpRes.strip(','),portOtherRes.strip(','),i,osname,accuracy+"%")    #写入excel
        # print(portTcpRes.strip(','))
        # print('------------------------')
#初始化excel
def excelcsh():
    ExcelFile = xlwt.Workbook(encoding='utf-8',style_compression=0)
    sheet1 = ExcelFile.add_sheet('nmap结果')
    #表格第一行
    sheet1.write(0,0,'ip地址')
    sheet1.write(0,1,'TCP端口')
    sheet1.write(0,2,'其他协议端口')
    sheet1.write(0,3,'系统版本')
    sheet1.write(0,4,'系统扫描精准度')
    return ExcelFile,sheet1
def output(sheet,ip,tcpport,otherproto,num,osversion,accuracy):
    #写入数据
    sheet.write(num,0,ip)
    sheet.write(num,1,tcpport)
    sheet.write(num,2,otherproto)
    sheet.write(num,3,osversion)
    sheet.write(num,4,accuracy)
    #刷新缓存
    sheet.flush_row_data()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="xml解析")#.decode('utf-8').encode('gbk')
    parser.add_argument('-x',action="store",required=False,dest="xml",type=str,help='nmap result(xml file)')
    parser.add_argument('-o',action="store",required=False,dest="outfile",type=str,help='outputName',default="excel.xls")
    # parser.add_argument('--file',action="store",required=False,dest="file",type=str,help='Input filename eg:a.txt')
    args = parser.parse_args()
    xml = args.xml
    outpath = args.outfile
    if xml:
        excelfile,sheet = excelcsh()
        try:
            parsexml(xml,sheet)
        except FileExistsError as e:
            print("xml文件不存在")
            print(e)
        excelfile.save(outpath)
        print("文件保存至 %s" % outpath)
    else:
        print('Error args')
        print('eg: python3 pythonXml.py -x nmap.xml -o nmap.xls')
3. 怎么用

    因为每个人的习惯都不通,写的鬼东西自己感觉方便,别人看来可能都运行不起来,所以简单说明一下
a. 需要安装xlwt库,命令行中运行:    pip install xlwt
b. 需要用python3.x运行,python2的兄弟可以把 print() 改成 print ,同时把开头效验版本的if语句注释掉
c. python3 nmapxml2excel -x test.xml -o test.xls

四、补充

  1. nmap扫描端口和服务


    T
  2. 脚本过滤


    A
  3. excel内容


    S
  4. 批量结果处理


    S

相关文章

网友评论

      本文标题:Nmap输出过滤-xml转excel

      本文链接:https://www.haomeiwen.com/subject/vhdltctx.html