From b31eb885f07bb30525140f623f53f06843abc52b Mon Sep 17 00:00:00 2001 From: Kekoa Kaaikala Date: Tue, 4 Oct 2022 21:26:55 +0000 Subject: [PATCH] Agent: Extract method _get_victim_os --- monkey/infection_monkey/exploit/sshexec.py | 44 ++++++++++++---------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/monkey/infection_monkey/exploit/sshexec.py b/monkey/infection_monkey/exploit/sshexec.py index 1b53ba7db..f6f703763 100644 --- a/monkey/infection_monkey/exploit/sshexec.py +++ b/monkey/infection_monkey/exploit/sshexec.py @@ -210,26 +210,8 @@ class SSHExploiter(HostExploiter): self._set_interrupted() return self.exploit_result - if not self.host.os.get("type"): - try: - _, stdout, _ = ssh.exec_command("uname -o", timeout=SSH_EXEC_TIMEOUT) - uname_os = stdout.read().lower().strip().decode() - if "linux" in uname_os: - self.exploit_result.os = OperatingSystem.LINUX - self.host.os["type"] = OperatingSystem.LINUX - else: - self.exploit_result.error_message = f"SSH Skipping unknown os: {uname_os}" - - if not uname_os: - logger.error(self.exploit_result.error_message) - return self.exploit_result - except Exception as exc: - self.exploit_result.error_message = ( - f"Error running uname os command on victim {self.host}: ({exc})" - ) - - logger.error(self.exploit_result.error_message) - return self.exploit_result + if not self.host.os.get("type") and not self._get_victim_os(ssh): + return self.exploit_result agent_binary_file_object = self.agent_binary_repository.get_agent_binary( self.exploit_result.os @@ -316,6 +298,28 @@ class SSHExploiter(HostExploiter): logger.error(self.exploit_result.error_message) return self.exploit_result + def _get_victim_os(self, ssh: paramiko.SSHClient) -> bool: + try: + _, stdout, _ = ssh.exec_command("uname -o", timeout=SSH_EXEC_TIMEOUT) + uname_os = stdout.read().lower().strip().decode() + if "linux" in uname_os: + self.exploit_result.os = OperatingSystem.LINUX + self.host.os["type"] = OperatingSystem.LINUX + else: + self.exploit_result.error_message = f"SSH Skipping unknown os: {uname_os}" + + if not uname_os: + logger.error(self.exploit_result.error_message) + return False + except Exception as exc: + self.exploit_result.error_message = ( + f"Error running uname os command on victim {self.host}: ({exc})" + ) + + logger.error(self.exploit_result.error_message) + return False + return True + def _upload_agent_binary( self, ssh: paramiko.SSHClient,