diff --git a/monkey/infection_monkey/exploit/powershell.py b/monkey/infection_monkey/exploit/powershell.py index 066dfc508..2a0d800de 100644 --- a/monkey/infection_monkey/exploit/powershell.py +++ b/monkey/infection_monkey/exploit/powershell.py @@ -20,15 +20,13 @@ from infection_monkey.exploit.powershell_utils.powershell_client import ( IPowerShellClient, PowerShellClient, ) -from infection_monkey.exploit.tools.helpers import get_monkey_depth +from infection_monkey.exploit.tools.helpers import get_monkey_depth, get_random_file_suffix from infection_monkey.model import DROPPER_ARG, RUN_MONKEY, VictimHost from infection_monkey.utils.commands import build_monkey_commandline from infection_monkey.utils.environment import is_windows_os logger = logging.getLogger(__name__) -TEMP_MONKEY_BINARY_FILEPATH = "./monkey_temp_bin" - class PowerShellRemotingDisabledError(Exception): pass @@ -177,16 +175,19 @@ class PowerShellExploiter(HostExploiter): ) def _copy_monkey_binary_to_victim(self, monkey_path_on_victim): - self._create_local_agent_file(TEMP_MONKEY_BINARY_FILEPATH) + + temp_monkey_binary_filepath = f"monkey_temp_bin_{get_random_file_suffix()}" + + self._create_local_agent_file(temp_monkey_binary_filepath) try: logger.info(f"Attempting to copy the monkey agent binary to {self.host.ip_addr}") - self._client.copy_file(TEMP_MONKEY_BINARY_FILEPATH, monkey_path_on_victim) + self._client.copy_file(temp_monkey_binary_filepath, monkey_path_on_victim) except Exception as ex: raise RemoteAgentCopyError(f"Failed to copy the agent binary to the victim: {ex}") finally: - if os.path.isfile(TEMP_MONKEY_BINARY_FILEPATH): - os.remove(TEMP_MONKEY_BINARY_FILEPATH) + if os.path.isfile(temp_monkey_binary_filepath): + os.remove(temp_monkey_binary_filepath) def _create_local_agent_file(self, binary_path): agent_binary_bytes = self.agent_repository.get_agent_binary("windows") diff --git a/monkey/infection_monkey/exploit/tools/helpers.py b/monkey/infection_monkey/exploit/tools/helpers.py index 7a72606bf..0492223ed 100644 --- a/monkey/infection_monkey/exploit/tools/helpers.py +++ b/monkey/infection_monkey/exploit/tools/helpers.py @@ -2,6 +2,8 @@ import logging from typing import Any, Mapping from infection_monkey.model import VictimHost +import string +from random import SystemRandom logger = logging.getLogger(__name__) @@ -23,6 +25,13 @@ def get_target_monkey_by_os(is_windows, is_32bit): ) +def get_random_file_suffix() -> str: + character_set = list(string.ascii_letters + string.digits + "_" + "-") + safe_random = SystemRandom() + random_string = "".join(safe_random.choices(character_set, k=8)) + return random_string + + def get_monkey_depth(): from infection_monkey.config import WormConfiguration