Skip to content

Commit d4513de

Browse files
committed
Added throttling and threading semaphore
1 parent 1c3023a commit d4513de

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

cve_prioritizer.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
__author__ = "Mario Rojas"
44
__license__ = "BSD 3-clause"
5-
__version__ = "1.2.0"
5+
__version__ = "1.3.0"
66
__maintainer__ = "Mario Rojas"
77
__status__ = "Production"
88

@@ -11,6 +11,8 @@
1111
import os
1212
import re
1313
import threading
14+
import time
15+
from threading import Semaphore
1416

1517
from dotenv import load_dotenv
1618

@@ -21,6 +23,7 @@
2123
from scripts.helpers import cve_trends
2224

2325
load_dotenv()
26+
Throttle_msg = ""
2427

2528
# argparse setup
2629
parser = argparse.ArgumentParser(description="CVE Prioritizer", epilog='Happy Patching',
@@ -32,7 +35,8 @@
3235
required=False, metavar='')
3336
parser.add_argument('-n', '--cvss', type=float, help='CVSS threshold (Default 6.0)', default=6.0, metavar='')
3437
parser.add_argument('-o', '--output', type=str, help='Output filename', required=False, metavar='')
35-
parser.add_argument('-t', '--threads', type=str, help='Number of concurrent threads', required=False, metavar='')
38+
parser.add_argument('-t', '--threads', type=int, help='Number of concurrent threads', required=False, metavar='',
39+
default=100)
3640
parser.add_argument('-v', '--verbose', help='Verbose mode', action='store_true')
3741
parser.add_argument('-l', '--list', help='Space separated list of CVEs', nargs='+', required=False, metavar='')
3842

@@ -46,6 +50,7 @@
4650
header = SIMPLE_HEADER
4751
epss_threshold = args.epss
4852
cvss_threshold = args.cvss
53+
sem = Semaphore(args.threads)
4954

5055
# Temporal lists
5156
cve_list = []
@@ -64,15 +69,19 @@
6469
elif args.list:
6570
cve_list = args.list
6671
if not os.getenv('NIST_API'):
67-
print(LOGO + 'Warning: Using this tool without specifying a NIST API may result in errors'
68-
+ '\n\n' + header)
72+
if len(cve_list) > 75:
73+
Throttle_msg = "Large number of CVEs detected, requests will be throttle to avoid API issues"
74+
print(LOGO + Throttle_msg + '\n'
75+
+ 'Warning: Using this tool without specifying a NIST API may result in errors' + '\n\n' + header)
6976
else:
7077
print(LOGO + header)
7178
elif args.file:
7279
cve_list = [line.rstrip() for line in args.file]
7380
if not os.getenv('NIST_API'):
74-
print(LOGO + 'Warning: Using this tool without specifying a NIST API may result in errors'
75-
+ '\n\n' + header)
81+
if len(cve_list) > 75:
82+
Throttle_msg = "Large number of CVEs detected, requests will be throttle to avoid API issues"
83+
print(LOGO + Throttle_msg + '\n'
84+
+ 'Warning: Using this tool without specifying a NIST API may result in errors' + '\n\n' + header)
7685
else:
7786
print(LOGO + header)
7887
elif args.demo:
@@ -94,13 +103,18 @@
94103
output_file.write("cve_id,priority,epss,cvss,cvss_version,cvss_severity,cisa_kev"+"\n")
95104

96105
for cve in cve_list:
106+
throttle = 1
107+
if len(cve_list) > 75 and not os.getenv('NIST_API'):
108+
throttle = 6
97109
if not re.match("(CVE|cve-\d{4}-\d+$)", cve):
98110
print(f"{cve} Error: CVEs should be provided in the standard format CVE-0000-0000*")
99111
else:
112+
sem.acquire()
100113
t = threading.Thread(target=worker, args=(cve.upper().strip(), cvss_threshold, epss_threshold, args.verbose,
101-
args.output))
114+
sem, args.output))
102115
threads.append(t)
103116
t.start()
117+
time.sleep(throttle)
104118

105119
for t in threads:
106120
t.join()

scripts/constants.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
__author__ = "Mario Rojas"
44
__license__ = "BSD 3-clause"
5-
__version__ = "1.2.0"
5+
__version__ = "1.3.0"
66
__maintainer__ = "Mario Rojas"
77
__status__ = "Production"
88

@@ -18,6 +18,6 @@
1818
# / _ \____(_)__ ____(_) /_(_)__ ___ ____
1919
# / ___/ __/ / _ \/ __/ / __/ /_ // -_) __/
2020
# /_/ /_/ /_/\___/_/ /_/\__/_//__/\__/_/
21-
# BY TURROKS
21+
# v1.3.0 BY TURROKS
2222
2323
"""""

scripts/helpers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def print_and_write(working_file, cve_id, priority, epss, cvss_base_score, cvss_
127127

128128

129129
# Main function
130-
def worker(cve_id, cvss_score, epss_score, verbose_print, save_output=None):
130+
def worker(cve_id, cvss_score, epss_score, verbose_print, sem, save_output=None):
131131
nist_result = nist_check(cve_id)
132132
epss_result = epss_check(cve_id)
133133

@@ -164,6 +164,8 @@ def worker(cve_id, cvss_score, epss_score, verbose_print, save_output=None):
164164
if working_file:
165165
working_file.close()
166166

167+
sem.release()
168+
167169

168170
# Function retrieves data from CVE Trends
169171
def cve_trends():

0 commit comments

Comments
 (0)