From 6ef365d3e58a7d6b78fd8cdaec6d1bd049556698 Mon Sep 17 00:00:00 2001 From: vakaris_zilius Date: Wed, 6 Apr 2022 07:09:10 +0000 Subject: [PATCH] Agent: Improve ssh exception handling in sshexec.py --- monkey/infection_monkey/exploit/sshexec.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/monkey/infection_monkey/exploit/sshexec.py b/monkey/infection_monkey/exploit/sshexec.py index dab29ae03..2b13b719e 100644 --- a/monkey/infection_monkey/exploit/sshexec.py +++ b/monkey/infection_monkey/exploit/sshexec.py @@ -86,13 +86,15 @@ class SSHExploiter(HostExploiter): self.exploit_result.exploitation_success = True self.report_login_attempt(True, user, ssh_key=ssh_string) return ssh - except Exception: + except paramiko.AuthenticationException as err: ssh.close() - logger.debug( - "Error logging into victim %r with %s" " private key", self.host, ssh_string + logger.info( + f"Failed logging into victim {self.host} with {ssh_string} private key: {err}", ) self.report_login_attempt(False, user, ssh_key=ssh_string) continue + except Exception as err: + logger.error(f"Unknown error while attempting to login with ssh key: {err}") raise FailedExploitationError def exploit_with_login_creds(self, port) -> paramiko.SSHClient: @@ -130,16 +132,18 @@ class SSHExploiter(HostExploiter): self.report_login_attempt(True, user, current_password) return ssh - except Exception as exc: + except paramiko.AuthenticationException as err: logger.debug( - "Error logging into victim %r with user" " %s: (%s)", + "Failed logging into victim %r with user" " %s: (%s)", self.host, user, - exc, + err, ) self.report_login_attempt(False, user, current_password) ssh.close() continue + except Exception as err: + logger.error(f"Unknown error occurred while trying to login to ssh: {err}") raise FailedExploitationError def _exploit_host(self) -> ExploiterResultData: