有一次需要测试安全某系统

发现默认配置类似Zzxxxxx#的默认密码 比如Zz654321#

那就很简单的了 用python生成哈希表 默认只有6位随机数字 那就是10w个字典

import hashlib
import sqlite3
import os
import tkinter as tk
from tkinter import filedialog, messagebox

DB_FILE = "md5_hashes.db"


#检查数据库是否存在
def check_and_create_db():
    if os.path.exists(DB_FILE):
        print("数据库已存在,跳过创建")
        return

    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS hash_table (md5 TEXT PRIMARY KEY, password TEXT)")

    print("正在生成 MD5 哈希...")
    for num in range(1000000):
        num_str = f"{num:06d}"
        passwords = [
            f"Zz{num_str}#",  # ZzXXXXXX#
        ]
        for pwd in passwords:
            md5_hash = hashlib.md5(pwd.encode()).hexdigest()
            cursor.execute("INSERT OR IGNORE INTO hash_table (md5, password) VALUES (?, ?)", (md5_hash, pwd))

    conn.commit()
    conn.close()
    print("数据库创建完成!")


#查询单个 MD5
def query_md5(md5_hash):
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    cursor.execute("SELECT password FROM hash_table WHERE md5=?", (md5_hash,))
    result = cursor.fetchone()
    conn.close()
    return result[0] if result else "未找到"


#单个查询
def search_single():
    md5_input = entry_single.get().strip()
    if md5_input:
        result = query_md5(md5_input)
        output_text.delete("1.0", tk.END)
        output_text.insert(tk.END, f"{md5_input} -> {result}\n")
    else:
        messagebox.showwarning("警告", "请输入 MD5 值")


#批量查询
def batch_query():
    file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
    if not file_path:
        return

    output_text.delete("1.0", tk.END)
    results = []

    with open(file_path, "r") as file:
        for line in file:
            md5_hash = line.strip()
            if md5_hash:
                result = query_md5(md5_hash)
                results.append(f"{md5_hash} -> {result}")

    output_text.insert(tk.END, "\n".join(results))


#导出查询
def export_results():
    result_text = output_text.get("1.0", tk.END).strip()
    if not result_text:
        messagebox.showwarning("警告", "没有数据可导出")
        return

    file_path = filedialog.asksaveasfilename(defaultextension=".txt",
                                             filetypes=[("Text files", "*.txt")])
    if file_path:
        with open(file_path, "w") as file:
            file.write(result_text)
        messagebox.showinfo("成功", "结果已导出")


#创建 GUI 界面
def create_gui():
    global entry_single, output_text
    root = tk.Tk()
    root.title("MD5 哈希查询工具")
    root.geometry("600x400")

    #单个查询
    frame_single = tk.Frame(root)
    frame_single.pack(pady=10)

    tk.Label(frame_single, text="输入 MD5:").pack(side=tk.LEFT)
    entry_single = tk.Entry(frame_single, width=40)
    entry_single.pack(side=tk.LEFT, padx=5)
    tk.Button(frame_single, text="查询", command=search_single).pack(side=tk.LEFT)

    #批量查询
    tk.Button(root, text="批量查询 (TXT)", command=batch_query).pack(pady=10)

    #结果显示
    output_text = tk.Text(root, height=15, width=70)
    output_text.pack(pady=10)

    #导出结果
    tk.Button(root, text="导出结果", command=export_results).pack(pady=5)

    root.mainloop()

if __name__ == "__main__":
    check_and_create_db() 
    create_gui()