nmap掃描檔案提取工具

weixin_34148340發表於2017-05-24
# 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

3094537-9b5a358dfb66b992.png
圖片.png

批量提取多個埠

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

3094537-f40d2acebc04dd3d.png
圖片.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,"
提取單個埠的也同樣。

3094537-5105ff93473e1c7f.png
圖片.png

單個檔案提取單個埠

123.py "nmap.xml" 21

3094537-201713081aee12ee.png
圖片.png

相關文章