mal-simulator 0.2.3__tar.gz → 0.2.5__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 (30) hide show
  1. {mal_simulator-0.2.3/mal_simulator.egg-info → mal_simulator-0.2.5}/PKG-INFO +1 -1
  2. {mal_simulator-0.2.3 → mal_simulator-0.2.5/mal_simulator.egg-info}/PKG-INFO +1 -1
  3. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/mal_simulator.egg-info/SOURCES.txt +1 -0
  4. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/__init__.py +2 -2
  5. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/agents/__init__.py +6 -1
  6. mal_simulator-0.2.5/malsim/agents/heuristic_agent.py +125 -0
  7. mal_simulator-0.2.5/malsim/agents/searchers.py +117 -0
  8. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/envs/gym_envs.py +3 -3
  9. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/envs/malsim_vectorized_obs_env.py +3 -8
  10. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/mal_simulator.py +27 -12
  11. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/scenario.py +5 -1
  12. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/pyproject.toml +1 -1
  13. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/tests/test_mal_simulator.py +7 -6
  14. mal_simulator-0.2.3/malsim/agents/searchers.py +0 -109
  15. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/AUTHORS +0 -0
  16. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/LICENSE +0 -0
  17. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/README.md +0 -0
  18. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/mal_simulator.egg-info/dependency_links.txt +0 -0
  19. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/mal_simulator.egg-info/entry_points.txt +0 -0
  20. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/mal_simulator.egg-info/requires.txt +0 -0
  21. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/mal_simulator.egg-info/top_level.txt +0 -0
  22. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/__main__.py +0 -0
  23. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/agents/decision_agent.py +0 -0
  24. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/agents/keyboard_input.py +0 -0
  25. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/agents/passive_agent.py +0 -0
  26. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/envs/__init__.py +0 -0
  27. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/malsim/envs/base_classes.py +0 -0
  28. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/setup.cfg +0 -0
  29. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/tests/test_main.py +0 -0
  30. {mal_simulator-0.2.3 → mal_simulator-0.2.5}/tests/test_scenario.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: mal-simulator
3
- Version: 0.2.3
3
+ Version: 0.2.5
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>
6
6
  License: Apache Software License
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: mal-simulator
3
- Version: 0.2.3
3
+ Version: 0.2.5
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>
6
6
  License: Apache Software License
@@ -14,6 +14,7 @@ malsim/mal_simulator.py
14
14
  malsim/scenario.py
15
15
  malsim/agents/__init__.py
16
16
  malsim/agents/decision_agent.py
17
+ malsim/agents/heuristic_agent.py
17
18
  malsim/agents/keyboard_input.py
18
19
  malsim/agents/passive_agent.py
