一個簡單的python腳本恢筝,將nmap輸出的xml格式提取為excel
一诞外、Nmap掃描
精準(zhǔn)打擊
????在信息收集階段鲸伴,一個重要的環(huán)節(jié)就是端口掃描府蔗,如果是一兩個目標(biāo)直接干就完了;但是如果是一堆IP地址段怎么辦汞窗,這時掃描結(jié)果呼啦一大片姓赤,隨便挑幾個深入,這樣顯得不夠?qū)I(yè)吧
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的參數(shù)有很多仲吏,不列舉了不铆,sS 使用SYN的快速掃描(速度快),sV 探測端口服務(wù)裹唆,-O 探測目標(biāo)操作系統(tǒng)
二誓斥、結(jié)果輸出
????添加-oX參數(shù)指定輸出為xml格式
1. nmap -sS 192.168.1.1 -T4 --open -n -O -oX test.xml
三、xml結(jié)果過濾
1. 關(guān)于xml文件解析
????python中解析xml有四種方法许帐,其實對于我們這些偶爾拿來吃雞的來說劳坑,隨便一個就行了
常用的xml.dom.minidom和xml.etree.ElementTree,其中xml.etree.ElementTree有一個C語言的實現(xiàn)舞吭,即xml.etree.cElementTree泡垃,聽說速度會快一點析珊。(python3.3+版本后,ElemenTree模塊會自動優(yōu)先使用C加速器蔑穴,如果不存在C實現(xiàn)忠寻,則會使用Python實現(xiàn))
2. 代碼實現(xiàn)
#coding:utf-8
import xml.etree.ElementTree as ET #解析xml,python3已經(jīng)默認(rèn)使用cElementTree
import xlwt #寫excel
import argparse #運行前參數(shù)
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() #獲取根節(jié)點
hosts = root.findall("host")
i = 0 #寫入excel的計數(shù)器
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")
#獲取系統(tǒng)版本的掃描結(jié)果
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結(jié)果中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: #其他協(xié)議
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結(jié)果')
#表格第一行
sheet1.write(0,0,'ip地址')
sheet1.write(0,1,'TCP端口')
sheet1.write(0,2,'其他協(xié)議端口')
sheet1.write(0,3,'系統(tǒng)版本')
sheet1.write(0,4,'系統(tǒng)掃描精準(zhǔn)度')
return ExcelFile,sheet1
def output(sheet,ip,tcpport,otherproto,num,osversion,accuracy):
#寫入數(shù)據(jù)
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. 怎么用
????因為每個人的習(xí)慣都不通存和,寫的鬼東西自己感覺方便奕剃,別人看來可能都運行不起來,所以簡單說明一下
a. 需要安裝xlwt庫捐腿,命令行中運行:????pip install xlwt
b. 需要用python3.x運行纵朋,python2的兄弟可以把 print() 改成 print ,同時把開頭效驗版本的if語句注釋掉
c. python3 nmapxml2excel -x test.xml -o test.xls
四茄袖、補充
-
nmap掃描端口和服務(wù)
T -
腳本過濾
A -
excel內(nèi)容
S -
批量結(jié)果處理
S