我正在研究一个简单的字典攻击脚本,它会检查每个英文小写单词和密文,并且我的第一个版本运行良好。它看起来像这样:
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)
hashlib
包含一些具有动态长度输出的哈希函数。具体而言,shake_128
和shake_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)