2017-12-31 42 views
0

私は暗号文に対してハッシュされたすべての英語小文字をチェックする簡単な辞書攻撃スクリプトを作成しています。これは次のように見えます:hexdigest()throws TypeError:必要な引数 'length'(pos 1)が見つかりません。

import hashlib 
import random 


def dict_attack(pwds): 
    f = open('Dictionary.txt', 'r') 
    words = f.readlines() 
    f.close() 
    cracked = [] 
    for pwd in pwds: 
     for w in words: 
      word = w.strip('\n') 
      word = word.strip(' ') 
      hashed = hashlib.md5(word.encode()) 
      if hashed.hexdigest() == pwd: 
       print("[+] Found {} as {}, updating...".format(pwd, word)) 
       cracked.append(word) 
       break 
    print("[-] {}/{} passwords found!".format(len(cracked), len(pwds))) 
    return cracked 

def main(): 
    # To generate new ciphertext 
    f = open('Dictionary.txt', 'r') 
    words = f.readlines() 
    f.close() 
    for b in range(0, 10): 
     passwords.append(random.choice(words)) 
     passwords[b] = passwords[b].strip('\n') 
     passwords[b] = passwords[b].strip(' ') 
    hashed_passwords = [] 
    for p in passwords: 
     hashed_passwords.append(hashlib.md5(p.encode()).hexdigest()) 
    #print(hashed_passwords) 
    print(dict_attack(hashed_passwords)) 

main() 

ご覧のとおり、関数dict_attackはmd5ハッシュのみを使用していました。このスクリプトの次のバージョンでは、hashlib.algorithms_guaranteedライブラリの各アルゴリズムを順番に調べ、辞書の各単語を各アルゴリズムで暗号化し、暗号文と照合することを計画しました。このコードは、このように見えた:私は私のコードを実行したときに

import hashlib 


arguments = [[hashlib.md5('hello'.encode())]] 
f = open('Dictionary.txt', 'r') 
words = f.readlines() 
f.close() 
# Remember to strip the \n's 
cracked = {} 
for ciphertext in arguments[0]: 
    for word in words: 
     for alg in hashlib.algorithms_guaranteed: 
      exec("hashed = hashlib.{}(word.encode())".format(alg)) 
      if hashed.hexdigest() == ciphertext: 
       cracked[ciphertext] = [word, alg] 
       print("[+] Found {} as {} with {} algorithm!".format(ciphertext, word, alg)) 
       break 
print(cracked) 

は、しかし、それはこのエラーを投げた:

TypeError: Required argument 'length' (pos 1) not found 

なぜこの出来事で、そしてどのように私はそれを修正することができますか?

+0

あなたはその時点でどんなアルゴリズムを見ていますか?補足として、最初の例でたくさんの繰り返し作業をしていますが、効率的ではありません。 –

+0

'shake_256'アルゴリズム' hexdigest'は[docs](https:// docs .python.org/3/library/hashlib.html#shake-variable-length-digests)を参照してください。 –

+0

@JonClementsええ、私の最初のものは本当に良くありませんでした。それはプログラマースクリプトよりもコーダースクリプトのほうが多く、私がv2を作った理由の1つです。 –

答えて

0

hashlibには、動的長さの出力を持つハッシュ関数がいくつか含まれています。具体的には、shake_128shake_256です。

import hashlib 


arguments = [[hashlib.md5('hello'.encode())]] 
f = open('Dictionary.txt', 'r') 
words = f.readlines() 
f.close() 
# Remember to strip the \n's 
cracked = {} 

for ciphertext in arguments[0]: 
    digest = ciphertext.digest() 
    for word in words: 
     for alg in hashlib.algorithms_guaranteed: 
      if alg.startswith('shake_'): 
       continue 
      hashed = getattr(hashlib, alg)(word.encode()) 
      if hashed.digest() == digest: 
       cracked[ciphertext] = [word, alg] 
       print("[+] Found {} as {} with {} algorithm!".format(ciphertext, word, alg)) 
       break 

print(cracked) 
関連する問題