-
-
Notifications
You must be signed in to change notification settings - Fork 952
Fix SSH brute-force success detection and error handling #1184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,57 @@ | ||
| import logging | ||
|
|
||
| from paramiko import SSHClient, AutoAddPolicy | ||
| import socket | ||
| from paramiko import SSHClient, AutoAddPolicy, AuthenticationException, SSHException | ||
| from paramiko.auth_strategy import NoneAuth, Password | ||
|
|
||
| from nettacker.core.lib.base import BaseEngine, BaseLibrary | ||
|
|
||
| logging.getLogger("paramiko.transport").disabled = True | ||
|
|
||
|
|
||
| class SshLibrary(BaseLibrary): | ||
| client = SSHClient | ||
|
|
||
| def brute_force(self, *args, **kwargs): | ||
| host = kwargs["host"] | ||
| port = kwargs["port"] | ||
| username = kwargs["username"] | ||
| password = kwargs["password"] | ||
| password = kwargs.get("password") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't change what's not broken |
||
| timeout = kwargs.get("timeout", 3) | ||
|
|
||
| connection = self.client() | ||
| connection.set_missing_host_key_policy(AutoAddPolicy()) | ||
| connection.connect( | ||
| **{ | ||
| "hostname": host, | ||
|
|
||
| try: | ||
| connection.connect( | ||
| hostname=host, | ||
| port=port, | ||
| auth_strategy=Password(username=username, password_getter=lambda: password) if password else NoneAuth(username=username), | ||
| timeout=timeout, | ||
| look_for_keys=False, | ||
| allow_agent=False, | ||
| banner_timeout=timeout, | ||
| ) | ||
| transport = connection.get_transport() | ||
| if not transport or not transport.is_active(): | ||
| return {} | ||
| return { | ||
| "host": host, | ||
| "port": port, | ||
| "auth_strategy": Password(username=username, password_getter=lambda: password) | ||
| if password | ||
| else NoneAuth(username=username), | ||
| "username": username, | ||
| "password": password if password else "", | ||
| } | ||
| ) | ||
|
|
||
| return { | ||
| "host": host, | ||
| "port": port, | ||
| "username": username, | ||
| "password": password, | ||
| } | ||
|
|
||
| except AuthenticationException: | ||
| return {} | ||
| except SSHException: | ||
| return {} | ||
| except (socket.timeout, socket.error, ConnectionRefusedError, OSError): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to use both OSError and socket.error. Python3+ they are basically aliases. Also, you really aren't doing anything special by handling them separately. |
||
| return {} | ||
| except Exception: | ||
| return {} | ||
| finally: | ||
| try: | ||
| connection.close() | ||
| except Exception: | ||
| pass | ||
|
|
||
| class SshEngine(BaseEngine): | ||
| library = SshLibrary | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you "probably" don't need this here... look at the other comment below