美文网首页
nmap扫描文件提取工具

nmap扫描文件提取工具

作者: 是Jonathan | 来源:发表于2017-05-24 14:38 被阅读131次
    # coding: utf-8
    
    import os
    import sys
    try:
        import xml.etree.cElementTree as ET
    except ImportError:
        import xml.etree.ElementTree as ET
    reload(__import__('sys')).setdefaultencoding('utf-8')
    
    '''
    nmap -Pn --open -n -vv -F -oX d:\\xxx.xml 10.129.246.1/24
    nmap -n -vv --open -oX d:\\vm.xml 192.168.106.1/24
    --open这个参数必须加
    nmap -Pn -n -vv -p80 -oX d:\\xxx.xml 10.129.246.1/24 检测单一端口的就不用执行端口提了
    '''
    
    
    def deal_nmap_xml(xml_name, save_name):
        try:
            root = ET.fromstring(xml_name)
            ip_list = []
        except:
            print u"请检查nmap扫描文件标签是否完整"
            sys.exit()
        res = open(save_name + '.txt', 'w')  # 轻易不要改动
        for host in root.findall('host'):
            if len(host) > 3:
                pass
                #print '\n', str(len(host)),  # 打印host标签中子元素个数
            if host[0].get('state') == "up":  # 判断IP是否存活
                ip = host[1].get('addr')  # 提取IP地址
                #print ip,
                ip_list.append(ip)  # 验证存活IP个数
                ip_ = '\n' + ip + '\t'
                res.writelines(ip_)
                # 提取端口
                if len(host) == 6:
                    for port in host[4][1:]:  # 若确认端口开放,但没有提取出端口请修改host[4][此处+1试试:],加一后为host[4][2:],下面的几处方法一样
                        #print port.get('portid'),
                        port_ = str(port.get('portid')) + ','
                        res.write(port_)
                elif len(host) == 5:
                    for port in host[3][2:]:
                        # print port.tag,
                        #print port.get('portid'),
                        port_ = str(port.get('portid')) + ','
                        res.write(port_)
                elif len(host) == 4:
                    for port in host[3][1:]:
                        #print port.get('portid'),
                        port_ = str(port.get('portid')) + ','
                        res.write(port_)
                elif len(host) < 4:
                    print host[0].get('state')
        res.close()
        print 'Alive IP Total:{} '.format(len(ip_list))
    
    #根据端口提取相应的IP
    def get_ip(save_name, port):
        with open(save_name+'.txt', 'r') as xxx:
            port_info = xxx.readlines()
            res_name = save_name+'-out'
            with open(res_name + '.txt', 'w+') as save_file:
                for x in port.split(','):
                    for line_info in port_info:
                        try:
                            if x in line_info.split('\t')[1].split(','):
                                line =line_info.split('\t')[0] +":"+x+ '\n'
                                #print line,
                                save_file.writelines(line)
                        except:
                            pass
        print "Done!"
    
    def main(path, port):
        if os.path.isfile(path):
            save_name = path.split('.')[0]
            print save_name
            bd = open(path, 'r').read()  # nmap的扫描结果文件
            print 'Start extracting IP&Port'
            deal_nmap_xml(bd, save_name)
            get_ip(save_name, port)        
        else:    
            for i in os.listdir(path.strip()):
                if i.endswith('.xml'):
                    #print i
                    xml_path = path + os.path.sep + i
                    save_name = i.split('.')[0]
                    print save_name
                    bd = open(xml_path, 'r').read()  # nmap的扫描结果文件
                    print 'Start extracting IP&Port'
                    deal_nmap_xml(bd, save_name)
                    get_ip(save_name, port)
    
    
    
    if __name__ == "__main__":
        #批量提取
        #123.py "C:\Users\Desktop\portscan\xml" 21
        #123.py "C:\Users\Desktop\portscan\xml" "21,22,23"
        
        #单个文件提取,将nmap扫描文件放在123.py同一目录
        #123.py nmap.xml 21
        #123.py nmap.xml "21,22,23"    
        
       
        
        path = sys.argv[1] #1.若是单个文件,需输入完整路径(文件和此脚本在同一目录可不写路径) 2.可以输入xml文件所在目录
        port = sys.argv[2] #提取的端口,多个使用','逗号隔开。如:"21,22,23,25,110"
        main(path, port)    
    
    

    批量提取单个端口

    123.py "C:\Users\^__^\Desktop\portscan\xml" 21

    图片.png

    批量提取多个端口

    123.py "C:\Users\^__^\Desktop\portscan\xml" "21,22,23,1521"

    图片.png

    单个文件提取多个端口
    nmap.xml文件需与123.py在同一目录下,
    123.py "nmap.xml" "21,22,23,1433,"
    若不在则如下:
    123.py ""C:\Users\^__^\Desktop\portscan\nmap.xml" "21,22,23,1433,"
    提取单个端口的也同样。

    图片.png

    单个文件提取单个端口

    123.py "nmap.xml" 21

    图片.png

    相关文章

      网友评论

          本文标题:nmap扫描文件提取工具

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