Path: blob/main/notebooks/chainnetwork-random.py
597 views
# ---1# jupyter:2# jupytext:3# formats: py:percent,ipynb4# 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# # Chain network CyberBattle Gym played by a random agent2223# %% [markdown]24# # Gym random agent attacking a chain-like network25#26# ## Chain network27# We consider a computer network of Windows and Linux machines where each machine has vulnerability28# granting access to another machine as per the following pattern:29#30# Start ---> (Linux ---> Windows ---> ... Linux ---> Windows)* ---> Linux[Flag]31#32# The network is parameterized by the length of the central Linux-Windows chain.33# The start node leaks the credentials to connect to all other nodes:34#35# For each `XXX ---> Windows` section, the XXX node has:36# - a local vulnerability exposing the RDP password to the Windows machine37# - a bunch of other trap vulnerabilities (high cost with no outcome)38# For each `XXX ---> Linux` section,39# - the Windows node has a local vulnerability exposing the SSH password to the Linux machine40# - a bunch of other trap vulnerabilities (high cost with no outcome)41#42# The chain is terminated by one node with a flag (reward).4344# %% [markdown]45# ## Benchmark46# The following plot shows the average and one standard deviation cumulative reward over time as a random agent attacks the network.4748# %%49# %%HTML50# <img src="random_plot.png" width="300">5152# %%53import sys54import logging55import gymnasium as gym56import cyberbattle.simulation.actions as actions57import cyberbattle._env.cyberbattle_env as cyberbattle_env58import cyberbattle.agents.random_agent as random_agent59import cyberbattle.samples.chainpattern.chainpattern as chainpattern60import importlib6162importlib.reload(actions)63importlib.reload(cyberbattle_env)64importlib.reload(chainpattern)6566logging.basicConfig(stream=sys.stdout, level=logging.ERROR, format="%(levelname)s: %(message)s")6768# %%69# chainpattern.create_network_chain_link(2)7071# %%72gym_env = gym.make("CyberBattleChain-v0", size=10, attacker_goal=None).unwrapped73assert isinstance(gym_env, cyberbattle_env.CyberBattleEnv)7475# %%76gym_env.environment7778# %%79gym_env.environment.network.nodes8081# %%82gym_env.action_space8384# %%85gym_env.action_space.sample()8687# %%88gym_env.observation_space.sample()8990# %%91for i in range(100):92gym_env.sample_valid_action()9394# %% tags=["parameters"]95iterations = 100009697# %%98random_agent.run_random_agent(1, iterations, gym_env)99100# %%101o, r, d, _, i = gym_env.step(gym_env.sample_valid_action())102103# %%104o105106# %%107108109