我的暴力破解方法在 Python 3 中不起作用

问题描述 投票:0回答:1

我有一个哈希破解工具,是我用 python 3.12 自己制作的。这是代码:

r"""
 _   _           _           ____                             _             
| | | | __ _ ___| |__       |  _ \  ___  ___ _ __ _   _ _ __ | |_ ___  _ __ 
| |_| |/ _` / __| '_ \ _____| | | |/ _ \/ __| '__| | | | '_ \| __/ _ \| '__|
|  _  | (_| \__ \ | | |_____| |_| |  __/ (__| |  | |_| | |_) | || (_) | |   
|_| |_|\__,_|___/_| |_|     |____/ \___|\___|_|   \__, | .__/ \__\___/|_|   
                                                  |___/|_|

[*] Author : Mowland Production
[*] Github : https://github.com/cyberpnx05
[*] Version : 1.0

Warning! This program is for educational purpose only...!

"""

import argparse
import hashlib
import itertools
import os.path
import string
import sys as _sys
import textwrap
import time


salt = string.ascii_letters+string.digits


# Hash definition
SHA256 = hashlib.sha256()
MD5 = hashlib.md5()
SHA1 = hashlib.sha1()

# Coloring
ERROR = "\x1b[31;1m"
WARNING = "\x1b[33;1m"
SUCCESS = "\x1b[32;1m"
INFO = "\x1b[34;1m"
NORMAL = "\x1b[0m"

def sha256_decrypt(hash_strings:str, wordfile:str):
    print(f"{WARNING}[*] Initializing....{NORMAL}\n")
    time.sleep(2)

    if args.decrypt_mode == "brute":
        for i in range(1, 20):
            for x in itertools.product(salt, repeat=i):
                word = "".join(x).rstrip()
                SHA256.update(word.encode())
                if hash_strings == SHA256.hexdigest():
                    print(f"{SUCCESS}[*]{NORMAL} {hash_strings} is Decrypted...")
                    print(f"{INFO}[*]{NORMAL} Decrypted string is ==> {INFO}{word}{NORMAL}")

                else:
                    print(f"{INFO}[*]{NORMAL} Attempting to decrypt {INFO}{hash_strings}{NORMAL} with --> {INFO}{SHA256.hexdigest()}{NORMAL} --> {word}")

    else:
        try:
            with open(wordfile, "r") as wordlist:
                for word in wordlist.readlines():
                    SHA256.update(word.rstrip("\n").encode())
                    if hash_strings == SHA256.hexdigest():
                        print(f"{SUCCESS}[*]{NORMAL} {hash_strings} is Decrypted...")
                        print(f"{INFO}[*]{NORMAL} Decrypted string is ==> {INFO}{word.rstrip("\n")}{NORMAL}")

                    else:
                        print(f"{INFO}[*]{NORMAL} Attempting to decrypt {INFO}{hash_strings}{NORMAL} with --> {INFO}{SHA256.hexdigest()}{NORMAL} --> {word.rstrip("\n")}")

        except FileNotFoundError:
            print(f"{ERROR}[*]{NORMAL} The specified path is not found...!")

def sha1_decrypt(hash_strings:str, wordfile:str):
    print(f"{WARNING}[*] Initializing....{NORMAL}\n")
    time.sleep(3)

    if args.decrypt_mode == "brute":
        for i in range(1, 20):
            for x in itertools.product(salt, repeat=i):
                SHA1.update(("".join(x)).encode())
                if hash_strings == SHA1.hexdigest():
                    print(f"{SUCCESS}[*]{NORMAL} {hash_strings} is Decrypted...")
                    print(f"{INFO}[*]{NORMAL} Decrypted string is ==> {INFO}{"".join(x)}{NORMAL}")

                else:
                    print(f"{INFO}[*]{NORMAL} Attempting to decrypt hash with string --> {INFO}{"".join(x)}{NORMAL}")

    else:
        try:
            with open(wordfile, "r") as wordlist:
                for word in wordlist.readline().strip("\n"):
                    SHA1.update(word.encode())
                    if hash_strings == SHA1.hexdigest():
                        print(f"{SUCCESS}[*]{NORMAL} {hash_strings} is Decrypted...")
                        print(f"{INFO}[*]{NORMAL} Decrypted string is ==> {INFO}{word}{NORMAL}")

                    else:
                        print(f"{INFO}[*]{NORMAL} Attempting to decrypt hash with string --> {INFO}{word}{NORMAL}")

        except FileNotFoundError:
            print(f"{ERROR}[*]{NORMAL} The specified path is not found...!")

