forked from XcNgg/chatgpt-on-wechat-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchCNNVD.py
More file actions
284 lines (262 loc) · 11.8 KB
/
SearchCNNVD.py
File metadata and controls
284 lines (262 loc) · 11.8 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""
CNNVD 漏洞查询
https://www.cnnvd.org.cn/home/loophole
"""
import pprint
import requests
import openpyxl
import json
from my_fake_useragent import UserAgent
import os
from bridge.context import ContextType
import hashlib
import time
from common.log import logger
import requests
from bridge.reply import Reply, ReplyType
from plugins import *
hazardLevel ={
0:"未知❓",
1:"超危❗❗❗",
2:"高危❗❗",
3:"中危❗",
4:"低危⚠️"
}
def search_cnnvd(number,e_context:EventContext):
"""
number : CNNVD编号 例如 CNNVD-202311-1534
"""
#
content = f"🔍【{number}】\n"
# 最大二次请求条数
max_data = 2
logger.info(f"🔍【{number}】")
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/json;charset=UTF-8",
"Origin": "https://www.cnnvd.org.cn",
"Pragma": "no-cache",
"Referer": "https://www.cnnvd.org.cn/home/loophole",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": UserAgent().random(),
"sec-ch-ua": "^\\^Google",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "^\\^Windows^^",
}
url = "https://www.cnnvd.org.cn/web/homePage/cnnvdVulList"
data = '{{"pageIndex":1,"pageSize":{},"keyword":"{}","hazardLevel":"","vulType":"","vendor":"","product":"","dateType":""}}'.format(
max_data, number)
response = requests.post(url, headers=headers, data=data)
"""
{
'code': 200,
'data': {'pageIndex': 1,
'pageSize': 10,
'records': [{'cnnvdCode': 'CNNVD-202311-1534',
'createTime': '2023-11-17',
'cveCode': 'CVE-2023-6178',
'hazardLevel': 0,
'id': 'b53f7d7e933e4876b5e8452153719738',
'publishTime': '2023-11-17',
'typeName': None,
'updateTime': '2023-11-19',
'vulName': 'Nessus 安全漏洞',
'vulType': '0'}],
'total': 1},
'message': '操作成功',
'success': True,
'time': '2023-11-20 15:54:26'
}
"""
try:
response_data = response.json()
response_status = response_data.get('code',400)
print(response_data)
if response_status == 200:
total = response_data.get('data').get('total')
print(f"总共{total}条")
logger.info(f" (共查询到{total}条漏洞信息)")
# 如果条数为0
if total == 0:
content += "未查询到相关漏洞信息"
# 如果条数小于等于设定的max_data
elif total <= max_data:
content += f" (共查询到{total}条漏洞信息)\n"
records = response_data.get('data').get('records')
for record in records:
loophole_id = record.get('id')
cnnvd_code = record.get('cnnvdCode')
content += get_loophole(loophole_id,cnnvd_code=cnnvd_code)
# 如果条数大于设定的max_data
# 则发送Excel
elif total > max_data:
_send_info(e_context=e_context,content=f"🔍【{number}】相关信息共{total}条,已整理为Excel概览")
frequency = total // 50
frequency = 1 if frequency == 0 else frequency
print(f"需要请求{frequency}次")
result_data = []
for page in range(1,frequency+1):
print(f"第{page}次请求")
url = "https://www.cnnvd.org.cn/web/homePage/cnnvdVulList"
data = '{{"pageIndex":{},"pageSize":{},"keyword":"{}","hazardLevel":"","vulType":"","vendor":"","product":"","dateType":""}}'.format(
page,50, number)
result_data += requests.post(url, headers=headers, data=data).json().get('data').get('records')
file_path = "tmp"
if not os.path.exists(file_path):
os.makedirs(file_path)
file_name = hashlib.md5(str(time.time()).encode()).hexdigest() + ".xlsx"
file_path = os.path.join(file_path, file_name)
print(file_path)
# 创建一个新的工作簿
workbook = openpyxl.Workbook()
# 选择默认的工作表
sheet = workbook.active
sheet.append(['漏洞编号','漏洞等级','漏洞名称','收录时间','更新时间','查询id',])
print(len(result_data))
for record in result_data:
data = [
record.get('cnnvdCode'),
hazardLevel[record.get('hazardLevel')],
record.get('vulName'),
record.get('createTime'),
record.get('updateTime'),
record.get('id'),
]
print(data)
sheet.append(data)
sheet.append([])
sheet.append([f"信息量大于{max_data}条"])
sheet.append([f"已自动为您生成【{number}】相关简报"])
# 保存工作簿
workbook.save(file_path)
reply = Reply()
# 发送文件
reply.type = ReplyType.FILE
reply.content = file_path
logger.info(f"查询结果:{file_path}")
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS
# 返回:暂时无用
return file_path,reply
except Exception as e:
print(e)
logger.info(f"查询漏洞发生错误:{e}")
content += f"发生错误:{e}\n"
reply = Reply()
reply.type = ReplyType.TEXT
reply.content = content
logger.info(reply.content)
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS
# 返回:暂时无用
return content
# 进一步查询漏洞详细信息
def get_loophole(loophole_id,cnnvd_code="CNNVD-202311-299"):
content = ""
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/json;charset=UTF-8",
"Origin": "https://www.cnnvd.org.cn",
"Pragma": "no-cache",
"Referer": "https://www.cnnvd.org.cn/home/loophole",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": UserAgent().random(),
"sec-ch-ua": "^\\^Google",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "^\\^Windows^^",
}
url = "https://www.cnnvd.org.cn/web/cnnvdVul/getCnnnvdDetailOnDatasource"
data = '{"id":"%s","vulType":"0","cnnvdCode":"%s"}' % (loophole_id,cnnvd_code)
# response_data = requests.post(url=url, headers=headers, data=data).text
# print(response_data)
response_data = requests.post(url=url, headers=headers, data=data).json().get('data').get('cnnvdDetail')
content += f"【漏洞名称】{response_data.get('vulName')}\n"
content += f"【漏洞等级】{hazardLevel[response_data.get('hazardLevel')]}\n"
content += f"【CNNVD编号】{response_data.get('cnnvdCode')}\n"
content += f"【CVE编号】{response_data.get('cveCode')}\n"
content += f"【CVE】{response_data.get('cveCode')}\n"
content += f"【漏洞类型】{response_data.get('vulType')}\n"
content += f"【漏洞描述】{response_data.get('vulDesc')}\n"
content += f"【收录时间】{response_data.get('publishTime')}\n"
content += f"【更新时间】\n{response_data.get('updateTime')}\n"
content += f"【官方补丁】{response_data.get('patch')}\n"
content += f"【参考网址】\n{response_data.get('referUrl')}\n"
content += f"----------\n"
"""
{'code': 200,
'data': {'cnnvdDetail': {'affectedProduct': None,
'affectedSystem': None,
'affectedVendor': 'Subrion',
'cnnvdCode': 'CNNVD-202311-299',
'cnnvdFiledShow': 'vul_name,cnnvd_code,_code,publish_time,is_official,vendor,hazard_level,vul_type,vul_desc,affected_product,affected_vendor,product_desc,affected_system,refer_url,patch_id,product,update_time,patch',
'createTime': None,
'createUid': None,
'createUname': None,
'cveCode': 'CVE-2023-46947',
'cveFiledShow': None,
'cveVulVO': None,
'deleted': None,
'hazardLevel': 1,
'huaweiFiledShow': None,
'huaweiVulVO': None,
'ibmFiledShow': None,
'ibmVulVO': None,
'icsCertFiledShow': None,
'icsCertVulVO': None,
'id': None,
'isOfficial': 0,
'microsoftFiledShow': None,
'microsoftVulVO': None,
'nvdFiledShow': None,
'nvdVulVO': None,
'patch': None,
'patchId': None,
'productDesc': None,
'publishTime': '2023-11-03 00:00:00',
'referUrl': '来源:MISC\r\n'
'链接:https://github.com/intelliants/subrion/issues/909\r\n'
'\r\n'
'来源:cxsecurity.com\r\n'
'链接:https://cxsecurity.com/cveshow/CVE-2023-46947/',
'updateTime': '2023-11-13 00:00:00',
'updateUid': None,
'updateUname': None,
'varchar1': '其他',
'vendor': '1005853',
'version': None,
'vulDesc': 'Subrion '
'CMS是Subrion团队的一套基于PHP的内容管理系统(CMS)。该系统可被集成到网站,并支持多种扩展插件等。\r\n'
'Subrion CMS '
'4.2.1版本存在安全漏洞。攻击者利用该漏洞可以远程执行代码。',
'vulName': 'Subrion CMS 安全漏洞',
'vulType': '其他',
'vulTypeName': '其他'},
'receviceVulDetail': None},
'message': '操作成功',
'success': True,
'time': '2023-11-20 17:05:15'}
"""
pprint.pprint(response_data)
return content
def _send_info(e_context: EventContext, content: str):
reply = Reply(ReplyType.TEXT, content)
channel = e_context["channel"]
channel.send(reply, e_context["context"])
if __name__ == '__main__':
# content = ""
# number = "CNNVD-202311-224"
# result = search_cnnvd(number=number)
# if isinstance(result,str):
# content += result
# print(content)
print(get_loophole(loophole_id="c03637953d3b498f8f0becc3b6989e35"))