# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import urllib2,urllib
import re
import time,os,threading
from pyExcelerator import *
import Tkinter, Tkconstants, tkFileDialog
def Get_Bsobj(url):
'''
获取网页的bsobj对象
'''
req = urllib2.Request(url = url)
response = urllib2.urlopen(req)
bsObj=BeautifulSoup(response,"html.parser")
return bsObj
def Get_Path():
'''
获取当前目录下的html文件名
'''
try:
options={}
root = Tkinter.Tk()
root.attributes("-alpha", 0)#隐藏窗口
options['defaultextension'] = '.txt'
options['filetypes'] = [('html files', '.html')]
options['initialdir'] = os.getcwd()
options['title'] = u'请选择文件(可多选)'
options['parent'] = root
FileNames=tkFileDialog.askopenfilenames(**options)
root.destroy()
for path in FileNames:
PageUrl="file:///"+path
threading.Thread( target=GetUrl, args=(PageUrl,path.split("/")[-1]) ).start()
return FileNames
except Exception,e:
print e
return False
def GetUrl(PageUrl,FileName):
'''
获取具体内容
'''
try:
global mutex,VUL
bsObj=Get_Bsobj(PageUrl)
print len(bsObj.find("div",{"id":"ipList"}).findAll("tr",{"class":re.compile("v_7")}))+len(bsObj.find("div",{"id":"ipList"}).findAll("tr",{"class":re.compile("v_5")}))
#先获取漏洞名称的信息和解决方案
for bs in bsObj.find("div",{"id":"vulnDistribution"}).find("table",{"id":"vulDataTable"}).find("tbody").findAll("tr",{"onclick":"showTheIp(this);"}):
if(not bs.findAll("img")[-1]["src"]=="images/vl.gif"):#过滤绿色
VulName=bs.find("a").get_text()
bs=bs.find_next("tr")
Description=bs.find("table").findAll("tr")[1].findAll("td")[-1].get_text()
Solution=bs.find("table").findAll("tr")[2].findAll("td")[-1].get_text()
VUL.append([VulName,Description,Solution])
for url in bsObj.find("div",{"id":"ipList"}).findAll("tr",{"class":re.compile("v_7")}):
if(url.find("img",{"src":"images/fcwx.gif",})):
U=url.find("a",{"target":"_blank"})['href']
U=PageUrl.split("index")[0]+U
I=url.find("a",{"target":"_blank"}).get_text()
GetMessage(U,I,u"非常危险")
for url in bsObj.find("div",{"id":"ipList"}).findAll("tr",{"class":re.compile("v_5")}):
if(url.find("img",{"src":"images/bjwx.gif",})):
U=url.find("a",{"target":"_blank"})['href']
U=PageUrl.split("index")[0]+U
I=url.find("a",{"target":"_blank"}).get_text()
GetMessage(U,I,u"比较危险")
except Exception,e:
print e
def GetMessage(U,I,Lv):
bs=Get_Bsobj(U)
RiskValue=""
Content=[]
VuN=0
for Vul in bs.findAll("table",{"class":"cmn_table"})[1].findAll("li",{"class":"vh"}):
VulName=Vul.find("a").get_text()
Ob=Vul.find_parent().find_parent().find_parent()
Port=Ob.findAll("td")[0].get_text()
Protocol=Ob.findAll("td")[1].get_text()
Service=Ob.findAll("td")[2].get_text()
L=u"[高]"
Content.append([VulName,L,Port,Protocol,Service])
VuN=VuN+1
#time.sleep(123)
VuN=0
for Vul in bs.findAll("table",{"class":"cmn_table"})[1].findAll("li",{"class":"vm"}):
VulName=Vul.find("a").get_text()
Ob=Vul.find_parent().find_parent().find_parent()
Port=Ob.findAll("td")[0].get_text()
Protocol=Ob.findAll("td")[1].get_text()
Service=Ob.findAll("td")[2].get_text()
L=u"[中]"
Content.append([VulName,L,Port,Protocol,Service])
VuN=VuN+1
mutex.acquire(1)
SaveVulnerability(I,Content)
mutex.release()
def SaveVulnerability(I,Content):
'''
保存漏洞信息
'''
global VUL
try:
global mutex,AllNum,ws,TitStyle,BodyStyle
for Con in Content:
ws.write(AllNum,0,I,BodyStyle)
ws.write(AllNum,1,Con[0],BodyStyle)
ws.write(AllNum,2,Con[1],BodyStyle)
ws.write(AllNum,3,Con[2],BodyStyle)
ws.write(AllNum,4,Con[3],BodyStyle)
ws.write(AllNum,5,Con[4],BodyStyle)
for vul in VUL:
if(vul[0]==Con[0]):
ws.write(AllNum,6,vul[1],BodyStyle)
ws.write(AllNum,7,vul[2],BodyStyle)
AllNum=AllNum+1
ws.col(0).width=4000
ws.col(1).width=8000
ws.col(2).width=2000
ws.col(3).width=2000
ws.col(4).width=2000
ws.col(5).width=4000
ws.col(6).width=8000
ws.col(7).width=8000
print u"%15s 读取完毕"%(I)
except Exception,e:
print e
#print u"运行出错 关闭从新运行即可"
def Get_SaveFileName():
root = Tkinter.Tk()
root.attributes("-alpha", 0)#隐藏窗口
options={}
options['defaultextension'] = '.xls'
options['initialfile'] = 'OutFile'
options['filetypes'] = [('excel files', '.xls')]
options['initialdir'] = os.getcwd()
options['title'] = u'保存文件'
options['parent'] = root
try:
SaveFileName=tkFileDialog.asksaveasfilename(**options)
root.destroy()
return SaveFileName
except:
print SaveFileName
print u"未选择保存文件"
return False
if __name__=="__main__":
VUL=[]
mutex = threading.Lock()
AllNum=1
print u" --欢迎来到RSR漏洞信息导出系统--"
w = Workbook() #创建一个工作簿
ws = w.add_sheet("111") #创建一个工作表
title=[u"IP地址",u"漏洞名称",u"漏洞危险程度",u"端口",u"协议",u"服务",u"漏洞描述",u"解决方案"]
#设置头部属性
TitStyle=XFStyle()
TitStyle.font=Font()
TitStyle.font.bold=True
TitStyle.font.name=u"等线"
TitStyle.pattern=Pattern()
TitStyle.pattern.pattern=2
TitStyle.pattern.pattern_fore_colour="gray"
TitStyle.pattern.pattern_back_colour="gray"
TitStyle.alignment=Alignment()
TitStyle.alignment.horz = Alignment.HORZ_CENTER
TitStyle.alignment.vert = Alignment.VERT_CENTER
for tit in xrange(0,len(title)):
ws.write(0,tit,title[tit],TitStyle)
#设置body属性
BodyStyle=XFStyle()
BodyStyle.font=Font()
BodyStyle.alignment=Alignment()
BodyStyle.alignment.horz = Alignment.HORZ_CENTER
BodyStyle.alignment.vert = Alignment.VERT_CENTER
BodyStyle.alignment.wrap=Alignment.WRAP_AT_RIGHT
Great=Get_Path()
while(len(threading.enumerate())>2):
time.sleep(1)
if(Great):
SaveFileName=Get_SaveFileName()
if(SaveFileName):
w.save(SaveFileName) #保存
else:
SaveFileName=str(time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time() ) ))+".xls"
w.save(SaveFileName)
print u"您未命名文件,默认保存为",SaveFileName
print u"All End!!!"
else:
print U"您未选择要操作的html文件"
print u"--->>>系统将在10s后关闭"
time.sleep(10)
exit()