cnvd监控

 

web安全学习了解: web渗透测试     
官网: 宣紫科技     

这个项目写的比较早了,是在19年2月份的时候写的了。当时在甲方主要考虑关注有没有一些新的漏洞发布对公司业务中间件应用等会造成影响的。后来离职出来就一直没咋关心了。

程序主要分3个文件:

cnvd.py   主程序
config.py   配置
smtpSend.py  发信格式

cnvd.py 内容:

import requests
import re
import time
import copy
import schedule
from config import SCHEDULE_TASK_DAY_AT, RECEIVERS, LOOP_SLEEP, WHITE_KEYWORD_LIST
from smtpSend import SmtpSender


typeid_list = {
    "29": "WebApp",
    "32": "WebProduct",
    "28": "App",
    "27": "System",
    "30": "Database",
    "31": "NetworkDevices"
}
request = requests.Session()
headers = {
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "X-Requested-With": "XMLHttpRequest",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0"
}


# 这两个用来对比数据
all = []
new_all = []


def check_list(type_id, today):
    today_list_info = []
    page = 1

    while True:
        p = (page - 1) * 20
        target = "http://www.cnvd.org.cn/flaw/typeResult?typeId={typeid}&max=20&offset={p}".format(typeid=type_id, p=p)
        # pages = re.findall(r'class="step">(\d+)</a>', r.text)[-1]
        r = requests.post(target, headers=headers)

        # 匹配
        titles = re.findall(r'<a href="(/flaw/show/CNVD-\d{4}-\d+)" title="(.*?)">', r.text)
        times = re.findall(r'<td width="13%">(\d{4}-\d{2}-\d{2})</td>', r.text)

        for index, linkInfo in enumerate(titles):
            link, title = linkInfo
            date = times[index]

            # 将今天的数据爬下来保存到list里面
            if date == today:
                # 白名单审核
                for white_keyword in WHITE_KEYWORD_LIST:
                    if white_keyword.lower() in title.lower():
                        # print("Keyword: {}, Title: {}".format(white_keyword, title))
                        today_list_info.append({"title": title, "date": date, "link": link})
            else:
                return today_list_info
        page += 1


def task():
    all = copy.copy(new_all)
    # 获取今天的数据
    t = time.localtime(time.time())
    today = "{year}-{month}-{day}".format(year=t.tm_year, month="0" + str(t.tm_mon) if t.tm_mon < 10 else t.tm_mon,
                                              day=t.tm_mday)

    with open("s.txt") as f:
        sign = f.read().strip()

    if sign != today:
        # 保存到时间和今天的时间不对等话,
        # 说明已经到了第二天, 将数组清空, 重新开始存储
        all = []
        with open("s.txt", "w") as f:
            f.write(today)

    # 迭代所有的分类板块爬虫
    for typeid, typeName in typeid_list.items():
        ret_check_data_list = check_list(typeid, today)
        print("[+] Check typeName {}, id {}, data total: {}".format(typeName, typeid, len(ret_check_data_list)))

        for data in ret_check_data_list:
            new_all.append(data.get("link"))
            # print("{}\t{}\t{}".format(data.get("title"), data.get("date"), data.get("link")))

        if len(ret_check_data_list) == 0:
            continue

        if len(set(new_all).difference(set(all))) != 0:
            message = "\n".join(["更新时间: {}\n漏洞标题: {}\n漏洞地址:http://www.cnvd.org.cn{}".format(item.get("date"),
                                                                                       item.get("title"), item.get("link"))
                                                  for item in ret_check_data_list])
            # 发信
            for receiver in RECEIVERS:
                SmtpSender("{}今日漏洞预警, 漏洞分类: {}".format(today, typeName), message, receiver)


if __name__ == '__main__':

    # 添加时间计划
    schedule_task_day_at = SCHEDULE_TASK_DAY_AT
    for day_at in schedule_task_day_at:
        print("[*] at {} put into the Schedule queue.".format(day_at))
        schedule.every().day.at(day_at).do(task)

    while True:
        schedule.run_pending()
        time.sleep(LOOP_SLEEP)

config.py内容:

# 定时的时间
SCHEDULE_TASK_DAY_AT = [
    "16:24",
    "16:26",
    "16:27"
]

# 发信邮件
SMTP_USER = "XXXXX@163.com"
SMTP_PASS = "XXXXX" #邮箱密码

# 接受者
RECEIVERS = [
    "3176184967@qq.com"
]

# 每多久进行一次循环
LOOP_SLEEP = 30

# 标题关键词白名单
WHITE_KEYWORD_LIST = [
    "apache", "apache"
]

smtpSend.py内容:

import smtplib
import email.mime.multipart
import email.mime.text
from config import SMTP_USER, SMTP_PASS


