Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/CyberBattleSim
Path: blob/main/notebooks/notebook_randlookups.py
597 views
1
# ---
2
# jupyter:
3
# jupytext:
4
# cell_metadata_filter: -all
5
# formats: py:percent,ipynb
6
# text_representation:
7
# extension: .py
8
# format_name: percent
9
# format_version: '1.3'
10
# jupytext_version: 1.16.4
11
# kernelspec:
12
# display_name: Python 3 (ipykernel)
13
# language: python
14
# name: python3
15
# ---
16
17
# %%
18
# Copyright (c) Microsoft Corporation.
19
# Licensed under the MIT License.
20
21
"""Random exploration with credential lookup exploitation (notebook)
22
23
This notebooks can be run directly from VSCode, to generate a
24
traditional Jupyter Notebook to open in your browser
25
you can run the VSCode command `Export Currenty Python File As Jupyter Notebook`.
26
"""
27
28
# pylint: disable=invalid-name
29
30
# %%
31
import os
32
import gymnasium as gym
33
import logging
34
import sys
35
from cyberbattle._env.cyberbattle_env import AttackerGoal
36
from cyberbattle.agents.baseline.agent_randomcredlookup import CredentialCacheExploiter
37
import cyberbattle.agents.baseline.learner as learner
38
import cyberbattle.agents.baseline.plotting as p
39
import cyberbattle.agents.baseline.agent_wrapper as w
40
from cyberbattle.agents.baseline.agent_wrapper import Verbosity
41
from cyberbattle._env.cyberbattle_env import CyberBattleEnv
42
43
# %%
44
# %matplotlib inline
45
46
# %%
47
logging.basicConfig(stream=sys.stdout, level=logging.ERROR, format="%(levelname)s: %(message)s")
48
49
50
# %%
51
cyberbattlechain_10 = gym.make("CyberBattleChain-v0", size=10, attacker_goal=AttackerGoal(own_atleast_percent=1.0)).unwrapped
52
assert isinstance(cyberbattlechain_10, CyberBattleEnv)
53
54
# %%
55
ep = w.EnvironmentBounds.of_identifiers(maximum_total_credentials=12, maximum_node_count=12, identifiers=cyberbattlechain_10.identifiers)
56
57
# %% {"tags": ["parameters"]}
58
iteration_count = 9000
59
training_episode_count = 50
60
eval_episode_count = 5
61
plots_dir = 'plots'
62
63
# %%
64
os.makedirs(plots_dir, exist_ok=True)
65
66
credexplot = learner.epsilon_greedy_search(
67
cyberbattlechain_10,
68
learner=CredentialCacheExploiter(),
69
environment_properties=ep,
70
episode_count=training_episode_count,
71
iteration_count=iteration_count,
72
epsilon=0.90,
73
render=False,
74
epsilon_multdecay=0.75, # 0.999,
75
epsilon_minimum=0.01,
76
verbosity=Verbosity.Quiet,
77
title="Random+CredLookup",
78
)
79
80
# %%
81
randomlearning_results = learner.epsilon_greedy_search(
82
cyberbattlechain_10,
83
environment_properties=ep,
84
learner=CredentialCacheExploiter(),
85
episode_count=eval_episode_count,
86
iteration_count=iteration_count,
87
epsilon=1.0, # purely random
88
render=False,
89
verbosity=Verbosity.Quiet,
90
title="Random search",
91
)
92
93
94
# %%
95
p.plot_episodes_length([credexplot])
96
97
p.plot_all_episodes(credexplot)
98
99
all_runs = [credexplot, randomlearning_results]
100
p.plot_averaged_cummulative_rewards(title=f"Benchmark -- max_nodes={ep.maximum_node_count}, episodes={eval_episode_count},\n", all_runs=all_runs,
101
save_at=os.path.join(plots_dir, "randlookups-cumreward.png"))
102
103