@Author:Runsen
不担保一定成功
相信大家都知道设置压缩文件的时候, 可以设置密码, 这种可以让 zip 或者 rar 压缩包进行密码的添加.
再一次解压需要输入之前设置的密码.
看似很难破解, 对于一个使用过 Kali 的我来说, 其实编写一个简单的 Python 脚本很快可以解决.
我们需要尝试使用 Kali Linux 中的单词表, 破解 zip 文件的密码, 成功率大约可以达到 70%.(这里不敢说 100%, 因为在 Kali 破解的成功率不可能达到 100% 的)
我们将使用 Python 的内置 zipfile 模块和第三方 tqdm 库来快速打印进度条:
pip install tqdm
破解的方法是一样的, 暴力破解. 这意味着我们将需要一个单词列表以暴力破解此受密码保护的 zip 文件. 我们将使用较大的 rockyou 单词表(大小约为 133MB), 如果您使用的是 Kali Linux, 则可以在 / usr/share/wordlists/rockyou.txt.gz 路径下找到它. 否则, 您可以在此处下载.
下载完成会是下面的密码本.
打开一个新的 Python 文件, 然后执行以下操作:
- import zipfile
- from tqdm import tqdm
让我们指定目标 zip 文件以及单词列表路径:
- # 要使用的密码列表路径必须在当前目录中可用
- wordlist = "rockyou.txt"
- # 要破解其密码的, zip 文件
- zip_file = "data.zip"
要在 Python 中读取 zip 文件, 使用 zipfile.ZipFile 类, 该类具有打开, 读取, 写入, 关闭, 列出和提取 zip 文件的方法 (此处仅使用 extractall() 方法):
- # 初始化 Zip 文件对象
- zip_file = zipfile.ZipFile(zip_file)
- # 计算此单词列表中的单词数
- n_words = len(list(open(wordlist, "rb")))
- # 打印密码总数
- print("Total passwords to test:", n_words)
- Total passwords to test: 14344391
我们读取了整个单词表, 然后仅获取要测试的密码数量, 这对于 tqdm 证明是有用的, 因此我们可以跟踪暴力破解过程中的位置, 以下剩余的代码:
- with open(wordlist, "rb") as wordlist:
- for Word in tqdm(wordlist, total=n_words, unit="word"):
- try:
- zip_file.extractall(pwd=Word.strip())
- except:
- continue
- else:
- print("[+] Password found:", Word.decode().strip())
- exit(0)
- print("[!] Password not found, try other wordlist.")
我们打开单词表并逐个单词地读取它, 并尝试将其作为密码来提取 zip 文件, 读取整行将带有换行符, 因此, 我们使用 strip()方法删除空格.
每当密码不正确时, 方法 extractall()都会引发异常, 因此在这种情况下, 我们可以传递到下一个密码, 否则, 我们将打印正确的密码并退出程序.
下面完整代码
- import zipfile
- from tqdm import tqdm
- wordlist = "rockyou.txt"
- # 要破解其密码的 rar 文件, zip 文件
- zip_file = "data.zip"
- # 初始化 Zip 文件对象
- zip_file = zipfile.ZipFile(zip_file)
- # 计算此单词列表中的单词数
- n_words = len(list(open(wordlist, "rb")))
- # 打印密码总数
- print("Total passwords to test:", n_words)
- with open(wordlist, "rb") as wordlist:
- for Word in tqdm(wordlist, total=n_words, unit="word"):
- try:
- zip_file.extractall(pwd=Word.strip())
- except:
- continue
- else:
- print("[+] Password found:", Word.decode().strip())
- exit(0)
- print("[!] Password not found, try other wordlist.")
这里我说明下, rockyou 单词表有超过 1400 万个单词, 它们是目前密码按频率排序的最常用密码集合(当然, 这种不一定成功).
好了, 今天 Runsen 教大家学习了构建了一个简单但有用的脚本来破解 zip 文件密码, 如果无法使用此列表来破解它, 请尝试使用更大的单词列表, 但比这个 Kali 中的密码本更大的, 应该没有多少.
最后, Please give me a Like and pay attention me
来源: https://blog.csdn.net/weixin_44510615/article/details/117111605