mal-simulator 2.0.1__tar.gz → 2.3.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.
Files changed (75) hide show
  1. {mal_simulator-2.0.1/mal_simulator.egg-info → mal_simulator-2.3.0}/PKG-INFO +1 -1
  2. {mal_simulator-2.0.1 → mal_simulator-2.3.0/mal_simulator.egg-info}/PKG-INFO +1 -1
  3. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/__init__.py +9 -3
  4. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/base_classes.py +5 -2
  5. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/attack_surface.py +3 -1
  6. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/attacker_state_factories.py +37 -15
  7. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/attacker_step.py +1 -1
  8. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/defender_step.py +1 -1
  9. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/run_simulation.py +0 -1
  10. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/simulator.py +110 -84
  11. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/simulator_state.py +3 -2
  12. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/keyboard_input.py +0 -1
  13. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/pyproject.toml +1 -1
  14. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/tests/test_mal_simulator.py +118 -3
  15. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/AUTHORS +0 -0
  16. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/LICENSE +0 -0
  17. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/README.md +0 -0
  18. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/mal_simulator.egg-info/SOURCES.txt +0 -0
  19. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/mal_simulator.egg-info/dependency_links.txt +0 -0
  20. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/mal_simulator.egg-info/entry_points.txt +0 -0
  21. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/mal_simulator.egg-info/requires.txt +0 -0
  22. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/mal_simulator.egg-info/top_level.txt +0 -0
  23. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/__main__.py +0 -0
  24. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/config/__init__.py +0 -0
  25. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/config/agent_settings.py +0 -0
  26. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/config/agent_settings_factories.py +0 -0
  27. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/config/node_property_rule.py +0 -0
  28. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/config/sim_settings.py +0 -0
  29. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/__init__.py +0 -0
  30. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/graph/__init__.py +0 -0
  31. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/graph/graph_env.py +0 -0
  32. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/graph/mal_spaces.py +0 -0
  33. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/graph/serialization.py +0 -0
  34. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/graph/utils.py +0 -0
  35. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/graph/wrapper.py +0 -0
  36. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/legacy/__init__.py +0 -0
  37. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/legacy/gym_envs.py +0 -0
  38. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/envs/legacy/malsim_vectorized_obs_env.py +0 -0
  39. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/__init__.py +0 -0
  40. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/agent_state.py +0 -0
  41. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/attacker_state.py +0 -0
  42. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/defender_state.py +0 -0
  43. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/defender_state_factories.py +0 -0
  44. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/defense_surface.py +0 -0
  45. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/false_alerts.py +0 -0
  46. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/graph_processing.py +0 -0
  47. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/graph_state.py +0 -0
  48. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/graph_utils.py +0 -0
  49. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/node_getters.py +0 -0
  50. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/observability.py +0 -0
  51. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/register_agent.py +0 -0
  52. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/reset_agent.py +0 -0
  53. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/rewards.py +0 -0
  54. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/state_query.py +0 -0
  55. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/mal_simulator/ttc_utils.py +0 -0
  56. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/__init__.py +0 -0
  57. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/attackers/__init__.py +0 -0
  58. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/attackers/searchers.py +0 -0
  59. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/attackers/shortest_path.py +0 -0
  60. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/attackers/ttc_soft_min.py +0 -0
  61. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/decision_agent.py +0 -0
  62. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/defenders/__init__.py +0 -0
  63. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/defenders/heuristic_agent.py +0 -0
  64. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/passive_agent.py +0 -0
  65. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/random_agent.py +0 -0
  66. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/policies/utils/path_finding.py +0 -0
  67. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/scenario/__init__.py +0 -0
  68. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/scenario/scenario.py +0 -0
  69. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/types.py +0 -0
  70. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/malsim/visualization/malsim_gui_client.py +0 -0
  71. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/setup.cfg +0 -0
  72. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/tests/test_graph_processing.py +0 -0
  73. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/tests/test_main.py +0 -0
  74. {mal_simulator-2.0.1 → mal_simulator-2.3.0}/tests/test_scenario.py +0 -0
  75. {mal_simulator-2.0.1 → mal_simulator-2.3.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.0.1
3
+ Version: 2.3.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.0.1
3
+ Version: 2.3.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,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # MAL Simulator v2.0.1
2
+ # MAL Simulator v2.3.0
3
3
  # Copyright 2025, Andrei Buhaiu.
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,8 +27,14 @@ from malsim.scenario import Scenario
27
27
  from malsim.config import AttackerSettings, DefenderSettings
28
28
 
29
29
  __title__ = 'malsim'
30
- __version__ = '2.0.1'
31
- __authors__ = ['Andrei Buhaiu', 'Joakim Loxdal', 'Jakob Nyberg', 'Nikolaos Kakouros', 'Sandor Berglund']
30
+ __version__ = '2.3.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
 
@@ -2,6 +2,9 @@ from abc import ABC, abstractmethod
2
2
  from typing import Any, Optional
3
3
 
4
4
  from maltoolbox.attackgraph import AttackGraphNode
5
+
6
+ from ..mal_simulator.defender_state import MalSimDefenderState
7
+ from ..mal_simulator.attacker_state import MalSimAttackerState
5
8
  from ..mal_simulator import MalSimulator, MalSimAgentState
6
9
 
7
10
 
@@ -14,10 +17,10 @@ class MalSimEnv(ABC):
14
17
 
15
18
  def reset(
16
19
  self, seed: Optional[int] = None, options: Optional[dict[str, Any]] = None
17
- ) -> None:
20
+ ) -> dict[str, MalSimAttackerState | MalSimDefenderState]:
18
21
  if seed is not None:
