Nmap結合vulscan和nmap-vulner掃描結果解析
工作需求
工作過程中遇到高危端口掃描的需求,客戶要求對目標資產進行全端口掃描,并將開放的端口及該端口可能存在的CVE漏洞進行格式化輸出。由于目標較多,且客戶無POC探測需求。
故使用nmap+vulscan+nmap-vulner+python腳本,基于軟件版本進行掃描并編寫python腳本將xml文檔解析為excel格式輸出
基本知識-nmap常用參數
復習一下nmap常見參數
-sS SYN半連接掃描,需root權限
-sT TCP三次握手掃描,較慢,不建議使用
-sV 探測端口服務banner信息
-Pn 不進行主機存活掃描,直接掃描指定端口
-v 在shell界面顯示當前掃描過程
-oN/oX/oG 將nmap掃描結果輸出為正常/XML/grep格式,建議格式為xml,掃描結果更完整,更易解析。
-iL file 讀取文本內容作為掃描目標
--script xxx 使用內置腳本進行掃描
NSE內置腳本下載安裝
nmap-vulner和vulscan都是為了增強Nmap的版本檢測,為特定服務(如SSH,RDP,SMB等)生成相關的CVE信息。
nmap-vulner腳本下載
進入到nmap內置腳本目錄下
cd /usr/share/nmap/script/
下載腳本
git clone https://github.com/vulnersCom/nmap-vulners.git
更新腳本庫
nmap --script-updatedb
vulscan腳本下載
進入到nmap內置腳本目錄下
cd /usr/share/nmap/script/
下載腳本
git clone https://github.com/scipag/vulscan.git
更新漏洞庫
cd vulscan/utilities/updater/
chmod+x updateFiles.sh
掃描
命令:
結合vulscan和nmap-vulners兩個腳本進行掃描
nmap --script nmap-vulners/ --script-args vulscandb=scriptvuldb.csv -sV -Pn 10.211.55.7 10.211.55.3 -v -p1-65535 -T4 --oX nmap.xml

XML解析
Python解析腳本如下:
# -*- coding: UTF-8 -*-
# @Time :2021/7/5 10:08 上午
# @File :nmap_parser.py
import xml.dom.minidom as xmldom
import openpyxl
firstline_list=["IP地址","開放端口","應用軟件","軟件版本","CVE漏洞編號"]
info_list=[]
with open("nmapxml.xml", "r", encoding="utf-8") as f:
dom = xmldom.parse(f) # 讀取xml文件
root = dom.documentElement # 獲得xml文件中的元素對象
a = root.childNodes # 獲得所有子節點
# def ip_info():
# global ip_list
# for i1 in a:
# if i1.nodeName=="hosthint":
# ip=i1.getElementsByTagName("address")
# for i2 in ip:
# if i2.getAttribute("addrtype") == "ipv4":
# host=i2.getAttribute("addr")
# ip_list.append(host)
# else:
# pass
def port_info():
host=root.getElementsByTagName("host")
for i1 in host:
a=i1.childNodes
for i2 in a:
if i2.nodeName=="address":
if i2.getAttribute("addrtype")=="ipv4":
# ip_list.append((i2.getAttribute("addr")))
host=i2.getAttribute("addr")
# ip_list.append(host)
# print("存活主機:",host) #輸出ip地址
if i2.nodeName=="ports":
# print(host)
b=i2.childNodes
for i3 in b:
x=0
x=x+1
if i3.nodeName=="port":
c=i3.childNodes
for i4 in c:
if i4.nodeName=="service":
# print("port")
#有幾個開放端口就有幾個ip地址
# sheet.cell(row=1, column=i + 1).value = i3.getAttribute("portid")
# port_info=i3.getAttribute("portid")+i4.getAttribute("product")+i4.getAttribute("version")
# port_list.append(i3.getAttribute("portid"))
# version_list.append(i4.getAttribute("product"))
# version_list.append(i4.getAttribute("version"))
portid=i3.getAttribute("portid")
a=i4.getAttribute("product")
product=a.replace(" ","")
version=i4.getAttribute("version")
info=host+" "+portid+" "+product+" "+version
info_list.append(info)
print("存活主機:",host,"開放端口:",portid,"banner:",product,version)#輸出端口banner
if i4.nodeName=="script":
d=i4.childNodes
for i5 in d:
if i5.nodeName =="table":
e=i5.childNodes
for i6 in e:
if i6.nodeName == "table":
f=i6.childNodes
cve=' '
for i7 in f:
if i7.nodeName == "elem" and i7.getAttribute("key") == "id" and str(i7.firstChild.data).startswith("CVE"):
# name1 = str(i7.firstChild.data)
# # print(name1)
# if name1.startswith("CVE"):
# vul_info=name1
cve=i7.firstChild.data
# vul_list.append(i7.firstChild.data)
# print("CVE編號:",cve)
info = host + " " + portid + " " + product + " " + version+" "+cve
info_list.append(info)
def data2excel():
book = openpyxl.Workbook()
sheet = book.active
index=0
for i in range(len(firstline_list)):
sheet.cell(row=1,column=i+1).value=firstline_list[i]
for i1 in info_list:
index +=1
listA=i1.split(" ",6)
sheet.append(listA)
book.save('nmap2excel.xlsx')
if __name__ == '__main__':
port_info()
data2excel()
解析結果如下:

xml2excel輸出如下:
