Agent: Improve ssh exception handling in sshexec.py

This commit is contained in:
vakaris_zilius 2022-04-06 07:09:10 +00:00
parent 7fc49196d7
commit 6ef365d3e5
1 changed files with 10 additions and 6 deletions

View File

@ -86,13 +86,15 @@ class SSHExploiter(HostExploiter):
self.exploit_result.exploitation_success = True self.exploit_result.exploitation_success = True
self.report_login_attempt(True, user, ssh_key=ssh_string) self.report_login_attempt(True, user, ssh_key=ssh_string)
return ssh return ssh
except Exception: except paramiko.AuthenticationException as err:
ssh.close() ssh.close()
logger.debug( logger.info(
"Error logging into victim %r with %s" " private key", self.host, ssh_string f"Failed logging into victim {self.host} with {ssh_string} private key: {err}",
) )
self.report_login_attempt(False, user, ssh_key=ssh_string) self.report_login_attempt(False, user, ssh_key=ssh_string)
continue continue
except Exception as err:
logger.error(f"Unknown error while attempting to login with ssh key: {err}")
raise FailedExploitationError raise FailedExploitationError
def exploit_with_login_creds(self, port) -> paramiko.SSHClient: def exploit_with_login_creds(self, port) -> paramiko.SSHClient:
@ -130,16 +132,18 @@ class SSHExploiter(HostExploiter):
self.report_login_attempt(True, user, current_password) self.report_login_attempt(True, user, current_password)
return ssh return ssh
except Exception as exc: except paramiko.AuthenticationException as err:
logger.debug( logger.debug(
"Error logging into victim %r with user" " %s: (%s)", "Failed logging into victim %r with user" " %s: (%s)",
self.host, self.host,
user, user,
exc, err,
) )
self.report_login_attempt(False, user, current_password) self.report_login_attempt(False, user, current_password)
ssh.close() ssh.close()
continue continue
except Exception as err:
logger.error(f"Unknown error occurred while trying to login to ssh: {err}")
raise FailedExploitationError raise FailedExploitationError
def _exploit_host(self) -> ExploiterResultData: def _exploit_host(self) -> ExploiterResultData: