diff --git a/monkey/infection_monkey/exploit/powershell.py b/monkey/infection_monkey/exploit/powershell.py index c9835566e..9e3d3d5dc 100644 --- a/monkey/infection_monkey/exploit/powershell.py +++ b/monkey/infection_monkey/exploit/powershell.py @@ -144,14 +144,18 @@ class PowerShellExploiter(HostExploiter): return True def _copy_monkey_binary_to_victim(self, monkey_path_on_victim) -> bool: - self._write_virtual_file_to_local_path() + try: + self._write_virtual_file_to_local_path() - logger.info(f"Attempting to copy the monkey agent binary to {self.host.ip_addr}") - is_monkey_copy_successful = self._client.copy_file( - TEMP_MONKEY_BINARY_FILEPATH, monkey_path_on_victim - ) - - os.remove(TEMP_MONKEY_BINARY_FILEPATH) + logger.info(f"Attempting to copy the monkey agent binary to {self.host.ip_addr}") + is_monkey_copy_successful = self._client.copy_file( + TEMP_MONKEY_BINARY_FILEPATH, monkey_path_on_victim + ) + except Exception as ex: + raise ex + finally: + if os.path.isfile(TEMP_MONKEY_BINARY_FILEPATH): + os.remove(TEMP_MONKEY_BINARY_FILEPATH) return is_monkey_copy_successful