forked from adminlove520/cvetrends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
182 lines (169 loc) · 6.76 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import json
import requests
from cpe import CPE
from utils import Color
class feishuBot:
"""飞书群机器人
https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN
"""
def __init__(self, key, proxy_url='') -> None:
self.key = key
self.proxy = {'http': proxy_url, 'https': proxy_url} if proxy_url else {'http': None, 'https': None}
def make_card_trends(self, hit: bool, cve: dict):
vendor = product = None
if vendors := cve['vendors']:
vendor = vendors[0]['vendor']
product = vendors[0]['products'][0]['product']
publishedDate = cve['publishedDate'][:10] if cve['publishedDate'] else None
lastModifiedDate = cve['lastModifiedDate'][:10] if cve['lastModifiedDate'] else None
epss_score = '{:.2%}'.format(float(cve['epss_score'] or 0))
vendor_advisories = cve['vendor_advisories'][0] if cve['vendor_advisories'] else None
github = '\n'.join([i['url'] for i in cve['github_repos']])
reddit = '\n'.join([i['reddit_url'] for i in cve['reddit_posts']])
twitter = '\n'.join([f'https://twitter.com/{i["twitter_user_handle"]}/status/{i["tweet_id"]}' for i in cve['tweets']])
return {
'header': {
'template': 'red' if hit else 'orange',
'title': {
'content': f'【热门漏洞】{cve["cve"]} | {vendor} - {product}',
'tag': 'plain_text'
}
},
'elements': [
{
'tag': 'div',
'fields': [
{
'is_short': True,
'text': {
'content': f'**漏洞时间**\n公开:{publishedDate}\n更新:{lastModifiedDate}',
'tag': 'lark_md'
}
},
{
'is_short': True,
'text': {
'content': f'**漏洞等级**\nCVSS:{cve["severity"]}\nEPSS:{epss_score}',
'tag': 'lark_md'
}
}
]
},
{
'tag': 'div',
'text': {
'content': f'**漏洞公告**\nhttps://nvd.nist.gov/vuln/detail/{cve["cve"]}\n{vendor_advisories or ""}',
'tag': 'lark_md'
}
},
{
'tag': 'div',
'text': {
'content': f'**漏洞概要**\n{cve["description"] or cve["tweets"][0]["tweet_text"]}',
'tag': 'lark_md'
}
},
{
'tag': 'div',
'text': {
'content': f'**GitHub**\n{github}',
'tag': 'lark_md'
}
},
{
'tag': 'div',
'text': {
'content': f'**Reddit**\n{reddit}',
'tag': 'lark_md'
}
},
{
'tag': 'div',
'text': {
'content': f'**Twitter**\n{twitter}',
'tag': 'lark_md'
}
}
]
}
def make_card_last(self, hit: bool, cve: dict):
vendor = product = None
if cpe_list := cve['vulnerable_product']:
cpe = CPE(cpe_list[0])
vendor = cpe.get_vendor()[0]
product = cpe.get_product()[0]
Published = cve['Published'][:10] if cve['Published'] else None
Modified = cve['Modified'][:10] if cve['Modified'] else None
references = '\n'.join(cve['references'])
return {
'header': {
'template': 'red' if hit else 'orange',
'title': {
'content': f'【最新漏洞】{cve["id"]} | {vendor} - {product}',
'tag': 'plain_text'
}
},
'elements': [
{
'tag': 'div',
'fields': [
{
'is_short': True,
'text': {
'content': f'**漏洞时间**\n公开:{Published}\n更新:{Modified}',
'tag': 'lark_md'
}
},
{
'is_short': True,
'text': {
'content': f'**漏洞等级**\nCVSS:{str(cve["cvss"])}',
'tag': 'lark_md'
}
}
]
},
{
'tag': 'div',
'text': {
'content': f'**漏洞公告**\nhttps://nvd.nist.gov/vuln/detail/{cve["id"]}',
'tag': 'lark_md'
}
},
{
'tag': 'div',
'text': {
'content': f'**漏洞概要**\n{cve["summary"]}',
'tag': 'lark_md'
}
},
{
'tag': 'div',
'text': {
'content': f'**References**\n{references}',
'tag': 'lark_md'
}
}
]
}
def send_trends(self, cves: list):
for cve in cves:
r = self.send(self.make_card_trends(cve[0], cve[1]))
if r.status_code == 200:
Color.print_success(f'[+] feishuBot 发送成功 {cve[1]["cve"]}')
else:
Color.print_failed(f'[-] feishuBot 发送失败 {cve[1]["cve"]}')
print(r.text)
def send_last(self, cves: list):
for cve in cves:
r = self.send(self.make_card_last(cve[0], cve[1]))
if r.status_code == 200:
Color.print_success(f'[+] feishuBot 发送成功 {cve[1]["id"]}')
else:
Color.print_failed(f'[-] feishuBot 发送失败 {cve[1]["id"]}')
print(r.text)
def send(self, card: dict):
data = {'msg_type': 'interactive', 'card': card}
headers = {'Content-Type': 'application/json'}
url = f'https://open.feishu.cn/open-apis/bot/v2/hook/{self.key}'
return requests.post(url=url, headers=headers, data=json.dumps(data), proxies=self.proxy)