Below I have been working on a Excel password recovery tool for work as we have had a few occasions where project managers have password protected excels and then forgot the password and they have lost weeks of work because of this.
The below code seems to be running but doesn't get past the first word in the wordlist and then paste that the password has been found.
Example of output:
in cmd
C:\Users\eldri\OneDrive\Desktop>python xlcrka.py
[+] Excel to attack: C:\Users\eldri\OneDrive\Desktop\target.xlsx
[+] Wordlist: C:\Users\eldri\OneDrive\Desktop\Wordlists\rockyou.txt
[-] Password attempt: 123456
[+] Password Found: 123456
in Pycharm Terminal
C:\Users\eldri\PycharmProjects\CAPTCHA\venv\Scripts\python.exe "C:/Users/eldri/PycharmProjects/Bad codes/xlcrka.py"
[+] Excel to attack: C:\Users\eldri\OneDrive\Desktop\target.xlsx
[+] Wordlist: C:\Users\eldri\OneDrive\Desktop\Wordlists\rockyou.txt
[-] Password attempt: 123456
[+] Password Found: 123456
Below is the code I have got so far:
from pip._vendor.distlib.compat import raw_input
from win32com.client import Dispatch
file = raw_input('[+] Excel to attack: ')
wordlist = raw_input('[+] Wordlist: ')
word = open(wordlist, 'r', encoding='utf8', errors='ignore')
allpass = word.readlines()
word.close()
for password in allpass:
password = password.strip()
print ("[-] Password attempt: "+password)
instance = Dispatch('Excel.Application')
try:
instance.Workbooks.Open(file, False, True, None, password)
print ("[+] Password Found: "+password)
break
except:
pass
The outcome I want to achieve:
Learn why this is not working.
see whether anyone has any ideas on how to improve
Output for the code:
To go through the wordlist and find the correct password and print the password
解决方案
I found what you where missing, you needed a else: break in the try except statement so that once the password was found the loop will break and did not carry on printing incorrect password found statements. You also needed instance.Quit() to prevent the program from continuing the print incorrect password found statements if you re-ran the code. I moved instance from the loop as you don't need to open a new instance every time (that might have caused some issues thinking about it)
from win32com.client import Dispatch
from pywintypes import com_error
file = input('[+] Excel to attack: ')
wordlist = input('[+] Wordlist: ')
instance = Dispatch('Excel.Application')
word = open(wordlist, 'r', encoding='utf8', errors='ignore')
allpass = word.readlines()
word.close()
for password in allpass:
password = password.rstrip()
print("[-] Password attempt: " + password)
try:
instance.Workbooks.Open(file, False, True, None, password)
print("[+] Password Found: " + password)
except com_error:
instance.Workbooks.Close()
else:
instance.Workbooks.Close()
instance.Quit()
break
I also imported com_error from pywintypes for the exception handling, you should try to avoid a bare except statement as that could cause issues and is not good practice.