-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
304 lines (252 loc) · 11.6 KB
/
utils.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import numpy as np
import pandas as pd, tabulate
from tabulate import tabulate
import statistics
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import fpgrowth
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import MinMaxScaler
import domain
import re
from typing import List
def initializeControls(controls : List['domain.Control']) -> None:
dfcontrol = pd.read_excel('controls.xlsx', sheet_name='nist')
for row in dfcontrol.itertuples():
control = domain.Control(row.controlId)
control.baseline = row.baseline
control.cat = row.category
control.title = row.controlName
controls.append(control)
def initializeTactics(tactics : List['domain.Tactic']) -> None:
dfTactic = pd.read_excel('ttps.xlsx', sheet_name='tactic')
for row in dfTactic.itertuples():
tactic = domain.Tactic(row.tacticId)
tactic.title = row.tacticName
tactics.append(tactic)
def initializeTechniques(techniques : List['domain.Technique']) -> None:
dfTechnique = pd.read_excel('ttps.xlsx', sheet_name='technique')
for row in dfTechnique.itertuples():
ifAny = [x for x in techniques if x.name == row.techniqueId]
if len(ifAny) == 0:
technique = domain.Technique(row.techniqueId)
technique.title = row.techniqueName
techniques.append(technique)
def initializeTacticTechniqueMapping(tactics : List['domain.Tactic'], techniques : List['domain.Technique']) -> None:
dfTechnique = pd.read_excel('ttps.xlsx', sheet_name='technique')
for row in dfTechnique.itertuples():
technique = [x for x in techniques if x.name == row.techniqueId][0]
tactic = [x for x in tactics if x.name == row.tactics][0]
if tactic not in technique.tactics: technique.tactics.append(tactic)
if technique not in tactic.techniques: tactic.techniques.append(technique)
def initializeControlTechniqueMapping(controls : List['domain.Control'], techniques : List['domain.Technique']) -> None:
dfMapping = pd.read_excel('ttp-control.xlsx', sheet_name='map-nist')
for row in dfMapping.itertuples():
ifAny = [x for x in controls if x.name == row.controlId]
if len(ifAny) > 0:
control = ifAny[0]
technique = [x for x in techniques if x.name == row.techniqueId][0]
if technique not in control.ttps: control.ttps.append(technique)
if control not in technique.controls:
technique.controls.append(control)
for tactic in technique.tactics:
if control not in tactic.controls: tactic.controls.append(control)
for technique in techniques:
technique.scCount = len(technique.controls)
def initializeCocTTPs(groups, softwares, cocTTPs):
allTechniques = []
for g in groups:
for te in g.ttps:
allTechniques.append(te)
allTechniques = list( set(allTechniques) )
for s in softwares:
for te in s.ttps:
allTechniques.append(te)
allTechniques = list( set(allTechniques) )
allTechniques.sort(key=lambda t : t.name)
cocTTPs.extend([g.ttps for g in groups])
cocTTPs.extend([s.ttps for s in softwares])
return
def calculateTEC(controls : List['domain.Control']) -> None:
for control in controls:
control.tec = len(control.ttps)
return
def calculateTAC(controls : List['domain.Control'], tactics : List['domain.Tactic']) -> None:
controls = [x for x in controls if x.tec > 0]
tactics.sort(key = lambda t : t.name)
tacticsNameList = [x.name for x in tactics]
tacticsMemberCountList = [len(x.techniques) for x in tactics]
for control in controls:
for index in range(0, len(tacticsNameList)):
count = 0
for ttp in control.ttps:
if tacticsNameList[index] in [x.name for x in ttp.tactics]:
count += 1
control.tac.append( count / tacticsMemberCountList[index] )
control.mtac = statistics.mean(control.tac)
def calculateImportance(controls : List['domain.Control'], techniques : List['domain.Technique']) -> None:
controls = [x for x in controls if x.tec > 0]
techniques = [x for x in techniques if x.scCount > 0]
for ttp in techniques:
for control in ttp.controls:
control.ti += 1/ttp.scCount
for control in controls:
control.ri = control.ti/control.tec
def find_citation(text : str) -> str:
tokens = text.split('(Citation: ')
tokens = tokens[-1].strip().split(' ')
outputText = ''
for index in range(0, len(tokens)):
outputText = outputText + ' ' + tokens[index]
return outputText[0:-1].strip()
def findYear(text : str) -> str:
regexPattern = r'''[0-9][0-9][0-9][0-9]'''
match = re.search(regexPattern, text)
return text[match.span()[0] : match.span()[1]]
def initializeProcedures(procedures : List['domain.Procedure']) -> None:
dfProcedures = pd.read_excel('technique.xlsx', sheet_name='procedure')
dfProcedures = dfProcedures[['sourceId', 'targetId', 'citation']]
dfProcedures['targetId'] = dfProcedures['targetId'].apply(lambda row : row[0:5])
dfProcedures['citation'] = dfProcedures['citation'].apply(find_citation)
dfPRef = pd.read_excel('technique.xlsx', sheet_name='citations')
dfPRef['reference'] = dfPRef['reference'].apply(findYear)
dfPRef = dfPRef.drop_duplicates()
dfm = pd.merge(dfProcedures, dfPRef, how='left', left_on=['citation'], right_on=['citation'])
dfm.drop_duplicates()
for row in dfm.itertuples():
procedure = domain.Procedure(row.sourceId + ':' + row.targetId + ':' + '-'.join(str(row.citation).split(' ')))
procedure.ttp = row.targetId
procedure.year = row.reference
procedure.name = row.sourceId + ':' + row.targetId
if 'G' in procedure.id:
procedure.type = 'group'
else:
procedure.type = 'software'
procedures.append(procedure)
def initializeGroups(groups : List['domain.Group'], techniques : List['domain.Technique']) -> None:
dfGroups = pd.read_excel('groups.xlsx', sheet_name='ttps')
dfGroups = dfGroups[['sourceId', 'targetId']]
dfGroups['targetId'] = dfGroups['targetId'].apply(lambda v : v[0:5])
dfGroups = dfGroups.drop_duplicates()
dfg = dfGroups.groupby(['sourceId'])
for name, group in dfg:
g = domain.Group(name)
for row in group.itertuples():
ttp = [x for x in techniques if x.name == row.targetId][0]
if ttp not in g.ttps:
g.ttps.append(ttp)
for control in ttp.controls:
if control not in g.controls:
g.controls.append(control)
groups.append(g)
def initializeSoftwares(softwares : List['domain.Software'], techniques : List['domain.Technique']) -> None:
dfSoftwares = pd.read_excel('software.xlsx', sheet_name='ttps')
dfSoftwares = dfSoftwares[['sourceId', 'targetId']]
dfSoftwares['targetId'] = dfSoftwares['targetId'].apply(lambda v : v[0:5])
dfSoftwares = dfSoftwares.drop_duplicates()
dfg = dfSoftwares.groupby(['sourceId'])
for name, group in dfg:
software = domain.Software(name)
for row in group.itertuples():
ttp = [x for x in techniques if x.name == row.targetId][0]
if ttp not in software.ttps:
software.ttps.append(ttp)
for control in ttp.controls:
if control not in software.controls:
software.controls.append(control)
softwares.append(software)
def calculateProb(controls : List['domain.Control'], techniques : List['domain.Technique'], procedures : List['domain.Procedure']) -> None:
for p in procedures:
ttp = [x for x in techniques if x.name == p.ttp][0]
ttp.years.append(p.year)
ttp.occurrence += 1
for ttp in techniques:
ttp.prob = ttp.occurrence / len(procedures)
for control in controls:
count = 0
for ttp in control.ttps:
count += ttp.occurrence
control.prob = count / len(procedures)
return
def evaluateControlRedundancy(controls : List['domain.Control']):
mcontrols = [x for x in controls if len(x.ttps) > 0]
for control in mcontrols:
redundancyCount = [len(x.controls) - 1 for x in control.ttps]
control.red = sum(redundancyCount)/len(redundancyCount)
return
def computeTechniqueLikelihood(techniques, cocTTPs):
for te in techniques:
count = 0
for adv in cocTTPs:
if te in adv:
count += 1
te.lk = count/len(cocTTPs)
return
def buildDataSchema(controls : List['domain.Control'], tactics : List['domain.Tactic'], techniques : List['domain.Technique'], procedures : List['domain.Procedure'], groups : List['domain.Group'], softwares : List['domain.Software'], cocTTPs : List[List['domain.Technique']]) -> None:
initializeControls(controls)
initializeTactics(tactics)
initializeTechniques(techniques)
initializeTacticTechniqueMapping(tactics, techniques)
initializeControlTechniqueMapping(controls, techniques)
calculateTEC(controls)
calculateTAC(controls, tactics)
calculateImportance(controls, techniques)
initializeProcedures(procedures)
initializeGroups(groups, techniques)
initializeSoftwares(softwares, techniques)
calculateProb(controls, techniques, procedures)
evaluateControlRedundancy(controls)
initializeCocTTPs(groups, softwares, cocTTPs)
computeTechniqueSupport(techniques, cocTTPs)
computeTechniqueSeverity(techniques, tactics, cocTTPs)
return
def normalizeList(list: List['float']) -> List[float]:
minimum = min(list)
maximum = max(list)
normalizeList = []
for item in list:
normalizeList.append((item - minimum) / (maximum - minimum))
return normalizeList
def minSetCover(universe : set['domain.Technique'], subsets: set['domain.Control'], flag):
if flag == 'all':
elements = set(te for c in subsets for te in c.ttps)
if flag.startswith('TA'):
elements = set(te for c in subsets for te in c.ttps if te.tactics[0].name == flag)
if elements != universe:
return None
covered = set()
cover = []
while covered != elements:
subset = max(subsets, key = lambda c : len(set(c.ttps) - set(covered)))
cover.append(subset)
covered |= set(subset.ttps)
return cover
def computeTechniqueSupport(techniques, cocTTPs):
for te in techniques:
count = 0
for adv in cocTTPs:
if te in adv:
count += 1
te.support = count / len(cocTTPs)
return
def computeTechniqueSeverity(techniques, tactics, cocTTPs):
tactics.sort(key = lambda t : t.name)
for te1 in techniques:
timesTe1 = 0
cooc = []
for adv in cocTTPs:
if te1 in adv: timesTe1 += 1
for ta in tactics:
timesTe1AndTa = 0
for adv in cocTTPs:
if (te1 in adv) and (set(ta.techniques).isdisjoint(set(adv))) == False:
timesTe1AndTa += 1
if timesTe1 > 30: cooc.append(timesTe1AndTa/timesTe1)
else: cooc.append(0)
te1.severity = statistics.median(cooc)
return
def printTopN(objList, N, attr, attrType):
for idx in range(0, N):
if attrType == 'item': print(f'{objList[idx].name}: {objList[idx].title} ==> {getattr(objList[idx], attr)}')
if attrType == 'list': print(f'{objList[idx].name}: {objList[idx].title} ==> {getattr(objList[idx], attr)[0].name}: {getattr(objList[idx], attr)[0].title}')
return