forked from Guezone/SECMON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cve_poller.py
444 lines (423 loc) · 18.4 KB
/
cve_poller.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""
SECMON - Source code of the SECMON CVE poller script.
"""
__author__ = "Aubin Custodio"
__copyright__ = "Copyright 2021, SECMON"
__credits__ = ["Aubin Custodio","Guezone"]
__license__ = "CC BY-NC-SA 4.0"
__version__ = "2.0"
__maintainer__ = "Aubin Custodio"
__email__ = "[email protected]"
import base64, os, smtplib, re, requests, time, sqlite3, feedparser, colorama, warnings
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
from secmon_lib import getParsedCpe, translateText, getUserLanguage, getCpeList, pollCveIdFromCpe, writeCveTypeLog, writeMgmtTasksLog
warnings.filterwarnings("ignore", category=DeprecationWarning)
sender, receiver, password, smtpsrv, port, tls, keywords = '','','','','','',''
script_path = os.path.abspath(__file__)
dir_path = script_path.replace("cve_poller.py","")
log_file = dir_path+"logs.txt"
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def checkConfig(sender, receiver, password, smtpsrv, port, tls,keywords):
script_path = os.path.abspath(__file__)
dir_path = script_path.replace("cve_poller.py","")
con = sqlite3.connect(dir_path+'secmon.db')
cur = con.cursor()
cur.execute("SELECT sender FROM config")
db_result_tuple = cur.fetchone()
for value in db_result_tuple:
sender = value
cur.execute("SELECT password FROM config")
db_result_tuple = cur.fetchone()
for value in db_result_tuple:
b = value.encode("UTF-8")
bytes_password = base64.b64decode(b)
password = bytes_password.decode("UTF-8")
cur.execute("SELECT smtpsrv FROM config")
db_result_tuple = cur.fetchone()
for value in db_result_tuple:
smtpsrv = value
cur.execute("SELECT port FROM config")
db_result_tuple = cur.fetchone()
for value in db_result_tuple:
port = value
cur.execute("SELECT receiver FROM config")
db_result_tuple = cur.fetchone()
for value in db_result_tuple:
receivers = value.split(";")
cur.execute("SELECT tls FROM config")
db_result_tuple = cur.fetchone()
for value in db_result_tuple:
tls = value
cur.execute("SELECT language FROM config")
db_result_tuple = cur.fetchone()
for value in db_result_tuple:
language = value
cur.execute("SELECT receiver FROM config")
db_result_tuple = cur.fetchone()
for value in db_result_tuple:
receivers = value.split(";")
cur.execute("SELECT keyword FROM keyword_list")
db_result_list = cur.fetchall()
keywords = []
for db_result_tuple in db_result_list:
for result in db_result_tuple:
keywords.append(result)
if all(value != '' for value in [sender, receivers, password, smtpsrv, str(port), tls, language]):
print(bcolors.OKGREEN+"Configuration is good."+bcolors.ENDC)
cvePoller(sender, receivers, password, smtpsrv, port, tls, keywords, language)
else:
print(bcolors.FAIL+"Error in the config."+bcolors.ENDC)
exit()
def cvePoller(sender, receivers, password, smtpsrv, port, tls, keywords, language):
print("------------------------------------")
summary, url = "",""
cve_rss = []
current_feed = feedparser.parse("https://nvd.nist.gov/feeds/xml/cve/misc/nvd-rss.xml")
for entries in current_feed.entries:
title = entries.title
summary = entries.summary
cve_id = re.findall('CVE-\d{4}-\d{4,7}',title)
cve_id = cve_id[0]
cve_rss.append(cve_id+"(=)"+summary)
summary, url = "",""
new_cve_list = []
con = sqlite3.connect(dir_path+'secmon.db')
cur = con.cursor()
dest_language = getUserLanguage()
now_date = str(datetime.now()).split(" ")[0].split("-")
idx_date = now_date[2]+"/"+now_date[1]+"/"+now_date[0]
# RSS
print(bcolors.HEADER+"Polling NVD RSS feed (keyword based search)."+bcolors.ENDC+"\n")
for cve in cve_rss:
cve_id = cve.split("(=)")[0]
summary = cve.split("(=)")[1]
for key in keywords:
if key in summary:
key_match = key
cur.execute("SELECT CVE_ID FROM CVE_DATA WHERE CVE_ID = (?);", (cve_id,))
db_result_list = cur.fetchall()
db_result_str = ""
for db_result_tuple in db_result_list:
for result in db_result_tuple:
db_result_str+=result
if cve_id not in db_result_str:
nvd_base_url = "https://services.nvd.nist.gov/rest/json/cve/1.0/"
nvd_query = nvd_base_url+cve_id
nvd_response = requests.get(url=nvd_query)
nvd_data = nvd_response.json()
if 'result' in nvd_data.keys():
if 'reference_data' in nvd_data['result']['CVE_Items'][0]['cve']['references']:
nvd_links = nvd_data['result']['CVE_Items'][0]['cve']['references']['reference_data']
cve_sources = ""
for link in nvd_links:
cve_sources += (link['url']+"\n")
else:
cve_sources = "N/A"
cve_score = ""
fr_society = " (constructeur/éditeur)"
en_society = " (builder/publisher)"
if not "impact" in nvd_data['result']['CVE_Items'][0].keys():
cve_score = "N/A"
try:
webpage = requests.get("https://nvd.nist.gov/vuln/detail/{}".format(cve_id))
soup = BeautifulSoup(webpage.content, 'html.parser')
cve_cna_score = soup.find(id='Cvss3CnaCalculatorAnchor')
cve_cna_score = cve_cna_score.get_text()
if cve_cna_score != "":
if language == "fr" or language=="FR":
cve_score = cve_cna_score + fr_society
else:
cve_score = cve_cna_score + en_society
else:
cve_score = "N/A"
except:
cve_score = "N/A"
else:
try:
cve_score = nvd_data['result']['CVE_Items'][0]['impact']['baseMetricV3']['cvssV3']['baseScore']
except:
cve_score = "N/A"
published_date = nvd_data['result']['CVE_Items'][0]['publishedDate']
formatted_date = published_date.split("T")[0]
formatted_date = datetime.strptime(formatted_date,"%Y-%m-%d")
date_2_days_ago = datetime.now() - timedelta(days=7)
if (date_2_days_ago<=formatted_date) and (formatted_date<=datetime.now()):
cve_date_status = "Potentially new CVE"
else:
cve_date_status = "Potentially an update"
cve_date = published_date.split("T")[0]
if language == "fr" or language == "FR":
fr_date = cve_date.split("-")
cve_date = fr_date[2]+"/"+fr_date[1]+"/"+fr_date[0]
if (cve_score=="N/A"):
cve_status = cve_date_status+" - "+"Not yet rated (No score, no CPE)"
else:
cve_status = cve_date_status+" - "+"Valid evaluation"
cve_cpe = ""
if "configurations" in nvd_data['result']['CVE_Items'][0].keys():
if nvd_data['result']['CVE_Items'][0]['configurations']['nodes']:
for node in nvd_data['result']['CVE_Items'][0]['configurations']['nodes']:
if 'cpe_match' in node.keys():
for cpe_node in node['cpe_match']:
cve_cpe += (cpe_node['cpe23Uri']+'\n')
if cve_cpe == "":
cve_cpe = "N/A"
cve_description = nvd_data['result']['CVE_Items'][0]['cve']['description']['description_data'][0]['value']
nvd_link = 'https://nvd.nist.gov/vuln/detail/'+cve_id
if language == "fr" or language=="FR":
try:
cve_description = translateText(dest_language,cve_description)
except:
pass
if not "/" in cve_date:
cve_date = cve_date.split("-")[2]+"/"+cve_date.split("-")[1]+"/"+cve_date.split("-")[0]
try:
cve_status = translateText(dest_language,cve_status).replace("nouveau", "une nouvelle").replace("évalué","évaluée")
except:
pass
else:
cve_description = summary
date = str(datetime.now()).split(" ")[0].split("-")
cve_date = date[2]+"/"+date[1]+"/"+date[0]
cve_score = "N/A"
cve_sources = "N/A"
cve_status = "N/A"
cve_cpe = "N/A"
nvd_link = 'https://nvd.nist.gov/vuln/detail/'+cve_id
status = "Unread"
if cve_id not in new_cve_list:
cur.execute("INSERT INTO CVE_DATA (CVE_ID,KEYWORD,STATUS,CVE_SCORE,CVE_DATE,CVE_DESCRIPTION,CVE_EVAL,CVE_CPE,CVE_SOURCES,EXPLOIT_FIND,INDEXING_DATE) VALUES (?,?,?,?,?,?,?,?,?,?,?);", (cve_id,key_match,status,str(cve_score).split(" ")[0],cve_date,cve_description,cve_status,cve_cpe,cve_sources,"False",idx_date))
con.commit()
print("New CVE detected -> "+cve_id)
report="updated"
try:
sendAlert(sender, password, smtpsrv, port, tls, receivers, cve_id, cve_sources, str(cve_score).split(" ")[0], cve_status, cve_cpe, cve_date, cve_description, language,key_match)
alert = "sent"
except:
alert = "unsent"
new_cve_list.append(cve_id)
writeCveTypeLog("cve_poller",cve_id,"new",key_match,str(cve_score).split(" ")[0],cve_date,cve_cpe,report,alert)
if not new_cve_list:
print(bcolors.HEADER+"No new CVE matched with your keyword list."+bcolors.ENDC)
print("------------------------------------")
else:
print(bcolors.HEADER+"Database was updated with new CVE."+bcolors.ENDC)
new_cve_list = []
# CPE
cur.execute("SELECT cpe FROM cpe_list")
db_result_list = cur.fetchall()
cpes = getCpeList()
if cpes != []:
print(bcolors.HEADER+"Polling NVD related to your product list (CPE based search)."+bcolors.ENDC+"\n")
for cpe in cpes:
cve_ids = pollCveIdFromCpe(cpe)
cpe = cpe.replace("\n","").replace(" ","")
for cve_id in cve_ids:
cur.execute("SELECT CVE_ID FROM CVE_DATA WHERE CVE_ID = (?);", (cve_id,))
db_result_list = cur.fetchall()
db_result_str = ""
for db_result_tuple in db_result_list:
for result in db_result_tuple:
db_result_str+=result
if cve_id not in db_result_str:
nvd_base_url = "https://services.nvd.nist.gov/rest/json/cve/1.0/"
nvd_query = nvd_base_url+cve_id
nvd_response = requests.get(url=nvd_query)
nvd_data = nvd_response.json()
if 'result' in nvd_data.keys():
if 'reference_data' in nvd_data['result']['CVE_Items'][0]['cve']['references']:
nvd_links = nvd_data['result']['CVE_Items'][0]['cve']['references']['reference_data']
cve_sources = ""
for link in nvd_links:
cve_sources += (link['url']+"\n")
else:
cve_sources = "N/A"
key_match = cpe
cve_score = ""
fr_society = " (constructeur/éditeur)"
en_society = " (builder/publisher)"
if not "impact" in nvd_data['result']['CVE_Items'][0].keys():
cve_score = "N/A"
try:
webpage = requests.get("https://nvd.nist.gov/vuln/detail/{}".format(cve_id))
soup = BeautifulSoup(webpage.content, 'html.parser')
cve_cna_score = soup.find(id='Cvss3CnaCalculatorAnchor')
cve_cna_score = cve_cna_score.get_text()
if cve_cna_score != "":
if language == "fr" or language=="FR":
cve_score = cve_cna_score + fr_society
else:
cve_score = cve_cna_score + en_society
else:
cve_score = "N/A"
except:
cve_score = "N/A"
else:
try:
cve_score = nvd_data['result']['CVE_Items'][0]['impact']['baseMetricV3']['cvssV3']['baseScore']
except:
cve_score = "N/A"
published_date = nvd_data['result']['CVE_Items'][0]['publishedDate']
formatted_date = published_date.split("T")[0]
formatted_date = datetime.strptime(formatted_date,"%Y-%m-%d")
date_2_days_ago = datetime.now() - timedelta(days=7)
if (date_2_days_ago<=formatted_date) and (formatted_date<=datetime.now()):
cve_date_status = "Potentially new CVE"
else:
cve_date_status = "Potentially an update"
cve_date = published_date.split("T")[0]
if language == "fr" or language == "FR":
fr_date = cve_date.split("-")
cve_date = fr_date[2]+"/"+fr_date[1]+"/"+fr_date[0]
if (cve_score=="N/A"):
cve_status = cve_date_status+" - "+"Not yet rated (No score, no CPE)"
else:
cve_status = cve_date_status+" - "+"Valid evaluation"
cve_cpe = ""
if "configurations" in nvd_data['result']['CVE_Items'][0].keys():
if nvd_data['result']['CVE_Items'][0]['configurations']['nodes']:
for node in nvd_data['result']['CVE_Items'][0]['configurations']['nodes']:
if 'cpe_match' in node.keys():
for cpe_node in node['cpe_match']:
cve_cpe += (cpe_node['cpe23Uri']+'\n')
if cve_cpe == "":
cve_cpe = "N/A"
cve_description = nvd_data['result']['CVE_Items'][0]['cve']['description']['description_data'][0]['value']
if language == "fr" or language=="FR":
try:
cve_description = translateText(dest_language,cve_description)
except:
pass
if not "/" in cve_date:
cve_date = cve_date.split("-")[2]+"/"+cve_date.split("-")[1]+"/"+cve_date.split("-")[0]
try:
cve_status = translateText(dest_language,cve_status).replace("nouveau", "une nouvelle").replace("évalué","évaluée")
except:
pass
nvd_link = 'https://nvd.nist.gov/vuln/detail/'+cve_id
else:
cve_description = "N/A"
date = str(datetime.now()).split(" ")[0].split("-")
cve_date = date[2]+"/"+date[1]+"/"+date[0]
cve_score = "N/A"
cve_sources = "N/A"
cve_status = "N/A"
cve_cpe = "N/A"
nvd_link = 'https://nvd.nist.gov/vuln/detail/'+cve_id
status = "Unread"
if cve_id not in new_cve_list:
cur.execute("INSERT INTO CVE_DATA (CVE_ID,KEYWORD,STATUS,CVE_SCORE,CVE_DATE,CVE_DESCRIPTION,CVE_EVAL,CVE_CPE,CVE_SOURCES,EXPLOIT_FIND,INDEXING_DATE) VALUES (?,?,?,?,?,?,?,?,?,?,?);", (cve_id,key_match,status,str(cve_score).split(" ")[0],cve_date,cve_description,cve_status,cve_cpe,cve_sources,"False",idx_date))
con.commit()
report="updated"
print("New CVE detected -> "+cve_id)
try:
sendAlert(sender, password, smtpsrv, port, tls, receivers, cve_id, cve_sources, str(cve_score).split(" ")[0], cve_status, cve_cpe, cve_date, cve_description, language,key_match)
alert = "sent"
except:
alert = "unsent"
new_cve_list.append(cve_id)
writeCveTypeLog("cve_poller",cve_id,"new",key_match,str(cve_score).split(" ")[0],cve_date,cve_cpe,report,alert)
if not new_cve_list:
print(bcolors.HEADER+"No new CVE related to your (CPE) product list."+bcolors.ENDC)
print("------------------------------------")
timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
print(bcolors.OKBLUE+"Finished at : "+timestamp+bcolors.ENDC)
print("------------------------------------")
else:
timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
print(bcolors.OKGREEN+"Database was updated. Goodbye."+bcolors.ENDC)
print("------------------------------------")
timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
print(bcolors.OKBLUE+"Finished at : "+timestamp+bcolors.ENDC)
print("------------------------------------")
def sendAlert(sender, password, smtpsrv, port, tls, receivers, cve_id, cve_sources, cve_score, cve_status, cve_cpe, cve_date, cve_description, language,key_match):
body = ""
nvd_url = "https://nvd.nist.gov/vuln/detail/"+cve_id
for receiver in receivers:
with open(dir_path+'cve_template.html', 'r') as template:
html_code = template.read()
if "cpe" in key_match:
key_match = getParsedCpe(key_match)
if language == "fr" or language=="FR":
html_code = html_code.replace("Responsive HTML email templates","Alerte")
html_code = html_code.replace("$CVE",cve_id+" (Produit : {})".format(key_match))
try:
cve_description = translateText(dest_language,cve_description)
except:
pass
html_code = html_code.replace("$DESCRIPTION", cve_description.replace("u'","").replace("u ``",""))
if not "/" in cve_date:
cve_date = cve_date.split("-")[2]+"/"+cve_date.split("-")[1]+"/"+cve_date.split("-")[0]
html_code = html_code.replace("$DATE", cve_date)
html_code = html_code.replace("$SCORE", str(cve_score))
try:
cve_status = translateText(dest_language,cve_status).replace("nouveau", "une nouvelle").replace("évalué","évaluée")
except:
pass
html_code = html_code.replace("$STATUS", cve_status)
html_code = html_code.replace("$CPE", cve_cpe)
html_code = html_code.replace("$SOURCES", cve_sources)
html_code = html_code.replace("See more details", "Voir plus de détails")
html_code = html_code.replace("Publication date", "Date de publication")
html_code = html_code.replace("Status", "Statut")
html_code = html_code.replace("CPE/Product", "CPE/Produit")
html_code = html_code.replace("CVSS Score (V3)", "Score CVSS (V3)")
html_code = html_code.replace("This email was sent by", "Cet email a été envoyé par")
html_code = html_code.replace("CVE module", "Module CVE")
body = html_code.replace("URL OF THE NEWS",nvd_url)
else:
html_code = html_code.replace("Responsive HTML email templates","Alert")
html_code = html_code.replace("$CVE",cve_id+" (Product : {})".format(key_match))
html_code = html_code.replace("$DESCRIPTION", cve_description.replace("u'","").replace("u ``",""))
html_code = html_code.replace("$DATE", cve_date)
html_code = html_code.replace("$SCORE", str(cve_score))
html_code = html_code.replace("$STATUS", cve_status)
html_code = html_code.replace("$CPE", cve_cpe)
html_code = html_code.replace("$SOURCES", cve_sources)
body = html_code.replace("URL OF THE NEWS",nvd_url)
print(bcolors.HEADER+"Sending alert at {}...".format(receiver)+bcolors.ENDC)
try:
smtpserver = smtplib.SMTP(smtpsrv,port)
msg = MIMEMultipart()
msg['Subject'] = 'SECMON - CVE'
msg['From'] = sender
msg['To'] = receiver
msg.attach(MIMEText(body, 'html'))
except:
print(bcolors.FAIL+"Failed to send alert at {}.".format(receiver)+bcolors.ENDC)
exit()
try:
if tls == "yes":
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.login(sender, password)
smtpserver.sendmail(sender, receiver, msg.as_string())
print(bcolors.HEADER+"Alert was sent at {}\n".format(receiver)+bcolors.ENDC)
elif tls == "no":
smtpserver.login(sender, password)
smtpserver.sendmail(sender, receiver, msg.as_string())
print(bcolors.HEADER+"Alert was sent at {}\n".format(receiver)+bcolors.ENDC)
except Exception as e:
print(bcolors.FAIL+"An error occurred during authentication with the SMTP server. Check the configuration and try again."+bcolors.ENDC)
exit()
def main():
colorama.init()
print("------------ CVE Module ------------")
timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
print(bcolors.OKBLUE+"Starting at : "+timestamp+bcolors.ENDC)
print("------------------------------------")
checkConfig(sender, receiver, password, smtpsrv, port, tls, keywords)
main()