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| 6 |
+
|
| 7 |
+
# -----------------------------
|
| 8 |
+
# HASH DETECTION
|
| 9 |
+
# -----------------------------
|
| 10 |
+
|
| 11 |
+
def identify_hash(hash_value):
|
| 12 |
+
possible = []
|
| 13 |
+
|
| 14 |
+
if hash_value.startswith("$2a$") or hash_value.startswith("$2b$") or hash_value.startswith("$2y$"):
|
| 15 |
+
possible.append("bcrypt")
|
| 16 |
+
|
| 17 |
+
if hash_value.startswith("$argon2"):
|
| 18 |
+
possible.append("Argon2")
|
| 19 |
+
|
| 20 |
+
if hash_value.startswith("$6$"):
|
| 21 |
+
possible.append("SHA512-Crypt")
|
| 22 |
+
|
| 23 |
+
if hash_value.startswith("$5$"):
|
| 24 |
+
possible.append("SHA256-Crypt")
|
| 25 |
+
|
| 26 |
+
if re.fullmatch(r"[a-fA-F0-9]{32}", hash_value):
|
| 27 |
+
possible.extend(["MD5", "NTLM"])
|
| 28 |
+
|
| 29 |
+
if re.fullmatch(r"[a-fA-F0-9]{40}", hash_value):
|
| 30 |
+
possible.append("SHA1")
|
| 31 |
+
|
| 32 |
+
if re.fullmatch(r"[a-fA-F0-9]{64}", hash_value):
|
| 33 |
+
possible.append("SHA256")
|
| 34 |
+
|
| 35 |
+
if re.fullmatch(r"[a-fA-F0-9]{128}", hash_value):
|
| 36 |
+
possible.append("SHA512")
|
| 37 |
+
|
| 38 |
+
if not possible:
|
| 39 |
+
possible.append("Unknown")
|
| 40 |
+
|
| 41 |
+
return possible
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# -----------------------------
|
| 45 |
+
# HASH VERIFICATION
|
| 46 |
+
# -----------------------------
|
| 47 |
+
|
| 48 |
+
def verify_hash(password, hash_value):
|
| 49 |
+
results = []
|
| 50 |
+
|
| 51 |
+
possible = identify_hash(hash_value)
|
| 52 |
+
|
| 53 |
+
for alg in possible:
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
if alg == "MD5":
|
| 57 |
+
generated = hashlib.md5(password.encode()).hexdigest()
|
| 58 |
+
if generated.lower() == hash_value.lower():
|
| 59 |
+
results.append("β
MD5 match")
|
| 60 |
+
|
| 61 |
+
elif alg == "SHA1":
|
| 62 |
+
generated = hashlib.sha1(password.encode()).hexdigest()
|
| 63 |
+
if generated.lower() == hash_value.lower():
|
| 64 |
+
results.append("β
SHA1 match")
|
| 65 |
+
|
| 66 |
+
elif alg == "SHA256":
|
| 67 |
+
generated = hashlib.sha256(password.encode()).hexdigest()
|
| 68 |
+
if generated.lower() == hash_value.lower():
|
| 69 |
+
results.append("β
SHA256 match")
|
| 70 |
+
|
| 71 |
+
elif alg == "SHA512":
|
| 72 |
+
generated = hashlib.sha512(password.encode()).hexdigest()
|
| 73 |
+
if generated.lower() == hash_value.lower():
|
| 74 |
+
results.append("β
SHA512 match")
|
| 75 |
+
|
| 76 |
+
elif alg == "NTLM":
|
| 77 |
+
generated = hashlib.new(
|
| 78 |
+
'md4',
|
| 79 |
+
password.encode('utf-16le')
|
| 80 |
+
).hexdigest()
|
| 81 |
+
|
| 82 |
+
if generated.lower() == hash_value.lower():
|
| 83 |
+
results.append("β
NTLM match")
|
| 84 |
+
|
| 85 |
+
elif alg == "bcrypt":
|
| 86 |
+
if bcrypt.checkpw(
|
| 87 |
+
password.encode(),
|
| 88 |
+
hash_value.encode()
|
| 89 |
+
):
|
| 90 |
+
results.append("β
bcrypt match")
|
| 91 |
+
|
| 92 |
+
except Exception as e:
|
| 93 |
+
results.append(f"Error testing {alg}: {str(e)}")
|
| 94 |
+
|
| 95 |
+
return possible, "\n".join(results) if results else "No matches found."
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# -----------------------------
|
| 99 |
+
# MAIN FUNCTION
|
| 100 |
+
# -----------------------------
|
| 101 |
+
|
| 102 |
+
def analyze(password, hash_value):
|
| 103 |
+
possible, verification = verify_hash(password, hash_value)
|
| 104 |
+
|
| 105 |
+
return (
|
| 106 |
+
"\n".join(possible),
|
| 107 |
+
verification
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# -----------------------------
|
| 112 |
+
# GRADIO UI
|
| 113 |
+
# -----------------------------
|
| 114 |
+
|
| 115 |
+
with gr.Blocks(title="Hash Analyzer Lab") as demo:
|
| 116 |
+
gr.Markdown("# Hash Analyzer / Verification Tool")
|
| 117 |
+
gr.Markdown(
|
| 118 |
+
"Educational hash identification and password verification utility."
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
password_input = gr.Textbox(
|
| 122 |
+
label="Known Password",
|
| 123 |
+
type="password",
|
| 124 |
+
placeholder="Enter known plaintext password"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
hash_input = gr.Textbox(
|
| 128 |
+
label="Hash",
|
| 129 |
+
lines=4,
|
| 130 |
+
placeholder="Paste hash here"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
analyze_btn = gr.Button("Analyze")
|
| 134 |
+
|
| 135 |
+
possible_output = gr.Textbox(
|
| 136 |
+
label="Possible Algorithms"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
verification_output = gr.Textbox(
|
| 140 |
+
label="Verification Results",
|
| 141 |
+
lines=10
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
analyze_btn.click(
|
| 145 |
+
fn=analyze,
|
| 146 |
+
inputs=[password_input, hash_input],
|
| 147 |
+
outputs=[possible_output, verification_output]
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
demo.launch()
|