/
cve_mitre.py
executable file
·162 lines (126 loc) · 3.92 KB
/
cve_mitre.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
#!/usr/bin/env python
import re
import urlparse
import fileinput
import os
import sys
import time
from dateutil import parser
from datetime import datetime
from time import mktime
from xml.sax.handler import ContentHandler
from xml.sax import parse
from common import CRITERIA, VULNERABILITY
from common import log, to_date, get_statistics, get_criteria_one
from common import DataFileWriter
statistics = {}
statistics_compare = {}
class MyContentHandler(ContentHandler):
def __init__(self):
ContentHandler.__init__(self)
self.seqs = []
self.date = []
self.desc = []
self.index = 0
self.indesc = False
self.result_criteria_one = 0
def startElement(self, name, attrs):
if name == "item":
self.seqs.append(attrs.getValue("seq"))
if name == "phase":
assert len(self.seqs) == 1
self.date.append(attrs.getValue("date"))
if name == "desc":
if len(self.date) == 0:
self.date.append("19990101")
self.indesc = True
def endElement(self, name):
if name == "desc":
self.index += 1
self.indesc = False
assert len(self.seqs) == 1
assert len(self.date) == 1
assert len(self.desc) == 1
seqs = self.seqs.pop()
date = self.date.pop()
desc = self.desc.pop()
quarter = get_quarter(date)
stat = get_statistics(desc)
stat_criteria = get_criteria_one(desc)
if not statistics.has_key(quarter):
statistics[quarter] = []
for i in range(len(VULNERABILITY)):
statistics[quarter].append(0)
statistics[quarter][stat] += 1
if not statistics_compare.has_key(quarter):
statistics_compare[quarter] = []
for i in range(len(CRITERIA)):
statistics_compare[quarter].append(0)
statistics_compare[quarter][stat_criteria] += 1
print "[{}] {} [{},{}] - {} - {}".format(self.index, quarter, seqs,date, statistics[quarter], statistics_compare[quarter])
def characters(self, data):
if self.indesc:
if len(self.desc) == 1:
old_data = self.desc.pop()
data = old_data + data
self.desc.append(data)
def get_quarter(date):
d1 = time.strptime(date, "%Y%m%d")
d2 = datetime.fromtimestamp(mktime(d1))
current_year = d2.year
if d2.month <= 3:
current_q = 1
elif d2.month <= 6:
current_q = 2
elif d2.month <= 9:
current_q = 3
else:
current_q = 4
return str(current_year) + "Q" + str(current_q)
def main(argv):
filename_1 = "cve_mitre.dat"
filename_2 = "cve_mitre_criteria.dat"
file_1 = DataFileWriter(filename_1)
file_2 = DataFileWriter(filename_2)
handler = MyContentHandler()
parse('allitems.xml', handler)
for k in sorted(statistics.keys()):
v = statistics[k]
v2 = statistics_compare[k]
line_1 = []
line_2 = []
date = to_date(k)
use_after = v2[CRITERIA['criteria_1']]
stackc = v[VULNERABILITY['stackc']]
heapc = v[VULNERABILITY['heapc']]
intc = v[VULNERABILITY['intc']]
pointc = v[VULNERABILITY['pointc']]
fmtc = v[VULNERABILITY['fmtc']]
otherc = v[VULNERABILITY['otherc']]
total = stackc + heapc + intc + pointc + fmtc + otherc
if total == 0:
print "ERROR (total=0) - {} - {} <-> {} <-> {}".format(k,v, v2, percentage)
percentage = 0
else:
percentage = (float(use_after) / float(total)) * float(100)
line_1.append(str(date))
line_1.append(str(stackc))
line_1.append(str(heapc))
line_1.append(str(intc))
line_1.append(str(pointc))
line_1.append(str(fmtc))
line_1.append(str(otherc))
line_1.append(str(total))
line_2.append(str(date))
line_2.append(str(use_after))
line_2.append(str(total))
line_2.append(str(percentage))
file_1.append("\t".join(line_1))
file_2.append("\t".join(line_2))
print "{} - {} <-> {} <-> {}".format(k,v, v2, percentage)
file_1.close()
file_2.close()
return 0
# ENTRY POINT
if __name__ == "__main__":
sys.exit(main(sys.argv))