From 7c504d220d3c0df54172712a190a3aa6350cc2d9 Mon Sep 17 00:00:00 2001 From: vakaris_zilius Date: Wed, 23 Mar 2022 13:11:54 +0000 Subject: [PATCH] Agent: Upload binary with random string when using powershell --- monkey/infection_monkey/exploit/powershell.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/monkey/infection_monkey/exploit/powershell.py b/monkey/infection_monkey/exploit/powershell.py index 1c496fc68..1c3536821 100644 --- a/monkey/infection_monkey/exploit/powershell.py +++ b/monkey/infection_monkey/exploit/powershell.py @@ -1,5 +1,6 @@ import logging import os +from pathlib import Path from typing import List, Optional from infection_monkey.exploit.HostExploiter import HostExploiter @@ -19,7 +20,7 @@ from infection_monkey.exploit.powershell_utils.powershell_client import ( IPowerShellClient, PowerShellClient, ) -from infection_monkey.exploit.tools.helpers import get_random_file_suffix +from infection_monkey.exploit.tools.helpers import get_agent_dest_path, 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 @@ -170,7 +171,7 @@ class PowerShellExploiter(HostExploiter): raise ValueError(f"Unknown secret type {credentials.secret_type}") def _execute_monkey_agent_on_victim(self): - monkey_path_on_victim = self.options["dropper_target_path_win_64"] + monkey_path_on_victim = get_agent_dest_path(self.host, self.options) self._copy_monkey_binary_to_victim(monkey_path_on_victim) logger.info("Successfully copied the monkey binary to the victim.") @@ -182,7 +183,7 @@ class PowerShellExploiter(HostExploiter): f"Failed to execute the agent binary on the victim: {ex}" ) - def _copy_monkey_binary_to_victim(self, monkey_path_on_victim): + def _copy_monkey_binary_to_victim(self, monkey_path_on_victim: Path): temp_monkey_binary_filepath = f"monkey_temp_bin_{get_random_file_suffix()}" @@ -190,7 +191,7 @@ class PowerShellExploiter(HostExploiter): 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, str(monkey_path_on_victim)) except Exception as ex: raise RemoteAgentCopyError(f"Failed to copy the agent binary to the victim: {ex}") finally: