九零登录

[【通过】] SSH弱口令扫描工具(python版)

[复制链接]
发表于 2015-10-22 23:57:16 | 显示全部楼层 |阅读模式
本程序包括一个python脚本文件:ssh.py

        *****************************************
        The tool for scan ssh weak password
                                        by zonadu
        *****************************************
        
usage: ssh.py [-h] -t  [-p] [-user] [-pass] [-uf] [-pf] [-rk] [-dk] [-kp] [-o]

The tool for scan ssh weak password

optional arguments:
  -h, --help            show this help message and exit
  -t , --target         the ip of target , eg: 127.0.0.1 or
                        127.0.0.1-127.0.0.255 or 127.0.0.1,172.0.0.2
  -p , --port           the port of ssh
  -user , --username    username
  -pass , --password    password
  -uf , --user-file     the dict of username
  -pf , --pass-file     the dict of password
  -rk , --rsa-key       use rsa private key filepath
  -dk , --dsa-key       use dsa private key filepath
  -kp , --key-password
                        the password of private key file
  -o , --output-file    write output to file


特点介绍:
        支持IP多种形式,如:192.168.1.1
                                                        192.168.1.1,192.168.1.5,192.168.1.100
                                                        192.168.1.1--192.168.1.255
        支持自定义SSH的端口
        支持使用单个ssh用户名和密码
        支持使用ssh用户名字典和密码字典
        支持使用rsa和dsa的私钥进行扫描,可以配置私钥对应的密码,可以指定私钥扫描时对应的ssh用户名
        支持输出结果到文件

源代码:ssh.py
[Python] 纯文本查看 复制代码
#!/usr/bin/python

import sys
import paramiko
import argparse

#****login to ssh****
#
#ip: ip address of ssh connect
#post: port of ssh connect
#username: username of login for ssh
#password: password of login for ssh
#pkey: the private key for ssh
#
def scan(ip,port,username=None,password=None, pkey=None):
        port=int(port)
        #print ip+":"+str(port)+" ==> "+username+"/"+password
        try:
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(ip, port, username, password, pkey)
                ssh.close()
        except paramiko.AuthenticationException,e:
                try:
                        print e
                        ssh.close()
                except:
                        pass
                return -1
        except Exception,e:
                print e
                try:
                        ssh.close()
                except:
                        pass
                return -2
        if(username!=None) and (password!=None):
                print ip+":"+str(port)+" ==> "+username+"/"+password+"...success"
        elif(username!=None) and (pkey!=None):
                print username+"@"+ip+"...succcess"
        return 0


