【原创分享】python获取乌云最新提交的漏洞,邮件发送

时间:2022-10-14 18:30:11
 #!/usr/bin/env python
# coding:utf-8
# @Date : 2016年4月21日 15:08:44
# @Author : sevck (sevck@jdsec.com)
# @Link : http://www.qingteng.cn
#------------------------------------------------------------------------- import time
import urllib2
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
import smtplib #===============================================================================
# 导入smtplib和MIMEText
#===============================================================================
from email.mime.text import MIMEText
import smtplib #===============================================================================
# 要发给谁,这里指定发送的邮箱地址(支持多个逗号间隔)
#===============================================================================
mailto_list=["sevck@jdsec.com"] #===============================================================================
# 设置服务器,用户名、口令以及邮箱的后缀
#===============================================================================
mail_host="smtp.qq.com"
mail_user=""
mail_pass=""
mail_postfix="qq.com"
mail_usernnick="XX播报平台" #===============================================================================
#定义厂商列表:
#务必填写在乌云厂商名字:比如360在乌云的名称是:奇虎360
#===============================================================================
list=["乌云厂商名称"]
buglist=[]
#===============================================================================
#获取当天时间
#===============================================================================
def gettime():
data=time.strftime('%Y-%m-%d',time.localtime(time.time()))
return data
#================================================================================
#获取html代码
#================================================================================
def getUrlRespHtmlSimply(url):
req = urllib2.Request(url)
res = urllib2.urlopen(req)
html = res.read()
res.close()
return html
#================================================================================
#获取html代码
#================================================================================
def getUrlRespHtml(url):
heads = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset':'GB2312,utf-8;q=0.7,*;q=0.7',
'Accept-Language':'zh-cn,zh;q=0.5',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Keep-Alive':'',
'User-Agent':'Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.14) Gecko/20110221 Ubuntu/10.10 (maverick) Firefox/3.6.14'} opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
req = urllib2.Request(url)
opener.addheaders = heads.items()
respHtml = opener.open(req).read()
return respHtml
#================================================================================
#处理爬虫数据
#================================================================================
def getwooyun():
for k in range(1,5):
url="http://www.wooyun.org/bugs/new_submit/page/"+str(k)
html=getUrlRespHtml(url).decode('utf-8')
#print html
#print "-----------------------------------"
soup=BeautifulSoup(html)
#print soup
tbody=soup.find("tbody")
#print tbody
tr=tbody.findAll("tr")
times=gettime()
for i in tr:
submittime=i.find("th").contents[0]
#print submittime
if times==submittime:
#print "ok"
title=i.find("td").find("a").contents[0]#漏洞标题
href="www.wooyun.org"+i.find("td").find("a").get("href")#漏洞地址
buglist.append(title)
buglist.append(href)
else:
#print "pass"
pass
#===============================================================================
#获取漏洞信息
#==============================================================================
con=""
def outbug():
for j in buglist:
global con
con+=str(j)+"\r\n"
print con #===============================================================================
# 发送邮件
#===============================================================================
def send_mail(to_list,sub,content):
'''''
to_list:发给谁
sub:主题
content:内容
send_mail("aaa@126.com","sub","content")
'''
me=mail_usernnick+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_charset="utf-8")
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP_SSL()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False def main():
date=gettime()
print con
getwooyun()
outbug()
if con!="":
if send_mail(mailto_list,"【WOOYUN-"+date+"-漏洞信息】",con.encode("utf-8")):
print "发送成功"
else:
print "发送失败" for i in list:
print i
if(i in con):
print "true"
print i
send_mail(mailto_list,"[紧急]【您关注的客户被爆漏洞了】",
"客户为:"+i.encode("utf-8")+"\r\n"+"\r\n"+
"详情请看如下:\r\n"+
"\r\n----------------------------\r\n"+
con.encode("utf-8"))
else:
print "false"
if __name__ == '__main__':
main()

python要改成支持ssl,不知道怎么改可以百度或者我的博客之前好像也写过。

linux下运行 crontab定时执行脚本即可