mal-simulator 2.0.0__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mal_simulator-2.0.0/mal_simulator.egg-info → mal_simulator-2.2.0}/PKG-INFO +1 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0/mal_simulator.egg-info}/PKG-INFO +1 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/mal_simulator.egg-info/SOURCES.txt +3 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/__init__.py +11 -5
- mal_simulator-2.2.0/malsim/config/__init__.py +12 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/config/agent_settings.py +0 -76
- mal_simulator-2.2.0/malsim/config/agent_settings_factories.py +80 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/attacker_state.py +1 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/attacker_step.py +1 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/defender_state.py +1 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/defender_step.py +2 -2
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/register_agent.py +1 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/reset_agent.py +1 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/run_simulation.py +1 -2
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/simulator.py +81 -48
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/simulator_state.py +3 -2
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/state_query.py +1 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/keyboard_input.py +0 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/scenario/scenario.py +2 -2
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/pyproject.toml +1 -1
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/tests/test_mal_simulator.py +26 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/AUTHORS +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/LICENSE +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/README.md +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/mal_simulator.egg-info/dependency_links.txt +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/mal_simulator.egg-info/entry_points.txt +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/mal_simulator.egg-info/requires.txt +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/mal_simulator.egg-info/top_level.txt +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/__main__.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/config/node_property_rule.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/config/sim_settings.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/__init__.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/base_classes.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/graph/__init__.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/graph/graph_env.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/graph/mal_spaces.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/graph/serialization.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/graph/utils.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/graph/wrapper.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/legacy/__init__.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/legacy/gym_envs.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/envs/legacy/malsim_vectorized_obs_env.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/__init__.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/agent_state.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/attack_surface.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/attacker_state_factories.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/defender_state_factories.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/defense_surface.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/false_alerts.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/graph_processing.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/graph_state.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/graph_utils.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/node_getters.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/observability.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/rewards.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/ttc_utils.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/__init__.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/attackers/__init__.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/attackers/searchers.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/attackers/shortest_path.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/attackers/ttc_soft_min.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/decision_agent.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/defenders/__init__.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/defenders/heuristic_agent.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/passive_agent.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/random_agent.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/policies/utils/path_finding.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/scenario/__init__.py +0 -0
- {mal_simulator-2.0.0/malsim/mal_simulator → mal_simulator-2.2.0/malsim}/types.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/visualization/malsim_gui_client.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/setup.cfg +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/tests/test_graph_processing.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/tests/test_main.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/tests/test_scenario.py +0 -0
- {mal_simulator-2.0.0 → mal_simulator-2.2.0}/tests/test_ttc_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mal-simulator
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: A MAL compliant simulator.
|
|
5
5
|
Author-email: Andrei Buhaiu <buhaiu@kth.se>, Joakim Loxdal <loxdal@kth.se>, Jakob Nyberg <jaknyb@kth.se>, Nikolaos Kakouros <nkak@kth.se>, Sandor Berglund <sandorb@kth.se>
|
|
6
6
|
License: Apache Software License
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mal-simulator
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: A MAL compliant simulator.
|
|
5
5
|
Author-email: Andrei Buhaiu <buhaiu@kth.se>, Joakim Loxdal <loxdal@kth.se>, Jakob Nyberg <jaknyb@kth.se>, Nikolaos Kakouros <nkak@kth.se>, Sandor Berglund <sandorb@kth.se>
|
|
6
6
|
License: Apache Software License
|
|
@@ -10,7 +10,10 @@ mal_simulator.egg-info/requires.txt
|
|
|
10
10
|
mal_simulator.egg-info/top_level.txt
|
|
11
11
|
malsim/__init__.py
|
|
12
12
|
malsim/__main__.py
|
|
13
|
+
malsim/types.py
|
|
14
|
+
malsim/config/__init__.py
|
|
13
15
|
malsim/config/agent_settings.py
|
|
16
|
+
malsim/config/agent_settings_factories.py
|
|
14
17
|
malsim/config/node_property_rule.py
|
|
15
18
|
malsim/config/sim_settings.py
|
|
16
19
|
malsim/envs/__init__.py
|
|
@@ -48,7 +51,6 @@ malsim/mal_simulator/simulator.py
|
|
|
48
51
|
malsim/mal_simulator/simulator_state.py
|
|
49
52
|
malsim/mal_simulator/state_query.py
|
|
50
53
|
malsim/mal_simulator/ttc_utils.py
|
|
51
|
-
malsim/mal_simulator/types.py
|
|
52
54
|
malsim/policies/__init__.py
|
|
53
55
|
malsim/policies/decision_agent.py
|
|
54
56
|
malsim/policies/keyboard_input.py
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
|
2
|
-
# MAL Simulator v2.
|
|
2
|
+
# MAL Simulator v2.2.0
|
|
3
3
|
# Copyright 2025, Andrei Buhaiu.
|
|
4
4
|
#
|
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -23,12 +23,18 @@ from malsim.mal_simulator import (
|
|
|
23
23
|
run_simulation,
|
|
24
24
|
)
|
|
25
25
|
|
|
26
|
-
from malsim.scenario
|
|
27
|
-
from malsim.config
|
|
26
|
+
from malsim.scenario import Scenario
|
|
27
|
+
from malsim.config import AttackerSettings, DefenderSettings
|
|
28
28
|
|
|
29
29
|
__title__ = 'malsim'
|
|
30
|
-
__version__ = '2.
|
|
31
|
-
__authors__ = [
|
|
30
|
+
__version__ = '2.2.0'
|
|
31
|
+
__authors__ = [
|
|
32
|
+
'Andrei Buhaiu',
|
|
33
|
+
'Joakim Loxdal',
|
|
34
|
+
'Jakob Nyberg',
|
|
35
|
+
'Nikolaos Kakouros',
|
|
36
|
+
'Sandor Berglund',
|
|
37
|
+
]
|
|
32
38
|
__license__ = 'Apache 2.0'
|
|
33
39
|
__docformat__ = 'restructuredtext en'
|
|
34
40
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from .agent_settings import AttackerSettings, DefenderSettings
|
|
2
|
+
from .sim_settings import MalSimulatorSettings, TTCMode, RewardMode
|
|
3
|
+
from .node_property_rule import NodePropertyRule
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
'AttackerSettings',
|
|
7
|
+
'DefenderSettings',
|
|
8
|
+
'MalSimulatorSettings',
|
|
9
|
+
'NodePropertyRule',
|
|
10
|
+
'RewardMode',
|
|
11
|
+
'TTCMode',
|
|
12
|
+
]
|
|
@@ -7,30 +7,6 @@ from typing import Any, Optional
|
|
|
7
7
|
from maltoolbox.attackgraph import AttackGraphNode
|
|
8
8
|
from malsim.config.node_property_rule import NodePropertyRule
|
|
9
9
|
|
|
10
|
-
from malsim.policies import (
|
|
11
|
-
BreadthFirstAttacker,
|
|
12
|
-
DepthFirstAttacker,
|
|
13
|
-
KeyboardAgent,
|
|
14
|
-
PassiveAgent,
|
|
15
|
-
DefendCompromisedDefender,
|
|
16
|
-
DefendFutureCompromisedDefender,
|
|
17
|
-
RandomAgent,
|
|
18
|
-
TTCSoftMinAttacker,
|
|
19
|
-
ShortestPathAttacker,
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
policy_name_to_class = {
|
|
23
|
-
'KeyboardAgent': KeyboardAgent,
|
|
24
|
-
'PassiveAgent': PassiveAgent,
|
|
25
|
-
'DepthFirstAttacker': DepthFirstAttacker,
|
|
26
|
-
'BreadthFirstAttacker': BreadthFirstAttacker,
|
|
27
|
-
'TTCSoftMinAttacker': TTCSoftMinAttacker,
|
|
28
|
-
'ShortestPathAttacker': ShortestPathAttacker,
|
|
29
|
-
'DefendCompromisedDefender': DefendCompromisedDefender,
|
|
30
|
-
'DefendFutureCompromisedDefender': DefendFutureCompromisedDefender,
|
|
31
|
-
'RandomAgent': RandomAgent,
|
|
32
|
-
}
|
|
33
|
-
|
|
34
10
|
|
|
35
11
|
class AgentType(Enum):
|
|
36
12
|
"""Enum for agent types"""
|
|
@@ -138,58 +114,6 @@ class DefenderSettings(AgentRuntimeMixin):
|
|
|
138
114
|
return d
|
|
139
115
|
|
|
140
116
|
|
|
141
|
-
def agent_settings_from_dict(
|
|
142
|
-
name: str,
|
|
143
|
-
d: dict[str, Any],
|
|
144
|
-
) -> AttackerSettings | DefenderSettings:
|
|
145
|
-
"""Load agent settings from a dict"""
|
|
146
|
-
|
|
147
|
-
agent_type = AgentType(d['type'])
|
|
148
|
-
|
|
149
|
-
# Resolve policy class if provided
|
|
150
|
-
policy = None
|
|
151
|
-
policy_name = d.get('policy') or d.get('agent_class')
|
|
152
|
-
if policy_name:
|
|
153
|
-
if policy_name not in policy_name_to_class:
|
|
154
|
-
raise LookupError(
|
|
155
|
-
f"Policy class '{policy_name}' not supported. "
|
|
156
|
-
f'Must be one of: {list(policy_name_to_class.keys())}'
|
|
157
|
-
)
|
|
158
|
-
policy = policy_name_to_class[policy_name]
|
|
159
|
-
|
|
160
|
-
config = d.get('config', {})
|
|
161
|
-
|
|
162
|
-
if agent_type == AgentType.ATTACKER:
|
|
163
|
-
return AttackerSettings(
|
|
164
|
-
name=name,
|
|
165
|
-
entry_points=set(d['entry_points']),
|
|
166
|
-
goals=set(d.get('goals', [])),
|
|
167
|
-
ttc_overrides=NodePropertyRule.from_optional_dict(d.get('ttc_overrides')),
|
|
168
|
-
policy=policy,
|
|
169
|
-
actionable_steps=NodePropertyRule.from_optional_dict(
|
|
170
|
-
d.get('actionable_steps')
|
|
171
|
-
),
|
|
172
|
-
rewards=NodePropertyRule.from_optional_dict(d.get('rewards')),
|
|
173
|
-
config=config,
|
|
174
|
-
)
|
|
175
|
-
|
|
176
|
-
# Defender
|
|
177
|
-
return DefenderSettings(
|
|
178
|
-
name=name,
|
|
179
|
-
policy=policy,
|
|
180
|
-
observable_steps=NodePropertyRule.from_optional_dict(d.get('observable_steps')),
|
|
181
|
-
actionable_steps=NodePropertyRule.from_optional_dict(d.get('actionable_steps')),
|
|
182
|
-
rewards=NodePropertyRule.from_optional_dict(d.get('rewards')),
|
|
183
|
-
false_positive_rates=NodePropertyRule.from_optional_dict(
|
|
184
|
-
d.get('false_positive_rates')
|
|
185
|
-
),
|
|
186
|
-
false_negative_rates=NodePropertyRule.from_optional_dict(
|
|
187
|
-
d.get('false_negative_rates')
|
|
188
|
-
),
|
|
189
|
-
config=config,
|
|
190
|
-
)
|
|
191
|
-
|
|
192
|
-
|
|
193
117
|
def get_defender_settings(
|
|
194
118
|
agent_settings: dict[str, DefenderSettings | AttackerSettings],
|
|
195
119
|
) -> dict[str, DefenderSettings]:
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from malsim.config.agent_settings import AgentType, AttackerSettings, DefenderSettings
|
|
4
|
+
|
|
5
|
+
from malsim.config.node_property_rule import NodePropertyRule
|
|
6
|
+
from malsim.policies import (
|
|
7
|
+
BreadthFirstAttacker,
|
|
8
|
+
DepthFirstAttacker,
|
|
9
|
+
KeyboardAgent,
|
|
10
|
+
PassiveAgent,
|
|
11
|
+
DefendCompromisedDefender,
|
|
12
|
+
DefendFutureCompromisedDefender,
|
|
13
|
+
RandomAgent,
|
|
14
|
+
TTCSoftMinAttacker,
|
|
15
|
+
ShortestPathAttacker,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
policy_name_to_class = {
|
|
19
|
+
'KeyboardAgent': KeyboardAgent,
|
|
20
|
+
'PassiveAgent': PassiveAgent,
|
|
21
|
+
'DepthFirstAttacker': DepthFirstAttacker,
|
|
22
|
+
'BreadthFirstAttacker': BreadthFirstAttacker,
|
|
23
|
+
'TTCSoftMinAttacker': TTCSoftMinAttacker,
|
|
24
|
+
'ShortestPathAttacker': ShortestPathAttacker,
|
|
25
|
+
'DefendCompromisedDefender': DefendCompromisedDefender,
|
|
26
|
+
'DefendFutureCompromisedDefender': DefendFutureCompromisedDefender,
|
|
27
|
+
'RandomAgent': RandomAgent,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def agent_settings_from_dict(
|
|
32
|
+
name: str,
|
|
33
|
+
d: dict[str, Any],
|
|
34
|
+
) -> AttackerSettings | DefenderSettings:
|
|
35
|
+
"""Load agent settings from a dict"""
|
|
36
|
+
|
|
37
|
+
agent_type = AgentType(d['type'])
|
|
38
|
+
|
|
39
|
+
# Resolve policy class if provided
|
|
40
|
+
policy = None
|
|
41
|
+
policy_name = d.get('policy') or d.get('agent_class')
|
|
42
|
+
if policy_name:
|
|
43
|
+
if policy_name not in policy_name_to_class:
|
|
44
|
+
raise LookupError(
|
|
45
|
+
f"Policy class '{policy_name}' not supported. "
|
|
46
|
+
f'Must be one of: {list(policy_name_to_class.keys())}'
|
|
47
|
+
)
|
|
48
|
+
policy = policy_name_to_class[policy_name]
|
|
49
|
+
|
|
50
|
+
config = d.get('config', {})
|
|
51
|
+
|
|
52
|
+
if agent_type == AgentType.ATTACKER:
|
|
53
|
+
return AttackerSettings(
|
|
54
|
+
name=name,
|
|
55
|
+
entry_points=set(d['entry_points']),
|
|
56
|
+
goals=set(d.get('goals', [])),
|
|
57
|
+
ttc_overrides=NodePropertyRule.from_optional_dict(d.get('ttc_overrides')),
|
|
58
|
+
policy=policy,
|
|
59
|
+
actionable_steps=NodePropertyRule.from_optional_dict(
|
|
60
|
+
d.get('actionable_steps')
|
|
61
|
+
),
|
|
62
|
+
rewards=NodePropertyRule.from_optional_dict(d.get('rewards')),
|
|
63
|
+
config=config,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
# Defender
|
|
67
|
+
return DefenderSettings(
|
|
68
|
+
name=name,
|
|
69
|
+
policy=policy,
|
|
70
|
+
observable_steps=NodePropertyRule.from_optional_dict(d.get('observable_steps')),
|
|
71
|
+
actionable_steps=NodePropertyRule.from_optional_dict(d.get('actionable_steps')),
|
|
72
|
+
rewards=NodePropertyRule.from_optional_dict(d.get('rewards')),
|
|
73
|
+
false_positive_rates=NodePropertyRule.from_optional_dict(
|
|
74
|
+
d.get('false_positive_rates')
|
|
75
|
+
),
|
|
76
|
+
false_negative_rates=NodePropertyRule.from_optional_dict(
|
|
77
|
+
d.get('false_negative_rates')
|
|
78
|
+
),
|
|
79
|
+
config=config,
|
|
80
|
+
)
|
|
@@ -5,7 +5,7 @@ from maltoolbox.attackgraph import AttackGraphNode
|
|
|
5
5
|
from malsim.config.node_property_rule import NodePropertyRule
|
|
6
6
|
from malsim.mal_simulator.agent_state import MalSimAgentState
|
|
7
7
|
from malsim.mal_simulator.ttc_utils import TTCDist
|
|
8
|
-
from malsim.
|
|
8
|
+
from malsim.types import AgentStates
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
@dataclass(frozen=True)
|
|
@@ -129,7 +129,7 @@ def attacker_step(
|
|
|
129
129
|
Returns: two lists with compromised, attempted nodes
|
|
130
130
|
"""
|
|
131
131
|
|
|
132
|
-
logger.
|
|
132
|
+
logger.debug('Stepping with agent %s', agent.name)
|
|
133
133
|
successful_compromises: list[AttackGraphNode] = []
|
|
134
134
|
attempted_compromises: list[AttackGraphNode] = []
|
|
135
135
|
|
|
@@ -4,7 +4,7 @@ from typing import Any, Optional
|
|
|
4
4
|
from maltoolbox.attackgraph import AttackGraphNode
|
|
5
5
|
from malsim.config.node_property_rule import NodePropertyRule
|
|
6
6
|
from malsim.mal_simulator.agent_state import MalSimAgentState
|
|
7
|
-
from malsim.
|
|
7
|
+
from malsim.types import AgentStates
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
@dataclass(frozen=True)
|
|
@@ -10,7 +10,7 @@ from malsim.mal_simulator.graph_utils import node_reward
|
|
|
10
10
|
from malsim.mal_simulator.simulator_state import MalSimulatorState
|
|
11
11
|
|
|
12
12
|
if TYPE_CHECKING:
|
|
13
|
-
from malsim.
|
|
13
|
+
from malsim.types import AgentStates
|
|
14
14
|
from malsim.mal_simulator.defender_state import MalSimDefenderState
|
|
15
15
|
|
|
16
16
|
logger = logging.getLogger(__name__)
|
|
@@ -42,7 +42,7 @@ def defender_step(
|
|
|
42
42
|
and `attack_steps_made_unviable`.
|
|
43
43
|
"""
|
|
44
44
|
|
|
45
|
-
logger.
|
|
45
|
+
logger.debug('Stepping with %s', agent.name)
|
|
46
46
|
enabled_defenses: list[AttackGraphNode] = []
|
|
47
47
|
attack_steps_made_unviable: set[AttackGraphNode] = set()
|
|
48
48
|
|
|
@@ -12,7 +12,7 @@ from malsim.mal_simulator.reset_agent import reset_agents
|
|
|
12
12
|
from malsim.mal_simulator.rewards import attacker_step_reward, defender_step_reward
|
|
13
13
|
from malsim.mal_simulator.simulator_state import MalSimulatorState
|
|
14
14
|
from malsim.config.agent_settings import AttackerSettings, DefenderSettings
|
|
15
|
-
from malsim.
|
|
15
|
+
from malsim.types import AgentRewards, AgentStates, AgentSettings
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
def register_attacker_settings(
|
|
@@ -10,7 +10,7 @@ from malsim.mal_simulator.defender_state_factories import initial_defender_state
|
|
|
10
10
|
from malsim.mal_simulator.rewards import attacker_step_reward, defender_step_reward
|
|
11
11
|
from malsim.mal_simulator.simulator_state import MalSimulatorState
|
|
12
12
|
from malsim.config.agent_settings import get_defender_settings, get_attacker_settings
|
|
13
|
-
from malsim.
|
|
13
|
+
from malsim.types import AgentRewards, AgentStates, AgentSettings
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
def reset_attackers(
|
|
@@ -4,7 +4,7 @@ from maltoolbox.attackgraph import AttackGraphNode
|
|
|
4
4
|
|
|
5
5
|
from malsim.policies.decision_agent import DecisionAgent
|
|
6
6
|
from malsim.mal_simulator.simulator import MalSimulator
|
|
7
|
-
from malsim.
|
|
7
|
+
from malsim.types import AgentSettings
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
def run_simulation(
|
|
@@ -39,7 +39,6 @@ def run_simulation(
|
|
|
39
39
|
|
|
40
40
|
if agent_action:
|
|
41
41
|
actions[agent_name] = [agent_action]
|
|
42
|
-
print(f'Agent {agent_name} chose action: {agent_action.full_name}')
|
|
43
42
|
|
|
44
43
|
# Store agent action
|
|
45
44
|
agent_actions.setdefault(agent_name, []).append(agent_action)
|
|
@@ -2,7 +2,7 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
from collections import defaultdict
|
|
4
4
|
import logging
|
|
5
|
-
from typing import Any, Optional
|
|
5
|
+
from typing import Any, NamedTuple, Optional
|
|
6
6
|
from collections.abc import Callable, Mapping, Set
|
|
7
7
|
|
|
8
8
|
import numpy as np
|
|
@@ -20,6 +20,7 @@ from malsim.mal_simulator.defender_step import (
|
|
|
20
20
|
defender_is_terminated,
|
|
21
21
|
defender_step,
|
|
22
22
|
)
|
|
23
|
+
from malsim.mal_simulator.graph_state import compute_initial_graph_state
|
|
23
24
|
from malsim.mal_simulator.node_getters import (
|
|
24
25
|
full_names_or_nodes_to_nodes,
|
|
25
26
|
get_node,
|
|
@@ -38,7 +39,7 @@ from malsim.mal_simulator.false_alerts import (
|
|
|
38
39
|
node_false_positive_rate,
|
|
39
40
|
)
|
|
40
41
|
from malsim.config.agent_settings import AttackerSettings, DefenderSettings
|
|
41
|
-
from malsim.
|
|
42
|
+
from malsim.types import (
|
|
42
43
|
AgentRewards,
|
|
43
44
|
AgentStates,
|
|
44
45
|
AgentSettings,
|
|
@@ -103,6 +104,20 @@ ENABLED_ATTACKS_FUNCS: Mapping[
|
|
|
103
104
|
BASE_SETTINGS = MalSimulatorSettings()
|
|
104
105
|
|
|
105
106
|
|
|
107
|
+
class MALSimulatorStaticData(NamedTuple):
|
|
108
|
+
attack_graph: AttackGraph
|
|
109
|
+
sim_settings: MalSimulatorSettings
|
|
110
|
+
rewards: Optional[dict[str, float] | dict[AttackGraphNode, float]] = None
|
|
111
|
+
false_positive_rates: Optional[dict[str, float] | dict[AttackGraphNode, float]] = (
|
|
112
|
+
None
|
|
113
|
+
)
|
|
114
|
+
false_negative_rates: Optional[dict[str, float] | dict[AttackGraphNode, float]] = (
|
|
115
|
+
None
|
|
116
|
+
)
|
|
117
|
+
node_actionabilities: Optional[dict[str, bool] | dict[AttackGraphNode, bool]] = None
|
|
118
|
+
node_observabilities: Optional[dict[str, bool] | dict[AttackGraphNode, bool]] = None
|
|
119
|
+
|
|
120
|
+
|
|
106
121
|
class MalSimulator:
|
|
107
122
|
"""A MAL Simulator that works on the AttackGraph
|
|
108
123
|
|
|
@@ -115,6 +130,7 @@ class MalSimulator:
|
|
|
115
130
|
self,
|
|
116
131
|
attack_graph: AttackGraph,
|
|
117
132
|
sim_settings: MalSimulatorSettings = BASE_SETTINGS,
|
|
133
|
+
agent_settings: Optional[AgentSettings] = None,
|
|
118
134
|
rewards: Optional[dict[str, float] | dict[AttackGraphNode, float]] = None,
|
|
119
135
|
false_positive_rates: Optional[
|
|
120
136
|
dict[str, float] | dict[AttackGraphNode, float]
|
|
@@ -132,45 +148,61 @@ class MalSimulator:
|
|
|
132
148
|
):
|
|
133
149
|
"""
|
|
134
150
|
Args:
|
|
135
|
-
attack_graph
|
|
136
|
-
sim_settings
|
|
151
|
+
attack_graph - The attack graph to use
|
|
152
|
+
sim_settings - Settings for simulator
|
|
153
|
+
agent_settings - The agents to pre-register
|
|
154
|
+
rewards - Global rewards per node
|
|
155
|
+
false_positive_rates - global fpr per node
|
|
156
|
+
false_negative_rates - global fnr per node
|
|
157
|
+
node_actionabilities - global actionabilities per node
|
|
158
|
+
node_observabilities - global obserabilities per node
|
|
159
|
+
send_to_api - Enable to send data to malsim-gui rest api
|
|
137
160
|
"""
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
self.rng = default_rng(self.sim_settings.seed)
|
|
161
|
+
rng = default_rng(sim_settings.seed)
|
|
162
|
+
rest_api_client = MalSimGUIClient() if send_to_api else None
|
|
141
163
|
|
|
142
|
-
|
|
143
|
-
self.rest_api_client = None
|
|
144
|
-
if send_to_api:
|
|
145
|
-
self.rest_api_client = MalSimGUIClient()
|
|
146
|
-
|
|
147
|
-
# Initialize all values
|
|
148
|
-
self.recording: Recording = defaultdict(dict)
|
|
149
|
-
self._agent_settings: dict[str, AttackerSettings | DefenderSettings] = {}
|
|
150
|
-
self._agent_states: AgentStates = {}
|
|
151
|
-
self._alive_agents: set[str] = set()
|
|
152
|
-
self._agent_rewards: AgentRewards = {}
|
|
153
|
-
|
|
154
|
-
self.sim_state = create_simulator_state(
|
|
164
|
+
static_sim_data = MALSimulatorStaticData(
|
|
155
165
|
attack_graph,
|
|
156
166
|
sim_settings,
|
|
157
|
-
self.rng,
|
|
158
167
|
rewards,
|
|
159
168
|
false_positive_rates,
|
|
160
169
|
false_negative_rates,
|
|
161
170
|
node_actionabilities,
|
|
162
171
|
node_observabilities,
|
|
163
172
|
)
|
|
164
|
-
|
|
165
|
-
self.performed_attacks_func = PERFORMED_ATTACKS_FUNCS[
|
|
173
|
+
performed_attacks_func = PERFORMED_ATTACKS_FUNCS[
|
|
166
174
|
sim_settings.attacker_reward_mode
|
|
167
175
|
]
|
|
168
|
-
|
|
169
|
-
sim_settings.defender_reward_mode
|
|
170
|
-
]
|
|
171
|
-
self.enabled_attacks_func = ENABLED_ATTACKS_FUNCS[
|
|
176
|
+
enabled_defenses_func = ENABLED_DEFENSES_FUNCS[
|
|
172
177
|
sim_settings.defender_reward_mode
|
|
173
178
|
]
|
|
179
|
+
enabled_attacks_func = ENABLED_ATTACKS_FUNCS[sim_settings.defender_reward_mode]
|
|
180
|
+
agent_settings = agent_settings or {}
|
|
181
|
+
|
|
182
|
+
agent_states, alive_agents, sim_state, agent_rewards, recording = reset(
|
|
183
|
+
static_sim_data,
|
|
184
|
+
agent_settings,
|
|
185
|
+
rng,
|
|
186
|
+
rest_api_client,
|
|
187
|
+
performed_attacks_func,
|
|
188
|
+
enabled_defenses_func,
|
|
189
|
+
enabled_attacks_func,
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
# Set all instance variables
|
|
193
|
+
self.rng = rng
|
|
194
|
+
self._agent_states = agent_states
|
|
195
|
+
self._alive_agents = alive_agents
|
|
196
|
+
self._agent_rewards = agent_rewards
|
|
197
|
+
self.sim_state = sim_state
|
|
198
|
+
self.recording = recording
|
|
199
|
+
self.enabled_attacks_func = enabled_attacks_func
|
|
200
|
+
self.enabled_defenses_func = enabled_defenses_func
|
|
201
|
+
self.performed_attacks_func = performed_attacks_func
|
|
202
|
+
self.sim_settings = sim_settings
|
|
203
|
+
self._agent_settings = agent_settings
|
|
204
|
+
self.rest_api_client = rest_api_client
|
|
205
|
+
self._static_data = static_sim_data
|
|
174
206
|
|
|
175
207
|
def __getstate__(self) -> dict[str, Any]:
|
|
176
208
|
"""This just ensures a pickled simulator doesn't contain some data structures"""
|
|
@@ -324,8 +356,14 @@ class MalSimulator:
|
|
|
324
356
|
return agent_is_terminated(self._agent_states, self._alive_agents, agent_name)
|
|
325
357
|
|
|
326
358
|
def reset(self) -> dict[str, MalSimAttackerState | MalSimDefenderState]:
|
|
327
|
-
|
|
359
|
+
(
|
|
360
|
+
self._agent_states,
|
|
361
|
+
self._alive_agents,
|
|
328
362
|
self.sim_state,
|
|
363
|
+
self._agent_rewards,
|
|
364
|
+
self.recording,
|
|
365
|
+
) = reset(
|
|
366
|
+
self._static_data,
|
|
329
367
|
self._agent_settings,
|
|
330
368
|
self.rng,
|
|
331
369
|
self.rest_api_client,
|
|
@@ -333,11 +371,6 @@ class MalSimulator:
|
|
|
333
371
|
self.enabled_defenses_func,
|
|
334
372
|
self.enabled_attacks_func,
|
|
335
373
|
)
|
|
336
|
-
self._agent_states = agent_states
|
|
337
|
-
self._alive_agents = alive_agents
|
|
338
|
-
self._agent_rewards = agent_rewards
|
|
339
|
-
self.recording = recording
|
|
340
|
-
self.sim_state = sim_state
|
|
341
374
|
return self._agent_states
|
|
342
375
|
|
|
343
376
|
def register_attacker(
|
|
@@ -533,7 +566,7 @@ def agent_is_terminated(
|
|
|
533
566
|
|
|
534
567
|
|
|
535
568
|
def reset(
|
|
536
|
-
|
|
569
|
+
static_data: MALSimulatorStaticData,
|
|
537
570
|
agent_settings: AgentSettings,
|
|
538
571
|
rng: np.random.Generator,
|
|
539
572
|
rest_api_client: Optional[MalSimGUIClient],
|
|
@@ -544,28 +577,28 @@ def reset(
|
|
|
544
577
|
AgentStates,
|
|
545
578
|
set[str],
|
|
546
579
|
MalSimulatorState,
|
|
547
|
-
|
|
580
|
+
AgentRewards,
|
|
548
581
|
Recording,
|
|
549
582
|
]:
|
|
550
583
|
"""Reset attack graph and reinitialize agents"""
|
|
551
584
|
logger.info('Resetting MAL Simulator.')
|
|
585
|
+
attack_graph = static_data.attack_graph
|
|
586
|
+
settings = static_data.sim_settings
|
|
552
587
|
|
|
553
588
|
# Re-calculate initial simulator state
|
|
589
|
+
graph_state = compute_initial_graph_state(attack_graph, settings, rng)
|
|
554
590
|
sim_state = create_simulator_state(
|
|
555
|
-
|
|
556
|
-
|
|
591
|
+
attack_graph,
|
|
592
|
+
graph_state,
|
|
593
|
+
settings,
|
|
557
594
|
rng,
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
595
|
+
static_data.rewards,
|
|
596
|
+
static_data.false_positive_rates,
|
|
597
|
+
static_data.false_negative_rates,
|
|
598
|
+
static_data.node_actionabilities,
|
|
599
|
+
static_data.node_observabilities,
|
|
563
600
|
)
|
|
564
601
|
|
|
565
|
-
# Reset rewards and recording
|
|
566
|
-
agent_rewards: dict[str, float] = {}
|
|
567
|
-
recording: Recording = defaultdict(dict)
|
|
568
|
-
|
|
569
602
|
agent_states, alive_agents, agent_rewards = reset_agents(
|
|
570
603
|
sim_state,
|
|
571
604
|
agent_settings,
|
|
@@ -576,9 +609,9 @@ def reset(
|
|
|
576
609
|
)
|
|
577
610
|
# Upload initial state to the REST API
|
|
578
611
|
if rest_api_client:
|
|
579
|
-
rest_api_client.upload_initial_state(
|
|
612
|
+
rest_api_client.upload_initial_state(attack_graph)
|
|
580
613
|
|
|
581
|
-
return agent_states, alive_agents, sim_state, agent_rewards,
|
|
614
|
+
return agent_states, alive_agents, sim_state, agent_rewards, defaultdict(dict)
|
|
582
615
|
|
|
583
616
|
|
|
584
617
|
def _pre_step_check(
|
|
@@ -5,7 +5,7 @@ from maltoolbox.attackgraph import AttackGraph, AttackGraphNode
|
|
|
5
5
|
import numpy as np
|
|
6
6
|
|
|
7
7
|
from malsim.config.sim_settings import MalSimulatorSettings
|
|
8
|
-
from malsim.mal_simulator.graph_state import GraphState
|
|
8
|
+
from malsim.mal_simulator.graph_state import GraphState
|
|
9
9
|
from malsim.mal_simulator.node_getters import full_name_dict_to_node_dict
|
|
10
10
|
|
|
11
11
|
|
|
@@ -23,6 +23,7 @@ class MalSimulatorState:
|
|
|
23
23
|
|
|
24
24
|
def create_simulator_state(
|
|
25
25
|
attack_graph: AttackGraph,
|
|
26
|
+
graph_state: GraphState,
|
|
26
27
|
sim_settings: MalSimulatorSettings,
|
|
27
28
|
rng: np.random.Generator,
|
|
28
29
|
rewards: Optional[dict[str, float] | dict[AttackGraphNode, float]] = None,
|
|
@@ -42,7 +43,7 @@ def create_simulator_state(
|
|
|
42
43
|
return MalSimulatorState(
|
|
43
44
|
attack_graph,
|
|
44
45
|
sim_settings,
|
|
45
|
-
|
|
46
|
+
graph_state,
|
|
46
47
|
full_name_dict_to_node_dict(attack_graph, rewards or {}),
|
|
47
48
|
full_name_dict_to_node_dict(attack_graph, false_positive_rates or {}),
|
|
48
49
|
full_name_dict_to_node_dict(attack_graph, false_negative_rates or {}),
|
|
@@ -9,7 +9,7 @@ from malsim.mal_simulator.attacker_state import MalSimAttackerState, get_attacke
|
|
|
9
9
|
from malsim.mal_simulator.defender_state import get_defender_agents
|
|
10
10
|
from malsim.mal_simulator.node_getters import full_name_or_node_to_node
|
|
11
11
|
from malsim.config.sim_settings import TTCMode
|
|
12
|
-
from malsim.
|
|
12
|
+
from malsim.types import AgentStates
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
def node_is_enabled_defense(
|
|
@@ -23,11 +23,11 @@ from maltoolbox.model import Model
|
|
|
23
23
|
from maltoolbox.language import LanguageGraph
|
|
24
24
|
from maltoolbox.attackgraph import create_attack_graph
|
|
25
25
|
|
|
26
|
-
from malsim.config
|
|
26
|
+
from malsim.config import (
|
|
27
27
|
DefenderSettings,
|
|
28
28
|
AttackerSettings,
|
|
29
|
-
agent_settings_from_dict,
|
|
30
29
|
)
|
|
30
|
+
from malsim.config.agent_settings_factories import agent_settings_from_dict
|
|
31
31
|
from malsim.config.node_property_rule import NodePropertyRule
|
|
32
32
|
|
|
33
33
|
|
|
@@ -25,6 +25,9 @@ from malsim.config.agent_settings import AttackerSettings, DefenderSettings
|
|
|
25
25
|
from dataclasses import asdict
|
|
26
26
|
import numpy as np
|
|
27
27
|
import pytest
|
|
28
|
+
|
|
29
|
+
from malsim.policies.random_agent import RandomAgent
|
|
30
|
+
from malsim.types import AgentSettings
|
|
28
31
|
from .conftest import get_node
|
|
29
32
|
|
|
30
33
|
if TYPE_CHECKING:
|
|
@@ -37,6 +40,29 @@ def test_init(corelang_lang_graph: LanguageGraph, model: Model) -> None:
|
|
|
37
40
|
MalSimulator(attack_graph)
|
|
38
41
|
|
|
39
42
|
|
|
43
|
+
def test_init_with_agent_settings(
|
|
44
|
+
corelang_lang_graph: LanguageGraph, model: Model
|
|
45
|
+
) -> None:
|
|
46
|
+
attack_graph = AttackGraph(corelang_lang_graph, model)
|
|
47
|
+
entry_points = {'OS App:localConnect'}
|
|
48
|
+
goals = {'OS App:fullAccess'}
|
|
49
|
+
|
|
50
|
+
agent_settings: AgentSettings = {
|
|
51
|
+
'Attacker1': AttackerSettings(
|
|
52
|
+
name='Attacker1', entry_points=entry_points, goals=goals, policy=RandomAgent
|
|
53
|
+
),
|
|
54
|
+
'Defender1': DefenderSettings(name='Defender1', policy=RandomAgent),
|
|
55
|
+
}
|
|
56
|
+
attack_graph = AttackGraph(corelang_lang_graph, model)
|
|
57
|
+
sim = MalSimulator(attack_graph, agent_settings=agent_settings)
|
|
58
|
+
|
|
59
|
+
# Make sure the agents were registered
|
|
60
|
+
assert sim.agent_states.keys() == {'Attacker1', 'Defender1'}
|
|
61
|
+
assert sim.agent_reward('Attacker1') == 0.0
|
|
62
|
+
assert sim.agent_reward('Defender1') == 0.0
|
|
63
|
+
assert sim._alive_agents == {'Attacker1', 'Defender1'}
|
|
64
|
+
|
|
65
|
+
|
|
40
66
|
def test_reset(corelang_lang_graph: LanguageGraph, model: Model) -> None:
|
|
41
67
|
"""Make sure attack graph is reset"""
|
|
42
68
|
attack_graph = AttackGraph(corelang_lang_graph, model)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/attacker_state_factories.py
RENAMED
|
File without changes
|
{mal_simulator-2.0.0 → mal_simulator-2.2.0}/malsim/mal_simulator/defender_state_factories.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|