def md5_decrypt(hash_strings:str, wordfile:str):
    print(f"{WARNING}[*] Initializing....{NORMAL}\n")
    time.sleep(3)

    if args.decrypt_mode == "brute":
        for i in range(1, 20):
            for x in itertools.product(salt, repeat=i):
                MD5.update(("".join(x)).encode())
                if hash_strings ==  MD5.hexdigest():
                    print(f"{SUCCESS}[*]{NORMAL} {hash_strings} is Decrypted...")
                    print(f"{INFO}[*]{NORMAL} Decrypted string is ==> {INFO}{"".join(x)}{NORMAL}")

                else:
                    print(f"{INFO}[*]{NORMAL} Attempting to decrypt hash with string --> {INFO}{"".join(x)}{NORMAL}")

    else:
        try:
            with open(wordfile, "r") as wordlist:
                for word in wordlist.readline().strip("\n"):
                    MD5.update(word.encode())
                    if hash_strings ==  MD5.hexdigest():
                        print(f"{SUCCESS}[*]{NORMAL} {hash_strings} is Decrypted...")
                        print(f"{INFO}[*]{NORMAL} Decrypted string is ==> {INFO}{word}{NORMAL}")

                    else:
                        print(f"{INFO}[*]{NORMAL} Attempting to decrypt hash with string --> {INFO}{word}{NORMAL}")

        except FileNotFoundError:
            print(f"{ERROR}[*]{NORMAL} The specified path is not found...!")


            



def main():
    parser = argparse.ArgumentParser(prog=os.path.basename(__file__), formatter_class=argparse.RawDescriptionHelpFormatter, description=textwrap.dedent(fr"""
{INFO} _   _           _           ____                             _             
| | | | __ _ ___| |__       |  _ \  ___  ___ _ __ _   _ _ __ | |_ ___  _ __ 
| |_| |/ _` / __| '_ \ _____| | | |/ _ \/ __| '__| | | | '_ \| __/ _ \| '__|
|  _  | (_| \__ \ | | |_____| |_| |  __/ (__| |  | |_| | |_) | || (_) | |   
|_| |_|\__,_|___/_| |_|     |____/ \___|\___|_|   \__, | .__/ \__\___/|_|   
                                                  |___/|_|{NORMAL}

{INFO}[*]{NORMAL} Author : Mowland Production
{INFO}[*]{NORMAL} Github : https://github.com/XHadow_21
{INFO}[*]{NORMAL} Version : 1.0

{WARNING}Warning! This program is for educational purpose only...!{NORMAL}
"""))
    parser.add_argument("-ht", "--hash-type", required=True, choices=["sha256", "sha1", "md5"], dest="hash_type", type=str, help="Define Hash type to decrypt")
    parser.add_argument("-hv", "--hash-value", required=True, dest="hash_string", type=str, help="Hash string to decrypt")
    parser.add_argument("-m", "--mode", choices=["brute", "wordlist"], dest="decrypt_mode", help="Method to decrypt the hash", default="brute")
    parser.add_argument("-w", "--wordlist", dest="wordlist", help="Wordlist file", metavar="FILE", default=None)

    global args

    args = parser.parse_args()

    if (args.decrypt_mode == "wordlist") and (args.wordlist == "" or args.wordlist == None):
        parser.print_help()
        exit(1)

    match(args.hash_type, args.decrypt_mode):
        case("sha256", "brute"):
            sha256_decrypt(args.hash_string, "None")

        case("sha256", "wordlist"):
            sha256_decrypt(args.hash_string, args.wordlist)

        case("sha1", "brute"):
            sha1_decrypt(args.hash_string, "None")

        case("sha1", "wordlist"):
            sha1_decrypt(args.hash_string, args.wordlist)

        case("md5", "brute"):
            md5_decrypt(args.hash_string, "None")

        case("md5", "wordlist"):
            md5_decrypt(args.hash_string, args.wordlist)

        case _:
            parser.print_help()
            exit(1)

if __name__ == "__main__":
    main()

创建此代码后,我在 Windows 终端中使用 python hashlib 生成 SHA256 哈希

import hashlib

sha256 = hashlib.sha256()
sha256.update("123".encode())
sha256.hexdigest()

它会生成这个哈希值a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3。 同时,在我上面的工具中,它生成相同的文本值 123 和完全不同的哈希值 5c8b2454d77163fa3f498410f02f90b2bbc43504b45981cf125986c060a70481 与我之前在 Windows 终端中生成的哈希值进行比较。这怎么可能……?

我希望它与我之前在 Windows 终端中生成的哈希值相同

python encryption sha256 brute-force
1个回答
0
投票

您对不同的输入重复使用相同的哈希对象

hashlib.sha256()
。对不同的输入调用
SHA256.update()
相当于连接所有输入,因此您不会获得每个输入单词的哈希值,而只能获得所有连接的最终哈希值。

您应该为要测试的每个输入创建一个新的哈希对象。例如:

import hashlib

SHA256 = hashlib.sha256()
SHA256.update("123".encode())
print(SHA256.hexdigest())
# a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3

SHA256.update("123".encode())
print(SHA256.hexdigest())
# 96cae35ce8a9b0244178bf28e4966c2ce1b8385723a96a6b838858cdd6ca0a1e

SHA256 = hashlib.sha256()
SHA256.update("123123".encode())
print(SHA256.hexdigest())
# 96cae35ce8a9b0244178bf28e4966c2ce1b8385723a96a6b838858cdd6ca0a1e
© www.soinside.com 2019 - 2024. All rights reserved.