0

我试图了解彩虹表是如何工作的,并试图在 python 中实现一个,但没有多大成功。

我有一些代码基本上在文本文件中创建一个字典,其中明文字符串映射到它们的哈希值,但无法弄清楚如何调整它以生成减少的彩虹表。

temp = itertools.product("abcdefghijklmnopqrstuvwxyz", repeat=5)
f = open("passwords.txt", "w")
for pw in temp:
    p = ''.join(pw)
    encode = hashlib.md5(p.encode()).hexdigest() 
    f.write(p + " " + encode + "\n")
f.close()

我遇到了归约函数并有点理解它们,因此将其定义为:

def reduction(hash):
    return hash[:5]

但我不知道从这里做什么:(

如何调整此代码以生成减少的彩虹表?

4

1 回答 1

1

您的缩减功能应该生成一个由您的字符集和长度为 5(在您的情况下)的字符组成的密码。这是一个将整数作为输入的示例。

import hashlib
chars="abcdefghijklmnopqrstuvwxyz"
chars_len = len(chars)

def reduce(i):
    # reduces int i to a 5 char password
    # think of i as a number encoded in base l
    pwd=""
    while len(pwd)<5:
        pwd = pwd + chars[ i%chars_len ]
        i = i // chars_len
    return pwd


table=[]
# generate 10 chains of 1000 pwd, print start and end
for s in range(0,10):
    # we can use reduce to generate the start of a chain
    start=reduce(s)

    p=start
    for i in range(0,1000):
        # hash
        h=hashlib.md5(p.encode('ascii')).hexdigest()
        # reduce
        p=reduce(int(h,16))

    table.append([start,p])

print (table)

您现在有一个可以破解大约 10k 个密码但只使用 20 个密码空间的表!

请注意,对于真正的彩虹表,您必须为每个步骤使用不同的归约函数。例如rainbow_reduce(i,k) = reduce(i+k)

使用该表从哈希中查找密码留作练习:-)(或另一个问题)

于 2019-08-06T17:31:58.700 回答