Python MD5哈希密码和字典

时间:2013-12-02 14:28:56

标签: python

我是Python的新手,我正在尝试创建一个简单的程序来收集MD5哈希密码,然后将它们与我用其中的常用密码创建的字典相匹配。

我可以收集MD5密码没问题,问题是当我尝试将它们与单词词典进行比较时,我根本无法使用它。

任何提示或指示都会受到赞赏,我对下一步该做什么一无所知,并且在寻求帮助之前我已经在网上搜索了很多天。

我的代码如下,

import sys, re, hashlib


def dict_attack(passwd_hash):
    print 'dict_attack(): Cracking hash:', passwd_hash
    #set up list of common password words
    passwords = open('J:/dictionary.txt')

    passwd_found = False


    if passwd_found:
        print 'dict_attack(): Password recovered: ' (passwd)
def main():
    print'[dict_crack] Tests'
    passwd_hash = '4297f44b13955235245b2497399d7a93'
    dict_attack(passwd_hash)
if __name__ == '__main__':
    main()

进一步问题的相关代码

 hash_to_crack = password
  dict_file = "J:/dictionary.txt"

with open(dict_file) as fileobj:
    for line in fileobj:
        line = line.strip()
        if hashlib.md5(line).hexdigest() == hash_to_crack:
            print "Successfully cracked the hash %s: It's %s" % (hash_to_crack, line)
            return ""
print "Failed to crack the file."

2 个答案:

答案 0 :(得分:1)

我在你的代码片段中遗漏了一些代码...也许这可能是一个详细阐述的起点(未经过测试,不确定是否有效):

from hashlib import md5

_hashes = { md5( pwd.strip() ).hexdigest() : pwd.strip()
        for pwd in open('J:/dictionary.txt') }

def main():
    print'[dict_crack] Tests'
    passwd_hash = '4297f44b13955235245b2497399d7a93'

    if passwd_hash in _hashes:
        print "found %s = %s" % (passwd_hash, _hashes[passwd_hash])

if __name__ == '__main__':
    main()

答案 1 :(得分:0)

以下脚本可以解决这个问题,并且可以处理非常大的文件,因为它不会立即读取整个字典。祝你的代码好运。如果您对此有任何问题或疑问,请发表评论。

import hashlib
hash_to_crack = "5badcaf789d3d1d09794d8f021f40f0e"
dict_file = "dict.txt"

def main():
    with open(dict_file) as fileobj:
        for line in fileobj:
            line = line.strip()
            if hashlib.md5(line).hexdigest() == hash_to_crack:
                print "Successfully cracked the hash %s: It's %s" % (hash_to_crack, line)
                return ""
    print "Failed to crack the file."

if __name__ == "__main__":
    main()

编辑:多个哈希破解。享受。

import hashlib
hashes_to_crack = ["5badcaf789d3d1d09794d8f021f40f0e", "d0763edaa9d9bd2a9516280e9044d885", "8621ffdbc5698829397d97767ac13db3"]
dict_file = "dict.txt"

def main(hash_to_crack):
    with open(dict_file) as fileobj:
        for line in fileobj:
            line = line.strip()
            if hashlib.md5(line).hexdigest() == hash_to_crack:
                print "Successfully cracked the hash %s: It's %s" % (hash_to_crack, line)
                return ""
    print "Failed to crack the file."

if __name__ == "__main__":
    for hashToCrack in hashes_to_crack:
        main(hashToCrack)