用法參考
http://www.reibang.com/p/3e49ff2ffa24
我的github:
https://github.com/ucstone/codelib
#!/usr/bin/env python
# -*-encoding: utf-8 -*-
__author__ = 'M1k3'
import argparse
import sys
import os
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
reload(__import__('sys')).setdefaultencoding('utf-8')
# 提取nmap掃描結(jié)果文件
def deal_nmap_xml(xml_name, save_name):
tree = ET.parse(xml_name)
# res_file = open(save_name + '-all.txt', 'w') #生成過程文件
root = tree.getroot()
count = [] # 統(tǒng)計(jì)存活I(lǐng)P地址數(shù)量
IP_ALL = [] # 開放端口總數(shù)
IP = ''
for child in root:
if child.tag == 'host': # 查找host標(biāo)簽
for xx in child:
for state in xx.iter('status'):
if state.attrib.get('state') == "up": # 判斷主機(jī)是否存活
for ports in child:
for neighbor in ports.iter('address'): # 提取主機(jī)IP地址
IP = neighbor.attrib['addr']
count.append(IP)
for port in ports: # 端口信息 ports標(biāo)簽
for neighbor in port.iter('state'):
# print neighbor.attrib['state']
if neighbor.attrib['state'] == "open": # 判斷端口是否開放
if port.attrib.has_key('portid'):
# print IP + ":" + port.attrib.get('portid')
lll = IP + ":" + port.attrib.get('portid') + '\n'
# res_file.write(lll)
IP_ALL.append(lll)
print "There are %d surviving!" % len(count)
print "Open port has: %d" % len(IP_ALL)
# res_file.close()
return IP_ALL
# 根據(jù)端口提取相應(yīng)的IP
def get_ip(ALL, ports):
port_file = []
for port in ports.split(','):
for line in ALL:
if len(line.split(':'))>2:
print line.strip()+" Exception"
if port == line.split(':')[1].strip():
# print line,
port_file.append(line)
print "Port extraction is complete!"
return port_file
# 比較新發(fā)現(xiàn)資產(chǎn)和合規(guī)平臺(tái)資產(chǎn)
def nmap_platform_cmp(file, pfile):
f1 = file # sys.argv[1] # 新發(fā)現(xiàn)資產(chǎn)比對(duì)
f2 = pfile # sys.argv[2] # 合規(guī)平臺(tái)資產(chǎn)導(dǎo)出的全部資產(chǎn)义钉,整理到一個(gè)txt文檔中
os.chdir(f1)
file_list = []
if os.path.exists('res'):
pass
else:
os.mkdir('res')
for txt_file in os.listdir('.'):
if txt_file.endswith('.txt'):
file_list.append(txt_file)
for txt_file in file_list:
result = []
if txt_file.endswith('.txt'):
with open(txt_file, 'r') as xx_file: # 打開新掃描的資產(chǎn)
xx_ip = xx_file.readlines()
print u"Before being removed", len(xx_ip)
with open(f2, 'r') as yy_file: # 打開合規(guī)平臺(tái)導(dǎo)出的資產(chǎn)
yy_ip = yy_file.readlines()
for xx in xx_ip:
if xx in yy_ip: # 若新掃描的資產(chǎn)在合規(guī)平臺(tái)中,則說明已經(jīng)上報(bào)并剔除
result.append(xx)
xx_ip.remove(xx) # 刪除已經(jīng)報(bào)備過的IP
# print xx_ip
print u"After removal", len(xx_ip)
# 將新發(fā)現(xiàn)的資產(chǎn)提取出來
with open(f1 + os.path.sep + 'res' + os.path.sep + txt_file.split('-')[0] + '-res.txt',
'w+') as zz_file:
for ip in xx_ip:
zz_file.writelines(ip)
print u"Has been reported:%d" % len(result)
with open(txt_file, 'r') as xx:
# xx.readlines()
print u"%s 未報(bào)備資產(chǎn) %s 個(gè)" % (txt_file, str(len(xx.readlines()) - len(result)))
print 15 * '###'
if __name__ == '__main__':
# 接受cmd參數(shù)
parser = argparse.ArgumentParser()
group1 = parser.add_argument_group(u'處理nmap掃描文件')
group1.add_argument("-xpath", type=str, help=u"批量轉(zhuǎn)換,輸入nmap掃描結(jié)果xml文件所在的目錄")
group1.add_argument("-xml", type=str, help=u"轉(zhuǎn)換單個(gè)xml文件,輸入nmap掃描結(jié)果xml文件")
group1.add_argument("-port", type=str, default='21', help=u'要提取的端口;提取多個(gè)的格式為"21,80,445"')
group2 = parser.add_argument_group(u'與合規(guī)平臺(tái)文件比對(duì)')
group2.add_argument("-pfile", type=str, help=u"合規(guī)平臺(tái)已經(jīng)上報(bào)的資產(chǎn)")
# parser.add_argument("-tfile", type=str, help=u'get ips or domains for this file')
group2.add_argument("-tpath", type=str, help=u"新掃描的資產(chǎn)文件码耐,只接受目錄")
args = parser.parse_args()
# nmap掃描結(jié)果處理相關(guān)參數(shù)
xml_path = args.xpath
xml_file = args.xml
ports = args.port
# 合規(guī)平臺(tái)資產(chǎn)比較相關(guān)參數(shù)
platform_file = args.pfile
# txt_file = args.tfile
txt_path = args.tpath
if xml_file and ports:
save_name = xml_file.split('.')[0]
# print save_name
print 'Began to extract %s open port IP' % xml_file
IP_ALL = deal_nmap_xml(xml_file, save_name)
get_ip(IP_ALL, ports)
if xml_path and ports:
os.chdir(xml_path)
ALL = []
for i in os.listdir(xml_path.strip()):
if i.endswith('.xml'):
xml_path_file = xml_path + os.path.sep + i
save_name = i.split('.')[0]
# print save_name
print 'Began to extract %s open port IP' % i
IP_ALL = deal_nmap_xml(xml_path_file, save_name)
ALL = ALL+IP_ALL
with open('port-res.txt','w+') as xx:
for ip in get_ip(ALL, ports):
xx.write(ip)
# print ip,
if txt_path and platform_file:
try:
if os.path.isfile(txt_path):
print u"請(qǐng)輸入要處理文件的目錄"
else:
nmap_platform_cmp(txt_path, platform_file)
except:
print u"請(qǐng)輸入要處理文件所在目錄"
else:
pass