Path: blob/main/notebooks/c2_interactive_interface.py
597 views
# ---1# jupyter:2# jupytext:3# formats: ipynb,py:percent4# text_representation:5# extension: .py6# format_name: percent7# format_version: '1.3'8# jupytext_version: 1.16.49# kernelspec:10# display_name: cybersim11# language: python12# name: cybersim13# ---1415# %% [markdown]16# pyright: reportUnusedExpression=false1718# %% [markdown]19# Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.20#21# # Command and Control interface22# 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.2324# %%25import networkx as nx26from tabulate import tabulate27import cyberbattle.simulation.model as model28import cyberbattle.simulation.actions as actions29import cyberbattle.simulation.commandcontrol as commandcontrol30import importlib3132importlib.reload(model)33importlib.reload(actions)34importlib.reload(commandcontrol)35import plotly.offline as plo3637plo.init_notebook_mode(connected=True) # type: ignore38# %matplotlib inline3940# %% [markdown]41# We first create a simulation environment from a randomly generated network graph.4243# %%44g = nx.erdos_renyi_graph(35, 0.05, directed=True)45g = model.assign_random_labels(g)46env = model.Environment(network=g, vulnerability_library=dict([]), identifiers=model.SAMPLE_IDENTIFIERS)4748# %% [markdown]49# We create the `CommandControl` object used to the environment and execute actions, and plot the graph explored so far.50#5152# %%53c = commandcontrol.CommandControl(env)5455# %%56c.plot_nodes()57print("Nodes disovered so far: " + str(c.list_nodes()))58starting_node = c.list_nodes()[0]["id"]5960# %% [markdown]61# 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.6263# %%64dbg = commandcontrol.EnvironmentDebugging(c)6566# %%67env.plot_environment_graph()68print(nx.info(env.network)) # type: ignore6970# %%71print(tabulate(c.list_all_attacks(), {}))7273# %%74outcome = c.run_attack(starting_node, "RecentlyAccessedMachines")75outcome7677# %%78c.plot_nodes()7980# %%81print(tabulate(c.list_nodes(), {}))8283# %%84print(tabulate(c.list_all_attacks(), {}))858687