Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/CyberBattleSim
Path: blob/main/notebooks/chainnetwork-random.py
597 views
1
# ---
2
# jupyter:
3
# jupytext:
4
# formats: py:percent,ipynb
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
# # Chain network CyberBattle Gym played by a random agent
23
24
# %% [markdown]
25
# # Gym random agent attacking a chain-like network
26
#
27
# ## Chain network
28
# We consider a computer network of Windows and Linux machines where each machine has vulnerability
29
# granting access to another machine as per the following pattern:
30
#
31
# Start ---> (Linux ---> Windows ---> ... Linux ---> Windows)* ---> Linux[Flag]
32
#
33
# The network is parameterized by the length of the central Linux-Windows chain.
34
# The start node leaks the credentials to connect to all other nodes:
35
#
36
# For each `XXX ---> Windows` section, the XXX node has:
37
# - a local vulnerability exposing the RDP password to the Windows machine
38
# - a bunch of other trap vulnerabilities (high cost with no outcome)
39
# For each `XXX ---> Linux` section,
40
# - the Windows node has a local vulnerability exposing the SSH password to the Linux machine
41
# - a bunch of other trap vulnerabilities (high cost with no outcome)
42
#
43
# The chain is terminated by one node with a flag (reward).
44
45
# %% [markdown]
46
# ## Benchmark
47
# The following plot shows the average and one standard deviation cumulative reward over time as a random agent attacks the network.
48
49
# %%
50
# %%HTML
51
# <img src="random_plot.png" width="300">
52
53
# %%
54
import sys
55
import logging
56
import gymnasium as gym
57
import cyberbattle.simulation.actions as actions
58
import cyberbattle._env.cyberbattle_env as cyberbattle_env
59
import cyberbattle.agents.random_agent as random_agent
60
import cyberbattle.samples.chainpattern.chainpattern as chainpattern
61
import importlib
62
63
importlib.reload(actions)
64
importlib.reload(cyberbattle_env)
65
importlib.reload(chainpattern)
66
67
logging.basicConfig(stream=sys.stdout, level=logging.ERROR, format="%(levelname)s: %(message)s")
68
69
# %%
70
# chainpattern.create_network_chain_link(2)
71
72
# %%
73
gym_env = gym.make("CyberBattleChain-v0", size=10, attacker_goal=None).unwrapped
74
assert isinstance(gym_env, cyberbattle_env.CyberBattleEnv)
75
76
# %%
77
gym_env.environment
78
79
# %%
80
gym_env.environment.network.nodes
81
82
# %%
83
gym_env.action_space
84
85
# %%
86
gym_env.action_space.sample()
87
88
# %%
89
gym_env.observation_space.sample()
90
91
# %%
92
for i in range(100):
93
gym_env.sample_valid_action()
94
95
# %% tags=["parameters"]
96
iterations = 10000
97
98
# %%
99
random_agent.run_random_agent(1, iterations, gym_env)
100
101
# %%
102
o, r, d, _, i = gym_env.step(gym_env.sample_valid_action())
103
104
# %%
105
o
106
107
# %%
108
109