This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
244 lines (205 loc) · 8.44 KB
/
main.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
import dash
import dash_html_components as html
import dash_core_components as dcc
import json
import ui.constants
import utils
from attack_graph import BaseGraph, DependencyAttackGraph, StateAttackGraph
from base64 import b64decode
from clustering.white_smyth import Spectral1, Spectral2
from dash.dependencies import Input, Output, State
from embedding.deepwalk import DeepWalk
from embedding.embedding import EmbeddingMethod
from embedding.graphsage import GraphSage
from embedding.hope import Hope
from generation import Generator
from ranking.abraham import ProbabilisticPath
from ranking.homer import RiskQuantifier
from ranking.mehta import PageRankMethod, KuehlmannMethod
from ranking.sheyner import ValueIteration
from typing import Dict, List, Tuple
from ui.drawing import DependencyAttackGraphDrawer, StateAttackGraphDrawer
from ui.layout import generate_layout
app = dash.Dash(__name__)
app.layout = generate_layout()
@app.callback(Output("attack-graph", "data"),
Input("upload-attack-graph", "contents"),
Input("button-generate", "n_clicks"),
State("upload-attack-graph", "filename"),
State("radio-items-graph-type", "value"),
State("input-number-exploits", "value"))
def update_attack_graph(graph_data: str, _: int, filename: str,
graph_type: str, n_exploits: int) -> str:
if graph_data is None:
# The user wants to generate an attack graph
generator = Generator(n_exploits=n_exploits)
if graph_type == "state":
attack_graph = generator.generate_state_attack_graph()
else:
attack_graph = generator.generate_dependency_attack_graph()
else:
# The user wants to load an existing attack graph
decoded_string = b64decode(graph_data.split(",")[1])
extension = utils.get_file_extension(filename)
attack_graph = get_attack_graph_from_string(decoded_string, extension)
if attack_graph is None:
return ""
else:
return attack_graph.write()
@app.callback(Output("table-exploit-ranking", "children"),
Input("attack-graph", "data"),
Input("dropdown-exploit-ranking-method", "value"))
def update_exploit_ranking(graph_data: str,
exploit_ranking_method: str) -> List[html.Div]:
# Get the current attack graph
attack_graph = get_attack_graph_from_string(graph_data)
if attack_graph is None:
return None
# Get an instance of the ranking method
instance = None
if isinstance(attack_graph, StateAttackGraph):
if exploit_ranking_method == "pagerank":
instance = PageRankMethod(attack_graph)
elif exploit_ranking_method == "kuehlmann":
instance = KuehlmannMethod(attack_graph)
elif exploit_ranking_method == "pp":
instance = ProbabilisticPath(attack_graph)
elif isinstance(attack_graph, DependencyAttackGraph):
if exploit_ranking_method == "homer":
instance = RiskQuantifier(attack_graph)
if exploit_ranking_method == "vi":
instance = ValueIteration(attack_graph)
# Apply the method
if instance is None:
return
ranking, scores = instance.rank_exploits()
# Update the UI
return get_table_exploit_ranking(ranking, scores)
@app.callback(Output("checklist-exploits", "options"),
Output("checklist-exploits", "value"),
Input("attack-graph", "data"))
def update_exploits(graph_data: str) -> Tuple[List[Dict[str, str]], List[str]]:
# Get the current attack graph
attack_graph = get_attack_graph_from_string(graph_data)
if attack_graph is None:
return None
# Create the list of exploits
exploits = []
selected_exploits = []
for id_exploit, data in attack_graph.exploits.items():
text: str = data["text"]
# Only keep the first 20 words
text = " ".join(text.split(" ")[:20])
# Add the id at the beginning of the text
text = "{}: {}".format(id_exploit, text)
exploits.append(dict(label=text, value=id_exploit))
selected_exploits.append(id_exploit)
return exploits, selected_exploits
@app.callback(Output("table-clustering", "children"),
Output("parameters", "data"), Input("attack-graph", "data"),
Input("dropdown-clustering-method", "value"),
Input("checklist-exploits", "value"))
def update_clusters_and_parameters(
graph_data: str, clustering_method: str,
selected_exploits: List[str]) -> Tuple[List[html.Div], dict]:
# Get the current attack graph
attack_graph = get_attack_graph_from_string(graph_data)
if attack_graph is None:
return None
# Remove the exploits that are not selected
pruned_graph = attack_graph.get_pruned_graph(
[int(i) for i in selected_exploits])
# Get the list of clusters
clusters = get_clusters(pruned_graph, clustering_method)
# Create the table of clusters
table_clustering = []
if clusters is not None:
table_clustering = []
for id_cluster, data in clusters.items():
table_clustering += [
html.Div(className="table-cell", children=str(id_cluster)),
html.Div(
className="table-cell",
style=dict(backgroundColor="{}".format(data["color"]))),
html.Div(className="table-cell",
children=str(len(data["nodes"])))
]
# Create the dictionary of parameters
parameters = {}
parameters["selected_exploits"] = selected_exploits
parameters["clusters"] = clusters
return table_clustering, parameters
@app.callback(Output("zone-attack-graph", "children"),
Input("parameters", "data"), State("attack-graph", "data"))
def display_attack_graph(parameters: dict, graph_data: str) -> dcc.Graph:
# Get the current attack graph
attack_graph = get_attack_graph_from_string(graph_data)
if attack_graph is None:
return None
if isinstance(attack_graph, StateAttackGraph):
return StateAttackGraphDrawer(attack_graph, parameters).apply()
elif isinstance(attack_graph, DependencyAttackGraph):
return DependencyAttackGraphDrawer(attack_graph, parameters).apply()
else:
return None
def get_table_exploit_ranking(ranking: Dict[int, int],
scores: Dict[int, float]) -> List[html.Div]:
table = []
i_line = 0
while len(table) // 3 < len(ranking):
for id_exploit, position in ranking.items():
if position == i_line:
score = "{:.2e}".format(scores[id_exploit])
if id_exploit is None:
id_exploit = "None"
table += [position, id_exploit, score]
break
i_line += 1
return [
html.Div(className="table-cell", children=table[i])
for i in range(len(table))
]
def get_attack_graph_from_string(string: str,
extension: str = "json") -> BaseGraph:
if string is None:
return None
# Get the type of the current attack graph
data = json.loads(string)
graph_type = data["type"]
# Parse the data
if graph_type == "state":
attack_graph = StateAttackGraph()
else:
attack_graph = DependencyAttackGraph()
attack_graph.parse(string, extension)
return attack_graph
def get_clusters(attack_graph: BaseGraph,
clustering_method: str) -> Dict[str, dict]:
# Get an instance of the clustering method
instance = None
if clustering_method == "spectral1":
instance = Spectral1(attack_graph)
elif clustering_method == "spectral2":
instance = Spectral2(attack_graph)
elif clustering_method == "deepwalk":
instance = DeepWalk(attack_graph)
elif clustering_method == "graphsage":
instance = GraphSage(attack_graph)
elif clustering_method == "hope":
instance = Hope(attack_graph)
if instance is None:
return None
elif isinstance(instance, EmbeddingMethod):
instance.embed()
# Apply clustering
instance.cluster()
# Create the result dictionary
results = {}
clusters = instance.clusters
for i_cluster in sorted(clusters):
nodes = clusters[i_cluster]
color = ui.constants.colors_clusters[i_cluster]
results[str(i_cluster)] = dict(color=color, nodes=nodes)
return results
if __name__ == "__main__":
app.run_server(debug=True)