19
22
  self.sim.sim_settings.seed = seed
20
- self.sim.reset()
23
+ return self.sim.reset()
21
24
 
22
25
  def register_attacker(
23
26
  self, attacker_name: str, entry_points: set[AttackGraphNode]
@@ -31,7 +31,9 @@ def get_effects_of_attack_step(
31
31
  while potential_effects:
32
32
  effect = potential_effects.popleft()
33
33
  has_visited = performed | set(effects)
34
- if node_is_traversable(sim_state, has_visited, effect):
34
+ if effect not in has_visited and node_is_traversable(
35
+ sim_state, has_visited, effect
36
+ ):
35
37
  effects.add(effect)
36
38
  potential_effects += (
37
39
  n for n in effect.children if n.causal_mode == 'effect'
@@ -6,7 +6,10 @@ from types import MappingProxyType
6
6
  from typing import Optional, TYPE_CHECKING
7
7
 
8
8
  from malsim.config.node_property_rule import NodePropertyRule
9
- from malsim.mal_simulator.attack_surface import get_attack_surface
9
+ from malsim.mal_simulator.attack_surface import (
10
+ get_attack_surface,
11
+ get_effects_of_attack_step,
12
+ )
10
13
  from malsim.mal_simulator.attacker_state import MalSimAttackerState
11
14
  from malsim.mal_simulator.node_getters import (
12
15
  full_name_dict_to_node_dict,
@@ -31,8 +34,8 @@ def create_attacker_state(
31
34
  sim_state: MalSimulatorState,
32
35
  name: str,
33
36
  entry_points: Set[AttackGraphNode],
37
+ step_compromised_nodes: Set[AttackGraphNode],
34
38
  goals: Set[AttackGraphNode] = frozenset(),
35
- step_compromised_nodes: Set[AttackGraphNode] = frozenset(),
36
39
  step_attempted_nodes: Set[AttackGraphNode] = frozenset(),
37
40
  step_nodes_made_unviable: Set[AttackGraphNode] = frozenset(),
38
41
  ttc_overrides: Mapping[AttackGraphNode, TTCDist] = MappingProxyType({}),
@@ -48,9 +51,7 @@ def create_attacker_state(
48
51
  """
49
52
 
50
53
  if previous_state is None:
51
- # Initial compromised nodes
52
- if sim_state.settings.compromise_entrypoints_at_start:
53
- step_compromised_nodes |= entry_points
54
+ # Initial compromised nodes are just the step compromised nodes
54
55
  compromised_nodes = step_compromised_nodes
55
56
 
56
57
  # Create an initial attack surface
@@ -141,6 +142,21 @@ def create_attacker_state(
141
142
  )
142
143
 
143
144
 
145
+ def get_entrypoint_compromises(
146
+ sim_state: MalSimulatorState,
147
+ entry_points: set[AttackGraphNode],
148
+ ) -> set[AttackGraphNode]:
149
+ """Compromise entry points and return compromised nodes including effects"""
150
+ step_compromised_nodes: set[AttackGraphNode] = set()
151
+ for entry_point in entry_points:
152
+ step_compromised_nodes.add(entry_point)
153
+ # Perform effects of entry point compromises
154
+ step_compromised_nodes |= get_effects_of_attack_step(
155
+ sim_state, entry_point, step_compromised_nodes
156
+ )
157
+ return step_compromised_nodes
158
+
159
+
144
160
  def initial_attacker_state(
145
161
  sim_state: MalSimulatorState,
146
162
  attacker_settings: AttackerSettings,
@@ -148,24 +164,30 @@ def initial_attacker_state(
148
164
  rng: np.random.Generator,
149
165
  ) -> MalSimAttackerState:
150
166
  """Create an attacker state from attacker settings"""
167
+
151
168
  ttc_overrides, ttc_value_overrides, impossible_steps = (
152
169
  attacker_overriding_ttc_settings(
153
170
  sim_state.attack_graph, attacker_settings, ttc_mode, rng
154
171
  )
155
172
  )
173
+ entry_points = set(
174
+ full_names_or_nodes_to_nodes(
175
+ sim_state.attack_graph, attacker_settings.entry_points
176
+ )
177
+ )
178
+ goals = set(
179
+ full_names_or_nodes_to_nodes(sim_state.attack_graph, attacker_settings.goals)
180
+ )
181
+ step_compromised_nodes: Set[AttackGraphNode] = set()
182
+ if sim_state.settings.compromise_entrypoints_at_start:
183
+ step_compromised_nodes = get_entrypoint_compromises(sim_state, entry_points)
184
+
156
185
  return create_attacker_state(
157
186
  sim_state=sim_state,
158
187
  name=attacker_settings.name,
159
- entry_points=set(
160
- full_names_or_nodes_to_nodes(
161
- sim_state.attack_graph, attacker_settings.entry_points
162
- )
163
- ),
164
- goals=set(
165
- full_names_or_nodes_to_nodes(
166
- sim_state.attack_graph, attacker_settings.goals
167
- )
168
- ),
188
+ entry_points=entry_points,
189
+ goals=goals,
190
+ step_compromised_nodes=step_compromised_nodes,
169
191
  ttc_overrides=ttc_overrides,
170
192
  ttc_value_overrides=ttc_value_overrides,
171
193
  impossible_step_overrides=impossible_steps,
@@ -129,7 +129,7 @@ def attacker_step(
129
129
  Returns: two lists with compromised, attempted nodes
130
130
  """
131
131
 
132
- logger.info('Stepping with agent %s', agent.name)
132
+ logger.debug('Stepping with agent %s', agent.name)
133
133
  successful_compromises: list[AttackGraphNode] = []
134
134
  attempted_compromises: list[AttackGraphNode] = []
135
135
 
@@ -42,7 +42,7 @@ def defender_step(
42
42
  and `attack_steps_made_unviable`.
43
43
  """
44
44
 
45
- logger.info('Stepping with %s', agent.name)
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
 
@@ -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,
@@ -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 - The attack graph to use
136
- sim_settings - Settings for simulator
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
- logger.info('Creating Base MAL Simulator.')
139
- self.sim_settings = sim_settings
140
- self.rng = default_rng(self.sim_settings.seed)
141
-
142
- # Initialize the REST API client
143
- self.rest_api_client = None
144
- if send_to_api:
145
- self.rest_api_client = MalSimGUIClient()
161
+ rng = default_rng(sim_settings.seed)
162
+ rest_api_client = MalSimGUIClient() if send_to_api else None
146
163
 
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
- self.enabled_defenses_func = ENABLED_DEFENSES_FUNCS[
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
- agent_states, alive_agents, sim_state, agent_rewards, recording = reset(
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(
@@ -441,7 +474,6 @@ class MalSimulator:
441
474
  self._agent_states,
442
475
  self._alive_agents,
443
476
  self.rng,
444
- self._alive_agents,
445
477
  self._agent_rewards,
446
478
  self.performed_attacks_func,
447
479
  self.enabled_defenses_func,
@@ -533,7 +565,7 @@ def agent_is_terminated(
533
565
 
534
566
 
535
567
  def reset(
536
- sim_state: MalSimulatorState,
568
+ static_data: MALSimulatorStaticData,
537
569
  agent_settings: AgentSettings,
538
570
  rng: np.random.Generator,
539
571
  rest_api_client: Optional[MalSimGUIClient],
@@ -544,28 +576,28 @@ def reset(
544
576
  AgentStates,
545
577
  set[str],
546
578
  MalSimulatorState,
547
- dict[str, float],
579
+ AgentRewards,
548
580
  Recording,
549
581
  ]:
550
582
  """Reset attack graph and reinitialize agents"""
551
583
  logger.info('Resetting MAL Simulator.')
584
+ attack_graph = static_data.attack_graph
585
+ settings = static_data.sim_settings
552
586
 
553
587
  # Re-calculate initial simulator state
588
+ graph_state = compute_initial_graph_state(attack_graph, settings, rng)
554
589
  sim_state = create_simulator_state(
555
- sim_state.attack_graph,
556
- sim_state.settings,
590
+ attack_graph,
591
+ graph_state,
592
+ settings,
557
593
  rng,
558
- sim_state.global_rewards,
559
- sim_state.global_false_positive_rates,
560
- sim_state.global_false_negative_rates,
561
- sim_state.global_actionability,
562
- sim_state.global_observability,
594
+ static_data.rewards,
595
+ static_data.false_positive_rates,
596
+ static_data.false_negative_rates,
597
+ static_data.node_actionabilities,
598
+ static_data.node_observabilities,
563
599
  )
564
600
 
565
- # Reset rewards and recording
566
- agent_rewards: dict[str, float] = {}
567
- recording: Recording = defaultdict(dict)
568
-
569
601
  agent_states, alive_agents, agent_rewards = reset_agents(
570
602
  sim_state,
571
603
  agent_settings,
@@ -576,9 +608,9 @@ def reset(
576
608
  )
577
609
  # Upload initial state to the REST API
578
610
  if rest_api_client:
579
- rest_api_client.upload_initial_state(sim_state.attack_graph)
611
+ rest_api_client.upload_initial_state(attack_graph)
580
612
 
581
- return agent_states, alive_agents, sim_state, agent_rewards, recording
613
+ return agent_states, alive_agents, sim_state, agent_rewards, defaultdict(dict)
582
614
 
583
615
 
584
616
  def _pre_step_check(
@@ -596,7 +628,7 @@ def _pre_step_check(
596
628
  print(msg)
597
629
 
598
630
  if done(alive_agents):
599
- msg = 'Simulation is done, step has no effect'
631
+ msg = 'Simulation is done but you can still step'
600
632
  logger.warning(msg)
601
633
  print(msg)
602
634
 
@@ -611,7 +643,6 @@ def step(
611
643
  agent_states: AgentStates,
612
644
  alive_agents: set[str],
613
645
  rng: np.random.Generator,
614
- live_agents: set[str],
615
646
  agent_rewards: dict[str, float],
616
647
  performed_attacks_func: Callable[[MalSimAttackerState], frozenset[AttackGraphNode]],
617
648
  enabled_defenses_func: Callable[[MalSimDefenderState], frozenset[AttackGraphNode]],
@@ -629,7 +660,7 @@ def step(
629
660
  - A dictionary containing the agent state views keyed by agent names
630
661
  """
631
662
 
632
- _pre_step_check(agent_states, live_agents, actions)
663
+ _pre_step_check(agent_states, alive_agents, actions)
633
664
 
634
665
  # Populate these from the results for all agents' actions.
635
666
  step_compromised_nodes: list[AttackGraphNode] = []
@@ -638,9 +669,7 @@ def step(
638
669
  current_iteration = 0
639
670
 
640
671
  # Perform defender actions first
641
- for defender_state in get_defender_agents(
642
- agent_states, live_agents, only_alive=True
643
- ):
672
+ for defender_state in get_defender_agents(agent_states, alive_agents):
644
673
  agent_actions = list(
645
674
  full_names_or_nodes_to_nodes(
646
675
  sim_state.attack_graph, actions.get(defender_state.name, [])
@@ -654,9 +683,7 @@ def step(
654
683
  step_nodes_made_unviable |= unviable
655
684
 
656
685
  # Perform attacker actions afterwards
657
- for attacker_state in get_attacker_agents(
658
- agent_states, live_agents, only_alive=True
659
- ):
686
+ for attacker_state in get_attacker_agents(agent_states, alive_agents):
660
687
  agent_actions = list(
661
688
  full_names_or_nodes_to_nodes(
662
689
  sim_state.attack_graph, actions.get(attacker_state.name, [])
@@ -691,35 +718,34 @@ def step(
691
718
  sim_state.settings.ttc_mode,
692
719
  )
693
720
 
694
- # Update defender states and remove 'dead' agents of any type
695
- for agent_name in live_agents.copy():
696
- agent_state = agent_states[agent_name]
697
-
698
- if isinstance(agent_state, MalSimDefenderState):
699
- current_iteration = agent_state.iteration
700
- # Update defender state
701
- updated_defender_state = create_defender_state(
702
- sim_state=sim_state,
703
- name=agent_name,
704
- step_compromised_nodes=set(step_compromised_nodes),
705
- step_enabled_defenses=set(step_enabled_defenses),
706
- step_nodes_made_unviable=step_nodes_made_unviable,
707
- previous_state=agent_state,
708
- rng=rng,
709
- )
710
- agent_states[agent_name] = updated_defender_state
721
+ # Update defender states and rewards
722
+ for defender_state in get_defender_agents(agent_states, alive_agents):
723
+ current_iteration = defender_state.iteration
724
+ # Update defender state
725
+ updated_defender_state = create_defender_state(
726
+ sim_state=sim_state,
727
+ name=defender_state.name,
728
+ step_compromised_nodes=set(step_compromised_nodes),
729
+ step_enabled_defenses=set(step_enabled_defenses),
730
+ step_nodes_made_unviable=step_nodes_made_unviable,
731
+ previous_state=defender_state,
732
+ rng=rng,
733
+ )
734
+ agent_states[defender_state.name] = updated_defender_state
711
735
 
712
- # Update defender reward
713
- agent_rewards[agent_state.name] = defender_step_reward(
714
- enabled_defenses_func,
715
- enabled_attacks_func,
716
- updated_defender_state,
717
- )
736
+ # Update defender reward
737
+ agent_rewards[defender_state.name] = defender_step_reward(
738
+ enabled_defenses_func,
739
+ enabled_attacks_func,
740
+ updated_defender_state,
741
+ )
718
742
 
743
+ # Mark terminated agents as dead
744
+ for agent_name in alive_agents.copy():
719
745
  # Remove agents that are terminated
720
- if agent_is_terminated(agent_states, alive_agents, agent_state.name):
721
- logger.info('Agent %s terminated', agent_state.name)
722
- live_agents.remove(agent_state.name)
746
+ if agent_is_terminated(agent_states, alive_agents, agent_name):
747
+ logger.info('Agent %s terminated', agent_name)
748
+ alive_agents.remove(agent_name)
723
749
 
724
750
  # the way current_iteration is used here is flawed.
725
751
  if rest_api_client:
@@ -728,4 +754,4 @@ def step(
728
754
  current_iteration,
729
755
  )
730
756
 
731
- return agent_states, recording, sim_state, agent_rewards, live_agents
757
+ return agent_states, recording, sim_state, agent_rewards, alive_agents
@@ -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, compute_initial_graph_state
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
- compute_initial_graph_state(attack_graph, sim_settings, rng),
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 {}),
@@ -16,7 +16,6 @@ class KeyboardAgent(DecisionAgent):
16
16
 
17
17
  def __init__(self, _: Any, **kwargs: Any):
18
18
  super().__init__(**kwargs)
19
- logger.info('Creating KeyboardAgent')
20
19
 
21
20
  def get_next_action(
22
21
  self, agent_state: MalSimAgentState, **kwargs: Any
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "mal-simulator"
3
- version = "2.0.1"
3
+ version = "2.3.0"
4
4
  authors = [
5
5
  { name="Andrei Buhaiu", email="buhaiu@kth.se" },
6
6
  { name="Joakim Loxdal", email="loxdal@kth.se" },
@@ -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)
@@ -959,6 +985,29 @@ def test_simulator_false_positives() -> None:
959
985
  assert len(defender_state.observed_nodes) > len(defender_state.compromised_nodes)
960
986
 
961
987
 
988
+ def test_simulator_false_positives_after_done() -> None:
989
+ """Create a simulator with false positives"""
990
+
991
+ scenario = Scenario.load_from_file(
992
+ 'tests/testdata/scenarios/traininglang_fp_fn_scenario.yml'
993
+ )
994
+
995
+ scenario.false_negative_rates = None
996
+ sim = MalSimulator.from_scenario(scenario, MalSimulatorSettings(seed=100))
997
+ run_simulation(sim, scenario.agent_settings)
998
+ assert sim.done()
999
+
1000
+ # Simulation is done, but we can still observe false positives
1001
+ false_positives: set[AttackGraphNode] = set()
1002
+ for _ in range(100):
1003
+ states = sim.step({})
1004
+ defender_state = states['defender']
1005
+ assert isinstance(defender_state, MalSimDefenderState)
1006
+ false_positives |= defender_state.observed_nodes
1007
+
1008
+ assert false_positives
1009
+
1010
+
962
1011
  def test_simulator_false_positives_reset() -> None:
963
1012
  """Create a simulator with false positives"""
964
1013
 
@@ -1118,8 +1167,16 @@ def test_simulator_multiple_attackers() -> None:
1118
1167
  sim.get_node('Host:1:connect'),
1119
1168
  ],
1120
1169
  },
1121
- 2: {'Defender1': [], 'Attacker1': [sim.get_node('Host:0:authenticate')]},
1122
- 3: {'Defender1': [], 'Attacker1': [sim.get_node('Host:0:access')]},
1170
+ 2: {
1171
+ 'Defender1': [],
1172
+ 'Attacker1': [sim.get_node('Host:0:authenticate')],
1173
+ 'Attacker2': [],
1174
+ },
1175
+ 3: {
1176
+ 'Defender1': [],
1177
+ 'Attacker1': [sim.get_node('Host:0:access')],
1178
+ 'Attacker2': [],
1179
+ },
1123
1180
  4: {
1124
1181
  'Defender1': [],
1125
1182
  'Attacker1': [
@@ -1127,8 +1184,13 @@ def test_simulator_multiple_attackers() -> None:
1127
1184
  sim.get_node('Data:2:modify'),
1128
1185
  sim.get_node('Network:3:access'),
1129
1186
  ],
1187
+ 'Attacker2': [],
1188
+ },
1189
+ 5: {
1190
+ 'Defender1': [],
1191
+ 'Attacker1': [sim.get_node('Host:1:connect')],
1192
+ 'Attacker2': [],
1130
1193
  },
1131
- 5: {'Defender1': [], 'Attacker1': [sim.get_node('Host:1:connect')]},
1132
1194
  }
1133
1195
 
1134
1196
 
@@ -1545,6 +1607,59 @@ def test_actions_effects() -> None:
1545
1607
  'SecretData:attemptRead',
1546
1608
  }
1547
1609
 
1610
+ # But in the recording each step performs an action and an effect
1611
+ expected_recording = {
1612
+ 1: {'Attacker': ['Net1:attemptScan', 'Net1:scan']},
1613
+ 2: {'Attacker': ['ComputerA:attemptConnect', 'ComputerA:connect']},
1614
+ 3: {'Attacker': ['Net2:attemptScan', 'Net2:scan']},
1615
+ 4: {'Attacker': ['ComputerA:attemptAccess']},
1616
+ 5: {'Attacker': ['Net3:attemptScan', 'Net3:scan']},
1617
+ 6: {'Attacker': ['ComputerD:attemptConnect', 'ComputerD:connect']},
1618
+ 7: {'Attacker': ['ComputerD:attemptAccess', 'ComputerD:access']},
1619
+ 8: {'Attacker': ['SecretData:attemptRead', 'SecretData:read']},
1620
+ }
1621
+
1622
+ # Convert recording nodes to full names for comparison
1623
+ for i in sim.recording:
1624
+ assert len(sim.recording[i]['Attacker']) == len(
1625
+ expected_recording[i]['Attacker']
1626
+ )
1627
+ for j, node in enumerate(sim.recording[i]['Attacker']):
1628
+ assert node.full_name == expected_recording[i]['Attacker'][j]
1629
+
1630
+
1631
+ def test_actions_effects_entrypoints() -> None:
1632
+ """Verify actions and effects works as intended with entrypoints"""
1633
+
1634
+ scenario = Scenario.load_from_file(
1635
+ 'tests/testdata/scenarios/actions_effects_scenario_entrypoint_with_effects.yml'
1636
+ )
1637
+ sim = MalSimulator.from_scenario(
1638
+ scenario,
1639
+ sim_settings=MalSimulatorSettings(
1640
+ ttc_mode=TTCMode.DISABLED,
1641
+ run_defense_step_bernoullis=False,
1642
+ run_attack_step_bernoullis=False,
1643
+ attack_surface_skip_unnecessary=False,
1644
+ compromise_entrypoints_at_start=True,
1645
+ attacker_reward_mode=RewardMode.SAMPLE_TTC,
1646
+ seed=1,
1647
+ ),
1648
+ )
1649
+ selected_actions = run_simulation(sim, scenario.agent_settings)
1650
+
1651
+ # Attacker only selects steps starting with 'attempt'
1652
+ assert {n.full_name for n in selected_actions['Attacker']} == {
1653
+ 'Net1:attemptScan',
1654
+ 'Net2:attemptScan',
1655
+ 'Net3:attemptScan',
1656
+ 'ComputerD:attemptConnect',
1657
+ 'ComputerA:attemptConnect',
1658
+ 'ComputerD:attemptAccess',
1659
+ 'ComputerC:attemptConnect',
1660
+ 'SecretData:attemptRead',
1661
+ }
1662
+
1548
1663
  # But in the recording each step performs an action and an effect
1549
1664
  for i in sim.recording:
1550
1665
  action = sim.recording[i]['Attacker'][0]
File without changes
File without changes
File without changes
File without changes