diff --git a/monkey/common/__init__.py b/monkey/common/__init__.py index fd9232730..c63b927d5 100644 --- a/monkey/common/__init__.py +++ b/monkey/common/__init__.py @@ -2,4 +2,4 @@ Used for a common things between agent and island """ from .di_container import DIContainer, UnresolvableDependencyError -from .operating_systems import OperatingSystems +from .operating_system import OperatingSystem diff --git a/monkey/common/operating_systems.py b/monkey/common/operating_system.py similarity index 90% rename from monkey/common/operating_systems.py rename to monkey/common/operating_system.py index 2ac2f64b3..6855a9e94 100644 --- a/monkey/common/operating_systems.py +++ b/monkey/common/operating_system.py @@ -1,7 +1,7 @@ from enum import Enum -class OperatingSystems(Enum): +class OperatingSystem(Enum): """ An Enum representing all supported operating systems diff --git a/monkey/infection_monkey/exploit/caching_agent_repository.py b/monkey/infection_monkey/exploit/caching_agent_repository.py index 7d3580258..028a5f869 100644 --- a/monkey/infection_monkey/exploit/caching_agent_repository.py +++ b/monkey/infection_monkey/exploit/caching_agent_repository.py @@ -5,7 +5,7 @@ from typing import Mapping import requests -from common import OperatingSystems +from common import OperatingSystem from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT from . import IAgentRepository @@ -24,7 +24,7 @@ class CachingAgentRepository(IAgentRepository): self._lock = threading.Lock() def get_agent_binary( - self, operating_system: OperatingSystems, architecture: str = None + self, operating_system: OperatingSystem, architecture: str = None ) -> io.BytesIO: # If multiple calls to get_agent_binary() are made simultaneously before the result of # _download_binary_from_island() is cached, then multiple requests will be sent to the @@ -34,7 +34,7 @@ class CachingAgentRepository(IAgentRepository): return io.BytesIO(self._download_binary_from_island(operating_system)) @lru_cache(maxsize=None) - def _download_binary_from_island(self, operating_system: OperatingSystems) -> bytes: + def _download_binary_from_island(self, operating_system: OperatingSystem) -> bytes: os_name = operating_system.value response = requests.get( # noqa: DUO123 diff --git a/monkey/infection_monkey/exploit/i_agent_repository.py b/monkey/infection_monkey/exploit/i_agent_repository.py index 308cf5418..cf5141201 100644 --- a/monkey/infection_monkey/exploit/i_agent_repository.py +++ b/monkey/infection_monkey/exploit/i_agent_repository.py @@ -1,7 +1,7 @@ import abc import io -from common import OperatingSystems +from common import OperatingSystem # TODO: The Island also has an IAgentRepository with a totally different interface. At the moment, # the Island and Agent have different needs, but at some point we should unify these. @@ -16,7 +16,7 @@ class IAgentRepository(metaclass=abc.ABCMeta): @abc.abstractmethod def get_agent_binary( - self, operating_system: OperatingSystems, architecture: str = None + self, operating_system: OperatingSystem, architecture: str = None ) -> io.BytesIO: """ Retrieve the appropriate agent binary from the repository. diff --git a/monkey/infection_monkey/exploit/log4shell.py b/monkey/infection_monkey/exploit/log4shell.py index cab4ed548..f511d75d9 100644 --- a/monkey/infection_monkey/exploit/log4shell.py +++ b/monkey/infection_monkey/exploit/log4shell.py @@ -2,7 +2,7 @@ import logging import time from pathlib import PurePath -from common import OperatingSystems +from common import OperatingSystem from common.common_consts.timeouts import LONG_REQUEST_TIMEOUT, MEDIUM_REQUEST_TIMEOUT from common.utils import Timer from infection_monkey.exploit.log4shell_utils import ( @@ -129,7 +129,7 @@ class Log4ShellExploiter(WebRCE): } def _build_java_class(self, exploit_command: str) -> bytes: - if OperatingSystems.LINUX == self.host.os["type"]: + if OperatingSystem.LINUX == self.host.os["type"]: return build_exploit_bytecode(exploit_command, LINUX_EXPLOIT_TEMPLATE_PATH) else: return build_exploit_bytecode(exploit_command, WINDOWS_EXPLOIT_TEMPLATE_PATH) diff --git a/monkey/infection_monkey/exploit/powershell.py b/monkey/infection_monkey/exploit/powershell.py index c9991b0b4..268b2835d 100644 --- a/monkey/infection_monkey/exploit/powershell.py +++ b/monkey/infection_monkey/exploit/powershell.py @@ -2,7 +2,7 @@ import logging from pathlib import Path, PurePath from typing import List, Optional -from common import OperatingSystems +from common import OperatingSystem from infection_monkey.exploit.HostExploiter import HostExploiter from infection_monkey.exploit.powershell_utils.auth_options import AuthOptions, get_auth_options from infection_monkey.exploit.powershell_utils.credentials import ( @@ -163,7 +163,7 @@ class PowerShellExploiter(HostExploiter): temp_monkey_binary_filepath.unlink() def _create_local_agent_file(self, binary_path): - agent_binary_bytes = self.agent_repository.get_agent_binary(OperatingSystems.WINDOWS) + agent_binary_bytes = self.agent_repository.get_agent_binary(OperatingSystem.WINDOWS) with open(binary_path, "wb") as f: f.write(agent_binary_bytes.getvalue()) diff --git a/monkey/infection_monkey/exploit/web_rce.py b/monkey/infection_monkey/exploit/web_rce.py index 45e4b58ee..a2a8ed2a6 100644 --- a/monkey/infection_monkey/exploit/web_rce.py +++ b/monkey/infection_monkey/exploit/web_rce.py @@ -3,7 +3,7 @@ from abc import abstractmethod from posixpath import join from typing import List, Tuple -from common import OperatingSystems +from common import OperatingSystem from common.utils.attack_utils import BITS_UPLOAD_STRING, ScanStatus from infection_monkey.exploit.HostExploiter import HostExploiter from infection_monkey.exploit.tools.http_tools import HTTPTools @@ -412,14 +412,14 @@ class WebRCE(HostExploiter): :return: Default monkey's destination path for corresponding host or False if failed. """ if not self.host.os.get("type") or ( - self.host.os["type"] != OperatingSystems.LINUX - and self.host.os["type"] != OperatingSystems.WINDOWS + self.host.os["type"] != OperatingSystem.LINUX + and self.host.os["type"] != OperatingSystem.WINDOWS ): logger.error("Target's OS was either unidentified or not supported. Aborting") return False - if self.host.os["type"] == OperatingSystems.LINUX: + if self.host.os["type"] == OperatingSystem.LINUX: return DROPPER_TARGET_PATH_LINUX - if self.host.os["type"] == OperatingSystems.WINDOWS: + if self.host.os["type"] == OperatingSystem.WINDOWS: return DROPPER_TARGET_PATH_WIN64 def get_target_url(self): diff --git a/monkey/infection_monkey/master/exploiter.py b/monkey/infection_monkey/master/exploiter.py index b74a542e7..ff21ae32b 100644 --- a/monkey/infection_monkey/master/exploiter.py +++ b/monkey/infection_monkey/master/exploiter.py @@ -7,7 +7,7 @@ from queue import Queue from threading import Event from typing import Callable, Dict, Sequence -from common import OperatingSystems +from common import OperatingSystem from common.agent_configuration.agent_sub_configurations import ( ExploitationConfiguration, PluginConfiguration, @@ -26,14 +26,14 @@ Callback = Callable[[ExploiterName, VictimHost, ExploiterResultData], None] SUPPORTED_OS = { - "HadoopExploiter": [OperatingSystems.LINUX, OperatingSystems.WINDOWS], - "Log4ShellExploiter": [OperatingSystems.LINUX, OperatingSystems.WINDOWS], - "MSSQLExploiter": [OperatingSystems.WINDOWS], - "PowerShellExploiter": [OperatingSystems.WINDOWS], - "SSHExploiter": [OperatingSystems.LINUX], - "SmbExploiter": [OperatingSystems.WINDOWS], - "WmiExploiter": [OperatingSystems.WINDOWS], - "ZerologonExploiter": [OperatingSystems.WINDOWS], + "HadoopExploiter": [OperatingSystem.LINUX, OperatingSystem.WINDOWS], + "Log4ShellExploiter": [OperatingSystem.LINUX, OperatingSystem.WINDOWS], + "MSSQLExploiter": [OperatingSystem.WINDOWS], + "PowerShellExploiter": [OperatingSystem.WINDOWS], + "SSHExploiter": [OperatingSystem.LINUX], + "SmbExploiter": [OperatingSystem.WINDOWS], + "WmiExploiter": [OperatingSystem.WINDOWS], + "ZerologonExploiter": [OperatingSystem.WINDOWS], } diff --git a/monkey/infection_monkey/model/host.py b/monkey/infection_monkey/model/host.py index 167bef246..bcfcf2f16 100644 --- a/monkey/infection_monkey/model/host.py +++ b/monkey/infection_monkey/model/host.py @@ -1,6 +1,6 @@ from typing import Optional -from common import OperatingSystems +from common import OperatingSystem class VictimHost(object): @@ -17,7 +17,7 @@ class VictimHost(object): return self.__dict__ def is_windows(self) -> bool: - return OperatingSystems.WINDOWS == self.os["type"] + return OperatingSystem.WINDOWS == self.os["type"] def __hash__(self): return hash(self.ip_addr) diff --git a/monkey/infection_monkey/network_scanning/ping_scanner.py b/monkey/infection_monkey/network_scanning/ping_scanner.py index cddf4bdd4..8b30191b4 100644 --- a/monkey/infection_monkey/network_scanning/ping_scanner.py +++ b/monkey/infection_monkey/network_scanning/ping_scanner.py @@ -5,7 +5,7 @@ import re import subprocess import sys -from common import OperatingSystems +from common import OperatingSystem from infection_monkey.i_puppet import PingScanData from infection_monkey.utils.environment import is_windows_os @@ -80,9 +80,9 @@ def _process_ping_command_output(ping_command_output: str) -> PingScanData: operating_system = None if ttl <= LINUX_TTL: - operating_system = OperatingSystems.LINUX + operating_system = OperatingSystem.LINUX else: # as far we we know, could also be OSX/BSD, but lets handle that when it comes up. - operating_system = OperatingSystems.WINDOWS + operating_system = OperatingSystem.WINDOWS return PingScanData(True, operating_system) diff --git a/monkey/infection_monkey/network_scanning/smb_fingerprinter.py b/monkey/infection_monkey/network_scanning/smb_fingerprinter.py index 438e13db0..b7293bf73 100644 --- a/monkey/infection_monkey/network_scanning/smb_fingerprinter.py +++ b/monkey/infection_monkey/network_scanning/smb_fingerprinter.py @@ -5,7 +5,7 @@ from typing import Dict from odict import odict -from common import OperatingSystems +from common import OperatingSystem from infection_monkey.i_puppet import ( FingerprintData, IFingerprinter, @@ -194,9 +194,9 @@ class SMBFingerprinter(IFingerprinter): logger.debug(f'os_version: "{os_version}", service_client: "{service_client}"') if os_version.lower() != "unix": - os_type = OperatingSystems.WINDOWS + os_type = OperatingSystem.WINDOWS else: - os_type = OperatingSystems.LINUX + os_type = OperatingSystem.LINUX smb_service["name"] = service_client diff --git a/monkey/infection_monkey/network_scanning/ssh_fingerprinter.py b/monkey/infection_monkey/network_scanning/ssh_fingerprinter.py index 86eb8f420..e17271817 100644 --- a/monkey/infection_monkey/network_scanning/ssh_fingerprinter.py +++ b/monkey/infection_monkey/network_scanning/ssh_fingerprinter.py @@ -1,7 +1,7 @@ import re from typing import Dict, Optional, Tuple -from common import OperatingSystems +from common import OperatingSystem from infection_monkey.i_puppet import FingerprintData, IFingerprinter, PingScanData, PortScanData SSH_REGEX = r"SSH-\d\.\d-OpenSSH" @@ -41,6 +41,6 @@ class SSHFingerprinter(IFingerprinter): for dist in LINUX_DIST_SSH: if banner.lower().find(dist) != -1: os_version = banner.split(" ").pop().strip() - os = OperatingSystems.LINUX + os = OperatingSystem.LINUX return os, os_version diff --git a/monkey/infection_monkey/telemetry/telem_encoder.py b/monkey/infection_monkey/telemetry/telem_encoder.py index 019569107..3abe77439 100644 --- a/monkey/infection_monkey/telemetry/telem_encoder.py +++ b/monkey/infection_monkey/telemetry/telem_encoder.py @@ -1,10 +1,10 @@ import json -from common import OperatingSystems +from common import OperatingSystem class TelemetryJSONEncoder(json.JSONEncoder): def default(self, obj): - if isinstance(obj, OperatingSystems): + if isinstance(obj, OperatingSystem): return obj.name return json.JSONEncoder.default(self, obj) diff --git a/monkey/monkey_island/cc/models/machine.py b/monkey/monkey_island/cc/models/machine.py index 1624ff522..1c4c4009c 100644 --- a/monkey/monkey_island/cc/models/machine.py +++ b/monkey/monkey_island/cc/models/machine.py @@ -3,7 +3,7 @@ from typing import Optional, Sequence from pydantic import Field, PositiveInt, validator -from common import OperatingSystems +from common import OperatingSystem from .base_models import MutableBaseModel from .transforms import make_immutable_sequence @@ -15,7 +15,7 @@ class Machine(MutableBaseModel): id: MachineID = Field(..., allow_mutation=False) hardware_id: Optional[PositiveInt] network_interfaces: Sequence[IPv4Interface] - operating_system: OperatingSystems + operating_system: OperatingSystem operating_system_version: str hostname: str diff --git a/monkey/tests/unit_tests/infection_monkey/exploit/tools/test_helpers.py b/monkey/tests/unit_tests/infection_monkey/exploit/tools/test_helpers.py index e99385c5b..cc06f3ed3 100644 --- a/monkey/tests/unit_tests/infection_monkey/exploit/tools/test_helpers.py +++ b/monkey/tests/unit_tests/infection_monkey/exploit/tools/test_helpers.py @@ -2,7 +2,7 @@ from unittest.mock import Mock import pytest -from common import OperatingSystems +from common import OperatingSystem from infection_monkey.exploit.tools.helpers import ( AGENT_BINARY_PATH_LINUX, AGENT_BINARY_PATH_WIN64, @@ -14,15 +14,15 @@ from infection_monkey.exploit.tools.helpers import ( def _get_host(os): host = Mock() host.os = {"type": os} - host.is_windows = lambda: os == OperatingSystems.WINDOWS + host.is_windows = lambda: os == OperatingSystem.WINDOWS return host @pytest.mark.parametrize( "os, path", [ - (OperatingSystems.LINUX, AGENT_BINARY_PATH_LINUX), - (OperatingSystems.WINDOWS, AGENT_BINARY_PATH_WIN64), + (OperatingSystem.LINUX, AGENT_BINARY_PATH_LINUX), + (OperatingSystem.WINDOWS, AGENT_BINARY_PATH_WIN64), ], ) def test_get_agent_dst_path(os, path): @@ -35,7 +35,7 @@ def test_get_agent_dst_path(os, path): def test_get_agent_dst_path_randomness(): - host = _get_host(OperatingSystems.WINDOWS) + host = _get_host(OperatingSystem.WINDOWS) path1 = get_agent_dst_path(host) path2 = get_agent_dst_path(host) @@ -44,7 +44,7 @@ def test_get_agent_dst_path_randomness(): def test_get_agent_dst_path_str_place(): - host = _get_host(OperatingSystems.WINDOWS) + host = _get_host(OperatingSystem.WINDOWS) rand_path = get_agent_dst_path(host) diff --git a/monkey/tests/unit_tests/infection_monkey/master/mock_puppet.py b/monkey/tests/unit_tests/infection_monkey/master/mock_puppet.py index 624d79471..67b4fa30f 100644 --- a/monkey/tests/unit_tests/infection_monkey/master/mock_puppet.py +++ b/monkey/tests/unit_tests/infection_monkey/master/mock_puppet.py @@ -2,7 +2,7 @@ import logging import threading from typing import Dict, Iterable, List, Sequence -from common import OperatingSystems +from common import OperatingSystem from common.credentials import Credentials, LMHash, Password, SSHKeypair, Username from infection_monkey.i_puppet import ( ExploiterResultData, @@ -186,19 +186,19 @@ class MockPuppet(IPuppet): successful_exploiters = { DOT_1: { "ZerologonExploiter": ExploiterResultData( - False, False, False, OperatingSystems.WINDOWS, {}, [], "Zerologon failed" + False, False, False, OperatingSystem.WINDOWS, {}, [], "Zerologon failed" ), "SSHExploiter": ExploiterResultData( False, False, False, - OperatingSystems.LINUX, + OperatingSystem.LINUX, info_ssh, attempts, "Failed exploiting", ), "WmiExploiter": ExploiterResultData( - True, True, False, OperatingSystems.WINDOWS, info_wmi, attempts, None + True, True, False, OperatingSystem.WINDOWS, info_wmi, attempts, None ), }, DOT_3: { @@ -206,7 +206,7 @@ class MockPuppet(IPuppet): False, False, False, - OperatingSystems.WINDOWS, + OperatingSystem.WINDOWS, info_wmi, attempts, "PowerShell Exploiter Failed", @@ -215,13 +215,13 @@ class MockPuppet(IPuppet): False, False, False, - OperatingSystems.LINUX, + OperatingSystem.LINUX, info_ssh, attempts, "Failed exploiting", ), "ZerologonExploiter": ExploiterResultData( - True, False, False, OperatingSystems.WINDOWS, {}, [], None + True, False, False, OperatingSystem.WINDOWS, {}, [], None ), }, } @@ -233,7 +233,7 @@ class MockPuppet(IPuppet): False, False, False, - OperatingSystems.LINUX, + OperatingSystem.LINUX, {}, [], f"{name} failed for host {host}", diff --git a/monkey/tests/unit_tests/infection_monkey/master/test_exploiter.py b/monkey/tests/unit_tests/infection_monkey/master/test_exploiter.py index 90f568eac..45288fbb7 100644 --- a/monkey/tests/unit_tests/infection_monkey/master/test_exploiter.py +++ b/monkey/tests/unit_tests/infection_monkey/master/test_exploiter.py @@ -7,7 +7,7 @@ from unittest.mock import MagicMock import pytest from tests.unit_tests.infection_monkey.master.mock_puppet import MockPuppet -from common import OperatingSystems +from common import OperatingSystem from common.agent_configuration.agent_sub_configurations import ( ExploitationConfiguration, PluginConfiguration, @@ -165,7 +165,7 @@ def test_exploiter_raises_exception(callback, hosts, hosts_to_exploit, run_explo def test_windows_exploiters_run_on_windows_host(callback, hosts, hosts_to_exploit, run_exploiters): host = VictimHost("10.0.0.1") - host.os["type"] = OperatingSystems.WINDOWS + host.os["type"] = OperatingSystem.WINDOWS q = enqueue_hosts([host]) run_exploiters(MockPuppet(), 1, q) @@ -177,7 +177,7 @@ def test_windows_exploiters_run_on_windows_host(callback, hosts, hosts_to_exploi def test_linux_exploiters_run_on_linux_host(callback, hosts, hosts_to_exploit, run_exploiters): host = VictimHost("10.0.0.1") - host.os["type"] = OperatingSystems.LINUX + host.os["type"] = OperatingSystem.LINUX q = enqueue_hosts([host]) run_exploiters(MockPuppet(), 1, q) diff --git a/monkey/tests/unit_tests/infection_monkey/network_scanning/test_ping_scanner.py b/monkey/tests/unit_tests/infection_monkey/network_scanning/test_ping_scanner.py index 9e2891e3f..9fa26456e 100644 --- a/monkey/tests/unit_tests/infection_monkey/network_scanning/test_ping_scanner.py +++ b/monkey/tests/unit_tests/infection_monkey/network_scanning/test_ping_scanner.py @@ -4,7 +4,7 @@ from unittest.mock import MagicMock import pytest -from common import OperatingSystems +from common import OperatingSystem from infection_monkey.network_scanning import ping from infection_monkey.network_scanning.ping_scanner import EMPTY_PING_SCAN @@ -92,7 +92,7 @@ def test_linux_ping_success(patch_subprocess_running_ping_with_ping_output): result = ping("192.168.1.1", 1.0) assert result.response_received - assert result.os == OperatingSystems.LINUX + assert result.os == OperatingSystem.LINUX @pytest.mark.usefixtures("set_os_linux") @@ -110,7 +110,7 @@ def test_windows_ping_success(patch_subprocess_running_ping_with_ping_output): result = ping("192.168.1.1", 1.0) assert result.response_received - assert result.os == OperatingSystems.WINDOWS + assert result.os == OperatingSystem.WINDOWS @pytest.mark.usefixtures("set_os_windows") diff --git a/monkey/tests/unit_tests/infection_monkey/network_scanning/test_ssh_fingerprinter.py b/monkey/tests/unit_tests/infection_monkey/network_scanning/test_ssh_fingerprinter.py index 09d0705ef..969104e57 100644 --- a/monkey/tests/unit_tests/infection_monkey/network_scanning/test_ssh_fingerprinter.py +++ b/monkey/tests/unit_tests/infection_monkey/network_scanning/test_ssh_fingerprinter.py @@ -1,6 +1,6 @@ import pytest -from common import OperatingSystems +from common import OperatingSystem from infection_monkey.i_puppet import FingerprintData, PortScanData, PortStatus from infection_monkey.network_scanning.ssh_fingerprinter import SSHFingerprinter @@ -57,7 +57,7 @@ def test_ssh_os(ssh_fingerprinter): results = ssh_fingerprinter.get_host_fingerprint("127.0.0.1", None, port_scan_data, None) assert results == FingerprintData( - OperatingSystems.LINUX, + OperatingSystem.LINUX, "Ubuntu-4ubuntu0.2", { "tcp-22": { @@ -79,7 +79,7 @@ def test_multiple_os(ssh_fingerprinter): results = ssh_fingerprinter.get_host_fingerprint("127.0.0.1", None, port_scan_data, None) assert results == FingerprintData( - OperatingSystems.LINUX, + OperatingSystem.LINUX, "Debian", { "tcp-22": { diff --git a/monkey/tests/unit_tests/monkey_island/cc/models/test_machine.py b/monkey/tests/unit_tests/monkey_island/cc/models/test_machine.py index a6d152dc0..50cdaa61d 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/models/test_machine.py +++ b/monkey/tests/unit_tests/monkey_island/cc/models/test_machine.py @@ -5,7 +5,7 @@ from typing import MutableSequence import pytest -from common import OperatingSystems +from common import OperatingSystem from monkey_island.cc.models import Machine MACHINE_OBJECT_DICT = MappingProxyType( @@ -13,7 +13,7 @@ MACHINE_OBJECT_DICT = MappingProxyType( "id": 1, "hardware_id": uuid.getnode(), "network_interfaces": [IPv4Interface("10.0.0.1/24"), IPv4Interface("192.168.5.32/16")], - "operating_system": OperatingSystems.WINDOWS, + "operating_system": OperatingSystem.WINDOWS, "operating_system_version": "eXtra Problems", "hostname": "my.host", } @@ -136,7 +136,7 @@ def test_operating_system_set_valid_value(): m = Machine(**MACHINE_OBJECT_DICT) # Raises exception_on_failure - m.operating_system = OperatingSystems.LINUX + m.operating_system = OperatingSystem.LINUX def test_operating_system_set_invalid_value():