19
20
  malsim/agents/searchers.py
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # MAL Simulator v0.2.3
2
+ # MAL Simulator v0.2.5
3
3
  # Copyright 2025, Andrei Buhaiu.
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,7 +24,7 @@ MAL Simulator
24
24
  """
25
25
 
26
26
  __title__ = "malsim"
27
- __version__ = "0.2.3"
27
+ __version__ = "0.2.5"
28
28
  __authors__ = ["Andrei Buhaiu", "Joakim Loxdal", "Jakob Nyberg", "Nikolaos Kakouros"]
29
29
  __license__ = "Apache 2.0"
30
30
  __docformat__ = "restructuredtext en"
@@ -2,11 +2,16 @@ from .decision_agent import DecisionAgent
2
2
  from .passive_agent import PassiveAgent
3
3
  from .keyboard_input import KeyboardAgent
4
4
  from .searchers import BreadthFirstAttacker, DepthFirstAttacker
5
+ from .heuristic_agent import (
6
+ DefendCompromisedDefender, DefendFutureCompromisedDefender
7
+ )
5
8
 
6
9
  __all__ = [
7
10
  'PassiveAgent',
8
11
  'DecisionAgent',
9
12
  'KeyboardAgent',
10
13
  'BreadthFirstAttacker',
11
- 'DepthFirstAttacker'
14
+ 'DepthFirstAttacker',
15
+ 'DefendCompromisedDefender',
16
+ 'DefendFutureCompromisedDefender'
12
17
  ]
@@ -0,0 +1,125 @@
1
+ from __future__ import annotations
2
+ from typing import Optional, TYPE_CHECKING
3
+ import logging
4
+ import math
5
+
6
+ import numpy as np
7
+
8
+ from .decision_agent import DecisionAgent
9
+
10
+ if TYPE_CHECKING:
11
+ from maltoolbox.attackgraph import AttackGraphNode
12
+ from ..mal_simulator import MalSimAgentStateView
13
+
14
+ logger = logging.getLogger(__name__)
15
+
16
+ class DefendCompromisedDefender(DecisionAgent):
17
+ """A defender that defends compromised assets using notPresent"""
18
+
19
+ def __init__(self, agent_config, **_):
20
+ # Seed and rng not currently used
21
+ seed = (
22
+ agent_config["seed"]
23
+ if agent_config.get("seed")
24
+ else np.random.SeedSequence().entropy
25
+ )
26
+ self.rng = (
27
+ np.random.default_rng(seed)
28
+ if agent_config.get("randomize")
29
+ else None
30
+ )
31
+
32
+ def get_next_action(
33
+ self, agent_state: MalSimAgentStateView, **kwargs
34
+ ) -> Optional[AttackGraphNode]:
35
+
36
+ """Return an action that disables a compromised node"""
37
+
38
+ selected_node_cost = math.inf
39
+ selected_node = None
40
+
41
+ # To make it deterministic
42
+ possible_choices = list(agent_state.action_surface)
43
+ possible_choices.sort(key=lambda n: n.id)
44
+
45
+ for node in possible_choices:
46
+
47
+ if node.is_enabled_defense():
48
+ continue
49
+
50
+ node_cost = node.extras.get('reward', 0)
51
+
52
+ # Strategy:
53
+ # - Enabled the cheapest defense node
54
+ # that has compromised child nodes
55
+ if node_cost < selected_node_cost:
56
+
57
+ node_has_compromised_child = (
58
+ any(
59
+ child_node.is_compromised()
60
+ for child_node in node.children
61
+ )
62
+ )
63
+
64
+ if node_has_compromised_child:
65
+ selected_node = node
66
+ selected_node_cost = node_cost
67
+
68
+ return selected_node
69
+
70
+
71
+ class DefendFutureCompromisedDefender(DecisionAgent):
72
+ """A defender that defends compromised assets using notPresent"""
73
+
74
+ def __init__(self, agent_config, **_):
75
+ # Seed and rng not currently used
76
+ seed = (
77
+ agent_config["seed"]
78
+ if agent_config.get("seed")
79
+ else np.random.SeedSequence().entropy
80
+ )
81
+ self.rng = (
82
+ np.random.default_rng(seed)
83
+ if agent_config.get("randomize")
84
+ else None
85
+ )
86
+
87
+ def get_next_action(
88
+ self, agent_state: MalSimAgentStateView, **kwargs
89
+ ) -> Optional[AttackGraphNode]:
90
+
91
+ """Return an action that disables a compromised node"""
92
+
93
+ selected_node_cost = math.inf
94
+ selected_node = None
95
+
96
+ # To make it deterministic
97
+ possible_choices = list(agent_state.action_surface)
98
+ possible_choices.sort(key=lambda n: n.id)
99
+
100
+ for node in possible_choices:
101
+
102
+ if node.is_enabled_defense():
103
+ continue
104
+
105
+ node_cost = node.extras.get('reward', 0)
106
+
107
+ # Strategy:
108
+ # - Enabled the cheapest defense node
109
+ # that has a non compromised child
110
+ # that has a compromised parent.
111
+ if node_cost < selected_node_cost:
112
+
113
+ node_has_child_that_can_be_compromised = (
114
+ any(
115
+ any(p.is_compromised() for p in child_node.parents)
116
+ and not child_node.is_compromised()
117
+ for child_node in node.children
118
+ )
119
+ )
120
+
121
+ if node_has_child_that_can_be_compromised:
122
+ selected_node = node
123
+ selected_node_cost = node_cost
124
+
125
+ return selected_node
@@ -0,0 +1,117 @@
1
+ from __future__ import annotations
2
+ import logging
3
+ import random
4
+
5
+ from collections import deque
6
+ from typing import Optional, TYPE_CHECKING, Any
7
+
8
+ from .decision_agent import DecisionAgent
9
+ from ..mal_simulator import MalSimAgentStateView
10
+
11
+ if TYPE_CHECKING:
12
+ from maltoolbox.attackgraph import AttackGraphNode
13
+
14
+ logger = logging.getLogger(__name__)
15
+
16
+
17
+ class BreadthFirstAttacker(DecisionAgent):
18
+ """A Breadth-First agent, with possible randomization at each level."""
19
+
20
+ # A human-friendly name for the agent.
21
+ name = 'Breadth First Attacker'
22
+
23
+ # Controls where newly discovered steps will be appended to the deque of
24
+ # available actions. Differentiates between BFS and DFS agents.
25
+ _extend_method = 'extendleft'
26
+
27
+ _default_settings: dict[str, Any] = {
28
+ # Whether to randomize next target selection, still respecting the
29
+ # policy of the agent (BFS or DFS).
30
+ 'randomize': False,
31
+ # The random seed to initialize the randomness engine with.
32
+ # If set, the simulation will be deterministic.
33
+ 'seed': None,
34
+ }
35
+
36
+ def __init__(self, agent_config: dict) -> None:
37
+ """Initialize a BFS/DFS agent.
38
+
39
+ Args:
40
+ agent_config: Dict with settings to override defaults
41
+ """
42
+ self._targets: deque[AttackGraphNode] = deque()
43
+ self._current_target: Optional[AttackGraphNode] = None
44
+ self._settings = self._default_settings | agent_config
45
+
46
+ self._rng = random.Random(self._settings.get('seed'))
47
+ self._started = False
48
+
49
+ def get_next_action(
50
+ self, agent_state: MalSimAgentStateView, **kwargs
51
+ ) -> Optional[AttackGraphNode]:
52
+ """Receive the next action according to agent policy (bfs/dfs)"""
53
+
54
+ self._update_targets(
55
+ new_nodes=(
56
+ agent_state.step_action_surface_additions
57
+ if self._started else agent_state.action_surface
58
+ ),
59
+ disabled_nodes=(
60
+ agent_state.step_action_surface_removals
61
+ )
62
+ )
63
+
64
+ self._select_next_target()
65
+ self._started = True
66
+ return self._current_target
67
+
68
+ def _update_targets(
69
+ self,
70
+ new_nodes: set[AttackGraphNode],
71
+ disabled_nodes: set[AttackGraphNode],
72
+ ):
73
+ new_targets: list[AttackGraphNode] = []
74
+ if self._settings['seed']:
75
+ # If a seed is set, we assume the user wants determinism in the
76
+ # simulation. Thus, we sort to an ordered list to make sure the
77
+ # non-deterministic ordering of the action_surface set does not
78
+ # break simulation determinism.
79
+ new_targets = sorted(new_nodes, key=lambda n: n.id)
80
+ else:
81
+ # sorted above returns a list already
82
+ new_targets = list(new_nodes)
83
+
84
+ if self._settings['randomize']:
85
+ self._rng.shuffle(new_targets)
86
+
87
+ if (
88
+ self._current_target
89
+ and not self._current_target.is_compromised()
90
+ and self._current_target not in disabled_nodes
91
+ ):
92
+ # If self.current_target was not compromised, e.g. due to TTCs,
93
+ # it remains in action surface and should be added as a target.
94
+ self._targets.append(self._current_target)
95
+
96
+ # Enabled defenses may remove previously possible attack steps.
97
+ if disabled_nodes:
98
+ self._targets = deque(
99
+ n for n in self._targets if n not in disabled_nodes
100
+ )
101
+
102
+ # Use the extend method to add new targets
103
+ getattr(self._targets, self._extend_method)(new_targets)
104
+
105
+ def _select_next_target(self) -> None:
106
+ """
107
+ Implement the actual next target selection logic.
108
+ """
109
+ if self._targets:
110
+ self._current_target = self._targets.pop()
111
+ else:
112
+ self._current_target = None
113
+
114
+
115
+ class DepthFirstAttacker(BreadthFirstAttacker):
116
+ name = 'Depth First Attacker'
117
+ _extend_method = 'extend'
@@ -120,9 +120,9 @@ class DefenderEnv(gym.Env):
120
120
  """Register attackers in simulator"""
121
121
  for agent_info in agents:
122
122
  if agent_info['type'] == AgentType.ATTACKER:
123
- agent_name = agent_info['name']
124
- attacker_id = agent_info['attacker_id']
125
- self.sim.register_attacker(agent_name, attacker_id)
123
+ self.sim.register_attacker(
124
+ agent_info['name'],
125
+ agent_info['attacker_id'])
126
126
 
127
127
  def _create_attacker_decision_agents(
128
128
  self, agents: list[dict], seed=None
@@ -387,8 +387,6 @@ class MalSimVectorizedObsEnv(ParallelEnv, MalSimEnv):
387
387
  self._agent_observations = {}
388
388
  self._agent_infos = {}
389
389
 
390
- self.reset()
391
-
392
390
  @property
393
391
  def agents(self):
394
392
  """Required by ParallelEnv"""
@@ -397,7 +395,7 @@ class MalSimVectorizedObsEnv(ParallelEnv, MalSimEnv):
397
395
  @property
398
396
  def possible_agents(self):
399
397
  """Required by ParallelEnv"""
400
- return list(self.sim._agents)
398
+ return list(self.sim._agent_states.keys())
401
399
 
402
400
  def _create_blank_observation(self, default_obs_state=-1):
403
401
  """Create the initial observation"""
@@ -537,8 +535,7 @@ class MalSimVectorizedObsEnv(ParallelEnv, MalSimEnv):
537
535
 
538
536
  if agent.type == AgentType.ATTACKER:
539
537
  # Attacker can only act on nodes that are not compromised
540
- attacker = \
541
- self.sim.attack_graph.attackers[agent.attacker_id]
538
+ attacker = agent.attacker
542
539
  if not node.is_compromised_by(attacker):
543
540
  index = self._id_to_index[node.id]
544
541
  available_actions[index] = 1
@@ -798,9 +795,7 @@ class MalSimVectorizedObsEnv(ParallelEnv, MalSimEnv):
798
795
  if child_obs == -1:
799
796
  agent_observation['observed_state'][child_index] = 0
800
797
 
801
- attacker = (
802
- self.sim.attack_graph.attackers[attacker_agent.attacker_id]
803
- )
798
+ attacker = attacker_agent.attacker
804
799
  attacker_observation = self._agent_observations[attacker_agent.name]
805
800
 
806
801
  for node in compromised_nodes:
@@ -9,7 +9,8 @@ from typing import Any, Optional
9
9
 
10
10
  from maltoolbox import neo4j_configs
11
11
  from maltoolbox.ingestors import neo4j
12
- from maltoolbox.attackgraph import AttackGraph, AttackGraphNode, query
12
+ from maltoolbox.attackgraph import (AttackGraph, AttackGraphNode,
13
+ Attacker, query)
13
14
  from maltoolbox.attackgraph.analyzers import apriori
14
15
 
15
16
  ITERATIONS_LIMIT = int(1e9)
@@ -34,12 +35,15 @@ class MalSimAgentState:
34
35
  # Attackers get positive rewards, defenders negative
35
36
  reward: int = 0
36
37
 
37
- # Contains the steps performed successfully in the last step
38
- step_performed_nodes: set[AttackGraphNode] = field(default_factory=set)
39
-
40
38
  # Contains possible actions for the agent in the next step
41
39
  action_surface: set[AttackGraphNode] = field(default_factory=set)
42
40
 
41
+ # Contains all nodes that this agent has performed successfully
42
+ performed_nodes: set[AttackGraphNode] = field(default_factory=set)
43
+
44
+ # Contains the steps performed successfully in the last step
45
+ step_performed_nodes: set[AttackGraphNode] = field(default_factory=set)
46
+
43
47
  # Contains possible actions that became available in the last step
44
48
  step_action_surface_additions: set[AttackGraphNode] = (
45
49
  field(default_factory = set))
@@ -60,9 +64,9 @@ class MalSimAgentState:
60
64
  class MalSimAttackerState(MalSimAgentState):
61
65
  """Stores the state of an attacker in the simulator"""
62
66
 
63
- def __init__(self, name: str, attacker_id: int):
67
+ def __init__(self, name: str, attacker: Attacker):
64
68
  super().__init__(name, AgentType.ATTACKER)
65
- self.attacker_id = attacker_id
69
+ self.attacker = attacker
66
70
 
67
71
 
68
72
  class MalSimDefenderState(MalSimAgentState):
@@ -222,7 +226,7 @@ class MalSimulator():
222
226
  """Give rewards for pre-enabled attack/defense steps"""
223
227
 
224
228
  for agent in self._get_attacker_agents():
225
- attacker = self.attack_graph.attackers[agent.attacker_id]
229
+ attacker = agent.attacker
226
230
  agent.reward = sum(
227
231
  n.extras.get("reward", 0) for n in attacker.reached_attack_steps
228
232
  )
@@ -241,7 +245,7 @@ class MalSimulator():
241
245
  for agent in self._agent_states.values():
242
246
 
243
247
  if isinstance(agent, MalSimAttackerState):
244
- attacker = self.attack_graph.attackers[agent.attacker_id]
248
+ attacker = agent.attacker
245
249
  agent.action_surface = query.calculate_attack_surface(
246
250
  attacker, skip_compromised = True
247
251
  )
@@ -261,7 +265,10 @@ class MalSimulator():
261
265
  for agent_state in self._get_attacker_agents():
262
266
  # Create a new agent state for the attacker
263
267
  self._agent_states[agent_state.name] = (
264
- MalSimAttackerState(agent_state.name, agent_state.attacker_id)
268
+ MalSimAttackerState(
269
+ agent_state.name,
270
+ self.attack_graph.attackers[agent_state.attacker.id]
271
+ )
265
272
  )
266
273
 
267
274
  for agent_state in self._get_defender_agents():
@@ -278,7 +285,13 @@ class MalSimulator():
278
285
  assert name not in self._agent_states, \
279
286
  f"Duplicate agent named {name} not allowed"
280
287
 
281
- agent_state = MalSimAttackerState(name, attacker_id)
288
+ attacker = self.attack_graph.attackers.get(attacker_id, None)
289
+ if attacker is None:
290
+ msg = ('Failed to register Attacker agent because no attacker '
291
+ f'with id {attacker_id} was found in the attack ' 'graph!')
292
+ logger.error(msg)
293
+ raise ValueError(msg)
294
+ agent_state = MalSimAttackerState(name, attacker)
282
295
  self._agent_states[name] = agent_state
283
296
  self._alive_agents.add(name)
284
297
 
@@ -325,7 +338,7 @@ class MalSimulator():
325
338
  - Uncompromise the node and remove rewards for it.
326
339
  """
