優化中...php
#! /usr/bin/env python # -*- coding:utf-8 -*- # Author: Tdcqma ''' v17.0920.1401 基本功能實現,漏洞標題與漏洞連接優化 v17.0922.1800 已實現對【Cisco 漏洞告警】提供受影響的版本信息 v17.0922.1913 優化代碼,對版本v17.0922.1800實現函數化 ''' import urllib.request import ssl,re import smtplib,email import datetime f = open("secInfo-lvmeng.txt", 'w', encoding='utf-8') #today = str(datetime.date.today()) # 指定以當前時間爲搜索條件 today = "2017-09-20" # 臨時指定測試時間 # 生產列表保存全部安全漏洞標題 tomcat_sec = ["Apache Tomcat 漏洞告警(當前生產版本爲7.0.68)\n\n"] Cisco_sec = ["Cisco 漏洞告警(當前生產版本爲1.0.35)\n\n"] WebLogic_sec = ["WebLogic 漏洞告警(當前生產版本爲10.33)\n\n"] Microsoft_sec = ["Microsoft 漏洞告警(當前生產版本爲windows2012)\n\n"] Noinfo_sec = "本日無爬蟲安全告警.\n\n" tomcat_msg = ''' ''' WebLogic_msg = ''' ''' Cisco_msg = ''' ''' Microsoft_msg = ''' ''' count = 0 # 計算告警漏洞總數 str_domain = "http://www.nsfocus.net" msg_fl = "" newline = "" def get_infected_vision(info_sec,info_msg,sub_url): line = " ♠ " + today + " " + title + "\n >> " + sub_url + '\n' info_msg += line # 逐行讀取,將其保存到msg字符變量裏。 info_sec.append(line) global count count += 1 # 進入漏洞詳細頁面,調取受影響的漏洞版本 vul_request = urllib.request.Request(sub_url) vul_response = urllib.request.urlopen(vul_request) vul_data = vul_response.read().decode('utf-8') # 正則表達式匹配受影響的版本 affected_version = re.findall("<blockquote>.*</blockquote>", vul_data, re.S) affected_version = " 受影響的版本:" + affected_version[0][12:-13], '\n' for newline in affected_version: newline = newline.replace('<', '<') info_sec.append(newline + '\n') for i in range(5): #指定被掃描網站須要掃描的網頁數範圍,默認讀取1-10頁,即一天的漏洞個數可能要佔用好幾個頁面 url = "http://www.nsfocus.net/index.php?act=sec_bug&type_id=&os=&keyword=&page=%s" % (i+1) request = urllib.request.Request(url) # 當嘗試訪問https開始當站點時,設置全局取消SSL證書驗證 ssl._create_default_https_context = ssl._create_unverified_context response = urllib.request.urlopen(request) data = response.read().decode('utf-8') if today in data: # 用於匹配內容的正則表達式部分 str_re = "<.*" + today + ".*" res = re.findall(str_re, data) for line in res: title_craw = re.findall("/vulndb/\d+.*</a>", line) # 獲取標題 title = title_craw[0][15:-4] url_craw = re.findall("/vulndb/\d+", line) # 獲取連接 sub_url = str_domain + url_craw[0] if "Apache Tomcat" in title: get_infected_vision(tomcat_sec,tomcat_msg,sub_url) elif "WebLogic" in title: get_infected_vision(WebLogic_sec,WebLogic_msg,sub_url) elif "Cisco" in title: get_infected_vision(Cisco_sec,Cisco_msg,sub_url) # 獲取漏洞的受影響版本 elif "Microsoft" in title: get_infected_vision(Microsoft_sec,Microsoft_msg,sub_url) msg_fl = [tomcat_sec, WebLogic_sec, Cisco_sec,Microsoft_sec] secu_msg = ''' ''' for i in range(len(msg_fl)): if len(msg_fl[i]) > 1: for j in range(len(msg_fl[i])): secu_msg += msg_fl[i][j] msg_fl = secu_msg if count == 0 : msg_fl += Noinfo_sec msg_fl += ("漏洞告警總數:" + str(count)) else: msg_fl += ("漏洞告警總數:" + str(count)) f.writelines(msg_fl) f.close() # print(msg_fl) # 發送郵件 chst = email.charset.Charset(input_charset = 'utf-8') header = ("From: %s\nTo: %s\nSubject: %s\n\n" % ("from_mail@163.com", "to_mail@163.com", chst.header_encode("[爬蟲安全通告-綠盟]"))) # 借用163smtp服務器發送郵件,將上面讀到的報警信息做爲郵件正文發送。 email_con = header.encode('utf-8') + msg_fl.encode('utf-8') smtp = smtplib.SMTP("smtp.163.com") smtp.login("from_mail@163.com","from_mail_password") smtp.sendmail('from_mail@163.com','to_mail@163.com',email_con) print('mail send success!') smtp.quit()
爬蟲郵件告警截圖:python