本文主要是介绍nsfocus远程安全评估系统报告提取信息脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# coding=utf-8
from bs4 import BeautifulSoup
import xlwt
"""
采集移动漏扫报中主机端口,banner信息和漏洞信息
python2.7
"""
ip_port = []
ip_alarm = []
#获取主机
def get_host_list(filename):
ips = []
with open(filename + "\\index.html", 'rb') as data:
soup = BeautifulSoup(data, 'html.parser')
div = soup.find_all("div", {"name": "ipList"})[0]
trs = div.find_all("a")
# print trs
for tr in trs:
ip = tr.string
# print ip
ips.append(ip)
return ips
#读取并保存到excel
def read_info(filename, ips):
global ip_port,ip_alarm
result = filename + "\\result.xls"
work = xlwt.Workbook()
#表一
sheet = work.add_sheet("ports", cell_overwrite_ok=True)
row = ['IP', u'端口',u'协议',u'服务',u'状态','Banner']
#表二
sheet1 = work.add_sheet("alarm", cell_overwrite_ok=True)
row1 = ['IP', u'端口',u'漏洞名称',u'级别']
for i in range(0, len(row)):
sheet.write(0, i, row[i])
for ip in ips:
(ip_port_temp , ip_alarm_temp) = write_port_banner_result(filename, ip)
ip_port = ip_port + ip_port_temp
ip_alarm = ip_alarm + ip_alarm_temp
print "------------------start print ip >>port and banner ---------------------------"
i = 1
k = len(ip_port)
print k
for key in range(0, k):
sheet.write(i, 0, ip_port[key][0])
sheet.write(i, 1, ip_port[key][1])
sheet.write(i, 2, ip_port[key][2])
sheet.write(i, 3, ip_port[key][3])
sheet.write(i, 4, ip_port[key][4])
sheet.write(i, 5, ip_port[key][5])
i += 1
for i in range(0, len(row1)):
sheet1.write(0, i, row1[i])
j = 1
k1 = len(ip_alarm)
print k1
for key in range(0, k1):
sheet1.write(j, 0, ip_alarm[key][0])
sheet1.write(j, 1, ip_alarm[key][1])
sheet1.write(j, 2, ip_alarm[key][2])
sheet1.write(j, 3, ip_alarm[key][3])
j += 1
work.save(result)
def write_port_banner_result(filename, ip):
global ip_port
# print(ip)
i = 0
banner_name = ""
temp = []
port_s = []
banner_s = []
alarm_s = []
ips = []
with open(filename + "\\hosts\\4816-" + ip + ".html", 'rb') as data:
soup = BeautifulSoup(data, "html.parser")
ths1 = soup.find(text="状态")
# print ths
if ths1 == u"状态":
# print ip + ": start get net"
ths1 = soup.find(text="状态").parent.parent.parent.find_all("td")
for k in range(len(ths1)/4):
temp = [ip,ths1[0+k*4].string,ths1[1+k*4].string,ths1[2+k*4].string,ths1[3+k*4].string,'']
port_s.append(temp)
# print port_s
else:
temp = [ip,"no find service","","","",""]
port_s.append(temp)
# print ip + ": stop get net"
temp = []
ths2 = soup.find(text="Banner")
if ths2 == u"Banner":
# print ip + ": start get Banner"
ths2 = soup.find(text = "Banner").parent.parent.parent.find_all("td")
for k in range(len(ths2)/2):
temp = [ip,ths2[0+k*2].string,ths2[1+k*2].string]
banner_s.append(temp)
# print banner_s
for k in range(len(banner_s)):
for j in range(len(port_s)):
if banner_s[k][1] == port_s[j][1]:
port_s[j][5] = banner_s[k][2]
# print ip + ": stop get Banner"
print "-------------print : "+ ip + "----port and banner ---------------------"
print port_s
print "-------------print : "+ ip + "----alarm ---------------------"
ths3 = soup.find("th",text="漏洞").parent.parent.find_all("tr")
if len(ths3)>3 :
for i in range(3,len(ths3)):
tr1 = ths3[i].find_all("td")
port_num = tr1[0].string
li1 = tr1[3].find_all("li")
for j in range(len(li1)):
# print li1[j].find("a").string
print li1[j].find("a").get("class")[0]
temp = [ip,port_num,li1[j].find("a").string,li1[j].find("a").get("class")[0]]
alarm_s.append(temp)
# print temp
print alarm_s
return port_s,alarm_s
#filename = "C:\Users\LY\Desktop\\2" #路径
filename="E:\\reports"
ips = get_host_list(filename)
# print ips
ips = ["117.169.64.140","117.169.64.142","211.138.211.66"]
# ips = "59.53.51.178"
#
read_info(filename, ips)#第一步任务
# write_port_banner_result(filename,ips)
这篇关于nsfocus远程安全评估系统报告提取信息脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!