【问题标题】:MD5 hashing and comparison in pythonpython中的MD5哈希和比较
【发布时间】:2012-02-05 02:54:13
【问题描述】:

对于我的评估,我应该从给定的字典中创建一个预先计算的哈希值文件,每个密码的盐为 0 - 255。我有哈希值,但是当我尝试将它们与给定的影子文件进行比较时,我什么也得不到。这让我相信我可能散列不正确?我的教授确实说过密码哈希是用 C 完成的。这有什么不同吗?

这是我的代码: 找到哈希

import hashlib

f = open('/root/dictionary/dictionary', 'r')
print f
i=0
def getMD5Hash(textToHash=None):
return hashlib.md5(textToHash).hexdigest()

for line in f:
    line = line.rstrip()
    #print line
    i=0

    while i <= 255:
            j=str(i)
            line1 = j+line
            md5=getMD5Hash(line1)
            print md5,':',line1
            i+=1

破解

f1 = open('/root/dictionary/shadow3','r')


def crack(Hash=None):
    f = open('/root/dictionary/HASHES','r')

    for line in f:
    line = line.rstrip()
    line1 = line.split(" ")[0]

    if line == Hash:
        print (line,"\n",Hash)
        return line




for line in f1:
    line = line.rstrip()
    line = line.split(":")[1:]
    print line[0]
    result = crack(line[0])
    print result

编辑:带有阴影的 Rar 文件:http://mediafire.com/?euwjpxr3np36brt

给定的字典文件 - http://mediafire.com/?psspoqo900x0hmq

【问题讨论】:

  • 关闭文件通常是个好主意。
  • 请确保你的标签在你的代码中是正确的——毕竟这是 Python :)

标签: python md5 hash


【解决方案1】:

编辑:

知道了,我想。看看你的crack() 函数。您打开哈希文件,然后 for line in f 剥离该行,然后将该行拆分为 line1 以从您的哈希文件中获取哈希。然后,您将完整的 line 而不是 line1 与您要破解的哈希值进行比较。当然,整行不仅包含哈希,因此无法匹配。为清楚起见,您可以将 line1 重命名为 generated_hash。然后,更明显的是你需要if generated_hash == Hash:

其他说明:

通过一些故障排除,我们确定问题中发布的示例哈希无效。我还确定种子解决方案中使用的方法确实是`hashlib.md5(salt+cleartext).hexdigest()。张贴者正确生成了哈希值,但在尝试将它们与给定的影子文件进行比较时失败了。最初,行尾存在一些问题。

由于我知道发布者能够毫无问题地生成散列,因此我发布了另一种生成散列并将它们存储在字典中的方法,这样就不必每次都从磁盘读取散列表。

import hashlib

#Initialize an empty dictionary.  We'll add entries to this as we read the 
#dictionary file in
hash_table = {}

print('Generating hashes...')

#Using with on the file object means that it will be closed automatically
#when the block is finished
with open('dictionary.txt', 'r') as inp_file:

    for word in inp_file.readlines():

        #strip off the trailing whitespace ('\n' or '\n\r' depending on the platform)
        word = word.strip()

        #The requirement is for a salt to be prepended to the cleartext
        #dictionary word.  For each possible salt value...
        for salt in range(0,256):
            #convert the salt from an int to a string here so we don't have to
            #continually do it below
            salt = str(salt)

            #Store the hash/cleartext pair in the dictionary.  The key of the
            #dictionary is the hash and the value is the salted cleartext
            hash_table[hashlib.md5(salt+word).hexdigest()] = salt+word

注意我是如何使用with fileobject as some_name: 的,它会在 with 块完成时自动关闭文件。哈希存储在 hash_table 中,它是一个键/值字典。我们使用散列作为键,使用明文作为值来快速匹配散列。如果您想知道特定哈希是否在 hash_table 中,if 'some_hex_hash' in hash_table: do stuff 是正确的方法。要获取哈希值的明文,只需 hash_table['some_hex_hash']。有关字典的更多信息,请参阅http://docs.python.org/tutorial/datastructures.html#dictionaries

当然,这是您已经工作的部分。现在的诀窍是正确加载影子哈希,然后检查它们是否在您的文件中(如果使用字典,则在 hash_table 中)。

【讨论】:

  • 我在 shadow3 中找到了一个匹配项。 6表达。我非常接近。为什么你认为其他人都不匹配?也许我的教授错误地创建了这些哈希文件?这就是我能想到的。再次感谢您在这方面的帮助。由于这项任务,我的 Python 技能大大提高了!
  • 听起来你现在已经开始工作了。可能是您的教授不希望您能够解密所有密码,并且可能会为其他人提供明文作为更强大密码的示例,这些密码不会被非常快速的字典攻击破解。不过,我本来希望这项任务会有更多的比赛。你最好问问他/听听你预计会破解多少个“密码”。
【解决方案2】:

我的直觉是,这不是在实现之间计算哈希的方式,而是哈希的内容。例如,您确定影子文件是通过将整数字符串添加到密码的开头来加盐的吗?你确定密码应该是strip()'d?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2018-09-18
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多