-
Notifications
You must be signed in to change notification settings - Fork 10
/
CWEInserter.py
168 lines (138 loc) · 6.01 KB
/
CWEInserter.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
import os
import fnmatch
from neo4j import exceptions
class CWEInserter:
def __init__(self, driver, import_path):
self.driver = driver
self.import_path = import_path
# Cypher Query to insert CWE reference Cypher Script
def query_cwe_reference_script(self, file):
cwes_cypher_file = open(self.import_path + "CWEs_reference.cypher", "r")
query = cwes_cypher_file.read()
query = query.replace('cweReferenceFilesToImport', f"'{file}'")
try:
with self.driver.session() as session:
session.run(query)
except exceptions.CypherError as e:
print(f"CypherError: {e}")
except exceptions.DriverError as e:
print(f"DriverError: {e}")
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {e}")
print("\nCWE Files: " + file + " insertion completed. \n----------")
# Cypher Query to insert CWE weakness Cypher Script
def query_cwe_weakness_script(self, file):
cwes_cypher_file = open(self.import_path + "CWEs_weakness.cypher", "r")
query = cwes_cypher_file.read()
query = query.replace('cweWeaknessFilesToImport', f"'{file}'")
try:
with self.driver.session() as session:
session.run(query)
except exceptions.CypherError as e:
print(f"CypherError: {e}")
except exceptions.DriverError as e:
print(f"DriverError: {e}")
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {e}")
print("\nCWE Files: " + file + " insertion completed. \n----------")
# Cypher Query to insert CWE category Cypher Script
def query_cwe_category_script(self, file):
cwes_cypher_file = open(self.import_path + "CWEs_category.cypher", "r")
query = cwes_cypher_file.read()
query = query.replace('cweCategoryFilesToImport', f"'{file}'")
try:
with self.driver.session() as session:
session.run(query)
except exceptions.CypherError as e:
print(f"CypherError: {e}")
except exceptions.DriverError as e:
print(f"DriverError: {e}")
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {e}")
print("\nCWE Files: " + file + " insertion completed. \n----------")
# Cypher Query to insert CWE view Cypher Script
def query_cwe_view_script(self, file):
cwes_cypher_file = open(self.import_path + "CWEs_view.cypher", "r")
query = cwes_cypher_file.read()
query = query.replace('cweViewFilesToImport', f"'{file}'")
try:
with self.driver.session() as session:
session.run(query)
except exceptions.CypherError as e:
print(f"CypherError: {e}")
except exceptions.DriverError as e:
print(f"DriverError: {e}")
except Exception as e:
# Handle other exceptions
print(f"An error occurred: {e}")
print("\nCWE Files: " + file + " insertion completed. \n----------")
# Configure CWE Files and CWE Cypher Script for insertion
def cwe_insertion(self):
print("\nInserting CWE Files to Database...")
files = self.files_to_insert_cwe_reference()
for f in files:
print('Inserting ' + f)
self.query_cwe_reference_script(f)
files = self.files_to_insert_cwe_weakness()
for f in files:
print('Inserting ' + f)
self.query_cwe_weakness_script(f)
files = self.files_to_insert_cwe_category()
for f in files:
print('Inserting ' + f)
self.query_cwe_category_script(f)
files = self.files_to_insert_cwe_view()
for f in files:
print('Inserting ' + f)
self.query_cwe_view_script(f)
# Define which Dataset and Cypher files will be imported on CWE reference Insertion
def files_to_insert_cwe_reference(self):
listOfFiles = os.listdir(self.import_path + "mitre_cwe/splitted/")
pattern = "*.json"
reference_files = []
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
if entry.startswith("cwe_reference"):
reference_files.append("mitre_cwe/splitted/" + entry)
else:
continue
return reference_files
# Define which Dataset and Cypher files will be imported on CWE weakness Insertion
def files_to_insert_cwe_weakness(self):
listOfFiles = os.listdir(self.import_path + "mitre_cwe/splitted/")
pattern = "*.json"
weakness_files = []
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
if entry.startswith("cwe_weakness"):
weakness_files.append("mitre_cwe/splitted/" + entry)
else:
continue
return weakness_files
# Define which Dataset and Cypher files will be imported on CWE category Insertion
def files_to_insert_cwe_category(self):
listOfFiles = os.listdir(self.import_path + "mitre_cwe/splitted/")
pattern = "*.json"
category_files = []
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
if entry.startswith("cwe_category"):
category_files.append("mitre_cwe/splitted/" + entry)
else:
continue
return category_files
# Define which Dataset and Cypher files will be imported on CWE view Insertion
def files_to_insert_cwe_view(self):
listOfFiles = os.listdir(self.import_path + "mitre_cwe/splitted/")
pattern = "*.json"
view_files = []
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
if entry.startswith("cwe_view"):
view_files.append("mitre_cwe/splitted/" + entry)
else:
continue
return view_files