mal-simulator 0.2.0__tar.gz → 0.2.2__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 (28) hide show
  1. {mal_simulator-0.2.0/mal_simulator.egg-info → mal_simulator-0.2.2}/PKG-INFO +1 -1
  2. {mal_simulator-0.2.0 → mal_simulator-0.2.2/mal_simulator.egg-info}/PKG-INFO +1 -1
  3. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/__init__.py +3 -3
  4. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/mal_simulator.py +50 -12
  5. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/pyproject.toml +1 -6
  6. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/tests/test_mal_simulator.py +52 -33
  7. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/AUTHORS +0 -0
  8. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/LICENSE +0 -0
  9. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/README.md +0 -0
  10. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/mal_simulator.egg-info/SOURCES.txt +0 -0
  11. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/mal_simulator.egg-info/dependency_links.txt +0 -0
  12. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/mal_simulator.egg-info/entry_points.txt +0 -0
  13. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/mal_simulator.egg-info/requires.txt +0 -0
  14. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/mal_simulator.egg-info/top_level.txt +0 -0
  15. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/__main__.py +0 -0
  16. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/agents/__init__.py +0 -0
  17. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/agents/decision_agent.py +0 -0
  18. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/agents/keyboard_input.py +0 -0
  19. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/agents/passive_agent.py +0 -0
  20. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/agents/searchers.py +0 -0
  21. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/envs/__init__.py +0 -0
  22. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/envs/base_classes.py +0 -0
  23. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/envs/gym_envs.py +0 -0
  24. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/envs/malsim_vectorized_obs_env.py +0 -0
  25. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/malsim/scenario.py +0 -0
  26. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/setup.cfg +0 -0
  27. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/tests/test_main.py +0 -0
  28. {mal_simulator-0.2.0 → mal_simulator-0.2.2}/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.0
3
+ Version: 0.2.2
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.0
3
+ Version: 0.2.2
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
  # -*- encoding: utf-8 -*-
2
- # MAL Simulator v0.2.0
3
- # Copyright 2024, Andrei Buhaiu.
2
+ # MAL Simulator v0.2.2
3
+ # Copyright 2025, Andrei Buhaiu.
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
6
6
  # you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@ MAL Simulator
24
24
  """
25
25
 
26
26
  __title__ = "malsim"
27
- __version__ = "0.2.0"
27
+ __version__ = "0.2.2"
28
28
  __authors__ = ["Andrei Buhaiu", "Joakim Loxdal", "Jakob Nyberg", "Nikolaos Kakouros"]
29
29
  __license__ = "Apache 2.0"
30
30
  __docformat__ = "restructuredtext en"
@@ -76,8 +76,13 @@ class MalSimDefenderState(MalSimAgentState):
76
76
  super().__init__(name, AgentType.DEFENDER)
77
77
 
78
78
 
79
- class MalSimAgentStateView:
80
- """Read-only interface to MalSimAgentState."""
79
+ class MalSimAgentStateView(MalSimAttackerState, MalSimDefenderState):
80
+ """Read-only interface to MalSimAgentState.
81
+
82
+ Subclassing from State classes only to satisfy static analysis and support
83
+ autocompletion that would otherwise be unavailable due to the dynamic
84
+ __getattr(ibute)__ method.
85
+ """
81
86
 
82
87
  _frozen = False
83
88
 
@@ -88,16 +93,34 @@ class MalSimAgentStateView:
88
93
  def __setattr__(self, key, value) -> None:
89
94
  if self._frozen:
90
95
  raise AttributeError("Cannot modify agent state view")
91
- self.__dict__[key] = value
96
+ super().__setattr__(key, value)
92
97
 
93
98
  def __delattr__(self, key) -> None:
94
99
  if self._frozen:
95
100
  raise AttributeError("Cannot modify agent state view")
96
101
  super().__delattr__(key)
97
102
 
98
- def __getattr__(self, attr) -> Any:
99
- """Return read-only version of proxied agent's properties."""
100
- value = getattr(self._agent, attr)
103
+ def __getattribute__(self, attr) -> Any:
104
+ """Return read-only version of proxied agent's properties.
105
+
106
+ If the attribute exists in the View only return it from there. Using
107
+ __getattribute__ instead of __getattr__ as the latter is called only for
108
+ missing properties; since this class is a subclass of State, it will
109
+ have all state properties and those would be returned from self, not
110
+ self._agent. Using __getattribute__ allows use to filter which
111
+ properties to return from self and which from self._agent.
112
+ """
113
+ if attr in ("_agent", "_frozen") or \
114
+ attr.startswith("__") and attr.endswith("__"):
115
+ return super().__getattribute__(attr)
116
+
117
+ agent = super().__getattribute__('_agent')
118
+ value = getattr(agent, attr)
119
+
120
+ if attr == '_agent':
121
+ return agent
122
+
123
+ value = getattr(agent, attr)
101
124
 
