github-cve-monitor/github_cve_monitor.py

89 lines
3.2 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
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\":\"",'')
2021-02-25 09:24:45 +08:00
2021-04-15 23:29:20 +08:00
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
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
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
msg = str(getNews())
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)
# 检查name 和 description 中是否存在test字样 和 是否更新
if regular(req) and total_count != getNews()[0]:
2021-02-25 09:24:45 +08:00
# 二选一即可,没配置的 注释或者删掉
server(text, msg)
dingding(text, msg)
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()