327
340
  for attacker_agent in self._get_attacker_agents():
328
- attacker = self.attack_graph.attackers[attacker_agent.attacker_id]
341
+ attacker = attacker_agent.attacker
329
342
 
330
343
  for unviable_node in attack_steps_to_uncompromise:
331
344
  if unviable_node.is_compromised_by(attacker):
@@ -352,7 +365,7 @@ class MalSimulator():
352
365
  """
353
366
 
354
367
  compromised_nodes = set()
355
- attacker = self.attack_graph.attackers[agent.attacker_id]
368
+ attacker = agent.attacker
356
369
 
357
370
  for node in nodes:
358
371
  assert node == self.attack_graph.nodes[node.id], (
@@ -370,6 +383,7 @@ class MalSimulator():
370
383
  if query.is_node_traversable_by_attacker(node, attacker) \
371
384
  and node in agent.action_surface:
372
385
  attacker.compromise(node)
386
+ agent.performed_nodes.add(node)
373
387
  agent.reward += node.extras.get('reward', 0)
374
388
  compromised_nodes.add(node)
375
389
 
@@ -446,6 +460,7 @@ class MalSimulator():
446
460
  apriori.propagate_viability_from_unviable_node(node)
447
461
  agent.reward -= node.extras.get("reward", 0)
448
462
  enabled_defenses.add(node)
463
+ agent.performed_nodes.add(node)
449
464
  logger.info(
450
465
  'Defender agent "%s" enabled "%s"(%d).',
451
466
  agent.name, node.full_name, node.id
@@ -22,7 +22,9 @@ from .agents import (
22
22
  BreadthFirstAttacker,
23
23
  DepthFirstAttacker,
24
24
  KeyboardAgent,
25
- PassiveAgent
25
+ PassiveAgent,
26
+ DefendCompromisedDefender,
27
+ DefendFutureCompromisedDefender
26
28
  )
27
29
 
28
30
  from .mal_simulator import AgentType, MalSimulator
@@ -32,6 +34,8 @@ agent_class_name_to_class = {
32
34
  'BreadthFirstAttacker': BreadthFirstAttacker,
33
35
  'KeyboardAgent': KeyboardAgent,
34
36
  'PassiveAgent': PassiveAgent,
37
+ 'DefendCompromisedDefender': DefendCompromisedDefender,
38
+ 'DefendFutureCompromisedDefender': DefendFutureCompromisedDefender
35
39
  }
36
40
 
37
41
  deprecated_fields = [
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "mal-simulator"
3
- version = "0.2.3"
3
+ version = "0.2.5"
4
4
  authors = [
5
5
  { name="Andrei Buhaiu", email="buhaiu@kth.se" },
6
6
  { name="Joakim Loxdal", email="loxdal@kth.se" },
@@ -54,11 +54,11 @@ def test_reset(corelang_lang_graph, model):
54
54
 
55
55
  def test_register_agent_attacker(corelang_lang_graph, model):
56
56
  attack_graph = AttackGraph(corelang_lang_graph, model)
57
+ attack_graph.attach_attackers()
57
58
  sim = MalSimulator(attack_graph)
58
59
 
59
- attacker = 1
60
60
  agent_name = "attacker1"
61
- sim.register_attacker(agent_name, attacker)
61
+ sim.register_attacker(agent_name, 0)
62
62
 
63
63
  assert agent_name in sim.agent_states
64
64
  assert agent_name in sim.agent_states
@@ -96,9 +96,8 @@ def test_simulator_initialize_agents(corelang_lang_graph, model):
96
96
 
97
97
  # Register the agents
98
98
  attacker_name = "attacker"
99
- attacker_id = 1
100
99
  defender_name = "defender"
101
- sim.register_attacker(attacker_name, attacker_id)
100
+ sim.register_attacker(attacker_name, 1)
102
101
  sim.register_defender(defender_name)
103
102
 
104
103
  sim.reset()
@@ -130,7 +129,8 @@ def test_attacker_step(corelang_lang_graph, model):
130
129
  attack_graph.add_attacker(attacker, attacker.id)
131
130
  sim = MalSimulator(attack_graph)
132
131
 
133
- sim.register_attacker(attacker.name, attacker.id)
132
+ sim.register_attacker(attacker.name,
133
+ attacker.id)
134
134
  sim.reset()
135
135
  attacker_agent = sim._agent_states[attacker.name]
136
136
 
@@ -189,7 +189,8 @@ def test_agent_state_views_simple(corelang_lang_graph, model):
189
189
  sim = MalSimulator(attack_graph)
190
190
  attacker_name = 'attacker'
191
191
  defender_name = 'defender'
192
- sim.register_attacker(attacker_name, attacker.id)
192
+ sim.register_attacker(attacker_name,
193
+ attacker.id)
193
194
  sim.register_defender(defender_name)
194
195
 
195
196
  # Evaluate the agent state views after reset
@@ -1,109 +0,0 @@
1
- from __future__ import annotations
2
- import logging
3
- import random
4
- import re
5
-
6
- from collections import deque
7
- from typing import Optional, TYPE_CHECKING, Any
8
-
9
- from .decision_agent import DecisionAgent
10
- from ..mal_simulator import MalSimAgentStateView
11
-
12
- if TYPE_CHECKING:
13
- from maltoolbox.attackgraph import AttackGraphNode
14
-
15
- logger = logging.getLogger(__name__)
16
-
17
-
18
- class BreadthFirstAttacker(DecisionAgent):
19
- """A Breadth-First agent, with possible randomization at each level."""
20
-
21
- _extend_method = 'extendleft'
22
- # Controls where newly discovered steps will be appended to the list of
23
- # available actions. Currently used to differentiate between BFS and DFS
24
- # agents.
25
-
26
- name = ' '.join(re.findall(r'[A-Z][^A-Z]*', __qualname__))
27
- # A human-friendly name for the agent.
28
-
29
- default_settings: dict[str, Any] = {
30
- 'randomize': False,
31
- # Whether to randomize next target selection, still respecting the
32
- # policy of the agent (e.g. BFS or DFS).
33
- 'seed': None,
34
- # The random seed to initialize the randomness engine with. If set, the
35
- # simulation will be deterministic.
36
- }
37
-
38
- def __init__(self, agent_config: dict) -> None:
39
- """Initialize a BFS agent.
40
-
41
- Args:
42
- agent_config: Dict with settings to override defaults
43
- """
44
- self.targets: deque[AttackGraphNode] = deque()
45
- self.current_target: Optional[AttackGraphNode] = None
46
-
47
- self.settings = self.default_settings | agent_config
48
-
49
- self.rng = random.Random(self.settings.get('seed'))
50
- self.started = False
51
-
52
- def get_next_action(
53
- self, agent_state: MalSimAgentStateView, **kwargs
54
- ) -> Optional[AttackGraphNode]:
55
- self._update_targets(
56
- agent_state.step_action_surface_additions
57
- if self.started
58
- else agent_state.action_surface,
59
- agent_state.step_action_surface_removals,
60
- **kwargs,
61
- )
62
- self._select_next_target(**kwargs)
63
-
64
- self.started = True
65
- return self.current_target
66
-
67
- def _update_targets(
68
- self,
69
- new_targets_set: set[AttackGraphNode],
70
- disabled_nodes_set: set[AttackGraphNode],
71
- **kwargs: Any,
72
- ):
73
- if self.settings['seed']:
74
- # If a seed is set, we assume the user wants determinism in the
75
- # simulation. Thus, we sort to an ordered list to make sure the
76
- # non-deterministic ordering of the action_surface set does not
77
- # break simulation determinism.
78
- new_targets = sorted(new_targets_set, key=lambda n: n.id)
79
- else:
80
- # sorted above returns a list already
81
- new_targets = list(new_targets_set)
82
-
83
- if self.settings['randomize']:
84
- self.rng.shuffle(new_targets)
85
-
86
- if self.current_target in new_targets:
87
- # If self.current_target is not yet compromised, e.g. due to TTCs,
88
- # keep using that as the target.
89
- new_targets.remove(self.current_target)
90
- new_targets.append(self.current_target)
91
-
92
- # Enabled defenses may remove previously possible attack steps.
93
- if disabled_nodes_set:
94
- self.targets = deque(n for n in self.targets if n.is_viable)
95
-
96
- getattr(self.targets, self._extend_method)(new_targets)
97
-
98
- def _select_next_target(self, **kwargs: Any) -> None:
99
- """
100
- Implement the actual next target selection logic.
101
- """
102
- try:
103
- self.current_target = self.targets.pop()
104
- except IndexError:
105
- self.current_target = None
106
-
107
-
108
- class DepthFirstAttacker(BreadthFirstAttacker):
109
- _extend_method = 'extend'
File without changes
File without changes
File without changes
File without changes