forked from dsto97/CWEParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parserUtils.py
238 lines (215 loc) · 8.75 KB
/
parserUtils.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
import os
from urllib.request import urlretrieve
import zipfile
import xml.etree.ElementTree as ET
class TextParser:
def __init__(self, cwe_file="cwec_v4.13.xml"):
self.cwe_file = cwe_file
def replaceDescription(self):
"""
Places a newline before every Description-tag to simplify parsing
:return: No return value. Generates a new file called tmp.xml with the newly placed tags
"""
tmp = open("tmp.xml", "w", encoding="utf-8")
with open(self.cwe_file, "r", encoding="utf-8") as f:
for line in f.readlines():
line = line.replace("</Description>", "\n</Description>")
tmp.write(line)
tmp.close()
def extractCWE(self):
"""
Extracts every Weakness with given description and prints them to a txt-file
:return: No return, only cwe.txt is created
"""
tmp = open("tmp.xml", "r", encoding="utf-8")
with open("cwe.txt", "w", encoding="utf-8") as f:
lines = tmp.readlines()
for i in range(len(lines)):
j = 1
if "<Weakness ID=" in lines[i]:
f.write(lines[i].replace("<Weakness", "\n<Weakness"))
nextline = lines[i + j]
while "</Description>" not in nextline:
f.write(nextline.replace("\n", ""))
j += 1
nextline = lines[i + j]
continue
tmp.close()
def cweToJson(self):
"""
Creates json-file by replacing tags with json-descriptors
:return: Only cwe.json is created
"""
clean = open("cwe.json", "w", encoding="utf-8")
clean.write("{ \"CWEs\": [")
with open("cwe.txt", "r", encoding="utf-8") as f:
for line in f.readlines():
line = line.replace("\"", "")
if "Weakness ID" in line:
clean.write("{")
line = line.replace("<Weakness ID=", "\"id\":\"")
line = line.replace("Name=", "\", \"name\":\"")
line = line.replace("Abstraction=", "\", \"abstraction\":\"")
line = line.replace("Structure=", "\", \"structure\":\"")
line = line.replace("Status=", "\", \"status\":\"")
line = line.replace("\n", "")
line = line.replace(">", "")
line = line.replace(u"\u0009", "")
line = line.replace("\\", "\\\\")
line = " ".join(line.split())
clean.write(line.strip())
clean.write("\"")
if "<Description>" in line:
line = line.replace("<Description>", ", \"description\":\"")
line = line.replace("</Description>", "")
line = line.replace(u"\u0009", "")
line = line.replace("\\", "\\\\")
line = " ".join(line.split())
clean.write(line.replace("\n", ""))
clean.write("\"},")
clean.write("\n")
clean.write("{}")
clean.write("]}")
clean.close()
def parseCWE(self):
"""
Parses downloaded CWE list and creates a .json file
:return:
"""
self.replaceDescription()
self.extractCWE()
self.cweToJson()
def removeTmpFiles(self):
"""
Removes unnecessary files (.zip, .txt, .xml)
:return: None
"""
filelist = os.listdir()
for item in filelist:
if item.endswith(".zip") or item.endswith(".txt") or item.endswith(".xml"):
os.remove(item)
class CWEDownload:
def __init__(self, filename="cwe_list.zip", link="https://cwe.mitre.org/data/xml/cwec_latest.xml.zip"):
self.link = link
self.filename = filename
def downloadCWEList(self):
"""
Downloads latest cwe list from https://cwe.mitre.org/data/xml/cwec_latest.xml.zip
:return: None
"""
urlretrieve(self.link, filename=self.filename)
def unzipCWEList(self):
"""
Unzips downloaded .zip file
:return: None
"""
with zipfile.ZipFile(self.filename, "r") as zip:
zip.extractall()
def loadAndUnzip(self):
"""
Downloads and unzips .zip archive of CWE list
:return: None
"""
self.downloadCWEList()
self.unzipCWEList()
class ElementTreeParser:
def __init__(self, cwe_file="cwec_v4.13.xml"):
self.cwe_file = cwe_file
def etParser(self):
"""
Uses ElementTree to parse the cwe .xml file to a .json file. Takes the path as argument which should be given by the constructor
:return: None. Produces a file called cwe.json
"""
tree = ET.parse(self.cwe_file)
cwe = open("cwe.json", "w")
cwe.write("{\"CWE\":[\n")
root = tree.getroot()
weaknesses = root.find("{http://cwe.mitre.org/cwe-7}Weaknesses")
for weakness in weaknesses:
id_name = "{{\"id\":\"{}\", \"name\": \"{}\", ".format(weakness.attrib["ID"],
weakness.attrib["Name"].replace("\\", "\\\\"))
for des in weakness:
if "{http://cwe.mitre.org/cwe-7}Description" == des.tag:
tmp_des = des.text
tmp_des = tmp_des.replace("\\", "\\\\").replace("\"", "\\\"")
id_name_des = id_name + "\"description\":\"{}\"}},".format(tmp_des). \
replace("\n", " "). \
replace(u"\u0009", " ") + "\n"
id_name_des = " ".join(id_name_des.split()) + "\n"
cwe.write(id_name_des)
cwe.close()
with open("cwe.json", "rb+") as f:
f.seek(-4, 2)
f.truncate()
f.write(b"}\n]}")
self.getCategories()
def getCategories(self):
"""
Creates another .json file with name, ids and summaries of all categories. It's automatically called during
the parsing process
:return: None
"""
tree = ET.parse(self.cwe_file)
cats = open("categories.json", "w")
cats.write("{\"Categories\":[\n")
root = tree.getroot()
categories = root.find("{http://cwe.mitre.org/cwe-7}Categories")
for category in categories:
id_name = "{{\"id\":\"{}\", \"name\": \"{}\", ".format(category.attrib["ID"],
category.attrib["Name"].replace("\\", "\\\\"))
for cat in category:
if "{http://cwe.mitre.org/cwe-7}Summary" == cat.tag:
tmp_des = cat.text
tmp_des = tmp_des.replace("\\", "\\\\").replace("\"", "\\\"")
id_name_des = id_name + "\"summary\":\"{}\"}},".format(tmp_des). \
replace("\n", " "). \
replace(u"\u0009", " ") + "\n"
id_name_des = " ".join(id_name_des.split()) + "\n"
cats.write(id_name_des)
cats.close()
with open("categories.json", "rb+") as f:
f.seek(-4, 2)
f.truncate()
f.write(b"}\n]}")
def combineJsonFiles(self):
"""
Combines both .json files to one .json file
:return: None. Creates a file called combined.json
"""
with open("combined.json", "w") as com:
with open("categories.json", "r") as cat:
with open("cwe.json", "r") as cwe:
for line in cwe:
if "]}" in line:
line = "],\n"
com.write(line)
for line in cat:
if "\"Categories\"" in line:
line = "\"Categories\":[\n"
com.write(line)
def downloadAndUnzip(self):
"""
Loads .xml file from https://cwe.mitre.org/data/downloads.html and unzips it
:return: None
"""
loader = CWEDownload()
loader.loadAndUnzip()
def removeTmpFiles(self):
"""
Removes unnecessary files (.zip, .txt, .xml)
:return: None
"""
filelist = os.listdir()
for item in filelist:
if item.endswith(".zip") or item.endswith(".txt") or item.endswith(".xml"):
os.remove(item)
def parseCWE(self, combine=False):
"""
Starts the whole parsing process and deletes unnecessary data afterwards
:return: None
"""
self.downloadAndUnzip()
self.etParser()
if combine:
self.combineJsonFiles()
self.removeTmpFiles()