-
Notifications
You must be signed in to change notification settings - Fork 261
/
randomnetwork.py
92 lines (73 loc) · 2.26 KB
/
randomnetwork.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# ---
# jupyter:
# jupytext:
# formats: py:percent,ipynb
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.4
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %% [markdown]
# pyright: reportUnusedExpression=false
# %% [markdown]
# Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.
#
# # Randomly generated CyberBattle network environment (experimental)
# %%
import numpy as np
import cyberbattle.simulation.model as model
import cyberbattle.agents.random_agent as random_agent
from typing import List
import cyberbattle.simulation.generate_network as g
# %matplotlib inline
# %%
traffic = g.generate_random_traffic_network(
seed=1,
n_clients=50,
n_servers={
"SMB": 15,
"HTTP": 15,
"RDP": 15,
},
alpha=np.array([(1, 1), (0.2, 0.5)], dtype=float),
beta=np.array([(1000, 10), (10, 100)], dtype=float),
)
# %%
network = g.cyberbattle_model_from_traffic_graph(traffic)
# %%
env = model.Environment(network=network, vulnerability_library=dict([]), identifiers=g.ENV_IDENTIFIERS)
env.plot_environment_graph()
# %%
network.nodes
# %%
def ports_from_vuln(vuln: model.VulnerabilityInfo) -> List[model.PortName]:
if isinstance(vuln.outcome, model.LeakedCredentials):
return [c.port for c in vuln.outcome.credentials]
else:
return []
# %%
all_existing_ports = (
set({port for _, v in env.vulnerability_library.items() for port in ports_from_vuln(v)})
.union({port for _, node_info in env.nodes() for _, v in node_info.vulnerabilities.items() for port in ports_from_vuln(v)})
.union({service.name for _, node_info in env.nodes() for service in node_info.services})
)
# %%
all_existing_ports
# %%
[node_info.services for _, node_info in env.nodes()]
# %%
# import sys, logging
# logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(levelname)s: %(message)s")
import gymnasium as gym
import cyberbattle._env.cyberbattle_env as cyberbattle_env
# %%
gym_env = gym.make("CyberBattleRandom-v0").unwrapped
# %%
assert isinstance(gym_env, cyberbattle_env.CyberBattleEnv)
random_agent.run_random_agent(1, 5600, gym_env)
# %%