From 520e98032a732516b251b57825f775cee055eb8d Mon Sep 17 00:00:00 2001 From: vakarisz Date: Wed, 16 Mar 2022 13:52:28 +0200 Subject: [PATCH] Agent, Island: Rename "alive" to "should_stop" in configuration "Alive" indicates state, when in fact we need a value indicating if stop command was sent to this monkey. Monkey alive state is already tracked elsewhere, in the Monkey document --- monkey/infection_monkey/config.py | 2 +- monkey/infection_monkey/example.conf | 2 +- monkey/monkey_island/cc/models/config.py | 2 +- .../monkey_island/cc/services/config_schema/internal.py | 7 +++---- monkey/monkey_island/cc/services/infection_lifecycle.py | 8 ++++---- monkey/monkey_island/cc/services/node.py | 2 +- .../monkey_island/cc/services/test_infection_lifecycle.py | 6 +++--- 7 files changed, 14 insertions(+), 15 deletions(-) diff --git a/monkey/infection_monkey/config.py b/monkey/infection_monkey/config.py index 9c239fd93..8a920cc52 100644 --- a/monkey/infection_monkey/config.py +++ b/monkey/infection_monkey/config.py @@ -81,7 +81,7 @@ class Configuration(object): # monkey config ########################### # sets whether or not the monkey is alive. if false will stop scanning and exploiting - alive = True + should_stop = False # depth of propagation depth = 2 diff --git a/monkey/infection_monkey/example.conf b/monkey/infection_monkey/example.conf index ebadf1429..f0cbb6e16 100644 --- a/monkey/infection_monkey/example.conf +++ b/monkey/infection_monkey/example.conf @@ -9,7 +9,7 @@ "inaccessible_subnets": [], "blocked_ips": [], "current_server": "192.0.2.0:5000", - "alive": true, + "should_stop": false, "collect_system_info": true, "should_use_mimikatz": true, "depth": 2, diff --git a/monkey/monkey_island/cc/models/config.py b/monkey/monkey_island/cc/models/config.py index 437f73b44..db5fd9e94 100644 --- a/monkey/monkey_island/cc/models/config.py +++ b/monkey/monkey_island/cc/models/config.py @@ -8,6 +8,6 @@ class Config(EmbeddedDocument): See https://mongoengine-odm.readthedocs.io/apireference.html#mongoengine.FieldDoesNotExist """ - alive = BooleanField() + should_stop = BooleanField() meta = {"strict": False} pass diff --git a/monkey/monkey_island/cc/services/config_schema/internal.py b/monkey/monkey_island/cc/services/config_schema/internal.py index 98ab8b95e..26326721c 100644 --- a/monkey/monkey_island/cc/services/config_schema/internal.py +++ b/monkey/monkey_island/cc/services/config_schema/internal.py @@ -19,11 +19,10 @@ INTERNAL = { "title": "Monkey", "type": "object", "properties": { - "alive": { - "title": "Alive", + "should_stop": { "type": "boolean", - "default": True, - "description": "Is the monkey alive", + "default": False, + "description": "Was stop command issued for this monkey", }, "aws_keys": { "type": "object", diff --git a/monkey/monkey_island/cc/services/infection_lifecycle.py b/monkey/monkey_island/cc/services/infection_lifecycle.py index 510e3deb6..871c279cc 100644 --- a/monkey/monkey_island/cc/services/infection_lifecycle.py +++ b/monkey/monkey_island/cc/services/infection_lifecycle.py @@ -16,7 +16,7 @@ logger = logging.getLogger(__name__) def set_stop_all(time: float): for monkey in Monkey.objects(): - monkey.config.alive = False + monkey.config.should_stop = True monkey.save() agent_controls = AgentControls.objects.first() agent_controls.last_stop_all = time @@ -25,11 +25,11 @@ def set_stop_all(time: float): def should_agent_die(guid: int) -> bool: monkey = Monkey.objects(guid=str(guid)).first() - return _is_monkey_marked_dead(monkey) or _is_monkey_killed_manually(monkey) + return _should_agent_stop(monkey) or _is_monkey_killed_manually(monkey) -def _is_monkey_marked_dead(monkey: Monkey) -> bool: - return not monkey.config.alive +def _should_agent_stop(monkey: Monkey) -> bool: + return monkey.config.should_stop def _is_monkey_killed_manually(monkey: Monkey) -> bool: diff --git a/monkey/monkey_island/cc/services/node.py b/monkey/monkey_island/cc/services/node.py index 688f67f92..6b672bc4d 100644 --- a/monkey/monkey_island/cc/services/node.py +++ b/monkey/monkey_island/cc/services/node.py @@ -249,7 +249,7 @@ class NodeService: # Cancel the force kill once monkey died if is_dead: - props_to_set["config.alive"] = True + props_to_set["config.should_stop"] = False mongo.db.monkey.update({"guid": monkey["guid"]}, {"$set": props_to_set}, upsert=False) diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_infection_lifecycle.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_infection_lifecycle.py index 4d4c229c8..541124248 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_infection_lifecycle.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_infection_lifecycle.py @@ -10,21 +10,21 @@ from monkey_island.cc.services.infection_lifecycle import should_agent_die @pytest.mark.usefixtures("uses_database") def test_should_agent_die_by_config(monkeypatch): monkey = Monkey(guid=str(uuid.uuid4())) - monkey.config = Config(alive=False) + monkey.config = Config(should_stop=True) monkey.save() assert should_agent_die(monkey.guid) monkeypatch.setattr( "monkey_island.cc.services.infection_lifecycle._is_monkey_killed_manually", lambda _: False ) - monkey.config.alive = True + monkey.config.should_stop = True monkey.save() assert not should_agent_die(monkey.guid) def create_monkey(launch_time): monkey = Monkey(guid=str(uuid.uuid4())) - monkey.config = Config(alive=True) + monkey.config = Config(should_stop=False) monkey.launch_time = launch_time monkey.save() return monkey