mal-simulator 0.2.4__tar.gz → 0.2.6__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 (29) hide show
  1. {mal_simulator-0.2.4/mal_simulator.egg-info → mal_simulator-0.2.6}/PKG-INFO +1 -1
  2. {mal_simulator-0.2.4 → mal_simulator-0.2.6/mal_simulator.egg-info}/PKG-INFO +1 -1
  3. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/__init__.py +2 -2
  4. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/envs/gym_envs.py +3 -3
  5. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/envs/malsim_vectorized_obs_env.py +2 -7
  6. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/mal_simulator.py +31 -14
  7. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/pyproject.toml +1 -1
  8. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/tests/test_mal_simulator.py +7 -6
  9. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/AUTHORS +0 -0
  10. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/LICENSE +0 -0
  11. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/README.md +0 -0
  12. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/mal_simulator.egg-info/SOURCES.txt +0 -0
  13. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/mal_simulator.egg-info/dependency_links.txt +0 -0
  14. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/mal_simulator.egg-info/entry_points.txt +0 -0
  15. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/mal_simulator.egg-info/requires.txt +0 -0
  16. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/mal_simulator.egg-info/top_level.txt +0 -0
  17. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/__main__.py +0 -0
  18. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/agents/__init__.py +0 -0
  19. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/agents/decision_agent.py +0 -0
  20. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/agents/heuristic_agent.py +0 -0
  21. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/agents/keyboard_input.py +0 -0
  22. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/agents/passive_agent.py +0 -0
  23. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/agents/searchers.py +0 -0
  24. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/envs/__init__.py +0 -0
  25. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/envs/base_classes.py +0 -0
  26. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/malsim/scenario.py +0 -0
  27. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/setup.cfg +0 -0
  28. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/tests/test_main.py +0 -0
  29. {mal_simulator-0.2.4 → mal_simulator-0.2.6}/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.4
3
+ Version: 0.2.6
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.4
3
+ Version: 0.2.6
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,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # MAL Simulator v0.2.4
2
+ # MAL Simulator v0.2.6
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.4"
27
+ __version__ = "0.2.6"
28
28
  __authors__ = ["Andrei Buhaiu", "Joakim Loxdal", "Jakob Nyberg", "Nikolaos Kakouros"]
29
29
  __license__ = "Apache 2.0"
30
30
  __docformat__ = "restructuredtext en"
@@ -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"""
@@ -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,9 +226,10 @@ 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
- n.extras.get("reward", 0) for n in attacker.reached_attack_steps
231
+ n.extras.get("reward", 0)
232
+ for n in attacker.reached_attack_steps
228
233
  )
229
234
 
230
235
  lost_reward = sum(
@@ -234,14 +239,15 @@ class MalSimulator():
234
239
  )
235
240
 
236
241
  for agent in self._get_defender_agents():
237
- agent.reward = lost_reward
242
+ # Defenders get negative reward for pre-enabled nodes
243
+ agent.reward = -lost_reward
238
244
 
239
245
  def _init_agent_action_surfaces(self):
240
246
  """Set agent action surfaces according to current state"""
241
247
  for agent in self._agent_states.values():
242
248
 
243
249
  if isinstance(agent, MalSimAttackerState):
244
- attacker = self.attack_graph.attackers[agent.attacker_id]
250
+ attacker = agent.attacker
245
251
  agent.action_surface = query.calculate_attack_surface(
246
252
  attacker, skip_compromised = True
247
253
  )
@@ -261,7 +267,10 @@ class MalSimulator():
261
267
  for agent_state in self._get_attacker_agents():
262
268
  # Create a new agent state for the attacker
263
269
  self._agent_states[agent_state.name] = (
264
- MalSimAttackerState(agent_state.name, agent_state.attacker_id)
270
+ MalSimAttackerState(
271
+ agent_state.name,
272
+ self.attack_graph.attackers[agent_state.attacker.id]
273
+ )
265
274
  )
266
275
 
267
276
  for agent_state in self._get_defender_agents():
@@ -278,7 +287,13 @@ class MalSimulator():
278
287
  assert name not in self._agent_states, \
279
288
  f"Duplicate agent named {name} not allowed"
280
289
 
281
- agent_state = MalSimAttackerState(name, attacker_id)
290
+ attacker = self.attack_graph.attackers.get(attacker_id, None)
291
+ if attacker is None:
292
+ msg = ('Failed to register Attacker agent because no attacker '
293
+ f'with id {attacker_id} was found in the attack ' 'graph!')
294
+ logger.error(msg)
295
+ raise ValueError(msg)
296
+ agent_state = MalSimAttackerState(name, attacker)
282
297
  self._agent_states[name] = agent_state
283
298
  self._alive_agents.add(name)
284
299
 
@@ -325,7 +340,7 @@ class MalSimulator():
325
340
  - Uncompromise the node and remove rewards for it.
326
341
  """
327
342
  for attacker_agent in self._get_attacker_agents():
328
- attacker = self.attack_graph.attackers[attacker_agent.attacker_id]
343
+ attacker = attacker_agent.attacker
329
344
 
330
345
  for unviable_node in attack_steps_to_uncompromise:
331
346
  if unviable_node.is_compromised_by(attacker):
@@ -352,7 +367,7 @@ class MalSimulator():
352
367
  """
353
368
 
354
369
  compromised_nodes = set()
355
- attacker = self.attack_graph.attackers[agent.attacker_id]
370
+ attacker = agent.attacker
356
371
 
357
372
  for node in nodes:
358
373
  assert node == self.attack_graph.nodes[node.id], (
@@ -370,6 +385,7 @@ class MalSimulator():
370
385
  if query.is_node_traversable_by_attacker(node, attacker) \
371
386
  and node in agent.action_surface:
372
387
  attacker.compromise(node)
388
+ agent.performed_nodes.add(node)
373
389
  agent.reward += node.extras.get('reward', 0)
374
390
  compromised_nodes.add(node)
375
391
 
@@ -446,6 +462,7 @@ class MalSimulator():
446
462
  apriori.propagate_viability_from_unviable_node(node)
447
463
  agent.reward -= node.extras.get("reward", 0)
448
464
  enabled_defenses.add(node)
465
+ agent.performed_nodes.add(node)
449
466
  logger.info(
450
467
  'Defender agent "%s" enabled "%s"(%d).',
451
468
  agent.name, node.full_name, node.id
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "mal-simulator"
3
- version = "0.2.4"
3
+ version = "0.2.6"
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
File without changes
File without changes
File without changes
File without changes