From d3fc8338137fd35cc8aed601180818b9c8cd4a56 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 23 Mar 2022 14:25:28 -0400 Subject: [PATCH] Agent: Use Paths in IPowerShellClient.copy_file() --- monkey/infection_monkey/exploit/powershell.py | 7 +++---- .../exploit/powershell_utils/powershell_client.py | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/monkey/infection_monkey/exploit/powershell.py b/monkey/infection_monkey/exploit/powershell.py index 12974aae5..8bdf7e571 100644 --- a/monkey/infection_monkey/exploit/powershell.py +++ b/monkey/infection_monkey/exploit/powershell.py @@ -1,5 +1,4 @@ import logging -import os from pathlib import Path from typing import List, Optional @@ -185,7 +184,7 @@ class PowerShellExploiter(HostExploiter): def _copy_monkey_binary_to_victim(self, monkey_path_on_victim: Path): - temp_monkey_binary_filepath = f"monkey_temp_bin_{get_random_file_suffix()}" + temp_monkey_binary_filepath = Path(f"./monkey_temp_bin_{get_random_file_suffix()}") self._create_local_agent_file(temp_monkey_binary_filepath) @@ -195,8 +194,8 @@ class PowerShellExploiter(HostExploiter): 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 temp_monkey_binary_filepath.is_file(): + temp_monkey_binary_filepath.unlink() 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/powershell_utils/powershell_client.py b/monkey/infection_monkey/exploit/powershell_utils/powershell_client.py index b1fa000c7..70e82bb66 100644 --- a/monkey/infection_monkey/exploit/powershell_utils/powershell_client.py +++ b/monkey/infection_monkey/exploit/powershell_utils/powershell_client.py @@ -64,7 +64,7 @@ class IPowerShellClient(Protocol, metaclass=abc.ABCMeta): pass @abc.abstractmethod - def copy_file(self, src: str, dest: Path) -> bool: + def copy_file(self, src: Path, dest: Path) -> bool: pass @abc.abstractmethod @@ -102,10 +102,9 @@ class PowerShellClient(IPowerShellClient): output, _, _ = self._client.execute_cmd(cmd) return output - def copy_file(self, src: str, dest: Path): - dest = str(dest) + def copy_file(self, src: Path, dest: Path): try: - self._client.copy(src, dest) + self._client.copy(str(src), str(dest)) logger.debug(f"Successfully copied {src} to {dest} on {self._ip_addr}") except Exception as ex: logger.error(f"Failed to copy {src} to {dest} on {self._ip_addr}: {ex}")