爬取阿里云安全公告实现漏洞告警通知

作者: print("") 分类: 未分类 发布时间: 2019-09-21 18:28

群里无意中讨论了。监控漏洞平台的那些安全预警

然后发现阿里云的安全公告挺及时的。就造了一个轮子。 emmm

此代码基于宝塔面板开发的。记得安装宝塔面板才能执行哦


代码如下:(代码你只需要修改你的邮箱。STMP密码。和stmp HOST , 需要发送的邮箱)

#!/usr/bin/python
# coding: utf-8
# Author: 1249648969@qq.com
# 漏洞预警
import sys, os
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
if sys.version_info[0] == 2:
    reload(sys)
    sys.setdefaultencoding('utf-8')
os.chdir('/www/server/panel')
sys.path.append("class/")
import time,requests,re,public
class pa_main:
    my_mail='1249648969@qq.com'  ####发送邮件的信息
    stmp_pass='xxx'             ##发送邮件的STMP
    stmp='smtp.qq.com'          # 发送邮件的STMP host
    to_mail = ["1249648969@qq.com"]    #需要发送的邮箱

    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3)AppleWebKit / 535.20(KHTML, like Gecko) Chrome / 19.0.1036.7Safari / 535.20',
    }

    #数据库标识符号
    __sql_path = '/tmp/sql_path___333______.path'

    def __init__(self):
        if not os.path.exists(self.__sql_path):
            public.M('sites').execute('''CREATE TABLE "wen_data" (
              "id" INTEGER NOT NULL,
              "title" TEXT,
              "url" TEXT,
              "wen_url" TEXT,
              "ok" TEXT,
              PRIMARY KEY ("id")
            );''')
            os.system('touch %s'%self.__sql_path)


    def pa(self,url):
        wen_zhang=[]
        data = requests.get(url=url, headers=self.headers, verify=False)
        data22=re.findall('''<a class="notice-list-item" href="(.*)">''',data.text)
        try:
            for i2 in data22:
                wen_data={}
                title = re.findall('<a class="notice-list-item" href="%s">\s(.*)\s(.*)\s(.*)' % i2, data.text)
                title = title[0]
                if not  len(title)==3:continue
                title = re.findall('<div class="title">(.*)</div>', title[2])
                wen_data['title']=title[0]
                wen_data['url']='https://m.aliyun.com%s'%i2
                wen_zhang.append(wen_data)
            return wen_zhang
        except:
            self.mail(self.my_mail, self.stmp_pass, '1249648969@qq.com','爬虫挂了', '爬虫挂了')
            return []

    # 存入数据库中
    def insert_data(self,data):
        print('存入数据库中')
        if len(data)==0:return False
        for i in data:
            if public.M('wen_data').where('url=?', (i['url'],)).count() ==0:
                list_data={"url":i['url'],"title":i['title'],"wen_url":'',"ok":False}
                public.M('wen_data').insert(list_data)
        print('数据库中已经存储OK')
        return True

    def get_html(self):
        list_data = public.M('wen_data').select()
        for i in list_data:
            if not 'm.aliyun.com' in i['url']: continue
            if i['ok'] == '0':
                for i2 in self.to_mail:
                   time.sleep(1)
                   send_html='<br>阿里云漏洞预警平台__</br><a href=%s>%s</a><br></br><br>'%(i['url'],i['title'])
                   #微信发送成
                   if self.mail(self.my_mail,self.stmp_pass,i2,'阿里云漏洞预警平台'+i['title'],send_html):
                       print('发送邮件成功%s'%i2)
                       list={"ok":'1'}
                       public.M('wen_data').where('url=?', (i['url'],)).update(list)
                   else:print('发送邮件失败%s'%i2)
        print('已经全部搞定了')

    #发送邮件
    def mail(self,my_sender, my_pass, my_user, title, body):
        time.sleep(10)
        ret = True
        try:
            msg = MIMEText(body, 'html', 'utf-8')
            msg['From'] = formataddr([my_sender, my_sender])
            msg['To'] = formataddr([my_user, my_user])
            msg['Subject'] = title
            server = smtplib.SMTP_SSL(self.stmp, 465)
            server.login(my_sender, my_pass)
            server.sendmail(my_sender, [my_user, ], msg.as_string())
            server.quit()
        except Exception:
            ret = False
        return ret

aa=pa_main()
print('爬取阿里云漏洞公告')
aa.insert_data(aa.pa('https://m.aliyun.com/doc/notice_list/9213612.html'))
aa.get_html()




测试咯

然后新建一个计划任务每3个小时执行一次

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

说点什么

avatar
  Subscribe  
提醒