mal-simulator 2.6.0__tar.gz → 3.0.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 (82) hide show
  1. {mal_simulator-2.6.0/mal_simulator.egg-info → mal_simulator-3.0.0}/PKG-INFO +1 -1
  2. {mal_simulator-2.6.0 → mal_simulator-3.0.0/mal_simulator.egg-info}/PKG-INFO +1 -1
  3. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/mal_simulator.egg-info/SOURCES.txt +6 -3
  4. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/__init__.py +2 -2
  5. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/config/agent_settings.py +36 -7
  6. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/config/agent_settings_factories.py +24 -1
  7. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/config/sim_settings.py +0 -3
  8. mal_simulator-3.0.0/malsim/envs/__init__.py +12 -0
  9. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/envs/graph/graph_env.py +32 -8
  10. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/agent_state.py +0 -2
  11. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/attack_surface.py +2 -6
  12. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/attacker_state.py +1 -7
  13. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/attacker_state_factories.py +35 -29
  14. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/attacker_step.py +9 -6
  15. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/defender_state.py +7 -8
  16. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/defender_state_factories.py +13 -3
  17. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/defender_step.py +2 -15
  18. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/defense_surface.py +3 -2
  19. mal_simulator-3.0.0/malsim/mal_simulator/event_logger.py +141 -0
  20. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/graph_processing.py +2 -2
  21. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/graph_state.py +3 -11
  22. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/graph_utils.py +27 -4
  23. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/simulator.py +9 -9
  24. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/simulator_state.py +16 -3
  25. mal_simulator-3.0.0/malsim/mal_simulator/simulator_static_data.py +14 -0
  26. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/pyproject.toml +1 -1
  27. mal_simulator-3.0.0/tests/test_attacker.py +250 -0
  28. mal_simulator-3.0.0/tests/test_attacker_settings.py +33 -0
  29. mal_simulator-3.0.0/tests/test_defender.py +36 -0
  30. mal_simulator-3.0.0/tests/test_event_logger.py +155 -0
  31. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/tests/test_graph_processing.py +78 -0
  32. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/tests/test_mal_simulator.py +86 -31
  33. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/tests/test_scenario.py +1 -0
  34. mal_simulator-2.6.0/malsim/envs/__init__.py +0 -18
  35. mal_simulator-2.6.0/malsim/envs/legacy/gym_envs.py +0 -332
  36. mal_simulator-2.6.0/malsim/envs/legacy/malsim_vectorized_obs_env.py +0 -564
  37. mal_simulator-2.6.0/malsim/policies/defenders/__init__.py +0 -0
  38. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/AUTHORS +0 -0
  39. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/LICENSE +0 -0
  40. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/README.md +0 -0
  41. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/mal_simulator.egg-info/dependency_links.txt +0 -0
  42. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/mal_simulator.egg-info/entry_points.txt +0 -0
  43. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/mal_simulator.egg-info/requires.txt +0 -0
  44. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/mal_simulator.egg-info/top_level.txt +0 -0
  45. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/__main__.py +0 -0
  46. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/config/__init__.py +0 -0
  47. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/config/node_property_rule.py +0 -0
  48. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/envs/base_classes.py +0 -0
  49. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/envs/graph/__init__.py +0 -0
  50. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/envs/graph/mal_spaces.py +0 -0
  51. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/envs/graph/serialization.py +0 -0
  52. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/envs/graph/utils.py +0 -0
  53. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/envs/graph/wrapper.py +0 -0
  54. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/__init__.py +0 -0
  55. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/agent_states.py +0 -0
  56. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/false_alerts.py +0 -0
  57. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/node_getters.py +0 -0
  58. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/observability.py +0 -0
  59. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/reset_agent.py +0 -0
  60. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/rewards.py +0 -0
  61. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/run_simulation.py +0 -0
  62. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/state_query.py +0 -0
  63. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/mal_simulator/ttc_utils.py +0 -0
  64. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/__init__.py +0 -0
  65. {mal_simulator-2.6.0/malsim/envs/legacy → mal_simulator-3.0.0/malsim/policies/attackers}/__init__.py +0 -0
  66. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/attackers/searchers.py +0 -0
  67. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/attackers/shortest_path.py +0 -0
  68. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/attackers/ttc_soft_min.py +0 -0
  69. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/decision_agent.py +0 -0
  70. {mal_simulator-2.6.0/malsim/policies/attackers → mal_simulator-3.0.0/malsim/policies/defenders}/__init__.py +0 -0
  71. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/defenders/heuristic_agent.py +0 -0
  72. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/keyboard_input.py +0 -0
  73. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/passive_agent.py +0 -0
  74. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/random_agent.py +0 -0
  75. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/policies/utils/path_finding.py +0 -0
  76. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/scenario/__init__.py +0 -0
  77. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/scenario/scenario.py +0 -0
  78. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/types.py +0 -0
  79. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/malsim/visualization/malsim_gui_client.py +0 -0
  80. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/setup.cfg +0 -0
  81. {mal_simulator-2.6.0 → mal_simulator-3.0.0}/tests/test_main.py +0 -0
  82. {mal_simulator-2.6.0 → mal_simulator-3.0.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.6.0
3
+ Version: 3.0.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.6.0
3
+ Version: 3.0.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
@@ -24,9 +24,6 @@ malsim/envs/graph/mal_spaces.py
24
24
  malsim/envs/graph/serialization.py
25
25
  malsim/envs/graph/utils.py
26
26
  malsim/envs/graph/wrapper.py
27
- malsim/envs/legacy/__init__.py
28
- malsim/envs/legacy/gym_envs.py
29
- malsim/envs/legacy/malsim_vectorized_obs_env.py
30
27
  malsim/mal_simulator/__init__.py
31
28
  malsim/mal_simulator/agent_state.py
32
29
  malsim/mal_simulator/agent_states.py
@@ -38,6 +35,7 @@ malsim/mal_simulator/defender_state.py
38
35
  malsim/mal_simulator/defender_state_factories.py
39
36
  malsim/mal_simulator/defender_step.py
40
37
  malsim/mal_simulator/defense_surface.py
38
+ malsim/mal_simulator/event_logger.py
41
39
  malsim/mal_simulator/false_alerts.py
42
40
  malsim/mal_simulator/graph_processing.py
43
41
  malsim/mal_simulator/graph_state.py
@@ -49,6 +47,7 @@ malsim/mal_simulator/rewards.py
49
47
  malsim/mal_simulator/run_simulation.py
50
48
  malsim/mal_simulator/simulator.py
51
49
  malsim/mal_simulator/simulator_state.py
50
+ malsim/mal_simulator/simulator_static_data.py
52
51
  malsim/mal_simulator/state_query.py
53
52
  malsim/mal_simulator/ttc_utils.py
54
53
  malsim/policies/__init__.py
@@ -66,6 +65,10 @@ malsim/policies/utils/path_finding.py
66
65
  malsim/scenario/__init__.py
67
66
  malsim/scenario/scenario.py
68
67
  malsim/visualization/malsim_gui_client.py
68
+ tests/test_attacker.py
69
+ tests/test_attacker_settings.py
70
+ tests/test_defender.py
71
+ tests/test_event_logger.py
69
72
  tests/test_graph_processing.py
70
73
  tests/test_main.py
71
74
  tests/test_mal_simulator.py
@@ -1,4 +1,4 @@
1
- # MAL Simulator v2.6.0
1
+ # MAL Simulator v3.0.0
2
2
  # Copyright 2025, Andrei Buhaiu.
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,7 +26,7 @@ from malsim.scenario import Scenario
26
26
  from malsim.config import AttackerSettings, DefenderSettings
27
27
 
28
28
  __title__ = 'malsim'
29
- __version__ = '2.6.0'
29
+ __version__ = '3.0.0'
30
30
  __authors__ = [
31
31
  'Andrei Buhaiu',
32
32
  'Joakim Loxdal',
@@ -47,12 +47,27 @@ class AgentRuntimeMixin:
47
47
  T = TypeVar('T', bound=AttackGraphNode | str, covariant=True)
48
48
 
49
49
 
50
+ def _entry_points_to_dict(
51
+ entry_points: tuple[Set[T], ...] | Set[T],
52
+ ) -> list[Set[str]] | set[str]:
53
+ if isinstance(entry_points, Set):
54
+ return {
55
+ ep.full_name if isinstance(ep, AttackGraphNode) else ep
56
+ for ep in entry_points
57
+ }
58
+ else:
59
+ return [
60
+ {ep.full_name if isinstance(ep, AttackGraphNode) else ep for ep in eps}
61
+ for eps in entry_points
62
+ ]
63
+
64
+
50
65
  @dataclass
51
66
  class AttackerSettings(AgentRuntimeMixin, Generic[T]):
52
67
  """Settings for an attacker in a scenario."""
53
68
 
54
69
  name: str
55
- entry_points: Set[T]
70
+ entry_points: tuple[Set[T], ...] | Set[T]
56
71
  policy: type | None = None
57
72
  actionable_steps: NodePropertyRule[bool] | None = None
58
73
  rewards: NodePropertyRule[float] | None = None
@@ -67,10 +82,7 @@ class AttackerSettings(AgentRuntimeMixin, Generic[T]):
67
82
  def to_dict(self) -> dict[str, Any]:
68
83
  d: dict[str, Any] = {
69
84
  'type': AgentType.ATTACKER.value,
70
- 'entry_points': {
71
- n.full_name if isinstance(n, AttackGraphNode) else n
72
- for n in self.entry_points
73
- },
85
+ 'entry_points': _entry_points_to_dict(self.entry_points),
74
86
  }
75
87
  if self.goals:
76
88
  d['goals'] = {
@@ -90,6 +102,23 @@ class AttackerSettings(AgentRuntimeMixin, Generic[T]):
90
102
  if isinstance(self.reward_mode, str):
91
103
  self.reward_mode = RewardMode[self.reward_mode]
92
104
 
105
+ def _convert_entry_points_to_nodes(
106
+ self, attack_graph: AttackGraph, entry_points: tuple[Set[T], ...] | Set[T]
107
+ ) -> tuple[Set[AttackGraphNode], ...] | Set[AttackGraphNode]:
108
+ """
109
+ Convert entry points from full names to nodes,
110
+ handling both single and multiple entry point sets
111
+ """
112
+ if isinstance(entry_points, Set):
113
+ return frozenset(
114
+ full_name_or_node_to_node(attack_graph, ep) for ep in entry_points
115
+ )
116
+ else:
117
+ return tuple(
118
+ frozenset(full_name_or_node_to_node(attack_graph, ep) for ep in eps)
119
+ for eps in entry_points
120
+ )
121
+
93
122
  def convert_to_attack_graph_nodes(
94
123
  self, attack_graph: AttackGraph
95
124
  ) -> AttackerSettings[AttackGraphNode]:
@@ -98,8 +127,8 @@ class AttackerSettings(AgentRuntimeMixin, Generic[T]):
98
127
  goals=frozenset(
99
128
  full_name_or_node_to_node(attack_graph, g) for g in self.goals
100
129
  ),
101
- entry_points=frozenset(
102
- full_name_or_node_to_node(attack_graph, ep) for ep in self.entry_points
130
+ entry_points=self._convert_entry_points_to_nodes(
131
+ attack_graph, self.entry_points
103
132
  ),
104
133
  name=self.name,
105
134
  policy=self.policy,
@@ -1,3 +1,4 @@
1
+ from collections.abc import Set
1
2
  from typing import Any
2
3
 
3
4
 
@@ -59,6 +60,28 @@ def _validate_agent_dict(d: dict[str, Any]) -> dict[str, Any]:
59
60
  return d
60
61
 
61
62
 
63
+ def _load_entry_points(d: Any) -> tuple[Set[str], ...] | Set[str]:
64
+ if d is None:
65
+ return frozenset()
66
+ elif isinstance(d, set):
67
+ return frozenset(d)
68
+ elif isinstance(d, (list, tuple)):
69
+ # Can be a list of strings or a list of sets/lists with strings
70
+ if all(isinstance(ep, str) for ep in d):
71
+ return frozenset(d)
72
+ elif all(isinstance(ep, (set, list)) for ep in d):
73
+ return tuple(frozenset(ep) for ep in d)
74
+ else:
75
+ raise ValueError(
76
+ 'entry_points list must contain either '
77
+ 'all strings or all sets/lists of strings'
78
+ )
79
+ else:
80
+ raise ValueError(
81
+ f'entry_points must be a set or list of strings, got {type(d)}'
82
+ )
83
+
84
+
62
85
  def agent_settings_from_dict(
63
86
  name: str,
64
87
  d: dict[str, Any],
@@ -84,7 +107,7 @@ def agent_settings_from_dict(
84
107
  if agent_type == AgentType.ATTACKER:
85
108
  return AttackerSettings(
86
109
  name=name,
87
- entry_points=frozenset(d['entry_points']),
110
+ entry_points=_load_entry_points(d.get('entry_points')),
88
111
  goals=frozenset(d.get('goals', [])),
89
112
  ttc_dists=NodePropertyRule.from_optional_dict(d.get('ttc_overrides')),
90
113
  policy=policy,
@@ -31,9 +31,6 @@ class AttackSurfaceSettings:
31
31
  # attack_surface_skip_compromised
32
32
  # - if true do not add already compromised nodes to the attack surface
33
33
  skip_compromised: bool = True
34
- # attack_surface_skip_unviable
35
- # - if true do not add unviable nodes to the attack surface
36
- skip_unviable: bool = True
37
34
  # attack_surface_skip_unnecessary
38
35
  # - if true do not add unnecessary nodes to the attack surface
39
36
  skip_unnecessary: bool = False
@@ -0,0 +1,12 @@
1
+ from .graph.graph_env import (
2
+ AttackerGraphEnv,
3
+ DefenderGraphEnv,
4
+ register_graph_envs,
5
+ )
6
+
7
+ # not needed, used to silence ruff F401
8
+ __all__ = [
9
+ 'AttackerGraphEnv',
10
+ 'DefenderGraphEnv',
11
+ 'register_graph_envs',
12
+ ]
@@ -2,6 +2,7 @@ import gymnasium as gym
2
2
  from typing import Any
3
3
 
4
4
  from malsim.config.sim_settings import AttackSurfaceSettings
5
+ from malsim.policies.decision_agent import DecisionAgent
5
6
 
6
7
  from .mal_spaces import (
7
8
  MALObsAttackStepSpace,
@@ -16,6 +17,7 @@ from os import PathLike
16
17
 
17
18
  from malsim.scenario.scenario import Scenario
18
19
  from malsim.config.agent_settings import AgentType
20
+ from malsim.config.agent_settings import AgentSettings
19
21
 
20
22
  from malsim.mal_simulator import (
21
23
  MalSimulator,
@@ -234,6 +236,17 @@ class MalSimGraph(ParallelEnv[str, MALObsInstance, np.int64]):
234
236
  self.possible_agents = list(self.sim.agent_states)
235
237
  self.agents = list(self.sim.alive_agents)
236
238
 
239
+ self.internal_agents: AgentSettings = {
240
+ agent_name: settings
241
+ for agent_name, settings in self.sim.agent_settings.items()
242
+ if settings.policy is not None
243
+ }
244
+ self.external_agents: AgentSettings = {
245
+ agent_name: settings
246
+ for agent_name, settings in self.sim.agent_settings.items()
247
+ if settings.policy is None
248
+ }
249
+
237
250
  def reset(
238
251
  self, seed: int | None = None, options: dict[str, Any] | None = None
239
252
  ) -> tuple[dict[str, MALObsInstance], dict[str, dict[str, Any]]]:
@@ -283,33 +296,44 @@ class MalSimGraph(ParallelEnv[str, MALObsInstance, np.int64]):
283
296
  else []
284
297
  for agent_name, action_idx in actions.items()
285
298
  }
299
+ for agent_name in self.internal_agents:
300
+ agent: DecisionAgent = self.internal_agents[agent_name].agent # type: ignore
301
+ action = agent.get_next_action(self.sim.agent_states[agent_name])
302
+ action_nodes[agent_name] = [action] if action is not None else []
286
303
  states = self.sim.step(action_nodes)
287
304
  self.agents = list(self.sim.alive_agents)
288
305
  self._obs = {
289
306
  agent_name: (
290
307
  full_obs2attacker_obs(
291
308
  self._full_obs,
292
- state,
309
+ self.sim.agent_states[agent_name], # type: ignore
293
310
  self.lang_serializer,
294
311
  see_defense_steps=self.see_def_steps,
295
312
  )
296
- if isinstance(state, AttackerState)
297
- else full_obs2defender_obs(self._full_obs, state, self.lang_serializer)
313
+ if isinstance(self.sim.agent_states[agent_name], AttackerState)
314
+ else full_obs2defender_obs(
315
+ self._full_obs,
316
+ self.sim.agent_states[agent_name], # type: ignore
317
+ self.lang_serializer,
318
+ )
298
319
  )
299
- for agent_name, state in states.items()
320
+ for agent_name in self.external_agents
300
321
  }
301
322
  for agent_name, obs in self._obs.items():
302
323
  self.action_space(agent_name)._mask = obs.steps.action_mask
303
324
  rewards = {
304
325
  agent_name: x
305
- for agent_name, state in states.items()
306
- if (x := self.sim.agent_reward(state)) is not None
326
+ for agent_name in self.external_agents
327
+ if (x := self.sim.agent_reward(self.sim.agent_states[agent_name]))
328
+ is not None
307
329
  }
308
330
  terminations = {
309
331
  agent_name: self.sim.agent_is_terminated(agent_name)
310
- for agent_name in states
332
+ for agent_name in self.external_agents
333
+ }
334
+ truncations = {
335
+ agent_name: self.sim.done() for agent_name in self.external_agents
311
336
  }
312
- truncations = {agent_name: self.sim.done() for agent_name in states}
313
337
  return (
314
338
  self._obs,
315
339
  rewards,
@@ -21,7 +21,5 @@ class AgentState:
21
21
  # Contains the order of performed nodes
22
22
  performed_nodes_order: Mapping[int, Set[AttackGraphNode]]
23
23
 
24
- unviable_nodes: Set[AttackGraphNode]
25
-
26
24
  # The iteration this state was created in
27
25
  iteration: int
@@ -9,7 +9,7 @@ from malsim.mal_simulator.graph_utils import (
9
9
  node_is_actionable,
10
10
  node_is_necessary,
11
11
  node_is_traversable,
12
- node_is_viable,
12
+ node_is_blocked,
13
13
  )
14
14
 
15
15
  if TYPE_CHECKING:
@@ -64,15 +64,11 @@ def get_attack_surface(
64
64
  from_nodes = from_nodes if from_nodes is not None else performed_nodes
65
65
 
66
66
  skip_compromised = settings.skip_compromised
67
- skip_unviable = settings.skip_unviable
68
67
  skip_unnecessary = settings.skip_unnecessary
69
68
 
70
69
  def uncompromised(node: AttackGraphNode) -> bool:
71
70
  return not (skip_compromised and node in performed_nodes)
72
71
 
73
- def viable(node: AttackGraphNode) -> bool:
74
- return node_is_viable(sim_state, node)
75
-
76
72
  def necessary(node: AttackGraphNode) -> bool:
77
73
  return node_is_necessary(sim_state, node)
78
74
 
@@ -87,8 +83,8 @@ def get_attack_surface(
87
83
  is_action = node.causal_mode != 'effect'
88
84
  return (
89
85
  is_action
86
+ and not node_is_blocked(sim_state, node)
90
87
  and (uncompromised(node) if skip_compromised else True)
91
- and (viable(node) if skip_unviable else True)
92
88
  and (necessary(node) if skip_unnecessary else True)
93
89
  and actionable(node)
94
90
  and traversable(node)
@@ -12,6 +12,7 @@ class AttackerState(AgentState):
12
12
  """Stores the state of an attacker in the simulator"""
13
13
 
14
14
  settings: AttackerSettings[AttackGraphNode]
15
+ entry_points: Set[AttackGraphNode]
15
16
  # Number of attempts to compromise a step (used for ttc caculations)
16
17
  num_attempts: Mapping[AttackGraphNode, int]
17
18
  # Steps attempted but not succeeded (because of TTC value)
@@ -76,10 +77,3 @@ class AttackerState(AgentState):
76
77
  self.previous_state.action_surface if self.previous_state else set()
77
78
  )
78
79
  return previous_action_surface - self.action_surface
79
-
80
- @property
81
- def step_unviable_nodes(self) -> Set[AttackGraphNode]:
82
- previous_unviable_nodes = (
83
- self.previous_state.unviable_nodes if self.previous_state else set()
84
- )
85
- return self.unviable_nodes - previous_unviable_nodes
@@ -38,16 +38,15 @@ def create_attacker_state(
38
38
  attack_surface_settings: AttackSurfaceSettings,
39
39
  attacker_settings: AttackerSettings[AttackGraphNode],
40
40
  name: str,
41
+ entry_points: Set[AttackGraphNode],
41
42
  new_performed_nodes: Set[AttackGraphNode],
42
43
  ttc_values: Mapping[AttackGraphNode, float],
43
44
  impossible_steps: Set[AttackGraphNode],
44
45
  new_attempted_nodes: Set[AttackGraphNode] = frozenset(),
45
- new_unviable_nodes: Set[AttackGraphNode] = frozenset(),
46
46
  previous_state: AttackerState | None = None,
47
47
  ) -> AttackerState:
48
48
  """
49
49
  Update a previous attacker state based on what the agent compromised
50
- and what nodes became unviable.
51
50
  """
52
51
 
53
52
  previous_performed_nodes = (
@@ -59,43 +58,28 @@ def create_attacker_state(
59
58
  previous_attempted_nodes = (
60
59
  previous_state.attempted_nodes if previous_state else set()
61
60
  )
62
- previous_action_surface = previous_state.action_surface if previous_state else set()
63
61
  previous_num_attempts = (
64
62
  previous_state.num_attempts
65
63
  if previous_state
66
64
  else dict.fromkeys(sim_state.attack_graph.attack_steps, 0)
67
65
  )
68
- previous_unviable_nodes = previous_state.unviable_nodes if previous_state else set()
69
66
 
70
67
  performed_nodes = previous_performed_nodes | new_performed_nodes
71
68
  performed_nodes_order = dict(previous_performed_nodes_order)
72
- unviable_nodes = previous_unviable_nodes | new_unviable_nodes
73
69
  attempted_nodes = previous_attempted_nodes | new_attempted_nodes
74
70
  num_attempts = dict(previous_num_attempts)
75
71
  for node in new_attempted_nodes:
76
72
  num_attempts[node] += 1
77
73
 
78
- # Build on previous attack surface (for performance)
79
- action_surface_additions = (
80
- get_attack_surface(
81
- attack_surface_settings,
82
- sim_state,
83
- attacker_settings.actionable_steps,
84
- previous_performed_nodes | new_performed_nodes,
85
- from_nodes=new_performed_nodes,
86
- )
87
- - previous_action_surface
74
+ action_surface = get_attack_surface(
75
+ attack_surface_settings,
76
+ sim_state,
77
+ attacker_settings.actionable_steps,
78
+ previous_performed_nodes | new_performed_nodes,
88
79
  )
89
80
 
90
81
  if not previous_state and not sim_state.settings.compromise_entrypoints_at_start:
91
- action_surface_additions |= attacker_settings.entry_points
92
-
93
- action_surface_removals = set(
94
- (new_unviable_nodes & previous_action_surface) | new_performed_nodes
95
- )
96
- action_surface = frozenset(
97
- (previous_action_surface - action_surface_removals) | action_surface_additions
98
- )
82
+ action_surface |= entry_points
99
83
 
100
84
  iteration = previous_state.iteration if previous_state else 0
101
85
  if new_performed_nodes:
@@ -103,13 +87,13 @@ def create_attacker_state(
103
87
 
104
88
  return AttackerState(
105
89
  name,
90
+ entry_points=entry_points,
106
91
  sim_state=sim_state,
107
92
  iteration=iteration + 1,
108
93
  performed_nodes_order=performed_nodes_order,
109
94
  settings=attacker_settings,
110
95
  ttc_values=ttc_values,
111
96
  impossible_steps=impossible_steps,
112
- unviable_nodes=unviable_nodes,
113
97
  performed_nodes=frozenset(performed_nodes),
114
98
  attempted_nodes=frozenset(attempted_nodes),
115
99
  action_surface=action_surface,
@@ -133,6 +117,31 @@ def get_entrypoint_compromises(
133
117
  return step_compromised_nodes
134
118
 
135
119
 
120
+ def get_entry_points(
121
+ sim_state: MalSimulatorState,
122
+ attacker_settings: AttackerSettings[AttackGraphNode],
123
+ rng: np.random.Generator,
124
+ ) -> Set[AttackGraphNode]:
125
+ """
126
+ Get entry points as set of AttackGraphNodes from attacker settings.
127
+ If multiple sets of entry points are given, sample one set from the options.
128
+ """
129
+
130
+ if isinstance(attacker_settings.entry_points, Set):
131
+ return frozenset(
132
+ full_names_or_nodes_to_nodes(
133
+ sim_state.attack_graph, attacker_settings.entry_points
134
+ )
135
+ )
136
+ else:
137
+ # Multiple potential entry point sets given
138
+ # - sample one set of entry points from the options
139
+ chosen_entry_points = rng.choice(list(attacker_settings.entry_points)) # type: ignore
140
+ return set(
141
+ full_names_or_nodes_to_nodes(sim_state.attack_graph, chosen_entry_points)
142
+ )
143
+
144
+
136
145
  def initial_attacker_state(
137
146
  sim_state: MalSimulatorState,
138
147
  sim_settings: MalSimulatorSettings,
@@ -154,11 +163,7 @@ def initial_attacker_state(
154
163
  sim_state.graph_state.impossible_attack_steps,
155
164
  )
156
165
  )
157
- entry_points = set(
158
- full_names_or_nodes_to_nodes(
159
- sim_state.attack_graph, attacker_settings.entry_points
160
- )
161
- )
166
+ entry_points = get_entry_points(sim_state, attacker_settings, rng)
162
167
  new_compromised_nodes: Set[AttackGraphNode] = set()
163
168
 
164
169
  if sim_state.settings.compromise_entrypoints_at_start:
@@ -172,6 +177,7 @@ def initial_attacker_state(
172
177
  ttc_values=ttc_values,
173
178
  impossible_steps=impossible_steps,
174
179
  new_performed_nodes=new_compromised_nodes,
180
+ entry_points=entry_points,
175
181
  )
176
182
 
177
183
 
@@ -9,7 +9,7 @@ from maltoolbox.attackgraph import AttackGraphNode
9
9
  from malsim.mal_simulator.attack_surface import get_effects_of_attack_step
10
10
  from malsim.mal_simulator.graph_utils import (
11
11
  node_is_traversable,
12
- node_is_viable,
12
+ node_is_blocked,
13
13
  )
14
14
  from malsim.mal_simulator.state_query import node_ttc_value
15
15
  from malsim.config.sim_settings import TTCMode
@@ -138,12 +138,15 @@ def attacker_step(
138
138
  )
139
139
 
140
140
  if node in agent.settings.entry_points:
141
- # Entrypoints can be compromised as long as they are viable
142
- can_compromise = node_is_viable(sim_state, node)
141
+ # Entrypoints can always be compromised
142
+ # TODO: should this actually be the case?
143
+ can_compromise = True
143
144
  else:
144
- # Otherwise it is limited by action surface and traversability
145
- can_compromise = node in agent.action_surface and node_is_traversable(
146
- sim_state, agent.performed_nodes, node
145
+ # Otherwise attacker is limited by attack surface and traversability
146
+ can_compromise = (
147
+ node in agent.action_surface
148
+ and not node_is_blocked(sim_state, node)
149
+ and node_is_traversable(sim_state, agent.performed_nodes, node)
147
150
  )
148
151
 
149
152
  if can_compromise:
@@ -1,9 +1,10 @@
1
1
  from __future__ import annotations
2
2
  from collections.abc import Set
3
- from dataclasses import dataclass
3
+ from dataclasses import dataclass, field
4
4
  from typing import Any
5
5
  from maltoolbox.attackgraph import AttackGraphNode
6
6
  from malsim.config.agent_settings import DefenderSettings
7
+ from malsim.mal_simulator.event_logger import LogEntry
7
8
  from malsim.mal_simulator.agent_state import AgentState
8
9
 
9
10
 
@@ -18,7 +19,12 @@ class DefenderState(AgentState):
18
19
  # in regards to false positives/negatives and observability
19
20
  observed_nodes: Set[AttackGraphNode]
20
21
 
22
+ # Settings for defender
21
23
  settings: DefenderSettings
24
+
25
+ # Logs generated from detectors
26
+ logs: tuple[LogEntry, ...] = field(default_factory=tuple)
27
+
22
28
  previous_state: DefenderState | None = None
23
29
 
24
30
  # Pickling
@@ -71,10 +77,3 @@ class DefenderState(AgentState):
71
77
  self.previous_state.action_surface if self.previous_state else set()
72
78
  )
73
79
  return previous_action_surface - self.action_surface
74
-
75
- @property
76
- def step_unviable_nodes(self) -> Set[AttackGraphNode]:
77
- previous_unviable_nodes = (
78
- self.previous_state.unviable_nodes if self.previous_state else set()
79
- )
80
- return self.unviable_nodes - previous_unviable_nodes
@@ -9,6 +9,7 @@ from malsim.config.agent_settings import DefenderSettings
9
9
  from malsim.mal_simulator.defender_state import DefenderState
10
10
  from malsim.mal_simulator.defense_surface import get_defense_surface
11
11
  from malsim.mal_simulator.observability import observed_nodes
12
+ from malsim.mal_simulator.event_logger import collect_logs, collect_false_positives
12
13
  from malsim.mal_simulator.simulator_state import MalSimulatorState
13
14
 
14
15
 
@@ -19,7 +20,6 @@ def create_defender_state(
19
20
  defender_settings: DefenderSettings,
20
21
  new_compromised_nodes: Set[AttackGraphNode] = frozenset(),
21
22
  new_enabled_defenses: Set[AttackGraphNode] = frozenset(),
22
- new_unviable_nodes: Set[AttackGraphNode] = frozenset(),
23
23
  previous_state: DefenderState | None = None,
24
24
  ) -> DefenderState:
25
25
  """
@@ -39,7 +39,6 @@ def create_defender_state(
39
39
  performed_nodes_order = (
40
40
  dict(previous_state.performed_nodes_order) if previous_state else {}
41
41
  )
42
- previous_unviable_nodes = previous_state.unviable_nodes if previous_state else set()
43
42
 
44
43
  action_surface = (
45
44
  get_defense_surface(sim_state, defender_settings.actionable_steps)
@@ -59,18 +58,29 @@ def create_defender_state(
59
58
  new_compromised_nodes,
60
59
  )
61
60
 
61
+ logs = collect_logs(
62
+ previous_state.iteration if previous_state else 0,
63
+ new_compromised_nodes,
64
+ previous_compromised_nodes,
65
+ rng,
66
+ ) + collect_false_positives(
67
+ previous_state.iteration if previous_state else 0,
68
+ sim_state.attack_graph.detectors,
69
+ rng,
70
+ )
71
+
62
72
  return DefenderState(
63
73
  name,
64
74
  sim_state=sim_state,
65
75
  settings=defender_settings,
66
76
  performed_nodes=frozenset(previous_enabled_defenses | new_enabled_defenses),
67
- unviable_nodes=frozenset(previous_unviable_nodes | new_unviable_nodes),
68
77
  compromised_nodes=frozenset(previous_compromised_nodes | new_compromised_nodes),
69
78
  observed_nodes=frozenset(previous_observed_nodes | new_observed_nodes),
70
79
  action_surface=frozenset(action_surface),
71
80
  iteration=iteration + 1,
72
81
  performed_nodes_order=performed_nodes_order,
73
82
  previous_state=previous_state,
83
+ logs=tuple(previous_state.logs + logs) if previous_state else tuple(logs),
74
84
  )
75
85
 
76
86
 
@@ -1,5 +1,4 @@
1
1
  from __future__ import annotations
2
- from collections.abc import Set
3
2
  from typing import TYPE_CHECKING
4
3
  from maltoolbox.attackgraph import AttackGraphNode
5
4
  import logging
@@ -7,7 +6,6 @@ import logging
7
6
 
8
7
  from malsim.mal_simulator.agent_states import attacker_states
9
8
  from malsim.mal_simulator.attacker_step import attacker_is_terminated
10
- from malsim.mal_simulator.graph_processing import make_node_unviable
11
9
  from malsim.mal_simulator.simulator_state import MalSimulatorState
12
10
 
13
11
  if TYPE_CHECKING:
@@ -31,7 +29,7 @@ def defender_step(
31
29
  sim_state: MalSimulatorState,
32
30
  agent: DefenderState,
33
31
  nodes: list[AttackGraphNode],
34
- ) -> tuple[list[AttackGraphNode], Set[AttackGraphNode]]:
32
+ ) -> list[AttackGraphNode]:
35
33
  """Enable defense step nodes with defender.
36
34
 
37
35
  Args:
@@ -39,12 +37,10 @@ def defender_step(
39
37
  nodes - the defense step nodes to enable
40
38
 
41
39
  Returns a tuple of a list and a set, `enabled_defenses`
42
- and `attack_steps_made_unviable`.
43
40
  """
44
41
 
45
42
  logger.debug('Stepping with %s', agent.name)
46
43
  enabled_defenses: list[AttackGraphNode] = []
47
- attack_steps_made_unviable: Set[AttackGraphNode] = set()
48
44
 
49
45
  for node in nodes:
50
46
  assert node == sim_state.attack_graph.nodes[node.id], (
@@ -64,19 +60,10 @@ def defender_step(
64
60
  )
65
61
  else:
66
62
  enabled_defenses.append(node)
67
- sim_state.graph_state.viability_per_node, made_unviable = (
68
- make_node_unviable(
69
- node,
70
- # TODO make this immutable
71
- dict(sim_state.graph_state.viability_per_node),
72
- sim_state.graph_state.impossible_attack_steps,
73
- )
74
- )
75
- attack_steps_made_unviable |= made_unviable
76
63
  logger.info(
77
64
  'Defender agent "%s" enabled "%s"',
78
65
  agent.name,
79
66
  node.full_name,
80
67
  )
81
68
 
82
- return enabled_defenses, attack_steps_made_unviable
69
+ return enabled_defenses