Python中的Excel密码恢复 [英] Excel password recovery in Python

查看:212
本文介绍了Python中的Excel密码恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,我一直在使用Excel密码恢复工具进行工作,因为在某些情况下,项目经理使用密码保护的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.

输出示例:

以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

在Pycharm终端机

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

推荐答案

我找到了您所缺少的内容,您需要在try else语句中输入else: break,以便一旦找到密码,该循环将中断且不会携带关于打印不正确的密码发现声明.如果重新运行代码,您还需要instance.Quit()来防止程序继续打印发现错误的密码语句.我将instance从循环中移出,因为您不必每次都打开一个新实例(这可能会引起一些思考问题)

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

我还从pywintypes导入了com_error进行异常处理,您应该尽量避免使用裸露的except语句,因为这可能会引起问题,并且不是一种好习惯.

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.

这篇关于Python中的Excel密码恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