Created
July 25, 2024 12:20
-
-
Save farzadhallaji/0e0499f6086e170477f29303cee64e8b to your computer and use it in GitHub Desktop.
Example for passwords starting with 'TOEFL' word
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import sleep | |
import zipfile | |
import string | |
import itertools | |
from concurrent.futures import ProcessPoolExecutor, as_completed | |
# Define the path to the zip file and character set for passwords | |
zip_file_path = '/content/0.zip' | |
last_password_file = '/content/last_password.txt' | |
characters = string.ascii_uppercase | |
# Perform parallel password brute-forcing with dynamic printing | |
def parallel_bruteforce(zip_file_path, lengths): | |
last_password = read_last_password(last_password_file) | |
chunk_size = 1000 | |
print_update_interval = 5000 # Update the console every 5000 tries | |
try_count = 0 | |
for length in lengths: | |
pwd_gen = generate_passwords(last_password, length) | |
while True: | |
chunk = list(itertools.islice(pwd_gen, chunk_size)) | |
if not chunk: | |
break | |
with ProcessPoolExecutor(max_workers=4) as executor: | |
futures = {executor.submit(try_password, zip_file_path, pwd): pwd for pwd in chunk} | |
for future in as_completed(futures): | |
try_count += 1 | |
password = future.result() | |
last_tried_password = futures[future] # Keep track of the last tried password | |
# Print the last tried password dynamically, but not too frequently | |
if try_count % print_update_interval == 0: | |
print(f"\rLast tried password: {last_tried_password}", end='', flush=True) | |
if password: | |
executor.shutdown(wait=False) | |
print(f"\rPassword found: {password} ") # Clear the line after found | |
return password | |
# Print the last tried password if not found | |
print(f"\rLast tried password: {last_tried_password} (completed)") # Clear the line | |
# Function to attempt to extract the zip file with a given password | |
def try_password(zip_file_path, password): | |
try: | |
with zipfile.ZipFile(zip_file_path) as zf: | |
zf.extractall(pwd=password.encode()) | |
return password | |
except: | |
return None | |
# Function to generate passwords | |
def generate_passwords(start_pwd, length): | |
num_suffix_chars = length - 5 | |
if start_pwd and len(start_pwd) > 5: | |
suffix = start_pwd[5:] | |
suffix_length = len(suffix) | |
else: | |
suffix = '' | |
suffix_length = 0 | |
start_index = 0 | |
if suffix: | |
try: | |
# Compute the index if a suffix is provided | |
start_index = sum(characters.index(char) * (len(characters) ** (suffix_length - i - 1)) | |
for i, char in enumerate(suffix)) | |
except ValueError: | |
start_index = 0 # Reset if character not in list | |
# Generate passwords from calculated index | |
all_combinations = itertools.product(characters, repeat=num_suffix_chars) | |
for _ in range(start_index): | |
next(all_combinations, None) # Safely handle StopIteration | |
# Yield passwords with prefix 'TOEFL' | |
for pwd_suffix in all_combinations: | |
yield 'TOEFL' + ''.join(pwd_suffix) | |
# Read the last password attempted from a file | |
def read_last_password(file_path): | |
try: | |
with open(file_path, 'r') as file: | |
return file.read().strip() | |
except FileNotFoundError: | |
return None | |
# Write the last attempted password to a file | |
def write_last_password(file_path, password): | |
with open(file_path, 'w') as file: | |
file.write(password) | |
# Main execution block | |
if __name__ == '__main__': | |
lengths = range(8, 13) # Password lengths from 8 to 12 | |
password_found = parallel_bruteforce(zip_file_path, lengths) | |
if password_found: | |
print(f"Password found: {password_found}") | |
else: | |
print("Password not found") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment