mal-simulator 0.2.4__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.
- {mal_simulator-0.2.4/mal_simulator.egg-info → mal_simulator-0.2.5}/PKG-INFO +1 -1
- {mal_simulator-0.2.4 → mal_simulator-0.2.5/mal_simulator.egg-info}/PKG-INFO +1 -1
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/__init__.py +2 -2
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/envs/gym_envs.py +3 -3
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/envs/malsim_vectorized_obs_env.py +2 -7
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/mal_simulator.py +27 -12
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/pyproject.toml +1 -1
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/tests/test_mal_simulator.py +7 -6
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/AUTHORS +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/LICENSE +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/README.md +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/mal_simulator.egg-info/SOURCES.txt +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/mal_simulator.egg-info/dependency_links.txt +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/mal_simulator.egg-info/entry_points.txt +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/mal_simulator.egg-info/requires.txt +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/mal_simulator.egg-info/top_level.txt +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/__main__.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/agents/__init__.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/agents/decision_agent.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/agents/heuristic_agent.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/agents/keyboard_input.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/agents/passive_agent.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/agents/searchers.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/envs/__init__.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/envs/base_classes.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/malsim/scenario.py +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/setup.cfg +0 -0
- {mal_simulator-0.2.4 → mal_simulator-0.2.5}/tests/test_main.py +0 -0
- {mal_simulator-0.2.4 → 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
|
+
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
|
+
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,5 +1,5 @@
|
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
|
2
|
-
# MAL Simulator v0.2.
|
|
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.
|
|
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"
|
|
@@ -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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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,
|
|
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,
|
|
67
|
+
def __init__(self, name: str, attacker: Attacker):
|
|
64
68
|
super().__init__(name, AgentType.ATTACKER)
|
|
65
|
-
self.
|
|
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 =
|
|
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 =
|
|
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(
|
|
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
|
-
|
|
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 =
|
|
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 =
|
|
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
|
|
@@ -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,
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|