if __name__=="__main__":
        print """
        *****************************************
        The tool for scan ssh weak password
                                        by zonadu
        *****************************************
        """
        parser=argparse.ArgumentParser(description="The tool for scan ssh weak password")
        parser.add_argument("-t","--target",metavar="",help="the ip of target , eg: 127.0.0.1 or 127.0.0.1-127.0.0.255 or 127.0.0.1,172.0.0.2",required=True)
        parser.add_argument("-p","--port",metavar="",help="the port of ssh",default="22")
        parser.add_argument("-user","--username",metavar="",help="username")
        parser.add_argument("-pass","--password",metavar="",help="password")
        parser.add_argument("-uf","--user-file",metavar="",help="the dict of username")
        parser.add_argument("-pf","--pass-file",metavar="",help="the dict of password")
        parser.add_argument("-rk","--rsa-key",metavar="",help="use rsa private key filepath")
        parser.add_argument("-dk","--dsa-key",metavar="",help="use dsa private key filepath")
        parser.add_argument("-kp","--key-password",metavar="",help="the password of private key file")
        parser.add_argument("-o","--output-file",metavar="",help="write output to file")
        args=parser.parse_args()
        ip=[]
        username=[]
        password=[]
        if(args.output_file!=None):
                filename=args.output_file
                output=sys.stdout
                f=open(filename,"a+")
                sys.stdout=f
        ###### ip ########
        if(args.target.find("-")!=-1):
                target=args.target.split("-")
                ip1=target[0].split(".")
                ip2=target[1].split(".")
                for a in range(int(ip1[0]),int(ip2[0])+1):
                        for b in range(int(ip1[1]),int(ip2[1])+1):
                                for c in range(int(ip1[2]),int(ip2[2])+1):
                                        for d in range(int(ip1[3]),int(ip2[3])+1):
                                                ip.append((str(a)+"."+str(b)+"."+str(c)+"."+str(d)))
                #print ip
        elif(args.target.find(",")!=-1):
                ip=args.target.split(",")
                #print ip
        else:
                ip1=args.target.split(".")
                ip1=str(ip1[0])+"."+str(ip1[1])+"."+str(ip1[2])+"."+str(ip1[3])
                ip.append(ip1)
        #print ip
        ###### port ##########
        port_tmp=args.port
        if(args.username!=None):
                ######## username ############
                username.append(args.username)
        if(args.password!=None):
                ######## password ############
                password.append(args.password)
        if(args.user_file!=None):
                #if in windows,please change \n to \r\n
                username_1=open(args.user_file).read().split('\n')
                username_1.pop()
                username=username+username_1
                #print username
        if(args.pass_file!=None):
                password_1=open(args.pass_file).read().split('\n')
                password_1.pop()
                password=password+password_1
                #print password
        if(args.rsa_key!=None):
                pkey_tmp=paramiko.RSAKey.from_private_key_file(args.rsa_key,args.key_password)
        elif(args.dsa_key!=None):
                pkey_tmp=paramiko.DSSKey.from_private_key_file(args.dsa_key,args.key_password)
        else:
                pkey_tmp=None
        ############ use username and password ##########
        if ip and username and password:
                for ip_tmp in ip:
                        print ip_tmp
                        for user_tmp in username:
                                for pass_tmp in password:
                                        scan(ip_tmp,port=port_tmp,username=user_tmp,password=pass_tmp)
        ############# use private key ################
        elif(pkey_tmp!=None) and ip:
                if username:
                        for ip_tmp in ip:
                                print ip_tmp
                                for user_tmp in username:
                                        scan(ip_tmp,port=port_tmp,username=user_tmp,pkey=pkey_tmp)
                else:
                        user_tmp="root"
                        for ip_tmp in ip:
                                print ip_tmp
                                scan(ip_tmp,port=port_tmp,username=user_tmp,pkey=pkey_tmp)
        ######
        if(args.output_file!=None):
                sys.stdout=output
                print "done. the result write to "+args.output_file
                f.close()


测试截图:
4b95a2c0da1a41ae68aab4a7534ecfa5.jpg

12b948ef11427b2ad7054629eff584e1.jpg

9645a47edc2c3daaace11f220f9b9679.jpg

评分

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

查看全部评分

发表于 2015-10-23 00:05:04 | 显示全部楼层
支持表哥
发表于 2015-10-23 10:07:20 | 显示全部楼层
年轻人,可以啊。
发表于 2015-10-25 22:54:54 | 显示全部楼层
我在想内网如果普通权限的机器不能装paramiko,怎么破?
发表于 2015-10-26 17:05:28 | 显示全部楼层
大表哥,这个不错啊,就是速度慢了些
发表于 2015-10-26 17:12:38 | 显示全部楼层
80uncle 发表于 2015-10-25 22:54
我在想内网如果普通权限的机器不能装paramiko,怎么破?

封装EXE哇。- -~

 楼主| 发表于 2015-10-28 09:19:15 | 显示全部楼层
ostrich 发表于 2015-10-26 17:05
大表哥,这个不错啊,就是速度慢了些

以后可以扩展一下使用多线程会好一点。
 楼主| 发表于 2015-10-28 09:24:21 | 显示全部楼层
80uncle 发表于 2015-10-25 22:54
我在想内网如果普通权限的机器不能装paramiko,怎么破?

这个没关系吧,本地把那个模块传上去,和python文件放在同一个目录下,就行了,import默认是先寻找当前目录。
 楼主| 发表于 2015-10-28 09:25:48 | 显示全部楼层

谢谢支持
发表于 2015-10-28 15:39:48 | 显示全部楼层
zonadu 发表于 2015-10-28 09:24
这个没关系吧,本地把那个模块传上去,和python文件放在同一个目录下,就行了,import默认是先寻找当前目 ...

哦哦,原来这样的,对python不怎么了解。谢谢

九零

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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