github-cve-monitor/github_cve_monitor.py

182 lines
6.6 KiB
Python
Raw Normal View History

2021-02-25 09:24:45 +08:00
#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Author : yhy
# 每3分钟检测一次github是否有新的cve漏洞提交记录若有则通过server酱和钉钉机器人推送二者配置一个即可
# 建议使用screen命令运行在自己的linux vps后台上就可以愉快的接收各种cve了
# https://my.oschina.net/u/4581868/blog/4380482
# https://github.com/kiang70/Github-Monitor
2021-04-15 23:29:20 +08:00
import requests, re, time
2021-02-25 09:24:45 +08:00
import dingtalkchatbot.chatbot as cb
import datetime
import hashlib
import json
from lxml import etree
# 创建md5对象
def nmd5(str):
m = hashlib.md5()
b = str.encode(encoding='utf-8')
m.update(b)
str_md5 = m.hexdigest()
return str_md5
# 有道翻译
def translate(word):
headerstr = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
bv = nmd5(headerstr)
lts = str(round(time.time()*1000))
salt = lts + '90'
# 如果翻译失败,{'errorCode': 50} 请查看 fanyi.min.js: https://shared.ydstatic.com/fanyi/newweb/v1.1.7/scripts/newweb/fanyi.min.js
# 搜索 fanyideskweb sign: n.md5("fanyideskweb" + e + i + "Y2FYu%TNSbMCxc3t2u^XT") Y2FYu%TNSbMCxc3t2u^XT是否改变替换即可
strexample = 'fanyideskweb' + word + salt + 'Y2FYu%TNSbMCxc3t2u^XT'
sign = nmd5(strexample)
data = {
'i': word,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt':salt,
'sign':sign,
'lts': lts,
'bv': bv,
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CLICKBUTTION',
}
url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
header={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
'Referer':'http://fanyi.youdao.com/',
'Origin': 'http://fanyi.youdao.com',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With':'XMLHttpRequest',
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Host': 'fanyi.youdao.com',
'cookie':'_ntes_nnid=937f1c788f1e087cf91d616319dc536a,1564395185984; OUTFOX_SEARCH_USER_ID_NCOO=; OUTFOX_SEARCH_USER_ID=-10218418@11.136.67.24; JSESSIONID=; ___rl__test__cookies=1'
}
res = requests.post(url=url, data=data, headers=header)
t = res.content.decode('utf8')
# 把返回来的json字符串解析成字典
result_dict = json.loads(t)
result = ""
for json_str in result_dict['translateResult'][0]:
tgt = json_str['tgt']
result += tgt
return result
2021-02-25 09:24:45 +08:00
def getNews():
try:
# 抓取本年的
year = datetime.datetime.now().year
api = "https://api.github.com/search/repositories?q=CVE-{}&sort=updated".format(year)
req = requests.get(api).text
2021-04-15 23:29:20 +08:00
cve_total_count = re.findall('"total_count":*.{1,10}"incomplete_results"',req)[0][14:17]
cve_description = re.findall('"description":*.{1,200}"fork"',req)[0].replace("\",\"fork\"",'').replace("\"description\":\"",'')
cve_url = re.findall('"svn_url":*.{1,200}"homepage"',req)[0].replace("\",\"homepage\"",'').replace("\"svn_url\":\"",'')
# 不推送 fork
if cve_description != '"description":null,"fork"':
return cve_total_count, cve_description, cve_url
2021-02-25 09:24:45 +08:00
except Exception as e:
2021-04-15 23:29:20 +08:00
print (e, "github链接不通")
2021-02-25 09:24:45 +08:00
# 钉钉
def dingding(text, msg):
# 将此处换为钉钉机器人的api
webhook = 'xxxxx'
ding = cb.DingtalkChatbot(webhook)
2021-04-15 23:29:20 +08:00
ding.send_text(msg = '{}\r\n{}'.format(text, msg), is_at_all=False)
2021-02-25 09:24:45 +08:00
# server酱 http://sc.ftqq.com/?c=code
def server(text, msg):
# 将 xxxx 换成自己的server SCKEY
uri = 'https://sc.ftqq.com/xxxx.send?text={}&desp={}'.format(text, msg)
2021-04-15 23:29:20 +08:00
requests.get(uri)
2021-02-25 09:24:45 +08:00
# 添加Telegram Bot推送支持
def tgbot(text,msg):
import telegram
# Your Telegram Bot Token
bot = telegram.Bot(token='123456:aaa-sdasdsa')
group_id='Your Group ID'
bot.send_message(chat_id=group_id,text='{}\r\n{}'.format(text,msg))
2021-04-15 23:29:20 +08:00
# 通过检查name 和 description 中是否存在test字样排除test
def regular(req):
cve_name = re.findall('"name":*.{1,200}"full_name"', req)[0].replace("\"name\":\"",'').replace("\",\"full_name\"",'')
cve_description = re.findall('"description":*.{1,200}"fork"', req)[0].replace("\",\"fork\"", '').replace(
"\"description\":\"", '')
2021-04-16 15:54:24 +08:00
if cve_name.lower().find('test') == -1 and cve_description.lower().find('test') == -1:
2021-04-15 23:29:20 +08:00
return True
return False
# 获取cve 名字
def get_cve_name(req):
cve_name = re.findall('"name":*.{1,200}"full_name"', req)[0].replace("\"name\":\"", '').replace("\",\"full_name\"",'')
return cve_name
# 根据cve 名字,获取描述,并翻译
def get_cve_des_zh(cve):
query_cve_url = "https://cve.mitre.org/cgi-bin/cvename.cgi?name=" + cve
response = requests.get(query_cve_url)
html = etree.HTML(response.text)
des = html.xpath('//*[@id="GeneratedTable"]/table//tr[4]/td/text()')[0].strip()
return translate(des)
2021-02-25 09:24:45 +08:00
def sendNews():
2021-04-15 23:29:20 +08:00
while True:
try:
2021-02-25 09:24:45 +08:00
print("cve 监控中 ...")
# 抓取本年的
year = datetime.datetime.now().year
api = "https://api.github.com/search/repositories?q=CVE-{}&sort=updated".format(year)
2021-04-15 23:29:20 +08:00
# 请求API
2021-02-25 09:24:45 +08:00
req = requests.get(api).text
2021-04-15 23:29:20 +08:00
# 正则获取
total_count = re.findall('"total_count":*.{1,10}"incomplete_results"', req)[0][14:17]
# 监控时间间隔3分钟
2021-02-25 09:24:45 +08:00
time.sleep(180)
2021-04-15 23:29:20 +08:00
# 推送正文内容
# 推送标题
2021-02-25 09:24:45 +08:00
text = r'有新的CVE送达'
2021-04-15 23:29:20 +08:00
regular(req)
2021-04-15 23:29:20 +08:00
# 检查name 和 description 中是否存在test字样 和 是否更新
if regular(req) and total_count != getNews()[0]:
cve = get_cve_name(req)
cve_zh = get_cve_des_zh(cve)
msg = "CVE编号"+ str(getNews()[1]) + "\r\n"+"Github地址"+ str(getNews()[2]) + "\r\n" + "CVE描述"+ cve_zh
# 三选一即可,没配置的 注释或者删掉
# server(text, msg)
2021-02-25 09:24:45 +08:00
dingding(text, msg)
# tgbot(text,msg)
2021-02-25 09:24:45 +08:00
print(msg)
else:
pass
2021-04-15 23:29:20 +08:00
except Exception as e:
raise e
2021-02-25 09:24:45 +08:00
if __name__ == '__main__':
2021-04-15 23:29:20 +08:00
sendNews()