Python线程重复值

时间:2016-08-08 19:51:49

标签: python multithreading queue

我正在尝试编写一个python SHA512野蛮人。

我使用Queue将值存储在wordlist中,然后将它们与加密的hash进行比较。

问题在于,不是从队列中弹出的值,而是由其他线程重用它们。所以基本上,不是让整个工作在线程之间分配以使事情变得更快,而是让几个线程做同样的事情。我该如何解决这个问题?

我想要这样的事情:https://github.com/WillPennell/Python/blob/master/Black-Hat-Python/BHP-Code/Chapter5/content_bruter.py#L20

import threading
import thread
import Queue
import os,sys
import crypt
import codecs
from datetime import datetime,timedelta
import argparse
today = datetime.today()
resume = None
threads = 5



def build_wordlist(wordlist_file):

    fd = open(wordlist_file,"rb")
    raw_words = fd.readlines()
    fd.close()

    found_resume = False
    words        = Queue.Queue()

    for word in raw_words:

        word = word.rstrip()

        if resume is not None:

            if found_resume:
                words.put(word)
            else:
                if word == resume:
                    found_resume = True
                    print "Resuming wordlist from: %s" % resume

        else:
            words.put(word)

    return words


def testPass(cryptPass,user):

    word_queue     =       build_wordlist('test.txt')


    while not word_queue.empty():
        attempt = word_queue.get()
        ctype = cryptPass.split("$")[1]
        if ctype == '6':
            print "[+] Hash type SHA-512 detected ..."
            salt = cryptPass.split("$")[2]
            insalt = "$" + ctype + "$" + salt + "$"
            word    =   attempt
            cryptWord = crypt.crypt(word,insalt)
            if (cryptWord == cryptPass):
                time = time = str(datetime.today() - today)
                print "[+] Found password for the user: " + user + " ====> " + word + " Time: "+time+"\n"
                return

    print "Password not found for the user: " + user
    print "Moving on to next user..."
    exit

def main():
    parse = argparse.ArgumentParser(description='A simple brute force /etc/shadow .')
    parse.add_argument('-f', action='store', dest='path', help='Path to shadow file, example: \'/etc/shadow\'')
    argus=parse.parse_args()
    if argus.path == None:
        parse.print_help()
        exit
    else:
        build_wordlist('test.txt')
        passFile = open (argus.path,'r')
        for line in passFile.readlines():
            line = line.replace("\n","").split(":")
            if  not line[1] in [ 'x' , '*' , '!' ]:
                user = line[0]
                cryptPass = line[1]
                for i in range(threads):
                    t = threading.Thread(target=testPass,args=(cryptPass,user))
                    t.daemon = True
                    t.start()
if __name__=="__main__":
    main()
编辑:我意识到有两种方法可以做到这一点: 首先,我可以为每个用户创建一个线程,这不是我想要的。 其次,我可以通过几个线程分割每个用户的工作,这就是我想要的。

2 个答案:

答案 0 :(得分:0)

这可以使用经典的生产者和消费者问题来解决。您可能会发现Solution to producer and consumer problem in python很有用。

答案 1 :(得分:0)

让我们看一下这段代码:

for i in range(threads):
    t = threading.Thread(target=testPass,args=(cryptPass,user))
    t.daemon = True
    t.start()

让我们描述一下你为每个开始的线程做了什么:

  1. 根据Queue
  2. 的定义,从test.txt创建新的build_wordlist个对象
  3. 处理步骤1中的队列
  4. 听起来您希望的行为是在单个队列上多线程处理一些处理步骤,而不是创建相同队列的重复项。所以这意味着你的" testPass"方法应该可以采用Queue对象。即。

    q = build_wordlist('test.txt')
    for i in range(threads):
        t = threading.Thread(target=testPass,args=(q, cryptPass,user))
        t.daemon = True
        t.start()
    

    testPass应如下所示:

    def testPass(queue, cryptPass, user):
    
        word_queue = queue
        ... stuff ...