102
125
  if isinstance(value, dict):
103
126
  return MappingProxyType(value)
@@ -110,7 +133,15 @@ class MalSimAgentStateView:
110
133
 
111
134
  def __dir__(self):
112
135
  """Dynamically resolve attribute names for REPL autocompletion."""
113
- return list(vars(self._agent).keys()) + ["_agent", "_frozen"]
136
+ dunder_attrs = [
137
+ attr
138
+ for attr in dir(self.__class__)
139
+ if attr.startswith("__") and attr.endswith("__")
140
+ ]
141
+
142
+ props = list(vars(self._agent).keys()) + ["_agent", "_frozen"]
143
+
144
+ return dunder_attrs + props
114
145
 
115
146
 
116
147
  @dataclass
@@ -211,7 +242,9 @@ class MalSimulator():
211
242
 
212
243
  if isinstance(agent, MalSimAttackerState):
213
244
  attacker = self.attack_graph.attackers[agent.attacker_id]
214
- agent.action_surface = query.calculate_attack_surface(attacker)
245
+ agent.action_surface = query.calculate_attack_surface(
246
+ attacker, skip_compromised = True
247
+ )
215
248
 
216
249
  elif isinstance(agent, MalSimDefenderState):
217
250
  agent.action_surface = \
@@ -310,14 +343,12 @@ class MalSimulator():
310
343
 
311
344
  def _attacker_step(
312
345
  self, agent: MalSimAttackerState, nodes: list[AttackGraphNode]
313
- ) -> set[AttackGraphNode]:
346
+ ):
314
347
  """Compromise attack step nodes with attacker
315
348
 
316
349
  Args:
317
350
  agent - the agent to compromise nodes with
318
351
  nodes - the nodes to compromise
319
-
320
- Returns: A set of attack steps nodes that were compromised
321
352
  """
322
353
 
323
354
  compromised_nodes = set()
@@ -352,9 +383,16 @@ class MalSimulator():
352
383
 
353
384
  # Update attacker action surface
354
385
  attack_surface_additions = query.calculate_attack_surface(
355
- attacker, from_nodes=compromised_nodes, skip_compromised=True
386
+ attacker, from_nodes = compromised_nodes, skip_compromised = True
356
387
  )
357
388
 
389
+ # Filter out nodes already in action surface, these are not additions.
390
+ attack_surface_additions -= agent.action_surface
391
+ # Also filter out nodes already compromised from the attack surface.
392
+ # TODO: Make this configurable in the future
393
+ agent.action_surface -= compromised_nodes
394
+ agent.step_action_surface_removals |= compromised_nodes
395
+
358
396
  agent.step_action_surface_additions = attack_surface_additions
359
397
  agent.action_surface |= attack_surface_additions
360
398
  agent.step_performed_nodes = compromised_nodes
@@ -1,12 +1,11 @@
1
1
  [project]
2
2
  name = "mal-simulator"
3
- version = "0.2.0"
3
+ version = "0.2.2"
4
4
  authors = [
5
5
  { name="Andrei Buhaiu", email="buhaiu@kth.se" },
6
6
  { name="Joakim Loxdal", email="loxdal@kth.se" },
7
7
  { name="Jakob Nyberg", email="jaknyb@kth.se" },
8
8
  { name="Nikolaos Kakouros", email="nkak@kth.se" }
9
-
10
9
  ]
11
10
  description = "A MAL compliant simulator."
