mal-simulator 3.2.1__tar.gz → 3.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 (88) hide show
  1. {mal_simulator-3.2.1/mal_simulator.egg-info → mal_simulator-3.3.0}/PKG-INFO +2 -2
  2. {mal_simulator-3.2.1 → mal_simulator-3.3.0/mal_simulator.egg-info}/PKG-INFO +2 -2
  3. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/mal_simulator.egg-info/SOURCES.txt +10 -0
  4. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/mal_simulator.egg-info/requires.txt +1 -1
  5. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/__init__.py +2 -2
  6. mal_simulator-3.3.0/malsim/dyna_mal_simulator/__init__.py +5 -0
  7. mal_simulator-3.3.0/malsim/dyna_mal_simulator/attacker_step.py +138 -0
  8. mal_simulator-3.3.0/malsim/dyna_mal_simulator/defender_step.py +61 -0
  9. mal_simulator-3.3.0/malsim/dyna_mal_simulator/graph_state.py +58 -0
  10. mal_simulator-3.3.0/malsim/dyna_mal_simulator/model_effects.py +346 -0
  11. mal_simulator-3.3.0/malsim/dyna_mal_simulator/model_state.py +90 -0
  12. mal_simulator-3.3.0/malsim/dyna_mal_simulator/process_assoc_traversal.py +318 -0
  13. mal_simulator-3.3.0/malsim/dyna_mal_simulator/simulator.py +388 -0
  14. mal_simulator-3.3.0/malsim/dyna_mal_simulator/simulator_state.py +60 -0
  15. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/attackers/ttc_soft_min.py +1 -1
  16. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/pyproject.toml +3 -2
  17. mal_simulator-3.3.0/tests/test_dyna_mal_simulator.py +1048 -0
  18. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/AUTHORS +0 -0
  19. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/LICENSE +0 -0
  20. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/README.md +0 -0
  21. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/mal_simulator.egg-info/dependency_links.txt +0 -0
  22. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/mal_simulator.egg-info/entry_points.txt +0 -0
  23. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/mal_simulator.egg-info/top_level.txt +0 -0
  24. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/__main__.py +0 -0
  25. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/config/__init__.py +0 -0
  26. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/config/agent_settings.py +0 -0
  27. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/config/agent_settings_factories.py +0 -0
  28. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/config/node_property_rule.py +0 -0
  29. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/config/sim_settings.py +0 -0
  30. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/envs/__init__.py +0 -0
  31. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/envs/base_classes.py +0 -0
  32. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/envs/graph/__init__.py +0 -0
  33. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/envs/graph/graph_env.py +0 -0
  34. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/envs/graph/mal_spaces.py +0 -0
  35. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/envs/graph/serialization.py +0 -0
  36. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/envs/graph/utils.py +0 -0
  37. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/envs/graph/wrapper.py +0 -0
  38. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/__init__.py +0 -0
  39. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/agent_state.py +0 -0
  40. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/agent_states.py +0 -0
  41. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/attack_surface.py +0 -0
  42. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/attacker_state.py +0 -0
  43. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/attacker_state_factories.py +0 -0
  44. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/attacker_step.py +0 -0
  45. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/defender_state.py +0 -0
  46. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/defender_state_factories.py +0 -0
  47. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/defender_step.py +0 -0
  48. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/defense_surface.py +0 -0
  49. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/event_logger.py +0 -0
  50. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/false_alerts.py +0 -0
  51. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/graph_processing.py +0 -0
  52. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/graph_state.py +0 -0
  53. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/graph_utils.py +0 -0
  54. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/node_getters.py +0 -0
  55. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/observability.py +0 -0
  56. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/reset_agent.py +0 -0
  57. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/rewards.py +0 -0
  58. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/run_simulation.py +0 -0
  59. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/simulator.py +0 -0
  60. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/simulator_state.py +0 -0
  61. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/simulator_static_data.py +0 -0
  62. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/state_query.py +0 -0
  63. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/mal_simulator/ttc_utils.py +0 -0
  64. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/__init__.py +0 -0
  65. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/attackers/__init__.py +0 -0
  66. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/attackers/searchers.py +0 -0
  67. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/attackers/shortest_path.py +0 -0
  68. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/decision_agent.py +0 -0
  69. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/defenders/__init__.py +0 -0
  70. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/defenders/heuristic_agent.py +0 -0
  71. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/keyboard_input.py +0 -0
  72. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/passive_agent.py +0 -0
  73. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/random_agent.py +0 -0
  74. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/policies/utils/path_finding.py +0 -0
  75. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/scenario/__init__.py +0 -0
  76. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/scenario/scenario.py +0 -0
  77. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/types.py +0 -0
  78. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/malsim/visualization/malsim_gui_client.py +0 -0
  79. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/setup.cfg +0 -0
  80. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/tests/test_attacker.py +0 -0
  81. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/tests/test_attacker_settings.py +0 -0
  82. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/tests/test_defender.py +0 -0
  83. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/tests/test_event_logger.py +0 -0
  84. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/tests/test_graph_processing.py +0 -0
  85. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/tests/test_main.py +0 -0
  86. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/tests/test_mal_simulator.py +0 -0
  87. {mal_simulator-3.2.1 → mal_simulator-3.3.0}/tests/test_scenario.py +0 -0
  88. {mal_simulator-3.2.1 → mal_simulator-3.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: 3.2.1
3
+ Version: 3.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
@@ -18,7 +18,7 @@ Requires-Python: >=3.10
18
18
  Description-Content-Type: text/markdown
19
19
  License-File: LICENSE
20
20
  License-File: AUTHORS
21
- Requires-Dist: mal-toolbox<3.0.0,>=2.0.0
21
+ Requires-Dist: mal-toolbox>=2.11.0
22
22
  Requires-Dist: PyYAML>=6.0.1
23
23
  Requires-Dist: numpy>=1.21.4
24
24
  Requires-Dist: scipy
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mal-simulator
3
- Version: 3.2.1
3
+ Version: 3.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
@@ -18,7 +18,7 @@ Requires-Python: >=3.10
18
18
  Description-Content-Type: text/markdown
19
19
  License-File: LICENSE
20
20
  License-File: AUTHORS
21
- Requires-Dist: mal-toolbox<3.0.0,>=2.0.0
21
+ Requires-Dist: mal-toolbox>=2.11.0
22
22
  Requires-Dist: PyYAML>=6.0.1
23
23
  Requires-Dist: numpy>=1.21.4
24
24
  Requires-Dist: scipy
@@ -16,6 +16,15 @@ malsim/config/agent_settings.py
16
16
  malsim/config/agent_settings_factories.py
17
17
  malsim/config/node_property_rule.py
18
18
  malsim/config/sim_settings.py
19
+ malsim/dyna_mal_simulator/__init__.py
20
+ malsim/dyna_mal_simulator/attacker_step.py
21
+ malsim/dyna_mal_simulator/defender_step.py
22
+ malsim/dyna_mal_simulator/graph_state.py
23
+ malsim/dyna_mal_simulator/model_effects.py
24
+ malsim/dyna_mal_simulator/model_state.py
25
+ malsim/dyna_mal_simulator/process_assoc_traversal.py
26
+ malsim/dyna_mal_simulator/simulator.py
27
+ malsim/dyna_mal_simulator/simulator_state.py
19
28
  malsim/envs/__init__.py
20
29
  malsim/envs/base_classes.py
21
30
  malsim/envs/graph/__init__.py
@@ -68,6 +77,7 @@ malsim/visualization/malsim_gui_client.py
68
77
  tests/test_attacker.py
69
78
  tests/test_attacker_settings.py
70
79
  tests/test_defender.py
80
+ tests/test_dyna_mal_simulator.py
71
81
  tests/test_event_logger.py
72
82
  tests/test_graph_processing.py
73
83
  tests/test_main.py
@@ -1,4 +1,4 @@
1
- mal-toolbox<3.0.0,>=2.0.0
1
+ mal-toolbox>=2.11.0
2
2
  PyYAML>=6.0.1
3
3
  numpy>=1.21.4
4
4
  scipy
@@ -1,4 +1,4 @@
1
- # MAL Simulator v3.2.1
1
+ # MAL Simulator v3.3.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__ = '3.2.1'
29
+ __version__ = '3.3.0'
30
30
  __authors__ = [
31
31
  'Andrei Buhaiu',
32
32
  'Joakim Loxdal',
@@ -0,0 +1,5 @@
1
+ """DynaMALSimulator related modules."""
2
+
3
+ from .simulator import DynaMalSimulator
4
+
5
+ __all__ = ['DynaMalSimulator']
@@ -0,0 +1,138 @@
1
+ from __future__ import annotations
2
+ from typing import TYPE_CHECKING
3
+ import logging
4
+
5
+ import numpy as np
6
+
7
+ from maltoolbox.attackgraph import AttackGraphNode
8
+
9
+ from malsim.config.sim_settings import TTCMode
10
+ from malsim.dyna_mal_simulator.model_effects import execute_model_effects
11
+ from malsim.dyna_mal_simulator.simulator_state import DynaMalSimulatorState
12
+ from malsim.mal_simulator.graph_utils import (
13
+ node_is_traversable,
14
+ )
15
+ from malsim.mal_simulator.attacker_step import (
16
+ attacker_step_effects,
17
+ )
18
+ from malsim.mal_simulator.state_query import node_ttc_value
19
+ from malsim.mal_simulator.ttc_utils import TTCDist
20
+
21
+ if TYPE_CHECKING:
22
+ from malsim.mal_simulator.attacker_state import AttackerState
23
+
24
+ logger = logging.getLogger(__name__)
25
+
26
+
27
+ def dyna_attacker_step(
28
+ sim_state: DynaMalSimulatorState,
29
+ agent: AttackerState,
30
+ nodes: list[AttackGraphNode],
31
+ rng: np.random.Generator,
32
+ ) -> tuple[list[AttackGraphNode], list[AttackGraphNode], DynaMalSimulatorState]:
33
+ """Compromise attack step nodes with attacker
34
+
35
+ Args:
36
+ agent - the agent to compromise nodes with
37
+ nodes - the nodes to compromise
38
+
39
+ Returns: two lists with compromised, attempted nodes
40
+ """
41
+
42
+ logger.debug('Stepping with agent %s', agent.name)
43
+ successful_compromises: list[AttackGraphNode] = []
44
+ attempted_compromises: list[AttackGraphNode] = []
45
+
46
+ for node in nodes:
47
+ assert node == sim_state.attack_graph.nodes[node.id], (
48
+ f'{agent.name} tried to enable a node that is not part '
49
+ 'of this simulators attack_graph. Make sure the node '
50
+ 'comes from the agents action surface.'
51
+ )
52
+
53
+ if node in agent.settings.entry_points:
54
+ # Entrypoints can always be compromised
55
+ # TODO: should this actually be the case?
56
+ can_compromise = True
57
+ else:
58
+ # Otherwise attacker is limited by attack surface and traversability
59
+ can_compromise = node in agent.action_surface and node_is_traversable(
60
+ sim_state, agent.performed_nodes, node
61
+ )
62
+
63
+ if can_compromise:
64
+ if dyna_attempt_attacker_step(
65
+ sim_state, rng, sim_state.settings.ttc_mode, agent, node
66
+ ):
67
+ successful_compromises.append(node)
68
+ logger.info(
69
+ 'Attacker agent "%s" compromised "%s"', agent.name, node.full_name
70
+ )
71
+ # Run effects as a compromise of performing `node`
72
+ sim_state = execute_model_effects(sim_state, node, rng)
73
+ step_effects = attacker_step_effects(sim_state, agent, node)
74
+ for effect_node in step_effects:
75
+ sim_state = execute_model_effects(sim_state, effect_node, rng)
76
+ successful_compromises += step_effects
77
+ else:
78
+ logger.info(
79
+ 'Attacker agent "%s" attempted "%s" (attempt %d).',
80
+ agent.name,
81
+ node.full_name,
82
+ agent.num_attempts[node],
83
+ )
84
+ attempted_compromises.append(node)
85
+
86
+ else:
87
+ logger.warning('Attacker could not compromise %s', node.full_name)
88
+
89
+ return successful_compromises, attempted_compromises, sim_state
90
+
91
+
92
+ def dyna_attempt_attacker_step(
93
+ sim_state: DynaMalSimulatorState,
94
+ rng: np.random.Generator,
95
+ ttc_mode: TTCMode,
96
+ agent: AttackerState,
97
+ node: AttackGraphNode,
98
+ ) -> bool:
99
+ """Attempt a step with a TTC distribution.
100
+
101
+ Return True if the attempt was successful.
102
+ """
103
+
104
+ # # New nodes may be created during the simulation,
105
+ # # so we need to check if the node is in the num_attempts dict
106
+ # if node not in agent.num_attempts:
107
+ # num_attempts = 0
108
+ # else:
109
+ # num_attempts = agent.num_attempts[node] + 1
110
+ num_attempts = agent.num_attempts.get(node, 0) + 1
111
+
112
+ if agent.settings.ttc_dists and node in agent.settings.ttc_dists:
113
+ # If this agent has custom ttc distribution set for this node, use it
114
+ ttc_dist = agent.settings.ttc_dists[node]
115
+ else:
116
+ ttc_dist = TTCDist.from_node(node)
117
+
118
+ if ttc_mode == TTCMode.DISABLED:
119
+ # Always suceed if disabled TTCs
120
+ return True
121
+
122
+ elif ttc_mode == TTCMode.EFFORT_BASED_PER_STEP_SAMPLE:
123
+ # Run trial to decide success if config says so (SANDOR mode)
124
+ return ttc_dist.attempt_ttc_with_effort(num_attempts, rng)
125
+
126
+ elif ttc_mode == TTCMode.PER_STEP_SAMPLE:
127
+ # Sample ttc value every time if config says so (ANDREI mode)
128
+ _node_ttc_value = ttc_dist.sample_value(rng)
129
+ return _node_ttc_value <= 1
130
+
131
+ # Compare attempts to ttc expected value in EXPECTED_VALUE mode
132
+ # or presampled ttcs in PRE_SAMPLE mode
133
+ elif ttc_mode in (TTCMode.EXPECTED_VALUE, TTCMode.PRE_SAMPLE):
134
+ _node_ttc_value = node_ttc_value(agent, node)
135
+ return num_attempts + 1 >= _node_ttc_value
136
+
137
+ else:
138
+ raise ValueError(f'Invalid TTC mode: {ttc_mode}')
@@ -0,0 +1,61 @@
1
+ from __future__ import annotations
2
+ from typing import TYPE_CHECKING
3
+ from maltoolbox.attackgraph import AttackGraphNode
4
+ import logging
5
+
6
+ import numpy as np
7
+
8
+
9
+ from malsim.dyna_mal_simulator.model_effects import execute_model_effects
10
+ from malsim.dyna_mal_simulator.simulator_state import DynaMalSimulatorState
11
+
12
+ if TYPE_CHECKING:
13
+ from malsim.mal_simulator.defender_state import DefenderState
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ def dyna_defender_step(
19
+ sim_state: DynaMalSimulatorState,
20
+ agent: DefenderState,
21
+ nodes: list[AttackGraphNode],
22
+ rng: np.random.Generator,
23
+ ) -> tuple[list[AttackGraphNode], DynaMalSimulatorState]:
24
+ """Enable defense step nodes with defender.
25
+
26
+ Args:
27
+ agent - the agent to activate defense nodes with
28
+ nodes - the defense step nodes to enable
29
+
30
+ Returns a tuple of a list and a set, `enabled_defenses`
31
+ """
32
+
33
+ logger.debug('Stepping with %s', agent.name)
34
+ enabled_defenses: list[AttackGraphNode] = []
35
+
36
+ for node in nodes:
37
+ assert node == sim_state.attack_graph.nodes[node.id], (
38
+ f'{agent.name} tried to enable a node that is not part '
39
+ 'of this simulators attack_graph. Make sure the node '
40
+ 'comes from the agents action surface.'
41
+ )
42
+
43
+ if node not in agent.action_surface:
44
+ logger.warning(
45
+ 'Defender agent "%s" tried to step through "%s"(%d), '
46
+ 'which is not part of its defense surface. Defender '
47
+ 'step will skip!',
48
+ agent.name,
49
+ node.full_name,
50
+ node.id,
51
+ )
52
+ else:
53
+ enabled_defenses.append(node)
54
+ sim_state = execute_model_effects(sim_state, node, rng)
55
+ logger.info(
56
+ 'Defender agent "%s" enabled "%s"',
57
+ agent.name,
58
+ node.full_name,
59
+ )
60
+
61
+ return enabled_defenses, sim_state
@@ -0,0 +1,58 @@
1
+ """Dataclass and function to store and create graph state used in the simulator"""
2
+
3
+ from __future__ import annotations
4
+ from collections.abc import Set
5
+
6
+ from numpy.random import Generator
7
+ from maltoolbox.attackgraph import AttackGraph, AttackGraphNode
8
+ from malsim.mal_simulator.ttc_utils import (
9
+ attack_step_ttc_values,
10
+ get_pre_enabled_defenses,
11
+ get_impossible_attack_steps,
12
+ )
13
+ from malsim.mal_simulator.graph_processing import calculate_necessity
14
+
15
+ from malsim.mal_simulator.graph_state import GraphState
16
+
17
+ from malsim.config.sim_settings import MalSimulatorSettings
18
+
19
+
20
+ def add_new_nodes_to_graph_state(
21
+ graph_state: GraphState,
22
+ sim_settings: MalSimulatorSettings,
23
+ attack_graph: AttackGraph,
24
+ new_nodes: set[AttackGraphNode],
25
+ rng: Generator,
26
+ ) -> tuple[GraphState, Set[AttackGraphNode]]:
27
+ """Update graph state based on new nodes added to the attack graph."""
28
+
29
+ new_attack_steps = {node for node in new_nodes if node.type in ('and', 'or')}
30
+ new_defense_steps = [node for node in new_nodes if node.type == 'defense']
31
+
32
+ # TTC (Time to compromise) for each attack step
33
+ # will only be set if TTCMode PRE_SAMLE/EXPECTED_VALUE is used
34
+ new_attack_step_ttc_values = attack_step_ttc_values(
35
+ new_attack_steps, rng=rng, ttc_mode=sim_settings.ttc_mode
36
+ )
37
+ # These steps will be enabled from the start of the simulation
38
+ # depending on if bernoullis are sampled or not
39
+ new_steps_enabled_defenses = get_pre_enabled_defenses(
40
+ new_defense_steps, sim_settings.run_defense_step_bernoullis, rng
41
+ )
42
+ enabled_defenses = graph_state.pre_enabled_defenses | new_steps_enabled_defenses
43
+ if sim_settings.run_attack_step_bernoullis:
44
+ # These steps will not be traversable
45
+ new_impossible_attack_steps = get_impossible_attack_steps(new_attack_steps, rng)
46
+ else:
47
+ new_impossible_attack_steps = set()
48
+
49
+ necessity_per_node = calculate_necessity(attack_graph, enabled_defenses)
50
+
51
+ return GraphState(
52
+ ttc_values={**graph_state.ttc_values, **new_attack_step_ttc_values},
53
+ pre_enabled_defenses=graph_state.pre_enabled_defenses
54
+ | new_steps_enabled_defenses,
55
+ impossible_attack_steps=graph_state.impossible_attack_steps
56
+ | new_impossible_attack_steps,
57
+ necessity_per_node=necessity_per_node,
58
+ ), new_steps_enabled_defenses