Python中的MD5哈希和比较

0 投票
2 回答
3195 浏览
提问于 2025-04-17 12:17

我在做一个作业,需要从一个给定的字典中创建一个预先计算好的哈希值文件,并且对每个密码加一个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

2 个回答

0

我的直觉是,问题不在于不同实现之间哈希计算的方式,而在于被哈希的内容。例如,你确定影子文件在密码前面加了一个整数字符串作为盐值吗?你确定密码需要用strip()去掉多余的空格吗?

1

编辑:

我明白了。看看你的 crack() 函数。你打开了哈希文件,然后用 for line in f 遍历每一行,接着去掉行首行尾的空白,再把这一行分割成 line1,这样就能从哈希文件中提取出哈希值。然后你是把完整的 line 和你想破解的哈希进行比较。显然,完整的行包含的不仅仅是哈希,所以它们无法匹配。为了更清楚,你可以把 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 中)。

撰写回答