12
11
  readme = "README.md"
@@ -44,7 +43,6 @@ dev = [
44
43
  "Bug Tracker" = "https://github.com/mal-lang/mal-simulator/issues"
45
44
  "Repository" = "https://github.com/mal-lang/mal-simulator"
46
45
 
47
-
48
46
  [project.scripts]
49
47
  malsim = "malsim.__main__:main"
50
48
 
@@ -55,8 +53,5 @@ build-backend = "setuptools.build_meta"
55
53
  [tool.setuptools.packages.find]
56
54
  include = ["malsim*"]
57
55
 
58
- [tool.setuptools.package-data]
59
- maltoolbox = ["tests*"]
60
-
61
56
  [tool.pytest.ini_options]
62
57
  pythonpath = ['.']
@@ -1,6 +1,6 @@
1
1
  """Test MalSimulator class"""
2
2
 
3
- from maltoolbox.attackgraph import AttackGraph, Attacker
3
+ from maltoolbox.attackgraph import AttackGraphNode, AttackGraph, Attacker
4
4
  from malsim.mal_simulator import MalSimulator
5
5
 
6
6
  from malsim.scenario import load_scenario
@@ -168,6 +168,12 @@ def test_defender_step(corelang_lang_graph, model):
168
168
 
169
169
 
170
170
  def test_agent_state_views_simple(corelang_lang_graph, model):
171
+
172
+ def get_node(full_name) -> AttackGraphNode:
173
+ node = sim.attack_graph.get_node_by_full_name(full_name)
174
+ assert node
175
+ return node
176
+
171
177
  attack_graph = AttackGraph(corelang_lang_graph, model)
172
178
  entry_point = attack_graph.get_node_by_full_name('OS App:fullAccess')
173
179
 
@@ -178,11 +184,12 @@ def test_agent_state_views_simple(corelang_lang_graph, model):
178
184
  attacker_id = 100
179
185
  )
180
186
  attack_graph.add_attacker(attacker, attacker.id)
187
+
188
+ # Create simulator and register agents
181
189
  sim = MalSimulator(attack_graph)
182
190
  attacker_name = 'attacker'
183
191
  defender_name = 'defender'
184
192
  sim.register_attacker(attacker_name, attacker.id)
185
-
186
193
  sim.register_defender(defender_name)
187
194
 
188
195
  # Evaluate the agent state views after reset
@@ -197,55 +204,67 @@ def test_agent_state_views_simple(corelang_lang_graph, model):
197
204
  assert asv.step_action_surface_removals == set()
198
205
  assert dsv.step_action_surface_removals == set()
199
206
 
207
+ # Save all relvant nodes in variables
208
+ program2_not_present = get_node('Program 2:notPresent')
209
+ os_app_attempt_deny = get_node('OS App:attemptDeny')
210
+ os_app_success_deny = get_node('OS App:successfulDeny')
211
+ os_app_not_present = get_node('OS App:notPresent')
212
+ os_app_access_netcon = get_node('OS App:accessNetworkAndConnections')
213
+ os_app_spec_access = get_node('OS App:specificAccess')
214
+
200
215
  # Evaluate the agent state views after stepping through an attack step and
201
216
  # a defense that will not impact it in any way
217
+
202
218
  state_views = sim.step({
203
- 'defender': {sim.attack_graph.get_node_by_full_name(
204
- 'Program 2:notPresent')},
205
- 'attacker': {sim.attack_graph.get_node_by_full_name(
206
- 'OS App:attemptDeny')}
219
+ 'defender': [program2_not_present],
220
+ 'attacker': [os_app_attempt_deny]
207
221
  })
208
222
  asv = state_views['attacker']
209
223
  dsv = state_views['defender']
210
- assert asv.step_performed_nodes == {
211
- sim.attack_graph.get_node_by_full_name('OS App:attemptDeny')}
212
- assert dsv.step_performed_nodes == {
213
- sim.attack_graph.get_node_by_full_name('Program 2:notPresent')}
214
- assert asv.step_action_surface_additions == {
215
- sim.attack_graph.get_node_by_full_name('OS App:successfulDeny')}
224
+ assert asv.step_performed_nodes == {os_app_attempt_deny}
225
+ assert dsv.step_performed_nodes == {program2_not_present}
226
+ assert asv.step_action_surface_additions == {os_app_success_deny}
216
227
  assert dsv.step_action_surface_additions == set()
