I am trying to solve this CTF challenge:
#!/usr/bin/env python3
from hashlib import md5, sha1, sha256
from datetime import datetime
print("Try to login on my super safe computer! Be fast, you have at most 30 minutes.")
now = str(datetime.now())
print(now)
try:
m = bytes.fromhex(input())
except:
print("Access denied!")
exit(1)
t = sha256(now.encode()).hexdigest()
if t.encode() not in m:
print("Access denied!")
exit(1)
if not md5(m).hexdigest().endswith("fc5c25"):
print("Access denied!")
exit(1)
if not sha1(m).hexdigest().endswith("fc5c25"):
print("Access denied!")
exit(1)
print("Welcome! Here is your flag:")
print(open("flag.txt").read())
Here is my latest attempt:
import hashlib
import time
from datetime import datetime
import sys
Target full suffix for both MD5 and SHA1
TARGET_SUFFIX = "fc5c25"
SHORT_SUFFIX = "25" # Faster filtering before full check
Get timestamp string (from challenge or current time)
try:
now_string = input("[] Enter timestamp from challenge: ").strip()
if not now_string:
now_string = str(datetime.now())
print(f"[] Using current time for testing: {now_string}")
except EOFError:
print("No input provided. Exiting.")
sys.exit(1)
Compute required SHA256-based prefix
prefix_sha256_hex = hashlib.sha256(now_string.encode()).hexdigest()
prefix_bytes = prefix_sha256_hex.encode('ascii')
print(f"[] Timestamp: {now_string}")
print(f"[] Required prefix (SHA256 hex encoded): {prefix_bytes.decode()}")
print(f"[] Target suffix: {TARGET_SUFFIX}")
print(f"[] Using short prefilter suffix: {SHORT_SUFFIX}")
print("[*] Starting optimised search...")
counter = 0
start_time = time.time()
BYTES_FOR_COUNTER = 8
last_print_time = start_time
while True:
counter_bytes = counter.to_bytes(BYTES_FOR_COUNTER, 'big')
candidate_bytes = prefix_bytes + counter_bytes
md5_hash = hashlib.md5(candidate_bytes).hexdigest()
# First cheap check: only proceed if MD5 ends with SHORT_SUFFIX
if md5_hash.endswith(SHORT_SUFFIX):
sha1_hash = hashlib.sha1(candidate_bytes).hexdigest()
# Second cheap check: only proceed if SHA1 ends with SHORT_SUFFIX
if sha1_hash.endswith(SHORT_SUFFIX):
# Now do the full check
if md5_hash.endswith(TARGET_SUFFIX) and sha1_hash.endswith(TARGET_SUFFIX):
end_time = time.time()
print("\n[+] Success! Found matching input.")
print(f"[*] Time taken: {end_time - start_time:.2f} seconds")
print(f"[*] Input bytes (hex): {candidate_bytes.hex()}")
print(f"[*] MD5 hash: {md5_hash}")
print(f"[*] SHA1 hash: {sha1_hash}")
print("\nSubmit this hex value to the challenge:")
print(candidate_bytes.hex())
break
# Print progress every ~2 seconds
if counter % 1000000 == 0 and counter > 0:
current_time = time.time()
if current_time - last_print_time >= 2.0:
elapsed = current_time - start_time
rate = counter / elapsed if elapsed > 0 else 0
print(f"[*] Checked {counter:,} candidates ({rate:,.0f} hashes/sec). Elapsed: {elapsed:.1f}s", end='\r')
last_print_time = current_time
counter += 1
Does it work in theory? (In practice, if it works at all, it seems far too slow...)