-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvd2stix.py
264 lines (247 loc) · 11.6 KB
/
nvd2stix.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
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved
#
# NVD2SSTIX
# Author: Taylor McCampbell
# Description: Give the program a search term or a cpe string and the program will create a stix bundle based on what the NVD returns when queried with those terms
import requests, json, sys, os, pandas
from requests.auth import HTTPBasicAuth
from tqdm import tqdm
from stix2 import Vulnerability, Bundle, Relationship, Software, Identity, Note, Report
from urllib3.exceptions import InsecureRequestWarning
from datetime import datetime
#For the CWE functionality to work you must modify OpenCVE username and password in the fields below. Otherwise, there will be no CWEs added to your bundle
opencve_user = 'CHANGEME'
opencve_pass = 'CHANGEME'
# Pass NVD query the correct api endpoint for cpe searching
def cpe_search(cpe_string):
cpeLink = f'https://services.nvd.nist.gov/rest/json/cves/2.0?cpeName={cpe_string}'
bundle = nvdQuery(cpeLink, cpe_string)
return bundle
#Pass NVD query the correct api endpoint for term searching
def term_search(search_terms):
#Base link without keywords
nvd_link = f'https://services.nvd.nist.gov/rest/json/cves/2.0?keywordSearch='
#Building terms list
terms = ''
for term in search_terms:
terms += f'{term} '
termSearchLink = f'{nvd_link}{terms}'
bundle = nvdQuery(termSearchLink, terms)
return bundle
#Query the NVD and create objects with the proper link
def nvdQuery(searchLink, query):
exploited_vulns = pandas.read_csv('known_exploited_vulnerabilities.csv', usecols=['cveID'])
#Disables warning for using insecure ssl requests option (this is only necessary on the INL network and should be removed if using outside of the network)
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
#Querying database
print(f'Querying database with link: {searchLink}')
try:
response = requests.get(searchLink, verify = False)
except Exception as e:
print('NVD request failed')
#Checking we got results from the search term
try:
results = json.loads(response.text)
except Exception as e:
print('Problem with converting request results to JSON')
sys.exit(0)
if results['totalResults'] == 0:
print('No NVD results for the inputted search terms')
sys.exit(0)
print("Creating objects for each vulnerability")
cve_list = []
stix_objs = []
previously_exploited = False
try:
for cve in tqdm(results['vulnerabilities']):
#If the CVE has been exploited, set the val to true in the custom STIX property
if cve['cve']['id'] in exploited_vulns.values:
previously_exploited = True
stix_objs.append(
Vulnerability(
name = cve['cve']['id'],
description = cve['cve']['descriptions'][0]['value'],
revoked = False,
external_references = [{'source_name': 'MITRE', 'url': f'https://www.cve.org/CVERecord?id={cve["cve"]["id"]}'}],
custom_properties={
"exploited": previously_exploited
})
)
target_reference = stix_objs[-1]['id']
cve_list.append(stix_objs[-1]['id'])
now = datetime.now()
for report in cve['cve']['references']:
stix_objs.append(
Report(
name = report['source'],
report_types = ['vulnerability'],
external_references = [{'source_name': 'CVE Reference','url' : f'{report["url"]}'}],
object_refs=[target_reference],
published = now
)
)
stix_objs.append(
Relationship(
target_ref=target_reference,
source_ref=stix_objs[-1]['id'],
relationship_type='derived-from'
)
)
#For every associated CWE, create a vulnerability STIX object. NOTE: This is not deduped yet
try:
for weakness in cve['cve']['weaknesses']:
if weakness['description'][0]['value'] == 'NVD-CWE-Other' or weakness['description'][0]['value'] == 'NVD-CWE-noinfo':
continue
try:
weakness_req = requests.get(f"https://www.opencve.io/api/cwe/{weakness['description'][0]['value']}", auth = HTTPBasicAuth(opencve_user, opencve_pass), verify = False)
weakness_details = json.loads(weakness_req.text)
except Exception as e:
continue
stix_objs.append(
Vulnerability(
name = weakness_details['id'],
description = f'{weakness_details["name"]}: {weakness_details["description"]}',
revoked = False,
external_references = [{'description' : weakness['type'], 'source_name': weakness['source']}]
)
)
source_reference = stix_objs[-1]['id']
stix_objs.append(
Relationship(
source_ref = source_reference,
target_ref = target_reference,
relationship_type = 'related-to'
)
)
except Exception as e:
print(str(e))
pass
#Getting vendor and software information from the CPE strings
vendors = []
software_objs = {}
try:
for configuration in cve['cve']['configurations']:
for node in configuration['nodes']:
for cpe_string in node['cpeMatch']:
vendor = cpe_string['criteria'].split(':')[3]
if vendor not in vendors:
vendors.append(vendor)
stix_objs.append(
Identity(
name = vendor,
)
)
source_reference = stix_objs[-1]['id']
stix_objs.append(
Relationship(
source_ref=source_reference,
target_ref= target_reference,
relationship_type = 'related-to'
)
)
software = cpe_string['criteria'].split(':')[4]
software_version = cpe_string['criteria'].split(':')[5]
if software in software_objs:
if software_version in software_objs[software]:
continue
else:
software_objs[software].append(software_version)
stix_objs.append(
Software(
name = f'{software}',
version = f'{software_version}'
)
)
source_reference = stix_objs[-1]['id']
stix_objs.append(
Relationship(
source_ref= source_reference,
target_ref= target_reference,
relationship_type='related-to'
)
)
else:
software_objs[software] = []
software_objs[software].append(software_version)
stix_objs.append(
Software(
name = f'{software}',
version = f'{software_version}'
)
)
source_reference = stix_objs[-1]['id']
stix_objs.append(
Relationship(
source_ref= source_reference,
target_ref= target_reference,
relationship_type='related-to'
)
)
except Exception as e:
continue
now = datetime.now()
time = now.strftime("%m/%d/%Y %H:%M:%S")
note_obj = Note(
content = f"NVD queried on {time} using search terms OR cpe_string: {query}",
object_refs = cve_list
)
stix_objs.append(note_obj)
note_id = stix_objs[-1]['id']
for obj in cve_list:
stix_objs.append(
Relationship(
source_ref=obj,
target_ref=note_id,
relationship_type='derived-from'
)
)
except Exception as e:
print(str(e))
#print('No CVEs created')
return stix_objs
def displayArgs():
print('Usage:')
print('\tUse a CPE string to query the NVD for CVEs')
print('\t python nvd2stix.py cpe <cpe_string> <output_file>')
print()
print('\tQuery the NVD using a custom search term')
print('\t python nvd2stix.py search <query query2 ...> <output_file>')
print()
print('\t query: Search term to query the NVD (e.g. adobe acrobat reader)')
print('\t cpe_string: CPE string to be searched (e.g. cpe:2.3:a:ssh:ssh:1.2.6:*:*:*:*:*:*:*)')
print('\t output_file: STIX file containing created vulnerability objects (e.g. STIXOutput.json)')
sys.exit(0)
if __name__=='__main__':
try:
sys.argv[1]
except:
print('Incorrect arguments')
displayArgs()
#CPE String search, rudimentary, only grabs CVEs
if sys.argv[1] == 'cpe':
objs = cpe_search(sys.argv[2])
bundle = Bundle(objs, allow_custom=True)
print("Writing Bundle")
if os.path.isfile(sys.argv[3]):
with open(sys.argv[3], 'w') as output:
output.write(bundle.serialize())
else:
with open(sys.argv[3], 'x') as output:
output.write(bundle.serialize())
#Search term, adds context with CWEs, product IDs, and note node displaying the date searched and terms used
elif sys.argv[1] == 'search':
terms = []
for i in range(2, len(sys.argv) - 1):
terms.append(sys.argv[i])
stix_objs = term_search(terms)
bundle = Bundle(stix_objs, allow_custom=True)
print("Writing Bundle")
if os.path.isfile(sys.argv[len(sys.argv) - 1]):
with open(sys.argv[len(sys.argv) - 1], 'w') as output:
output.write(bundle.serialize())
else:
with open(sys.argv[len(sys.argv) - 1], 'x') as output:
output.write(bundle.serialize())
else:
print('Incorrect arguments')
displayArgs()