-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis.py
120 lines (98 loc) · 5.03 KB
/
Analysis.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
import json
import os
from datetime import datetime
def categorize_cve_by_type(cve_description, clientSearch):
parts = clientSearch.split(" ")
for part in parts:
cve_description = cve_description.lower()
if part not in cve_description:
return None
return True
def load_categorized_cve_data(directory, clientSearch):
cve_details = []
for filename in os.listdir(directory):
if filename.endswith(".json"):
file_path = os.path.join(directory, filename)
with open(file_path, "r") as file:
data = json.load(file)
for item in data:
cve_id = item["cve"]["id"]
for desc in item["cve"]["descriptions"]:
if desc["lang"] == "en":
if (
categorize_cve_by_type(desc["value"], clientSearch)
== None
):
continue
# pub_date = datetime.strptime(
# item["cve"]["published"], "%Y-%m-%dT%H:%M:%S.%f"
# )
pub_date = item["cve"]["published"]
cve_status = item["cve"]["vulnStatus"]
if "cvssMetricV31" in item["cve"]["metrics"]:
cve_metrics = item["cve"]["metrics"]["cvssMetricV31"][0]
exploitability_score = cve_metrics.get(
"exploitabilityScore"
)
impact_score = cve_metrics.get("impactScore")
attack_vector = cve_metrics["cvssData"].get(
"attackVector"
)
base_severity = cve_metrics["cvssData"].get(
"baseSeverity"
)
elif "cvssMetricV30" in item["cve"]["metrics"]:
cve_metrics = item["cve"]["metrics"]["cvssMetricV30"][0]
exploitability_score = cve_metrics.get(
"exploitabilityScore"
)
impact_score = cve_metrics.get("impactScore")
attack_vector = cve_metrics["cvssData"].get(
"attackVector"
)
base_severity = cve_metrics["cvssData"].get(
"baseSeverity"
)
elif "cvssMetricV2" in item["cve"]["metrics"]:
cve_metrics = item["cve"]["metrics"]["cvssMetricV2"][0]
exploitability_score = cve_metrics.get(
"exploitabilityScore"
)
impact_score = cve_metrics.get("impactScore")
attack_vector = cve_metrics["cvssData"].get(
"accessVector"
)
base_severity = cve_metrics.get("baseSeverity")
else:
cve_metrics = None
exploitability_score = None
impact_score = None
attack_vector = None
base_severity = None
cve_details.append(
{
"id": cve_id,
"description": desc["value"],
"pub_date": pub_date,
"cve_status": cve_status,
"exploitability_score": exploitability_score,
"impact_score": impact_score,
"attack_vector": attack_vector,
"base_severity": base_severity,
}
)
return cve_details
def save_cve_data_by_type_to_json(cve_details, output_filename):
cve_by_type = {}
for cve_type, (cve_id, pub_date) in cve_details:
if cve_type not in cve_by_type:
cve_by_type[cve_type] = []
cve_by_type[cve_type].append(
{"id": cve_id, "published_date": pub_date.isoformat()}
)
with open(output_filename, "w") as file:
json.dump(cve_by_type, file, indent=4)
def analyze_search_string(clientSearch):
directory = "all_data"
cve_details = load_categorized_cve_data(directory, clientSearch)
return cve_details