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

  1 #!/usr/bin/env python
  2 # coding:utf-8
  3 # @Date    : 2016年4月21日 15:08:44
  4 # @Author  : sevck (sevck@jdsec.com)
  5 # @Link    : http://www.qingteng.cn
  6 #-------------------------------------------------------------------------
  7 
  8 import time
  9 import urllib2
 10 import sys
 11 reload(sys)
 12 sys.setdefaultencoding( "utf-8" )
 13 from bs4 import BeautifulSoup
 14 from email.mime.text import MIMEText
 15 import smtplib
 16 
 17 #===============================================================================  
 18 # 导入smtplib和MIMEText  
 19 #===============================================================================  
 20 from email.mime.text import MIMEText
 21 import smtplib
 22     
 23 #===============================================================================  
 24 # 要发给谁,这里指定发送的邮箱地址(支持多个逗号间隔) 
 25 #===============================================================================  
 26 mailto_list=["sevck@jdsec.com"]
 27 
 28 #===============================================================================  
 29 # 设置服务器,用户名、口令以及邮箱的后缀  
 30 #===============================================================================  
 31 mail_host="smtp.qq.com"
 32 mail_user=""
 33 mail_pass=""
 34 mail_postfix="qq.com"
 35 mail_usernnick="XX播报平台"
 36 
 37 #===============================================================================
 38 #定义厂商列表:
 39 #务必填写在乌云厂商名字:比如360在乌云的名称是:奇虎360
 40 #===============================================================================
 41 list=["乌云厂商名称"]
 42 buglist=[]
 43 #===============================================================================
 44 #获取当天时间
 45 #===============================================================================
 46 def gettime():
 47     data=time.strftime('%Y-%m-%d',time.localtime(time.time()))
 48     return data
 49 #================================================================================
 50 #获取html代码
 51 #================================================================================
 52 def getUrlRespHtmlSimply(url):
 53     req = urllib2.Request(url)
 54     res = urllib2.urlopen(req)
 55     html = res.read()
 56     res.close()
 57     return html
 58 #================================================================================
 59 #获取html代码
 60 #================================================================================
 61 def getUrlRespHtml(url):
 62     heads = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
 63             'Accept-Charset':'GB2312,utf-8;q=0.7,*;q=0.7',
 64             'Accept-Language':'zh-cn,zh;q=0.5',
 65             'Cache-Control':'max-age=0',
 66             'Connection':'keep-alive',
 67             'Keep-Alive':'115',
 68             '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'}
 69 
 70     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
 71     urllib2.install_opener(opener)
 72     req = urllib2.Request(url)
 73     opener.addheaders = heads.items()
 74     respHtml = opener.open(req).read()
 75     return respHtml
 76 #================================================================================
 77 #处理爬虫数据
 78 #================================================================================
 79 def getwooyun():
 80     for k in range(1,5):
 81         url="http://www.wooyun.org/bugs/new_submit/page/"+str(k)
 82         html=getUrlRespHtml(url).decode('utf-8')
 83         #print html
 84         #print "-----------------------------------"
 85         soup=BeautifulSoup(html)
 86         #print soup
 87         tbody=soup.find("tbody")
 88         #print tbody
 89         tr=tbody.findAll("tr")
 90         times=gettime() 
 91         for i in tr:
 92             submittime=i.find("th").contents[0]
 93             #print submittime
 94         if times==submittime:
 95             #print "ok"
 96             title=i.find("td").find("a").contents[0]#漏洞标题
 97             href="www.wooyun.org"+i.find("td").find("a").get("href")#漏洞地址
 98             buglist.append(title)
 99             buglist.append(href)
100         else:
101             #print "pass"
102             pass
103 #===============================================================================
104 #获取漏洞信息
105 #==============================================================================
106 con=""
107 def outbug():
108     for j in buglist:
109         global con
110     con+=str(j)+"
"
111 print con
112 
113 #===============================================================================  
114 # 发送邮件  
115 #===============================================================================  
116 def send_mail(to_list,sub,content):
117     ''''' 
118     to_list:发给谁 
119     sub:主题 
120     content:内容 
121     send_mail("aaa@126.com","sub","content") 
122     '''
123     me=mail_usernnick+"<"+mail_user+"@"+mail_postfix+">"
124     msg = MIMEText(content,_charset="utf-8")
125     msg['Subject'] = sub
126     msg['From'] = me
127     msg['To'] = ";".join(to_list)
128     try:
129         s = smtplib.SMTP_SSL()
130         s.connect(mail_host)
131         s.login(mail_user,mail_pass)
132         s.sendmail(me, to_list, msg.as_string())
133         s.close()
134         return True
135     except Exception, e:
136         print str(e)
137         return False
138 
139 
140 def main():
141     date=gettime()
142     print con
143     getwooyun()
144     outbug()
145     if con!="":
146     if send_mail(mailto_list,"【WOOYUN-"+date+"-漏洞信息】",con.encode("utf-8")):
147             print "发送成功"
148         else:
149             print "发送失败"
150 
151     for i in list:
152         print i
153     if(i in con):
154         print "true"
155         print i
156         send_mail(mailto_list,"[紧急]【您关注的客户被爆漏洞了】",
157                     "客户为:"+i.encode("utf-8")+"
"+"
"+
158                     "详情请看如下:
"+
159                     "
----------------------------
"+
160                     con.encode("utf-8"))
161     else:
162         print "false"
163 if __name__ == '__main__':
164    main()

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

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

原文地址:https://www.cnblogs.com/sevck/p/5627418.html