def SmtpSender(title, content, receiver):
    msg = email.mime.multipart.MIMEMultipart()
    login_user = SMTP_USER
    login_pass = SMTP_PASS
    receiver = receiver

    msg['Subject'] = title
    msg['From'] = login_user
    msg['To'] = receiver
    content = u"详情:\n {}".format(content)
    txt = email.mime.text.MIMEText(content, "plain", "utf-8")
    msg.attach(txt)

    # smtp = smtplib
    smtp = smtplib.SMTP()
    smtp.connect('smtp.163.com', '25')
    smtp.login(login_user, login_pass)
    smtp.sendmail(login_user, receiver, msg.as_string())
    smtp.quit()
    print("Receiver {} Send Success!!".format(receiver))


# SmtpSender("aa", "bb")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# -*- coding:utf-8 -*- import sys #print (u'系统默认编码为',sys.getdefaultencoding()) default_encoding = 'utf-8' #重新设置编码方式为uft-8 if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) #print (u'系统默认编码为',sys.getdefaultencoding()) import requests from bs4 import BeautifulSoup import traceback import re import xlwt def getURLDATA(url): #url = 'http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=CNNVD-201901-1014' header={ 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36', 'Connection': 'keep-alive',} r=requests.get(url,headers=header,timeout=30) #r.raise_for_status()抛出异常 html = BeautifulSoup(r.content.decode(),'html.parser') link=html.find(class_='detail_xq w770')#漏洞信息详情 link_introduce=html.find(class_='d_ldjj')#漏洞简介 link_others=html.find_all(class_='d_ldjj m_t_20')#其他 #print(len(link_introduce)) try: #print ("危害等级:"+link.contents[3].contents[3].find('a').text.lstrip().rstrip())#危害等级 list4.append(str(link.contents[3].contents[3].find('a').text.lstrip().rstrip())) except: #print("危害等级:is empty") list4.append("") try: #print ("CVE编号:"+link.contents[3].contents[5].find('a').text.lstrip().rstrip())#CVE编号 list5.append(str(link.contents[3].contents[5].find('a').text.lstrip().rstrip())) except: #print("CVE编号:is empty") list5.append("") try: #print ("漏洞类型:"+link.contents[3].contents[7].find('a').text.lstrip().rstrip())#漏洞类型 list6.append(str(link.contents[3].contents[7].find('a').text.lstrip().rstrip())) except : #print("漏洞类型:is empty") list6.append("") try: #print ("发布时间:"+link.contents[3].contents[9].find('a').text.lstrip().rstrip())#发布时间 list7.append(str(link.contents[3].contents[9].find('a').text.lstrip().rstrip())) except : #print("发布时间:is empty") list7.append("") try: #print ("威胁类型:"+link.contents[3].contents[11].find('a').text.lstrip().rstrip())#威胁类型 list8.append(str(link.contents[3].contents[11].find('a').text.lstrip().rstrip())) except : #print("威胁类型:is empty") list8.append("") try: #print ("更新时间:"+link.contents[3].contents[13].find('a').text.lstrip().rstrip())#更新时间 list9.append(str(link.contents[3].contents[13].find('a').text.lstrip().rstrip())) except : #print("更新时间:is empty") list9.append("") try: #print ("厂商:"+link.contents[3].contents[15].find('a').text.lstrip().rstrip())#厂商 list10.append(str(link.contents[3].contents[15].find('a').text.lstrip().rstrip())) except: #print("厂商:is empty") list10.append("") #link_introduce=html.find(class_='d_ldjj')#漏洞简介 try: link_introduce_data=BeautifulSoup(link_introduce.decode(),'html.parser').find_all(name='p') s="" for i in range(0,len(link_introduce_data)): ##print (link_introduce_data[i].text.lstrip().rstrip()) s=s+str(link_introduce_data[i].text.lstrip().rstrip()) #print(s) list11.append(s) except : list11.append("") if(len(link_others)!=0): #link_others=html.find_all(class_='d_ldjj m_t_20') #print(len(link_others)) try: #漏洞公告 link_others_data1=BeautifulSoup(link_others[0].decode(),'html.parser').find_all(name='p') s="" for i in range(0,len(link_others_data1)): ##print (link_others_data1[i].text.lstrip().rstrip()) s=s+str(link_others_data1[i].text.lstrip().rstrip()) #print(s) list12.append(s) except: list12.append("") try: #参考网址 link_others_data2=BeautifulSoup(link_others[1].decode(),'html.parser').find_all(name='p') s="" for i in range(0,len(link_others_data2)): ##print (link_others_data2[i].text.lstrip().rstrip()) s=s+str(link_others_data2[i].text.lstrip().rstrip()) #print(s) list13.append(s) except: list13.append("") try: #受影响实体 link_others_data3=BeautifulSoup(link_others[2].decode(),'html.parser').find_all('a',attrs={'class':'a_title2'}) s="" for i in range(0,len(link_others_data3)): ##print (link_others_data3[i].text.lstrip().rstrip()) s=s+str(link_others_data3[i].text.lstrip().rstrip()) #print(s) list14.append(s) except: list14.append("") try: #补丁 link_others_data3=BeautifulSoup(link_others[3].decode(),'html.parser').find_all('a',attrs={'class':'a_title2'}) s="" for i in range(0,len(link_others_data3)): ##print (link_others_data3[i].t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值