217
- assert asv.step_action_surface_removals == set()
218
- assert dsv.step_action_surface_removals == {
219
- sim.attack_graph.get_node_by_full_name('Program 2:notPresent')}
220
- assert dsv.step_all_compromised_nodes == {
221
- sim.attack_graph.get_node_by_full_name('OS App:attemptDeny')}
228
+ assert asv.step_action_surface_removals == {os_app_attempt_deny}
229
+ assert os_app_attempt_deny not in asv.action_surface
230
+ assert dsv.step_action_surface_removals == {program2_not_present}
231
+ assert dsv.step_all_compromised_nodes == {os_app_attempt_deny}
222
232
  assert len(dsv.step_unviable_nodes) == 49
223
233
 
234
+ # Go through an attack step that already has some children in the attack
235
+ # surface(OS App:accessNetworkAndConnections in this case)
236
+ assert os_app_access_netcon in asv.action_surface
237
+ state_views = sim.step({
238
+ 'defender': [],
239
+ 'attacker': [os_app_spec_access]
240
+ })
241
+ asv = state_views['attacker']
242
+ dsv = state_views['defender']
243
+ assert asv.step_performed_nodes == {os_app_spec_access}
244
+ assert dsv.step_performed_nodes == set()
245
+ assert os_app_access_netcon in asv.action_surface
246
+ assert os_app_access_netcon not in asv.step_action_surface_additions
247
+ assert dsv.step_action_surface_additions == set()
248
+ assert asv.step_action_surface_removals == {os_app_spec_access}
249
+ assert os_app_spec_access not in asv.action_surface
250
+ assert dsv.step_action_surface_removals == set()
251
+ assert dsv.step_all_compromised_nodes == {os_app_spec_access}
252
+ assert len(dsv.step_unviable_nodes) == 0
253
+
224
254
  # Evaluate the agent state views after stepping through an attack step and
225
255
  # a defense that would prevent it from occurring
226
256
  state_views = sim.step({
227
- 'defender': {sim.attack_graph.get_node_by_full_name(
228
- 'OS App:notPresent')},
229
- 'attacker': {sim.attack_graph.get_node_by_full_name(
230
- 'OS App:successfulDeny')}
257
+ 'defender': [os_app_not_present],
258
+ 'attacker': [os_app_success_deny]
231
259
  })
232
260
  asv = state_views['attacker']
233
261
  dsv = state_views['defender']
234
262
  assert asv.step_performed_nodes == set()
235
- assert dsv.step_performed_nodes == {
236
- sim.attack_graph.get_node_by_full_name('OS App:notPresent')}
263
+ assert dsv.step_performed_nodes == {os_app_not_present}
237
264
  assert asv.step_action_surface_additions == set()
238
265
  assert dsv.step_action_surface_additions == set()
239
- assert asv.step_action_surface_removals == {
240
- sim.attack_graph.get_node_by_full_name('OS App:accessNetworkAndConnections'),
241
- sim.attack_graph.get_node_by_full_name('OS App:specificAccess'),
242
- sim.attack_graph.get_node_by_full_name('OS App:attemptApplicationRespondConnectThroughData'),
243
- sim.attack_graph.get_node_by_full_name('OS App:attemptRead'),
244
- sim.attack_graph.get_node_by_full_name('OS App:attemptModify'),
245
- sim.attack_graph.get_node_by_full_name('OS App:successfulDeny'),
246
- }
247
- assert dsv.step_action_surface_removals == {
248
- sim.attack_graph.get_node_by_full_name('OS App:notPresent')}
266
+ assert len(asv.step_action_surface_removals) == 12
267
+ assert dsv.step_action_surface_removals == {os_app_not_present}
249
268
  assert dsv.step_all_compromised_nodes == set()
250
269
  assert len(dsv.step_unviable_nodes) == 55
251
270
 
File without changes
File without changes
File without changes
File without changes