2012-02-03 127 views
0

对于我的assingment,我应该创建一个预先计算的哈希值从给定字典与盐0到255到每个密码的文件。我有散列,但是当我尝试将它们与给定的影子文件进行比较时,我什么都没有。这导致我相信我可能哈希不正确?我的教授确实说过密码哈希是用C完成的。这是否有所作为?MD5哈希和Python中的比较

这里是我的代码: 找到哈希

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

关闭文件通常是一个好主意。 – 2012-02-03 22:16:09

+1

请确保你的标签在你的代码中是正确的 - 毕竟这是Python :) – 2012-02-03 22:25:28

回答

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:,它会自动关闭该文件与块结束时。哈希存储在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中)。

+0

嗯,我在shadow3中找到了一个匹配项。 6expression。我非常接近。你为什么认为其他人都不匹配?也许我的教授错误地创建了这些散列文件?这是我所能想到的。再次感谢您的帮助。作为这项任务的结果,我的Python技能正在大大提高! – Justin 2012-02-04 23:57:43

+0

听起来像你现在正在工作。这可能是因为你的教授并不希望你能够解密所有这些密码,并且可以为其他人提供明文,作为不会被非常快的字典攻击所破坏的更强密码的例子。对于这项任务,我预料会有更多的比赛。你可以很好地问他/听听你预计会有多少“密码”被破坏。 – gfortune 2012-02-05 00:37:48

0

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