Files
Python-Excel-Bruteforcer/child_process.py

148 lines
4.7 KiB
Python

import io
import sys
import msoffcrypto
import time
from termcolor import cprint
decrypted_workbook = io.BytesIO()
colors = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "light_grey", "dark_grey", "light_red",
"light_green", "light_yellow", "light_blue", "light_magenta", "light_cyan"]
total_processes = 0
total_decrypt_attempts = 0
update_rate = 100000
"""
Get a color for a process using an array
"""
def get_color(processNumbar):
return colors[int(processNumbar % (len(colors) - 1))]
"""
Calculates and prints the number of iterations per seconds
"""
def calculate_speed(processNumbar, start_time, last_update, word):
elapsed_time = time.time() - start_time
if elapsed_time > 0:
loop_speed = (total_decrypt_attempts - last_update) / elapsed_time
buifulNumbar = '{:,}'.format(round(loop_speed)).replace(',', ' ')
print_thread(processNumbar, "Loop speed: " + buifulNumbar + " iterations per second (at word " + word + ")")
"""
Formats and prints with colors for a process
"""
def print_thread(processNumbar, text):
cprint("[" + str(processNumbar) + "/" + str(total_processes) + "] " + text, get_color(processNumbar))
"""
Decryption function
"""
def decrypt_file(file, text):
global total_decrypt_attempts
total_decrypt_attempts = total_decrypt_attempts + 1 # Global loop iterations
try:
file.load_key(password=text)
file.decrypt(decrypted_workbook)
return True
except Exception as e:
return False
"""
Gets multiple different lines to decrypt
"""
def get_lines(line):
return [line, line.lower(), line.upper()]
"""
Global function of a child process
"""
def worldlist_bruteforce_partial(fileToCrack, wordlistPath, total_proc, processNumbar, startPos, endPos):
global total_processes
total_processes = total_proc
# Opens the excel file
with open(fileToCrack, 'rb') as file:
office_file = msoffcrypto.OfficeFile(file)
# First definitions...
last_update = total_decrypt_attempts
start_time = time.time()
# Opens the wordlist and begins cracking
with open(wordlistPath, 'r', encoding='latin_1') as wordlistToUse:
# Find the right start position in the wordlist for this process
print_thread(processNumbar, "Started new process [" + str(startPos) + " -> " + str(endPos) + "]")
wordlistToUse.seek(startPos)
# Move backward until a newline character is found or we reach the beginning of the file
position = startPos
while position > 0:
wordlistToUse.seek(position - 1)
if wordlistToUse.read(1) == '\n':
wordlistToUse.seek(position - 1)
break
position -= 1
# I love while True loops
while True:
try:
line = wordlistToUse.readline()
# End the thread if it has arrived to its given endPos
if wordlistToUse.tell() >= endPos:
print_thread(processNumbar, "Thread done at line " + line + " with " + str(
total_decrypt_attempts) + " attempts.")
break
except Exception as e:
print(e) # When a line fails to decode properly (Probably a wrong text encoding)
line = "error"
pass
# If the file is done break
if not line:
break
line = line.rstrip() # Removes line endings
# Print status every x lines read
if last_update <= total_decrypt_attempts - update_rate:
calculate_speed(processNumbar, start_time, last_update, line)
start_time = time.time()
last_update = total_decrypt_attempts
# Trying to decrypt the file
for lineToDecrypt in get_lines(line):
if decrypt_file(office_file, lineToDecrypt): # Decrypts
print_thread(processNumbar,
"!!FOUNDPASSWORD!! " + lineToDecrypt + " !!FOUNDPASSWORD!!")
# Writes pass to file
foundPassFile = open("found.txt", "w")
foundPassFile.write(lineToDecrypt)
foundPassFile.close()
print_thread(processNumbar,
"Saved in found.txt...")
sys.exit(111) # Exit with found code