2016-08-08 31 views
0

我想寫一個python SHA512蠻力forcer。重複值的Python線程

我使用隊列來存儲單詞表中的值,然後將它們與加密哈希進行比較。

問題是,而不是從隊列中彈出的值,它們被其他線程重複使用。所以基本上,不是讓線程之間的所有工作分開來讓事情變得更快,而是讓我有幾個線程完成同樣的事情。我怎樣才能解決這個問題?

我想是這樣的: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

讓我們來看看下面的代碼塊:

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

而且我們描述一下這個正在爲每個線程啓動:

  1. 創建由build_wordlist定義從test.txtQueue對象
  2. 處理來自步驟1的隊列

聽起來你想要的行爲是在一個隊列上多線程處理一些步驟,而不是創建同一隊列的重複。所以這意味着你的「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 ...