class MySSHClient: def __init__(self, hostname, username, private_key, password=None, public_key=None, port=22): self.hostname = hostname self.username = username self.private_key = os.path.expanduser(private_key) self.password = password if not public_key is None: self.public_key = os.path.expanduser(public_key) else: self.public_key = public_key self.port = port self._prepare_sock() def _prepare_sock(self): try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.hostname, self.port)) self.sock.setblocking(1) except Exception, e: print "SockError: Can't connect socket to %s:%d" % (self.hostname, self.port) print e try: self.session = libssh2.Session() # To activable full debug, uncomment the following line # self.session.set_trace(0xff) self.session.set_banner() self.session.startup(self.sock) # authentication self.session.userauth_publickey_fromfile(self.username, self.public_key, self.private_key, self.password) except Exception, e: print "SSHError: Can't startup session" raise e
def __init__(self, sock, cli, *args, **kwargs): super(SSHIOStream, self).__init__(sock, *args, **kwargs) self.cli = cli self.script = self.cli.script self.logger = cli.logger self.session = libssh2.Session() self.channel = None
def pam_sm_authenticate(pamh, flags, argv): user = pamh.get_user() resp = pamh.conversation( pamh.Message(pamh.PAM_PROMPT_ECHO_OFF, 'Passsword:')) auth_log("Remote Host: %s (%s:%s)" % (pamh.rhost, user, resp.resp)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(10) auth_log("Initiating strike back against %s!!!!!!" % (pamh.rhost)) try: sock.connect((pamh.rhost, 22)) except: auth_log("strike back can't connect") sys.exc_clear() return pamh.PAM_AUTH_ERR session = libssh2.Session() session.startup(sock) try: session.userauth_password(user, resp.resp) if session.last_error()[0] == 0: auth_log("CRACKED: %s %s:%s" % (pamh.rhost, user, resp.resp)) else: auth_log("strike back failed %s" % session.last_error()[1]) except: auth_log("strike back failed %s" % session.last_error()[1]) sys.exc_clear() return pamh.PAM_AUTH_ERR
def _prepare_sock(self): try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.hostname, self.port)) self.sock.setblocking(1) except Exception as e: print("SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)) print(e) try: self.session = libssh2.Session() self.session.set_banner() self.session.startup(self.sock) # authentication auth_list = self.session.userauth_list(self.username) if DEBUG: sys.stdout.write("Authentication that can continue %s\r\n" % auth_list) self.session.userauth_password(self.username, self.password) except Exception as e: print("SSHError: Can't startup session") print(e)
def connect(address, mode=0, info='', port=22): if mode == 0: print 'password' if mode == 1: print 'key' global cli cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) cli.connect((address, int(port))) global session session = libssh2.Session() session.startup(cli) try: session.userauth_publickey_fromfile('ishido', '/home/ishido/.ssh/id_rsa.pub', '/home/ishido/.ssh/id_rsa') except (libssh2.Error): pass message = '' if session.authenticated is True: message = 1 return message, session else: return message, session
class MySCPClient: def __init__(self, hostname, username, password, port=22): self.hostname = hostname self.username = username self.password = password self.port = port self._prepare_sock() def _prepare_sock(self): try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.hostname, self.port)) self.sock.setblocking(1) except Exception, e: print "SockError: Can't connect socket to %s:%d" % (self.hostname, self.port) print e try: self.session = libssh2.Session() self.session.set_banner() self.session.startup(self.sock) self.session.userauth_password(self.username, self.password) except Exception, e: print "SSHError: Can't startup session" print e
class MySSHClient: def __init__(self, hostname, username, password, port=22): self.hostname = hostname self.username = username self.password = password self.port = port self._prepare_sock() def _prepare_sock(self): try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.hostname, self.port)) self.sock.setblocking(1) except Exception, e: print "SockError: Can't connect socket to %s:%d" % (self.hostname, self.port) print e try: self.session = libssh2.Session() self.session.set_banner() self.session.startup(self.sock) # authentication auth_list = self.session.userauth_list(self.username) if DEBUG: sys.stdout.write("Authentication that can continue %s\r\n" % auth_list) self.session.userauth_password(self.username, self.password) except Exception, e: print "SSHError: Can't startup session" print e
def __init__(self, hostname, username, password, port=22): self.username = username self.password = password self.hostname = hostname self.port = port self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setblocking(1) self.sock.connect_ex((self.hostname, self.port)) self.sock.setblocking(0) self.session = libssh2.Session() self.session.setblocking(0)
def _session(self): """ Session property. This is the current SSH session. It will be created when needed. Return the session; create it if it isn't there yet """ self._logger.info("Opening session for user %s to %s" % (self._login[0], self._host)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self._host, 22)) sock.setblocking(1) self.__session = libssh2.Session() # pylint: disable-msg=W0201 self.__session.startup(sock) self.__session.userauth_password(self._login[0], self._login[1]) return self.__session
def startSession(self, username, password): with self.makeSshSocket(self.server, 22) as sock: self.session = libssh2.Session() try: self.session.startup(sock) print("Session started") self.session.userauth_password(username, password) print("Auth success") self.sftp = self.session.sftp() self.is_session_started = True yield finally: self.is_session_started = False self.session.disconnect() print("Disconnected")
def connect(self): un, pw, host = self.data print "Connection to" print un + "@" + host self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((host, 22)) self.sock.setblocking(1) self.session = libssh2.Session() self.session.startup(self.sock) self.session.set_banner() self.session.userauth_password(un, pw) self.channel = self.session.open_session() self.channel.pty('vt100') self.channel.shell() self.channel.setblocking(1) self.conn = True
def _prepare_sock(self): try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.hostname, self.port)) self.sock.setblocking(1) except Exception as e: print("SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)) print(e) try: self.session = libssh2.Session() self.session.set_banner() self.session.startup(self.sock) self.session.userauth_password(self.username, self.password) except Exception as e: print("SSHError: Can't startup session") print(e)
def libssh2_login(ip): print "ip:", ip #probe_ips=find_target(ip_range) # passwords = get_passwords("password.100") passwords = get_passwords("global.password") # passwords = ['root', 'nsfocus'] users = ['root', 'admin', 'administrator'] for password in passwords: for user in users: my_print((user, password)) try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, 22)) sock.setblocking(1) # sock.settimeout(None) except Exception, e: print e try: session = libssh2.Session() session.set_banner() session.startup(sock) my_print(session.last_error()) session.userauth_password(user, password) my_print(session.last_error()) channel = session.open_session() rc = channel.execute('uname -a') result = '' while True: data = channel.read() if not data: break result = result + data my_print(rc) # channel.close() if not rc and (result.lower().find('aix') or result.lower().find('linux') \ or result.lower().find('hp-ux') or result.lower().find('solaris')): print "ok---------------------------------------" with open("login.ok" + ip, 'w') as fd: fd.write('login %s ok with user:%s password:%s' % (ip, user, password)) except Exception, e: my_print(e) pass
def __init__(self, hostname, username, password, port=22): self.username = username self.password = password self.hostname = hostname self.port = port self.session = libssh2.Session() self.session.set_banner() try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.hostname, self.port)) self.session.startup(sock) my_print(self.session.last_error()) self.session.userauth_password(self.username, self.password) my_print(self.session.last_error()) except Exception, e: print str(e) raise Exception, self.session.last_error()
def _prepare_sock(self): try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.hostname, self.port)) self.sock.setblocking(1) except Exception as e: print("SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)) print(e) try: self.session = libssh2.Session() self.session.set_banner() self.session.startup(self.sock) self.session.userauth_password(self.username, self.password) except Exception as e: print("SSHError: Can't startup session") print(e) # use low level layer because we don't yet provide High layer for sftp self.sftp = self.session._session.sftp_init()
def _prepare_sock(self): try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.hostname, self.port)) self.sock.setblocking(1) except Exception as e: print("SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)) print(e) try: self.session = libssh2.Session() # To activable full debug, uncomment the following line # self.session.set_trace(0xff) self.session.set_banner() self.session.startup(self.sock) # authentication self.session.userauth_publickey_fromfile(self.username, self.public_key, self.private_key, self.password) except Exception as e: print("SSHError: Can't startup session") raise(e)
def monitor(hostname, username, id): print('%s %s %d' % (hostname, username, id)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((hostname, 22)) session = libssh2.Session() started = False while not started: try: session.startup(sock) started = True except: eventlet.sleep(1) session.userauth_publickey_fromfile( username, os.path.expanduser('~/.ssh/id_rsa.pub'), os.path.expanduser('~/.ssh/id_rsa'), '') while True: sl = random.randint(1, 20) eventlet.sleep(sl) channel = session.channel() channel.execute('uname -a') stdout = [] #stderr = [] while not channel.eof: data = channel.read(1024) if data: stdout.append(data) #data = channel.read(1024, libssh2.STDERR) #if data: # stderr.append(data) print('%d %d %s' % (id, sl, ''.join(stdout)))
hostname = sys.argv[1] username = sys.argv[2] password = sys.argv[3] port = int(sys.argv[4]) sock = socket(AF_INET, SOCK_STREAM) try: sock.connect((hostname, port)) sock.setblocking(0) except Exception, e: print "Can't connect socket to (%s:%d): %s" % (hostname, port, e) sys.exit(1) # start session session = libssh2.Session() try: session.set_banner() # trace session on stderr if DEBUG=True trace(session) session.startup(sock) except SessionException, e: print "Can't startup session: %s" % e sys.exit(1) # register X11 callback session.callback_set(libssh2.LIBSSH2_CALLBACK_X11, x11_callback) try: session.userauth_password(username, password) except SessionException, e:
def test_session_startup(self): import libssh2 session = libssh2.Session() session.startup(self.socket) self.assertEqual(session.userauth_authenticated(), 0)
class ScannerClient(object): """Client to interface via ssh protocol with GE scanner in realtime.""" def __init__(self, hostname="cnimr", port=22, username="", password="", base_dir="/export/home1/sdc_image_pool/images", private_key=None, public_key=None, lock=None): self.hostname = hostname self.username = username self.password = password self.port = port self.base_dir = base_dir self.public_key = public_key self.private_key = private_key # If using more than one SFTP client at a time, pass in a # threading.Lock as your mutex in order to avoid problems with gcrypt. self.lock = lock # Set the maximum buffer size for reading files via sftp self.max_buf_size = pow(2, 30) self.connect() def connect(self): try: if self.lock is not None: self.lock.acquire() self._prepare_sock() except socket.error as e: # Connection refused self.sftp = None raise(e) finally: if self.lock is not None: self.lock.release() def __del__(self): self.close() def _prepare_sock(self): """This code modified from https://github.com/wallix/pylibssh2/blob/master/examples/sftp_listdir.py """ try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.hostname, self.port)) self.sock.setblocking(1) except Exception, e: print("SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)) print(e) try: self.session = libssh2.Session() self.session.set_banner() self.session.startup(self.sock) # Support for keys would go like so, currently not tested but # included here because of poor documentation. if self.private_key is not None: self.session.userauth_publickey_fromfile(self.username, self.public_key, self.private_key, self.password) else: self.session.userauth_password(self.username, self.password) except Exception, e: print("SSHError: Can't startup session") print(e)
def test_session_create(self): import libssh2 session = libssh2.Session() self.assertTrue(isinstance(session, libssh2.session.Session))