python恢复excel表头_Python中的Excel密码恢复

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值