Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/CyberBattleSim
Path: blob/main/notebooks/c2_interactive_interface.py
597 views
1
# ---
2
# jupyter:
3
# jupytext:
4
# formats: ipynb,py:percent
5
# text_representation:
6
# extension: .py
7
# format_name: percent
8
# format_version: '1.3'
9
# jupytext_version: 1.16.4
10
# kernelspec:
11
# display_name: cybersim
12
# language: python
13
# name: cybersim
14
# ---
15
16
# %% [markdown]
17
# pyright: reportUnusedExpression=false
18
19
# %% [markdown]
20
# Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.
21
#
22
# # Command and Control interface
23
# This notebooks shows how to interact with the command&control server to observe the environment and initiate actions on the nodes where the attacker client is installed.
24
25
# %%
26
import networkx as nx
27
from tabulate import tabulate
28
import cyberbattle.simulation.model as model
29
import cyberbattle.simulation.actions as actions
30
import cyberbattle.simulation.commandcontrol as commandcontrol
31
import importlib
32
33
importlib.reload(model)
34
importlib.reload(actions)
35
importlib.reload(commandcontrol)
36
import plotly.offline as plo
37
38
plo.init_notebook_mode(connected=True) # type: ignore
39
# %matplotlib inline
40
41
# %% [markdown]
42
# We first create a simulation environment from a randomly generated network graph.
43
44
# %%
45
g = nx.erdos_renyi_graph(35, 0.05, directed=True)
46
g = model.assign_random_labels(g)
47
env = model.Environment(network=g, vulnerability_library=dict([]), identifiers=model.SAMPLE_IDENTIFIERS)
48
49
# %% [markdown]
50
# We create the `CommandControl` object used to the environment and execute actions, and plot the graph explored so far.
51
#
52
53
# %%
54
c = commandcontrol.CommandControl(env)
55
56
# %%
57
c.plot_nodes()
58
print("Nodes disovered so far: " + str(c.list_nodes()))
59
starting_node = c.list_nodes()[0]["id"]
60
61
# %% [markdown]
62
# For debugging purpose it's also convient to view the internals of the environment via the `EnvironmentDebugging` object. For instance we can use it to plot the entire graph, including nodes that were not discovered yet by the attacker.
63
64
# %%
65
dbg = commandcontrol.EnvironmentDebugging(c)
66
67
# %%
68
env.plot_environment_graph()
69
print(nx.info(env.network)) # type: ignore
70
71
# %%
72
print(tabulate(c.list_all_attacks(), {}))
73
74
# %%
75
outcome = c.run_attack(starting_node, "RecentlyAccessedMachines")
76
outcome
77
78
# %%
79
c.plot_nodes()
80
81
# %%
82
print(tabulate(c.list_nodes(), {}))
83
84
# %%
85
print(tabulate(c.list_all_attacks(), {}))
86
87