在乙方使用天镜进行众多漏洞扫描,其默认导出格式为HTML,因为工作需要将其发现漏洞全部贴到Excel表格中,动不动就是成百上千的漏洞,任务量吓人。于是写一脚本,减少工作量。
Code:
# -*- coding: utf-8 -*-
# ******************************************************
# Author : Nt
# Last modified: 2016-12-16 16:39
# Email : [email protected]
# Filename : html2excel.py
# Version : 1.0
# Description : 方便导出天镜漏洞报告中的漏洞列表。
# Example : html2excel.py /Report/files/Report_main.html outFileName.xls
# ******************************************************
import re
import sys
import xlwt
from bs4 import BeautifulSoup
excelTXT = [] # 存放所有的excel待写入内容
vbName = '' # 漏洞名字
vbType = '' # 漏洞类型
vbLevel = '' # 危险级别
vbCVE = '' # CVE编号
vbIP = '' # 主机IP
vbDesc = '' # 详细描述
vbRepair = '' # 修复建议
exp1 = re.compile("(?isu)]*>(.*?) ") # 正则寻找tr标签
exp2 = re.compile("(?isu)]*>(.*?)") # 正则寻找div标签
exp3 = re.compile("(?isu)]*>(.*?)") # 正则寻找a标签,因为CVE是a标签.比较特殊
htmlSource = open(sys.argv[1]).read() # 读入html文件,天镜目录为: files/Report_main.html
soup = BeautifulSoup(htmlSource,'html.parser')
htmlSource = str(soup.find(id='section_13_content')) # 通过寻找网页中ID=section_13_content这个代码块。如果更新,就在此处
splitTxt = '' # 正则内容放置此处
for row in exp1.findall(htmlSource): # 正则全文,分别为exp1、exp2、exp3
splitTxt += '===============\n'; # 分隔符,方便后续确认多行内容。
for col in exp2.findall(row):
splitTxt += col.strip().replace('
','').replace('
','')+'\n' # 正则结果追加至splitTxt
for col in exp3.findall(row):
splitTxt += col.strip().replace('
', '').replace('
','')+'\n' # 正则结果追加至splitTxt
arrTxt = splitTxt.split("===============") # 以分隔符分割文本为数组。
for line in arrTxt: #
splitTxt = line.split('\n') #分割每一行
for spLine in splitTxt: # 循环每一个数组成员
if (re.match(r'[【/d】]', spLine)):
num = str(re.findall('【(.*)】',splitTxt[1])).replace('[','').replace('\'','').replace(']','') # 取出当前是第几个漏洞
cve = str(re.findall('\(.*\)',splitTxt[1])).replace('[','').replace('\'','').replace(']','') # 取出标题中可能带有的的CVE编号
cve2 = str(re.findall('\(.*\)', splitTxt[1])).replace('[','').replace('\'','').replace(']','') # 中文括号,上面的是英文括号
vbName = splitTxt[1].replace('【' + num + '】', '').replace(cve, '').replace(cve2, '') # 替换【1】(cve-000-000)这种特殊字符
if (spLine == '漏洞类型'):
vbType = splitTxt[2].replace(' ','').strip()
if (spLine == '危险级别'):
vbLevel = splitTxt[2].replace(' ','').strip()
if (spLine == 'CVE编号'):
vbCVE = splitTxt[2].replace(' ','').strip()
if (spLine == '存在主机'):
vbIP = splitTxt[2].replace(' ','').strip()
if (spLine == '详细描述'):
vbDesc = splitTxt[2].replace(' ','').strip()
if (spLine == '修补建议'):
vbRepairLen = len(splitTxt)
tmpStr = ''
for i in range(vbRepairLen-1):
if (i>2):
tmpStr+=splitTxt[i]+'\n'
vbRepair = tmpStr.replace(' ','').strip()
#最后一个数据获取完,提交至excel
excelTXT.append(vbIP+'/**/'+vbType+'/**/'+vbLevel+'/**/'+vbName+'/**/'+vbCVE+'/**/'+vbDesc+'/**/'+vbRepair)
#创建workbook和sheet对象
workbook = xlwt.Workbook() #注意Workbook的开头W要大写:encoding='utf-8',style_compression=0
sheet1 = workbook.add_sheet('sheet1',cell_overwrite_ok=True)
sheet2 = workbook.add_sheet('sheet2',cell_overwrite_ok=True)
for i in range(len(excelTXT)):
splitRes = excelTXT[i].split('/**/')
# 向sheet页中写入数据
sheet1.write(i, 0, str(i+1).decode('utf-8'))
sheet1.write(i, 1, splitRes[0].decode('utf-8'))
sheet1.write(i, 2, splitRes[1].decode('utf-8'))
sheet1.write(i, 3, splitRes[2].decode('utf-8'))
sheet1.write(i, 4, splitRes[3].decode('utf-8'))
sheet1.write(i, 5, splitRes[4].decode('utf-8'))
sheet1.write(i, 6, splitRes[5].decode('utf-8'))
sheet1.write(i, 7, splitRes[6].decode('utf-8'))
#保存该excel文件,有同名文件时直接覆盖
workbook.save(sys.argv[2])
print '报告导出成功。'
使用帮助:
html2excel.py HTML文件 输出Excel文件
html2excel.py /Report/files/Report_main.html outFileName.xls
因为该版本是初次开发,可能会存在部分BUG,欢迎交流,指出不足:)