forked from cisagov/decider
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.py
145 lines (107 loc) · 4.72 KB
/
convert.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
import os
import re
import json
from openpyxl import load_workbook
import app.constants as constants
if __name__ == "__main__":
path = os.path.join(constants.BUILD_SOURCES_DIR, constants.mitigations_mapping_dir)
file = f"{path}/mappings.xlsx"
workbook = load_workbook(file, data_only=True)
main_sheet = workbook["Blue Team Guide"]
uses_sheet = workbook["MITRE Uses"]
mitre_sheet = workbook["MITRE Controls"]
nist_sheet = workbook["NIST Controls"]
ism_sheet = workbook["ISM Controls"]
mitigations = {}
uses = {}
isms = {}
nists = {}
# mitre control uses
overarching_mitigation = ""
overarching_technique = ""
for row_i, row in enumerate(uses_sheet.iter_rows()):
if row_i == 1: continue
technique = ""
# 0: id, 1: sub-technique, 3: use
for i, col in enumerate(row):
col = str(col.value)
if i == 0:
mitigation = re.findall(r".*?\((M[0-9]{4})\)", col)
technique = re.findall(r"(T[0-9]{4})", col)
if len(technique) != 0: overarching_technique = technique[0]
if len(mitigation) != 0: overarching_mitigation = mitigation[0]
if i == 1 and row_i != 0:
technique = overarching_technique
if col != "None":
technique = f"{overarching_technique}.{col.split('.')[1]}"
if i == 3 and row_i != 0 and col != "None" and col != "Use":
if not overarching_mitigation in uses:
uses[overarching_mitigation] = {}
uses[overarching_mitigation][technique] = { "use": col }
# main sheet with all information
overarching_technique = ""
for row_i, row in enumerate(main_sheet.iter_rows()):
if row_i == 0: continue
full_technique = ""
for i, col in enumerate(row):
col = str(col.value)
if i == 0:
technique = re.findall(r".*\((T[0-9]{4})\)", col)
subtechnique = re.findall(r".*\((\.[0-9]{3})\)", col)
if len(technique) != 0:
full_technique = technique[0]
overarching_technique = technique[0]
if len(subtechnique) != 0:
full_technique = overarching_technique + subtechnique[0]
if i == 6:
ism_controls = re.findall(r"ISM-[0-9]{4}", col)
for ism in ism_controls:
if ism not in isms:
isms[ism] = {}
isms[ism][full_technique] = { 'use': None }
if i == 7:
nist_controls = re.findall(r".*?([A-Z]{2}-[0-9]{1,2})", col)
for nist in nist_controls:
if nist not in nists:
nists[nist] = {}
nists[nist][full_technique] = { 'use': None }
# mitre controls
for i, rows in enumerate(mitre_sheet.iter_rows()):
if i == 0: continue
values = { "source": "MITRE", "techniques": [] }
# 0: id, 1: name, 2: description
for i, row in enumerate(rows):
if i == 1: values["name"] = row.value
if i == 2: values["description"] = row.value
if i == 0:
mitigations[row.value] = values
if row.value in uses:
mitigations[row.value]["techniques"] = uses[row.value]
# nist controls
for i, rows in enumerate(nist_sheet.iter_rows()):
if i == 0: continue
values = { "source": "NIST", "techniques": [] }
# 0: id, 1: name, 2: description
for i, row in enumerate(rows):
# not a nist sub-technique
if re.match(r"[A-Z]{2}-[0-9]{1,2}\([0-9]{1,2}\)", str(row.value)): continue
if i == 1: values["name"] = row.value
if i == 2: values["description"] = row.value
if i == 0:
mitigations[row.value] = values
if row.value in nists:
mitigations[row.value]["techniques"] = nists[row.value]
# ism controls
for i, rows in enumerate(ism_sheet.iter_rows()):
if i == 0: continue
values = { "source": "ISM", "techniques": [] }
# 0: id, 1: name, 2: description
for i, row in enumerate(rows):
if i == 1: values["name"] = row.value
if i == 2: values["description"] = row.value
if i == 0:
mitigations[row.value] = values
if row.value in isms:
mitigations[row.value]["techniques"] = isms[row.value]
with open(f"{path}/mappings-v15.1.json", "w") as outfile:
json.dump(mitigations, outfile)