From 305b2cf716b07d99957da79cdffef0717f72479a Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 24 Aug 2021 10:29:36 +0200 Subject: [PATCH 1/5] Zoo: Add PowerShell config and bb test --- .../blackbox/config_templates/powershell.py | 21 +++++++++++++++++++ envs/monkey_zoo/blackbox/test_blackbox.py | 6 ++++++ .../utils/config_generation_script.py | 2 ++ 3 files changed, 29 insertions(+) create mode 100644 envs/monkey_zoo/blackbox/config_templates/powershell.py diff --git a/envs/monkey_zoo/blackbox/config_templates/powershell.py b/envs/monkey_zoo/blackbox/config_templates/powershell.py new file mode 100644 index 000000000..76db9e248 --- /dev/null +++ b/envs/monkey_zoo/blackbox/config_templates/powershell.py @@ -0,0 +1,21 @@ +from copy import copy + +from envs.monkey_zoo.blackbox.config_templates.base_template import BaseTemplate +from envs.monkey_zoo.blackbox.config_templates.config_template import ConfigTemplate + + +class PowerShell(ConfigTemplate): + config_values = copy(BaseTemplate.config_values) + + config_values.update( + { + "basic.exploiters.exploiter_classes": ["PowerShellExploiter"], + "basic_network.scope.subnet_scan_list": ["10.2.2.45", "10.2.3.47"], + "basic.credentials.exploit_password_list": ["Passw0rd!", ""], + "basic_network.scope.depth": 2, + "basic.credentials.exploit_user_list": ["m0nk3y", "m0nk3y-user"], + "internal.classes.finger_classes": ["PingScanner"], + "internal.network.tcp_scanner.HTTP_PORTS": [], + "internal.network.tcp_scanner.tcp_target_ports": [], + } + ) diff --git a/envs/monkey_zoo/blackbox/test_blackbox.py b/envs/monkey_zoo/blackbox/test_blackbox.py index 5cd67d7ec..3c3934630 100644 --- a/envs/monkey_zoo/blackbox/test_blackbox.py +++ b/envs/monkey_zoo/blackbox/test_blackbox.py @@ -13,6 +13,7 @@ from envs.monkey_zoo.blackbox.config_templates.elastic import Elastic from envs.monkey_zoo.blackbox.config_templates.hadoop import Hadoop from envs.monkey_zoo.blackbox.config_templates.mssql import Mssql from envs.monkey_zoo.blackbox.config_templates.performance import Performance +from envs.monkey_zoo.blackbox.config_templates.powershell import PowerShell from envs.monkey_zoo.blackbox.config_templates.shellshock import ShellShock from envs.monkey_zoo.blackbox.config_templates.smb_mimikatz import SmbMimikatz from envs.monkey_zoo.blackbox.config_templates.smb_pth import SmbPth @@ -156,6 +157,11 @@ class TestMonkeyBlackbox: def test_mssql_exploiter(self, island_client): TestMonkeyBlackbox.run_exploitation_test(island_client, Mssql, "MSSQL_exploiter") + def test_powershell_exploiter(self, island_client): + TestMonkeyBlackbox.run_exploitation_test( + island_client, PowerShell, "PowerShell_Remoting_exploiter" + ) + def test_smb_and_mimikatz_exploiters(self, island_client): TestMonkeyBlackbox.run_exploitation_test( island_client, SmbMimikatz, "SMB_exploiter_mimikatz" diff --git a/envs/monkey_zoo/blackbox/utils/config_generation_script.py b/envs/monkey_zoo/blackbox/utils/config_generation_script.py index b2c69acda..f38a48d39 100644 --- a/envs/monkey_zoo/blackbox/utils/config_generation_script.py +++ b/envs/monkey_zoo/blackbox/utils/config_generation_script.py @@ -8,6 +8,7 @@ from envs.monkey_zoo.blackbox.config_templates.elastic import Elastic from envs.monkey_zoo.blackbox.config_templates.hadoop import Hadoop from envs.monkey_zoo.blackbox.config_templates.mssql import Mssql from envs.monkey_zoo.blackbox.config_templates.performance import Performance +from envs.monkey_zoo.blackbox.config_templates.powershell import PowerShell from envs.monkey_zoo.blackbox.config_templates.shellshock import ShellShock from envs.monkey_zoo.blackbox.config_templates.smb_mimikatz import SmbMimikatz from envs.monkey_zoo.blackbox.config_templates.smb_pth import SmbPth @@ -40,6 +41,7 @@ CONFIG_TEMPLATES = [ Hadoop, Mssql, Performance, + PowerShell, ShellShock, SmbMimikatz, SmbPth, From 9f2a4cb7e4f9832ce977d6d8ac042687506a570a Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 24 Aug 2021 11:56:09 +0200 Subject: [PATCH 2/5] Zoo: Update terraform scripts. Update gcp test machine list with new zone --- .../blackbox/config_templates/performance.py | 4 ++ .../blackbox/gcp_test_machine_list.py | 50 +++++++++++-------- .../blackbox/start_all_gcp_machines.py | 2 +- .../blackbox/stop_all_gcp_machines.py | 2 +- envs/monkey_zoo/blackbox/test_blackbox.py | 4 +- .../blackbox/utils/gcp_machine_handlers.py | 28 ++++++----- envs/monkey_zoo/terraform/images.tf | 8 +++ envs/monkey_zoo/terraform/monkey_zoo.tf | 36 +++++++++++++ 8 files changed, 95 insertions(+), 39 deletions(-) diff --git a/envs/monkey_zoo/blackbox/config_templates/performance.py b/envs/monkey_zoo/blackbox/config_templates/performance.py index e5213b649..b5b3e0655 100644 --- a/envs/monkey_zoo/blackbox/config_templates/performance.py +++ b/envs/monkey_zoo/blackbox/config_templates/performance.py @@ -10,6 +10,7 @@ class Performance(ConfigTemplate): "3Q=(Ge(+&w]*", "`))jU7L(w}", "t67TC5ZDmz", + "Passw0rd!", ], "basic.credentials.exploit_user_list": ["m0nk3y"], "basic.exploiters.exploiter_classes": [ @@ -24,6 +25,7 @@ class Performance(ConfigTemplate): "HadoopExploiter", "VSFTPDExploiter", "MSSQLExploiter", + "PowerShellExploiter", "ZerologonExploiter", ], "basic_network.network_analysis.inaccessible_subnets": [ @@ -58,5 +60,7 @@ class Performance(ConfigTemplate): "10.2.2.23", "10.2.2.24", "10.2.2.25", + "10.2.2.45", + "10.2.3.47", ], } diff --git a/envs/monkey_zoo/blackbox/gcp_test_machine_list.py b/envs/monkey_zoo/blackbox/gcp_test_machine_list.py index 43246ad24..852d2fb25 100644 --- a/envs/monkey_zoo/blackbox/gcp_test_machine_list.py +++ b/envs/monkey_zoo/blackbox/gcp_test_machine_list.py @@ -1,22 +1,28 @@ -GCP_TEST_MACHINE_LIST = [ - "sshkeys-11", - "sshkeys-12", - "elastic-4", - "elastic-5", - "hadoop-2", - "hadoop-3", - "mssql-16", - "mimikatz-14", - "mimikatz-15", - "struts2-23", - "struts2-24", - "tunneling-9", - "tunneling-10", - "tunneling-11", - "tunneling-12", - "weblogic-18", - "weblogic-19", - "shellshock-8", - "zerologon-25", - "drupal-28", -] +GCP_TEST_MACHINE_LIST = { + "europe-west3-a": [ + "sshkeys-11", + "sshkeys-12", + "elastic-4", + "elastic-5", + "hadoop-2", + "hadoop-3", + "mssql-16", + "powershell-45", + "mimikatz-14", + "mimikatz-15", + "struts2-23", + "struts2-24", + "tunneling-9", + "tunneling-10", + "tunneling-11", + "tunneling-12", + "weblogic-18", + "weblogic-19", + "shellshock-8", + "zerologon-25", + "drupal-28", + ], + "europe-west1-b": [ + "powershell-3-47", + ], +} diff --git a/envs/monkey_zoo/blackbox/start_all_gcp_machines.py b/envs/monkey_zoo/blackbox/start_all_gcp_machines.py index f31a072f9..9cab68d97 100755 --- a/envs/monkey_zoo/blackbox/start_all_gcp_machines.py +++ b/envs/monkey_zoo/blackbox/start_all_gcp_machines.py @@ -4,4 +4,4 @@ from gcp_test_machine_list import GCP_TEST_MACHINE_LIST from utils.gcp_machine_handlers import GCPHandler gcp_handler = GCPHandler() -gcp_handler.start_machines(" ".join(GCP_TEST_MACHINE_LIST)) +gcp_handler.start_machines(GCP_TEST_MACHINE_LIST) diff --git a/envs/monkey_zoo/blackbox/stop_all_gcp_machines.py b/envs/monkey_zoo/blackbox/stop_all_gcp_machines.py index 132191e94..7272df30b 100755 --- a/envs/monkey_zoo/blackbox/stop_all_gcp_machines.py +++ b/envs/monkey_zoo/blackbox/stop_all_gcp_machines.py @@ -4,4 +4,4 @@ from gcp_test_machine_list import GCP_TEST_MACHINE_LIST from utils.gcp_machine_handlers import GCPHandler gcp_handler = GCPHandler() -gcp_handler.stop_machines(" ".join(GCP_TEST_MACHINE_LIST)) +gcp_handler.stop_machines(GCP_TEST_MACHINE_LIST) diff --git a/envs/monkey_zoo/blackbox/test_blackbox.py b/envs/monkey_zoo/blackbox/test_blackbox.py index 3c3934630..aa6544e6c 100644 --- a/envs/monkey_zoo/blackbox/test_blackbox.py +++ b/envs/monkey_zoo/blackbox/test_blackbox.py @@ -55,14 +55,14 @@ def GCPHandler(request, no_gcp): if not no_gcp: try: GCPHandler = gcp_machine_handlers.GCPHandler() - GCPHandler.start_machines(" ".join(GCP_TEST_MACHINE_LIST)) + GCPHandler.start_machines(GCP_TEST_MACHINE_LIST) except Exception as e: LOGGER.error("GCP Handler failed to initialize: %s." % e) pytest.exit("Encountered an error while starting GCP machines. Stopping the tests.") wait_machine_bootup() def fin(): - GCPHandler.stop_machines(" ".join(GCP_TEST_MACHINE_LIST)) + GCPHandler.stop_machines(GCP_TEST_MACHINE_LIST) request.addfinalizer(fin) diff --git a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py index c438e92f5..38fc7125c 100644 --- a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py +++ b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py @@ -13,15 +13,12 @@ class GCPHandler(object): # Key path location relative to this file's directory RELATIVE_KEY_PATH = "../../gcp_keys/gcp_key.json" - DEFAULT_ZONE = "europe-west3-a" DEFAULT_PROJECT = "guardicore-22050661" def __init__( self, - zone=DEFAULT_ZONE, project_id=DEFAULT_PROJECT, ): - self.zone = zone abs_key_path = GCPHandler.get_absolute_key_path() subprocess.call(GCPHandler.get_auth_command(abs_key_path), shell=True) # noqa: DUO116 @@ -43,26 +40,31 @@ class GCPHandler(object): ) return absolute_key_path - def start_machines(self, machine_list): + @staticmethod + def start_machines(machine_list): """ Start all the machines in the list. - :param machine_list: A space-separated string with all the machine names. Example: - start_machines(`" ".join(["elastic-3", "mssql-16"])`) + :param machine_list: A dictionary with zone and machines per zone. """ LOGGER.info("Setting up all GCP machines...") try: - subprocess.call( # noqa: DUO116 - (GCPHandler.MACHINE_STARTING_COMMAND % (machine_list, self.zone)), shell=True - ) + for zone in machine_list: + subprocess.call( # noqa: DUO116 + (GCPHandler.MACHINE_STARTING_COMMAND % (" ".join(machine_list[zone]), zone)), + shell=True, + ) LOGGER.info("GCP machines successfully started.") except Exception as e: LOGGER.error("GCP Handler failed to start GCP machines: %s" % e) - def stop_machines(self, machine_list): + @staticmethod + def stop_machines(machine_list): try: - subprocess.call( # noqa: DUO116 - (GCPHandler.MACHINE_STOPPING_COMMAND % (machine_list, self.zone)), shell=True - ) + for zone in machine_list: + subprocess.call( # noqa: DUO116 + (GCPHandler.MACHINE_STOPPING_COMMAND % (" ".join(machine_list[zone]), zone)), + shell=True, + ) LOGGER.info("GCP machines stopped successfully.") except Exception as e: LOGGER.error("GCP Handler failed to stop network machines: %s" % e) diff --git a/envs/monkey_zoo/terraform/images.tf b/envs/monkey_zoo/terraform/images.tf index 866a4f174..3c4783389 100644 --- a/envs/monkey_zoo/terraform/images.tf +++ b/envs/monkey_zoo/terraform/images.tf @@ -57,6 +57,14 @@ data "google_compute_image" "mssql-16" { name = "mssql-16" project = local.monkeyzoo_project } +data "google_compute_image" "powershell-3-47" { + name = "powershell-3-47" + project = local.monkeyzoo_project +} +data "google_compute_image" "powershell-45" { + name = "powershell-45" + project = local.monkeyzoo_project +} data "google_compute_image" "weblogic-18" { name = "weblogic-18" project = local.monkeyzoo_project diff --git a/envs/monkey_zoo/terraform/monkey_zoo.tf b/envs/monkey_zoo/terraform/monkey_zoo.tf index 5eabc160b..a3411c608 100644 --- a/envs/monkey_zoo/terraform/monkey_zoo.tf +++ b/envs/monkey_zoo/terraform/monkey_zoo.tf @@ -26,6 +26,12 @@ resource "google_compute_subnetwork" "monkeyzoo-main" { network = google_compute_network.monkeyzoo.self_link } +resource "google_compute_subnetwork" "monkeyzoo-main-1" { + name = "${local.resource_prefix}monkeyzoo-main-1" + ip_cidr_range = "10.2.3.0/24" + network = google_compute_network.monkeyzoo.self_link +} + resource "google_compute_subnetwork" "tunneling-main" { name = "${local.resource_prefix}tunneling-main" ip_cidr_range = "10.2.1.0/28" @@ -307,6 +313,36 @@ resource "google_compute_instance_from_template" "mssql-16" { } } +resource "google_compute_instance_from_template" "powershell-3-47" { + name = "${local.resource_prefix}powershell-3-47" + source_instance_template = local.default_windows + boot_disk{ + initialize_params { + image = data.google_compute_image.powershell-3-47.self_link + } + auto_delete = true + } + network_interface { + subnetwork="${local.resource_prefix}monkeyzoo-main-1" + network_ip="10.2.3.47" + } +} + +resource "google_compute_instance_from_template" "powershell-45" { + name = "${local.resource_prefix}powershell-45" + source_instance_template = local.default_windows + boot_disk{ + initialize_params { + image = data.google_compute_image.powershell-45.self_link + } + auto_delete = true + } + network_interface { + subnetwork="${local.resource_prefix}monkeyzoo-main" + network_ip="10.2.2.45" + } +} + /* We need to alter monkey's behavior for this to upload 32-bit monkey instead of 64-bit (not yet developed) resource "google_compute_instance_from_template" "upgrader-17" { name = "${local.resource_prefix}upgrader-17" From 5cee9443ff97ff45b963f8a902cca5329d9a69d9 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 24 Aug 2021 15:11:22 +0200 Subject: [PATCH 3/5] Zoo: Remove GCPHandler class. Powershell-3-47 renamed to Powershell-3-46. Powershell-45 moved to different zone --- .../blackbox/config_templates/performance.py | 3 - .../blackbox/config_templates/powershell.py | 4 +- .../blackbox/gcp_test_machine_list.py | 5 +- .../blackbox/start_all_gcp_machines.py | 6 +- .../blackbox/stop_all_gcp_machines.py | 6 +- envs/monkey_zoo/blackbox/test_blackbox.py | 12 +- .../blackbox/utils/gcp_machine_handlers.py | 116 +++++++++--------- envs/monkey_zoo/terraform/images.tf | 8 +- envs/monkey_zoo/terraform/monkey_zoo.tf | 16 +-- vulture_allowlist.py | 1 + 10 files changed, 88 insertions(+), 89 deletions(-) diff --git a/envs/monkey_zoo/blackbox/config_templates/performance.py b/envs/monkey_zoo/blackbox/config_templates/performance.py index b5b3e0655..2662642e6 100644 --- a/envs/monkey_zoo/blackbox/config_templates/performance.py +++ b/envs/monkey_zoo/blackbox/config_templates/performance.py @@ -10,7 +10,6 @@ class Performance(ConfigTemplate): "3Q=(Ge(+&w]*", "`))jU7L(w}", "t67TC5ZDmz", - "Passw0rd!", ], "basic.credentials.exploit_user_list": ["m0nk3y"], "basic.exploiters.exploiter_classes": [ @@ -60,7 +59,5 @@ class Performance(ConfigTemplate): "10.2.2.23", "10.2.2.24", "10.2.2.25", - "10.2.2.45", - "10.2.3.47", ], } diff --git a/envs/monkey_zoo/blackbox/config_templates/powershell.py b/envs/monkey_zoo/blackbox/config_templates/powershell.py index 76db9e248..e6d2467ab 100644 --- a/envs/monkey_zoo/blackbox/config_templates/powershell.py +++ b/envs/monkey_zoo/blackbox/config_templates/powershell.py @@ -10,8 +10,8 @@ class PowerShell(ConfigTemplate): config_values.update( { "basic.exploiters.exploiter_classes": ["PowerShellExploiter"], - "basic_network.scope.subnet_scan_list": ["10.2.2.45", "10.2.3.47"], - "basic.credentials.exploit_password_list": ["Passw0rd!", ""], + "basic_network.scope.subnet_scan_list": ["10.2.3.45", "10.2.3.46"], + "basic.credentials.exploit_password_list": ["Passw0rd!"], "basic_network.scope.depth": 2, "basic.credentials.exploit_user_list": ["m0nk3y", "m0nk3y-user"], "internal.classes.finger_classes": ["PingScanner"], diff --git a/envs/monkey_zoo/blackbox/gcp_test_machine_list.py b/envs/monkey_zoo/blackbox/gcp_test_machine_list.py index 852d2fb25..52efeb670 100644 --- a/envs/monkey_zoo/blackbox/gcp_test_machine_list.py +++ b/envs/monkey_zoo/blackbox/gcp_test_machine_list.py @@ -7,7 +7,7 @@ GCP_TEST_MACHINE_LIST = { "hadoop-2", "hadoop-3", "mssql-16", - "powershell-45", + "powershell-3-45", "mimikatz-14", "mimikatz-15", "struts2-23", @@ -23,6 +23,7 @@ GCP_TEST_MACHINE_LIST = { "drupal-28", ], "europe-west1-b": [ - "powershell-3-47", + "powershell-3-45", + "powershell-3-46", ], } diff --git a/envs/monkey_zoo/blackbox/start_all_gcp_machines.py b/envs/monkey_zoo/blackbox/start_all_gcp_machines.py index 9cab68d97..c5e83671c 100755 --- a/envs/monkey_zoo/blackbox/start_all_gcp_machines.py +++ b/envs/monkey_zoo/blackbox/start_all_gcp_machines.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from gcp_test_machine_list import GCP_TEST_MACHINE_LIST -from utils.gcp_machine_handlers import GCPHandler +from utils.gcp_machine_handlers import initialize_gcp_client, start_machines -gcp_handler = GCPHandler() -gcp_handler.start_machines(GCP_TEST_MACHINE_LIST) +initialize_gcp_client() +start_machines(GCP_TEST_MACHINE_LIST) diff --git a/envs/monkey_zoo/blackbox/stop_all_gcp_machines.py b/envs/monkey_zoo/blackbox/stop_all_gcp_machines.py index 7272df30b..d5a489a52 100755 --- a/envs/monkey_zoo/blackbox/stop_all_gcp_machines.py +++ b/envs/monkey_zoo/blackbox/stop_all_gcp_machines.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from gcp_test_machine_list import GCP_TEST_MACHINE_LIST -from utils.gcp_machine_handlers import GCPHandler +from utils.gcp_machine_handlers import initialize_gcp_client, stop_machines -gcp_handler = GCPHandler() -gcp_handler.stop_machines(GCP_TEST_MACHINE_LIST) +initialize_gcp_client() +stop_machines(GCP_TEST_MACHINE_LIST) diff --git a/envs/monkey_zoo/blackbox/test_blackbox.py b/envs/monkey_zoo/blackbox/test_blackbox.py index aa6544e6c..221d783f6 100644 --- a/envs/monkey_zoo/blackbox/test_blackbox.py +++ b/envs/monkey_zoo/blackbox/test_blackbox.py @@ -40,7 +40,11 @@ from envs.monkey_zoo.blackbox.tests.performance.report_generation_from_telemetri from envs.monkey_zoo.blackbox.tests.performance.telemetry_performance_test import ( TelemetryPerformanceTest, ) -from envs.monkey_zoo.blackbox.utils import gcp_machine_handlers +from envs.monkey_zoo.blackbox.utils.gcp_machine_handlers import ( + initialize_gcp_client, + start_machines, + stop_machines, +) from monkey_island.cc.services.mode.mode_enum import IslandModeEnum DEFAULT_TIMEOUT_SECONDS = 5 * 60 @@ -54,15 +58,15 @@ LOGGER = logging.getLogger(__name__) def GCPHandler(request, no_gcp): if not no_gcp: try: - GCPHandler = gcp_machine_handlers.GCPHandler() - GCPHandler.start_machines(GCP_TEST_MACHINE_LIST) + initialize_gcp_client() + start_machines(GCP_TEST_MACHINE_LIST) except Exception as e: LOGGER.error("GCP Handler failed to initialize: %s." % e) pytest.exit("Encountered an error while starting GCP machines. Stopping the tests.") wait_machine_bootup() def fin(): - GCPHandler.stop_machines(GCP_TEST_MACHINE_LIST) + stop_machines(GCP_TEST_MACHINE_LIST) request.addfinalizer(fin) diff --git a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py index 38fc7125c..26b4b18a5 100644 --- a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py +++ b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py @@ -4,75 +4,71 @@ import subprocess LOGGER = logging.getLogger(__name__) +AUTHENTICATION_COMMAND = "gcloud auth activate-service-account --key-file=%s" +SET_PROPERTY_PROJECT = "gcloud config set project %s" +MACHINE_STARTING_COMMAND = "gcloud compute instances start %s --zone=%s" +MACHINE_STOPPING_COMMAND = "gcloud compute instances stop %s --zone=%s" -class GCPHandler(object): - AUTHENTICATION_COMMAND = "gcloud auth activate-service-account --key-file=%s" - SET_PROPERTY_PROJECT = "gcloud config set project %s" - MACHINE_STARTING_COMMAND = "gcloud compute instances start %s --zone=%s" - MACHINE_STOPPING_COMMAND = "gcloud compute instances stop %s --zone=%s" +# Key path location relative to this file's directory +RELATIVE_KEY_PATH = "../../gcp_keys/gcp_key.json" +DEFAULT_PROJECT = "guardicore-22050661" - # Key path location relative to this file's directory - RELATIVE_KEY_PATH = "../../gcp_keys/gcp_key.json" - DEFAULT_PROJECT = "guardicore-22050661" - def __init__( - self, - project_id=DEFAULT_PROJECT, - ): - abs_key_path = GCPHandler.get_absolute_key_path() +def initialize_gcp_client(): + abs_key_path = get_absolute_key_path() - subprocess.call(GCPHandler.get_auth_command(abs_key_path), shell=True) # noqa: DUO116 - LOGGER.info("GCP Handler passed key") + subprocess.call(get_auth_command(abs_key_path), shell=True) # noqa: DUO116 + LOGGER.info("GCP Handler passed key") - subprocess.call(GCPHandler.get_set_project_command(project_id), shell=True) # noqa: DUO116 - LOGGER.info("GCP Handler set project") - LOGGER.info("GCP Handler initialized successfully") + subprocess.call(get_set_project_command(DEFAULT_PROJECT), shell=True) # noqa: DUO116 + LOGGER.info("GCP Handler set project") + LOGGER.info("GCP Handler initialized successfully") - @staticmethod - def get_absolute_key_path() -> str: - file_dir = os.path.dirname(os.path.realpath(__file__)) - absolute_key_path = os.path.join(file_dir, GCPHandler.RELATIVE_KEY_PATH) - absolute_key_path = os.path.realpath(absolute_key_path) - if not os.path.isfile(absolute_key_path): - raise FileNotFoundError( - "GCP key not found. " "Add a service key to envs/monkey_zoo/gcp_keys/gcp_key.json" +def get_absolute_key_path() -> str: + file_dir = os.path.dirname(os.path.realpath(__file__)) + absolute_key_path = os.path.join(file_dir, RELATIVE_KEY_PATH) + absolute_key_path = os.path.realpath(absolute_key_path) + + if not os.path.isfile(absolute_key_path): + raise FileNotFoundError( + "GCP key not found. " "Add a service key to envs/monkey_zoo/gcp_keys/gcp_key.json" + ) + return absolute_key_path + + +def start_machines(machine_list): + """ + Start all the machines in the list. + :param machine_list: A dictionary with zone and machines per zone. + """ + LOGGER.info("Setting up all GCP machines...") + try: + for zone in machine_list: + subprocess.call( # noqa: DUO116 + (MACHINE_STARTING_COMMAND % (" ".join(machine_list[zone]), zone)), + shell=True, ) - return absolute_key_path + LOGGER.info("GCP machines successfully started.") + except Exception as e: + LOGGER.error("GCP Handler failed to start GCP machines: %s" % e) - @staticmethod - def start_machines(machine_list): - """ - Start all the machines in the list. - :param machine_list: A dictionary with zone and machines per zone. - """ - LOGGER.info("Setting up all GCP machines...") - try: - for zone in machine_list: - subprocess.call( # noqa: DUO116 - (GCPHandler.MACHINE_STARTING_COMMAND % (" ".join(machine_list[zone]), zone)), - shell=True, - ) - LOGGER.info("GCP machines successfully started.") - except Exception as e: - LOGGER.error("GCP Handler failed to start GCP machines: %s" % e) - @staticmethod - def stop_machines(machine_list): - try: - for zone in machine_list: - subprocess.call( # noqa: DUO116 - (GCPHandler.MACHINE_STOPPING_COMMAND % (" ".join(machine_list[zone]), zone)), - shell=True, - ) - LOGGER.info("GCP machines stopped successfully.") - except Exception as e: - LOGGER.error("GCP Handler failed to stop network machines: %s" % e) +def stop_machines(machine_list): + try: + for zone in machine_list: + subprocess.call( # noqa: DUO116 + (MACHINE_STOPPING_COMMAND % (" ".join(machine_list[zone]), zone)), + shell=True, + ) + LOGGER.info("GCP machines stopped successfully.") + except Exception as e: + LOGGER.error("GCP Handler failed to stop network machines: %s" % e) - @staticmethod - def get_auth_command(key_path): - return GCPHandler.AUTHENTICATION_COMMAND % key_path - @staticmethod - def get_set_project_command(project): - return GCPHandler.SET_PROPERTY_PROJECT % project +def get_auth_command(key_path): + return AUTHENTICATION_COMMAND % key_path + + +def get_set_project_command(project): + return SET_PROPERTY_PROJECT % project diff --git a/envs/monkey_zoo/terraform/images.tf b/envs/monkey_zoo/terraform/images.tf index 3c4783389..3f293736d 100644 --- a/envs/monkey_zoo/terraform/images.tf +++ b/envs/monkey_zoo/terraform/images.tf @@ -57,12 +57,12 @@ data "google_compute_image" "mssql-16" { name = "mssql-16" project = local.monkeyzoo_project } -data "google_compute_image" "powershell-3-47" { - name = "powershell-3-47" +data "google_compute_image" "powershell-3-46" { + name = "powershell-3-46" project = local.monkeyzoo_project } -data "google_compute_image" "powershell-45" { - name = "powershell-45" +data "google_compute_image" "powershell-3-45" { + name = "powershell-3-45" project = local.monkeyzoo_project } data "google_compute_image" "weblogic-18" { diff --git a/envs/monkey_zoo/terraform/monkey_zoo.tf b/envs/monkey_zoo/terraform/monkey_zoo.tf index a3411c608..241828557 100644 --- a/envs/monkey_zoo/terraform/monkey_zoo.tf +++ b/envs/monkey_zoo/terraform/monkey_zoo.tf @@ -313,33 +313,33 @@ resource "google_compute_instance_from_template" "mssql-16" { } } -resource "google_compute_instance_from_template" "powershell-3-47" { - name = "${local.resource_prefix}powershell-3-47" +resource "google_compute_instance_from_template" "powershell-3-46" { + name = "${local.resource_prefix}powershell-3-46" source_instance_template = local.default_windows boot_disk{ initialize_params { - image = data.google_compute_image.powershell-3-47.self_link + image = data.google_compute_image.powershell-3-46.self_link } auto_delete = true } network_interface { subnetwork="${local.resource_prefix}monkeyzoo-main-1" - network_ip="10.2.3.47" + network_ip="10.2.3.46" } } -resource "google_compute_instance_from_template" "powershell-45" { - name = "${local.resource_prefix}powershell-45" +resource "google_compute_instance_from_template" "powershell-3-45" { + name = "${local.resource_prefix}powershell-3-45" source_instance_template = local.default_windows boot_disk{ initialize_params { - image = data.google_compute_image.powershell-45.self_link + image = data.google_compute_image.powershell-3-45.self_link } auto_delete = true } network_interface { subnetwork="${local.resource_prefix}monkeyzoo-main" - network_ip="10.2.2.45" + network_ip="10.2.3.45" } } diff --git a/vulture_allowlist.py b/vulture_allowlist.py index b39d61dd8..e1454d876 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -196,3 +196,4 @@ environment # unused variable (monkey/monkey_island/cc/models/monkey.py:59) _.environment # unused attribute (monkey/monkey_island/cc/services/telemetry/processing/system_info_collectors/environment.py:10) _.instance_name # unused attribute (monkey/common/cloud/azure/azure_instance.py:35) _.instance_name # unused attribute (monkey/common/cloud/azure/azure_instance.py:64) +GCPHandler # unused function (envs/monkey_zoo/blackbox/test_blackbox.py:57) From e6ca0fd3b618b6b965c66c57b33c01ef11493e85 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 25 Aug 2021 10:07:41 +0200 Subject: [PATCH 4/5] Zoo: Parallelize start and stop of gcp machines --- .../blackbox/gcp_test_machine_list.py | 1 - .../blackbox/utils/gcp_machine_handlers.py | 29 ++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/envs/monkey_zoo/blackbox/gcp_test_machine_list.py b/envs/monkey_zoo/blackbox/gcp_test_machine_list.py index 52efeb670..86999ab6d 100644 --- a/envs/monkey_zoo/blackbox/gcp_test_machine_list.py +++ b/envs/monkey_zoo/blackbox/gcp_test_machine_list.py @@ -7,7 +7,6 @@ GCP_TEST_MACHINE_LIST = { "hadoop-2", "hadoop-3", "mssql-16", - "powershell-3-45", "mimikatz-14", "mimikatz-15", "struts2-23", diff --git a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py index 26b4b18a5..9c01c72c7 100644 --- a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py +++ b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py @@ -1,6 +1,7 @@ import logging import os import subprocess +from multiprocessing.dummy import Pool LOGGER = logging.getLogger(__name__) @@ -44,24 +45,24 @@ def start_machines(machine_list): """ LOGGER.info("Setting up all GCP machines...") try: + arglist = [] for zone in machine_list: - subprocess.call( # noqa: DUO116 - (MACHINE_STARTING_COMMAND % (" ".join(machine_list[zone]), zone)), - shell=True, - ) - LOGGER.info("GCP machines successfully started.") + arglist.append((MACHINE_STARTING_COMMAND, machine_list, zone)) + with Pool(2) as pool: + pool.map(run_gcp_command, arglist) + LOGGER.info("GCP machines successfully started.") except Exception as e: LOGGER.error("GCP Handler failed to start GCP machines: %s" % e) def stop_machines(machine_list): try: + arglist = [] for zone in machine_list: - subprocess.call( # noqa: DUO116 - (MACHINE_STOPPING_COMMAND % (" ".join(machine_list[zone]), zone)), - shell=True, - ) - LOGGER.info("GCP machines stopped successfully.") + arglist.append((MACHINE_STOPPING_COMMAND, machine_list, zone)) + with Pool(2) as pool: + pool.map(run_gcp_command, arglist) + LOGGER.info("GCP machines stopped successfully.") except Exception as e: LOGGER.error("GCP Handler failed to stop network machines: %s" % e) @@ -72,3 +73,11 @@ def get_auth_command(key_path): def get_set_project_command(project): return SET_PROPERTY_PROJECT % project + + +def run_gcp_command(arglist): + gcp_cmd, machine_list, zone = arglist + subprocess.call( # noqa DUO116 + (gcp_cmd % (" ".join(machine_list[zone]), zone)), + shell=True, + ) From 9a96e6ed3904e6c6be4fa1a13e96d619c5e4d531 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Thu, 26 Aug 2021 10:35:22 +0200 Subject: [PATCH 5/5] Zoo: Refactor start and stop gcp machine functions --- .../blackbox/utils/gcp_machine_handlers.py | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py index 9c01c72c7..aa12bfe73 100644 --- a/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py +++ b/envs/monkey_zoo/blackbox/utils/gcp_machine_handlers.py @@ -45,24 +45,16 @@ def start_machines(machine_list): """ LOGGER.info("Setting up all GCP machines...") try: - arglist = [] - for zone in machine_list: - arglist.append((MACHINE_STARTING_COMMAND, machine_list, zone)) - with Pool(2) as pool: - pool.map(run_gcp_command, arglist) - LOGGER.info("GCP machines successfully started.") + run_gcp_pool(MACHINE_STARTING_COMMAND, machine_list) + LOGGER.info("GCP machines successfully started.") except Exception as e: LOGGER.error("GCP Handler failed to start GCP machines: %s" % e) def stop_machines(machine_list): try: - arglist = [] - for zone in machine_list: - arglist.append((MACHINE_STOPPING_COMMAND, machine_list, zone)) - with Pool(2) as pool: - pool.map(run_gcp_command, arglist) - LOGGER.info("GCP machines stopped successfully.") + run_gcp_pool(MACHINE_STOPPING_COMMAND, machine_list) + LOGGER.info("GCP machines stopped successfully.") except Exception as e: LOGGER.error("GCP Handler failed to stop network machines: %s" % e) @@ -78,6 +70,13 @@ def get_set_project_command(project): def run_gcp_command(arglist): gcp_cmd, machine_list, zone = arglist subprocess.call( # noqa DUO116 - (gcp_cmd % (" ".join(machine_list[zone]), zone)), + (gcp_cmd % (" ".join(machine_list), zone)), shell=True, ) + + +def run_gcp_pool(gcp_command, machine_list): + arglist = [(gcp_command, machine_list[zone], zone) for zone in machine_list] + with Pool(2) as pool: + pool.map(run_gcp_command, arglist) +