Python暴力破解MD5加密字符串

首先,MD5是不可逆的消息摘要算法,也就是说,不能根据MD5值逆向解密出原信息;但是,MD5通常被用于对密码进行处理,而密码长度是有限的,我们只需要配对出这些组合的MD5值,就可以实现所谓的解密,这种方法也叫暴力破解;

这种暴力破解方法只适用于有限长度的密码,且密码长度每增加一位,有可能的组合数量都会呈几何级数增长;

一个纯数字的8位长度的密码,就有一亿种组合,如果包含大小写字母(不包含特殊字符),就有62^8种组合;

所以最好是使用弱密码字典来进行猜解,这里提供一个非常强大的密码字典:

1400多万个弱口令密码字典下载

Python暴力破解MD5源码

"""
    简易MD5破解工具
    https://www.perfcode.com/p/python-crack-md5-hash.html
"""

import threading
from hashlib import md5 as _md5

def MD5(bytes):
    m=_md5()
    m.update(bytes)
    return m.hexdigest()

class _PWDict:
    def __init__(self,filename):
        self.mutex = threading.Lock()
        self.fp = open(filename,'rb')

    def get(self):
        with self.mutex:
            bytes = self.fp.readline()
        return bytes.strip()

class _crackMD5:
    def __init__(self,md5,PWDict,func=None,threadQuantity=5):
        self.md5 = md5.lower()
        self.D = PWDict
        if func:
            self.func = func
        else:
            self.func = print

        self.threadQuantity = threadQuantity

        self.status = False
        self.T = []

    def _run(self):
        while self.status:
            bytes = self.D.get()
            if self.md5 == MD5(bytes):
                self.status = False
                self.func(bytes)
            
    def start(self):
        self.status = True
        for i in range(self.threadQuantity):
            thread = threading.Thread(target=self._run)
            thread.start()
            self.T.append(thread)

def main():
    import sys
    if len(sys.argv) < 3:
        print("usage:  crackmd5.py md5-hash dictionary_file [thread_quantity]")
        return

    md5 = sys.argv[1]

    if len(md5)!=32:
        print('只支持长度位32的MD5 HASH')
        return

    dict_file = sys.argv[2]

    try:
        D = _PWDict(dict_file)
    except Exception as e:
        print(e)
        return
    
    if len(sys.argv) >3:
        tc=int(sys.argv[3])
        if tc <1:
            print("错误的线程数.")
            return
    else:
        tc = 5

    def myPrint(v):
        print("破解成功:",v)

    crackMD5 = _crackMD5(md5,D,myPrint,tc)
    print('cracking ...')
    crackMD5.start()

if __name__ == '__main__':
    main()

使用方法

运行这个程序需要有Python3环境;将代码保存为crackmd5.py,下载好上文提到的密码字典,并解压到crackmd5.py同目录;

在命令行下使用(例):

python crackmd5.py 923ac3be93aaac2683e0d72cd712d94f dict.txt 10

923ac3be93aaac2683e0d72cd712d94f为要破解的MD5值,dict.txt 为密码字典,10为线程数;

运行效果:

python破解MD5

原创内容,如需转载,请注明出处;

本文地址: https://www.perfcode.com/p/python-crack-md5-hash.html

分类: 计算机技术
推荐阅读:
Rust获取代码的运行时间 在 Rust 中,你可以使用 std::time::Instant 模块来测量代码的运行时间。以下是一个简单的示例代码,演示如何在 Rust 中测量代码的运行时间:
禁用Visual Studio自动下载更新 近日,作者使用Visual Studio发现,Visual Studio会自己在后台下载更新内容,然后提醒你是否安装;因为Visual Studio的更新包体积庞大,不仅占用网络资源也会消耗磁盘性能,所以我决定禁用它;
Rust中的数据类型 在本文中,将介绍Rust中的整型、浮点型、布尔类型、字符类型、元组类型、数组类型的声明和简单使用方法;
Pythone内置函数 Python 内置函数是 Python 解释器提供的函数库,这些函数可以直接使用,无需导入任何模块。
Golang实现字符串的MD5加密 本文将使用 golang 自带的 crypto/md5 库实现MD5的字符串加密;
C语言中fopen()函数"w"和"w+"mode参数有什么不同? fopen()是C语言标准库的一部分,参数mode字符串表示文件的访问模式;w和w+模式有略微不同: