From d393a0b3c632b03420d0f96c868d8eed37cf09c1 Mon Sep 17 00:00:00 2001 From: vakarisz Date: Fri, 17 Jun 2022 18:05:26 +0300 Subject: [PATCH] Agent: Change credential collectors, payloads and pbas in flat config Flat config changes are made in order for config object to be serializable --- monkey/monkey_island/cc/services/config.py | 18 +++++++--- .../monkey_configs/flat_config.json | 1 + .../monkey_island/cc/services/test_config.py | 33 +++++++++---------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/monkey/monkey_island/cc/services/config.py b/monkey/monkey_island/cc/services/config.py index 82346258f..3b466cd31 100644 --- a/monkey/monkey_island/cc/services/config.py +++ b/monkey/monkey_island/cc/services/config.py @@ -357,6 +357,7 @@ class ConfigService: ConfigService._format_payloads_from_flat_config(config) ConfigService._format_pbas_from_flat_config(config) ConfigService._format_propagation_from_flat_config(config) + ConfigService._format_credential_collectors(config) # Ok, I'll admit this is just sort of jammed in here. But this code is going away very soon. del config["HTTP_PORTS"] @@ -376,9 +377,18 @@ class ConfigService: for field in fields_to_remove: config.pop(field, None) + @staticmethod + def _format_credential_collectors(config: Dict): + collectors = [ + {"name": collector, "options": {}} for collector in config["credential_collectors"] + ] + config["credential_collectors"] = collectors + @staticmethod def _format_payloads_from_flat_config(config: Dict): - config.setdefault("payloads", {})["ransomware"] = config["ransomware"] + config.setdefault("payloads", []).append( + {"name": "ransomware", "options": config["ransomware"]} + ) config.pop("ransomware", None) @staticmethod @@ -388,9 +398,9 @@ class ConfigService: flat_windows_command_field = "custom_PBA_windows_cmd" flat_windows_filename_field = "PBA_windows_filename" - formatted_pbas_config = {} - for pba in config.get("post_breach_actions", []): - formatted_pbas_config[pba] = {} + formatted_pbas_config = [ + {"name": pba, "options": {}} for pba in config.get("post_breach_actions", []) + ] config["custom_pbas"] = { "linux_command": config.get(flat_linux_command_field, ""), diff --git a/monkey/tests/data_for_tests/monkey_configs/flat_config.json b/monkey/tests/data_for_tests/monkey_configs/flat_config.json index 33bf50da1..441c176ed 100644 --- a/monkey/tests/data_for_tests/monkey_configs/flat_config.json +++ b/monkey/tests/data_for_tests/monkey_configs/flat_config.json @@ -27,6 +27,7 @@ "private_key": "my_private_key" } ], + "credential_collectors": ["MimikatzCollector", "SSHCollector"], "exploit_user_list": [ "Administrator", "root", diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py index 9e01b8365..108659ddf 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_config.py @@ -25,34 +25,33 @@ def test_format_config_for_agent__credentials_removed(): def test_format_config_for_agent__ransomware_payload(): expected_ransomware_options = { - "ransomware": { - "encryption": { - "enabled": True, - "directories": { - "linux_target_dir": "/tmp/ransomware-target", - "windows_target_dir": "C:\\windows\\temp\\ransomware-target", - }, + "encryption": { + "enabled": True, + "directories": { + "linux_target_dir": "/tmp/ransomware-target", + "windows_target_dir": "C:\\windows\\temp\\ransomware-target", }, - "other_behaviors": {"readme": True}, - } + }, + "other_behaviors": {"readme": True}, } flat_monkey_config = ConfigService.format_flat_config_for_agent() assert "payloads" in flat_monkey_config - assert flat_monkey_config["payloads"] == expected_ransomware_options + assert flat_monkey_config["payloads"][0]["name"] == "ransomware" + assert flat_monkey_config["payloads"][0]["options"] == expected_ransomware_options assert "ransomware" not in flat_monkey_config def test_format_config_for_agent__pbas(): - expected_pbas_config = { - "CommunicateAsBackdoorUser": {}, - "ModifyShellStartupFiles": {}, - "ScheduleJobs": {}, - "Timestomping": {}, - "AccountDiscovery": {}, - } + expected_pbas_config = [ + {"name": "CommunicateAsBackdoorUser", "options": {}}, + {"name": "ModifyShellStartupFiles", "options": {}}, + {"name": "ScheduleJobs", "options": {}}, + {"name": "Timestomping", "options": {}}, + {"name": "AccountDiscovery", "options": {}}, + ] flat_monkey_config = ConfigService.format_flat_config_for_agent() assert "post_breach_actions" in flat_monkey_config