Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print "[*] Testing SSH service on {0}...".format(host)
for credential in credentials:
try:
user = str(credential[0])
password = str(credential[1])
# This works for up-to-date SSH servers:
# ssh_conn = pxssh.pxssh()
# Old SSH servers running "ssh-dss" needs this option instead:
ssh_conn = pxssh.pxssh(options={"StrictHostKeyChecking": "no", "HostKeyAlgorithms": "+ssh-dss"})
ssh_conn.login(host, user, password)
self.log_results(host, port, user, password, service)
ssh_conn.logout()
ssh_conn.close()
except pxssh.EOF as EOF_error:
self.log_service_error(host, port, service, EOF_error)
except pxssh.ExceptionPxssh as error:
self.log_service_error(host, port, service, error)
except KeyboardInterrupt:
exit(0)
except threading.ThreadError as thread_error:
self.log_error(service, thread_error)
except KeyboardInterrupt:
exit(0)
finally:
self.CONNECTION_LOCK.release()
logging.info('Checking {0}:{1} on {2} for {3} service.'.format(user, password, host, service))
try:
# This works for up-to-date SSH servers:
# ssh_conn = pxssh.pxssh()
# Old SSH servers running "ssh-dss" needs this option instead:
ssh_conn = pxssh.pxssh(options={"StrictHostKeyChecking": "no", "HostKeyAlgorithms": "+ssh-dss"})
ssh_conn.login(server=host, username=user, password=password)
self.log_results(host, port, user, password, service)
vulnerable_host.put_credentials(service, port, user, password)
ssh_conn.logout()
ssh_conn.close()
return True
except pxssh.EOF as EOF_error:
logging.exception("{0}\t{1}\t{2}\t{3}".format(host, port, service, EOF_error))
return False
except pxssh.ExceptionPxssh as error:
logging.exception("{0}\t{1}\t{2}\t{3}".format(host, port, service, error))
return False
except KeyboardInterrupt:
exit(0)
self.CONNECTION_LOCK.release()
def _get_update(self):
try:
if not self.connected:
self._connect()
# If we still aren't connected at this point
# don't try to send anything to the AP.
if not self.connected:
return None
self.ssh.sendline(UNIFI_COMMAND)
self.ssh.prompt()
return self.ssh.before
except pxssh.ExceptionPxssh as err:
_LOGGER.error("Unexpected SSH error: %s", str(err))
self._disconnect()
return None
except (AssertionError, exceptions.EOF) as err:
_LOGGER.error("Connection to AP unavailable: %s", str(err))
self._disconnect()
return None
def connect_host(hostname, p):
self.hostList.append(hostname)
try:
# The sync_multiplier value is passed onto pexpect.pxssh which is used to determine timeout
# values for prompt verification after an ssh connection is established.
p.login(hostname, self.userName, sync_multiplier=sync_multiplier)
p.x_peer = hostname
p.x_pid = p.pid
good_list.append(p)
if self.verbose:
with print_lock:
print '[INFO] login %s' % hostname
except Exception as e:
with print_lock:
print '[ERROR] unable to login to %s' % hostname
if type(e) is pxssh.ExceptionPxssh:
print e
elif type(e) is pxssh.EOF:
print 'Could not acquire connection.'
else:
print 'hint: use gpssh-exkeys to setup public-key authentication between hosts'
pass
elif i==6: # Connection closed by remote host
self.close()
raise ExceptionPxssh('connection closed')
else: # Unexpected
self.close()
raise ExceptionPxssh('unexpected login response')
if not self.sync_original_prompt(sync_multiplier):
self.close()
raise ExceptionPxssh('could not synchronize with original prompt')
# We appear to be in.
# set shell prompt to something unique.
if auto_prompt_reset:
if not self.set_unique_prompt():
self.close()
raise ExceptionPxssh('could not set shell prompt '
'(received: %r, expected: %r).' % (
self.before, self.PROMPT,))
return True
self.close()
raise ExceptionPxssh('permission denied')
elif i==4: # terminal type again? WTF?
self.close()
raise ExceptionPxssh('Weird error. Got "terminal type" prompt twice.')
elif i==5: # Timeout
#This is tricky... I presume that we are at the command-line prompt.
#It may be that the shell prompt was so weird that we couldn't match
#it. Or it may be that we couldn't log in for some other reason. I
#can't be sure, but it's safe to guess that we did login because if
#I presume wrong and we are not logged in then this should be caught
#later when I try to set the shell prompt.
pass
elif i==6: # Connection closed by remote host
self.close()
raise ExceptionPxssh('connection closed')
else: # Unexpected
self.close()
raise ExceptionPxssh('unexpected login response')
if not self.sync_original_prompt(sync_multiplier):
self.close()
raise ExceptionPxssh('could not synchronize with original prompt')
# We appear to be in.
# set shell prompt to something unique.
if auto_prompt_reset:
if not self.set_unique_prompt():
self.close()
raise ExceptionPxssh('could not set shell prompt '
'(received: %r, expected: %r).' % (
self.before, self.PROMPT,))
return True
self.close()
raise ExceptionPxssh('Weird error. Got "terminal type" prompt twice.')
elif i==5: # Timeout
#This is tricky... I presume that we are at the command-line prompt.
#It may be that the shell prompt was so weird that we couldn't match
#it. Or it may be that we couldn't log in for some other reason. I
#can't be sure, but it's safe to guess that we did login because if
#I presume wrong and we are not logged in then this should be caught
#later when I try to set the shell prompt.
pass
elif i==6: # Connection closed by remote host
self.close()
raise ExceptionPxssh('connection closed')
else: # Unexpected
self.close()
raise ExceptionPxssh('unexpected login response')
if not self.sync_original_prompt(sync_multiplier):
self.close()
raise ExceptionPxssh('could not synchronize with original prompt')
# We appear to be in.
# set shell prompt to something unique.
if auto_prompt_reset:
if not self.set_unique_prompt():
self.close()
raise ExceptionPxssh('could not set shell prompt '
'(received: %r, expected: %r).' % (
self.before, self.PROMPT,))
return True
if not self._connected:
return None
self._ssh.sendline(self._command)
self._ssh.prompt()
value = self._ssh.before.decode("utf-8")
if self._value_template is not None:
self._state = self._value_template.render_with_possible_json_value(
value, STATE_UNKNOWN)
else:
self._state = value
_LOGGER.debug(self._state)
except pxssh.ExceptionPxssh as err:
_LOGGER.error("Unexpected SSH error: %s", str(err))
self._disconnect()
return None
except (AssertionError, exceptions.EOF) as err:
_LOGGER.error("Connection unavailable: %s", str(err))
self._disconnect()
return None
#This is tricky... I presume that we are at the command-line prompt.
#It may be that the shell prompt was so weird that we couldn't match
#it. Or it may be that we couldn't log in for some other reason. I
#can't be sure, but it's safe to guess that we did login because if
#I presume wrong and we are not logged in then this should be caught
#later when I try to set the shell prompt.
pass
elif i==6: # Connection closed by remote host
self.close()
raise ExceptionPxssh('connection closed')
else: # Unexpected
self.close()
raise ExceptionPxssh('unexpected login response')
if not self.sync_original_prompt(sync_multiplier):
self.close()
raise ExceptionPxssh('could not synchronize with original prompt')
# We appear to be in.
# set shell prompt to something unique.
if auto_prompt_reset:
if not self.set_unique_prompt():
self.close()
raise ExceptionPxssh('could not set shell prompt '
'(received: %r, expected: %r).' % (
self.before, self.PROMPT,))
return True