- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试提取受密码保护的.zip文件,其中包含一个.txt文件(对于这种情况,请说Congrats.txt
)。现在Congrats.txt
中包含文本,因此其大小不为0kb。将其放置在.zip中(出于线程的考虑,让其命名为.zip zipv1.zip
),并为此密码设置dominique
。该密码存储在另一个.txt中的其他单词和名称中(出于这个问题的缘故,我们将其命名为file.txt
)。现在,如果我通过执行python Program.py -z zipv1.zip -f file.txt
来运行下面的代码(假定所有这些文件都与Program.py
放在同一文件夹中),我的程序就会显示dominique
作为zipv1.zip
的正确密码,以及< cc>并提取file.txt
,但zipv1.zip
为空且大小为0kb。
现在我的代码如下:
import argparse
import multiprocessing
import zipfile
parser = argparse.ArgumentParser(description="Unzips a password protected .zip", usage="Program.py -z zip.zip -f file.txt")
# Creates -z arg
parser.add_argument("-z", "--zip", metavar="", required=True, help="Location and the name of the .zip file.")
# Creates -f arg
parser.add_argument("-f", "--file", metavar="", required=True, help="Location and the name of file.txt.")
args = parser.parse_args()
def extract_zip(zip_filename, password):
try:
zip_file = zipfile.ZipFile(zip_filename)
zip_file.extractall(pwd=password)
print(f"[+] Password for the .zip: {password.decode('utf-8')} \n")
except:
# If a password fails, it moves to the next password without notifying the user. If all passwords fail, it will print nothing in the command prompt.
pass
def main(zip, file):
if (zip == None) | (file == None):
# If the args are not used, it displays how to use them to the user.
print(parser.usage)
exit(0)
# Opens the word list/password list/dictionary in "read binary" mode.
txt_file = open(file, "rb")
# Allows 8 instances of Python to be ran simultaneously.
with multiprocessing.Pool(8) as pool:
# "starmap" expands the tuples as 2 separate arguments to fit "extract_zip"
pool.starmap(extract_zip, [(zip, line.strip()) for line in txt_file])
if __name__ == '__main__':
main(args.zip, args.file)
Congrats.txt
相同的方法以另一个zip(
zipv2.zip
)进行压缩,而只是将
zipv1.zip
压缩在一个文件夹中,并且该文件夹与
Congrats.txt
一起压缩,则我确实得到与
Congrats.txt
相同的结果,但这
zipv1.zip
沿着它所在的文件夹提取的时间,并且
Congrats.txt
完整无缺;其中的文字及其大小是完整的。
Congrats.txt
。因此,我确实将代码中的
RuntimeError
更改为
except:
并在尝试解压缩
except RuntimeError:
时遇到了此错误:
(venv) C:\Users\USER\Documents\Jetbrains\PyCharm\Program>Program.py -z zipv1.zip -f file.txt
[+] Password for the .zip: dominique
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 47, in starmapstar
return list(itertools.starmap(args[0], args[1]))
File "C:\Users\USER\Documents\Jetbrains\PyCharm\Program\Program.py", line 16, in extract_zip
zip_file.extractall(pwd=password)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1594, in extractall
self._extract_member(zipinfo, path, pwd)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1649, in _extract_member
shutil.copyfileobj(source, target)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 79, in copyfileobj
buf = fsrc.read(length)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 876, in read
data = self._read1(n)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 966, in _read1
self._update_crc(data)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 894, in _update_crc
raise BadZipFile("Bad CRC-32 for file %r" % self.name)
zipfile.BadZipFile: Bad CRC-32 for file 'Congrats.txt'
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\USER\Documents\Jetbrains\PyCharm\Program\Program.py", line 38, in <module>
main(args.zip, args.file)
File "C:\Users\USER\Documents\Jetbrains\PyCharm\Program\Program.py", line 33, in main
pool.starmap(extract_zip, [(zip, line.strip()) for line in txt_file])
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 276, in starmap
return self._map_async(func, iterable, starmapstar, chunksize).get()
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 657, in get
raise self._value
zipfile.BadZipFile: Bad CRC-32 for file 'Congrats.txt'
zipv1.zip
中找到了密码,提取了
file.txt
但
zipv1.zip
为空且大小为0kb。所以我再次运行了程序,但是这次是
Congrats.txt
,结果是这样的:
(venv) C:\Users\USER\Documents\Jetbrains\PyCharm\Program>Program.py -z zipv2.zip -f file.txt
[+] Password for the .zip: dominique
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 47, in starmapstar
return list(itertools.starmap(args[0], args[1]))
File "C:\Users\USER\Documents\Jetbrains\PyCharm\Program\Program.py", line 16, in extract_zip
zip_file.extractall(pwd=password)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1594, in extractall
self._extract_member(zipinfo, path, pwd)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1649, in _extract_member
shutil.copyfileobj(source, target)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 79, in copyfileobj
buf = fsrc.read(length)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 876, in read
data = self._read1(n)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 966, in _read1
self._update_crc(data)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 894, in _update_crc
raise BadZipFile("Bad CRC-32 for file %r" % self.name)
zipfile.BadZipFile: Bad CRC-32 for file 'Congrats.txt'
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\USER\Documents\Jetbrains\PyCharm\Program\Program.py", line 38, in <module>
main(args.zip, args.file)
File "C:\Users\USER\Documents\Jetbrains\PyCharm\Program\Program.py", line 33, in main
pool.starmap(extract_zip, [(zip, line.strip()) for line in txt_file])
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 276, in starmap
return self._map_async(func, iterable, starmapstar, chunksize).get()
File "C:\Users\USER\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 657, in get
raise self._value
zipfile.BadZipFile: Bad CRC-32 for file 'Congrats.txt'
zipv2.zip
及其内部的文本,并且其大小保持不变。
Congrats.txt
之后;该程序可以读取/处理一个小的单词列表/密码列表/字典,但是如果它的单词大(?)则不能。
with zipfile.ZipFile(zip_filename, 'r') as zip_file:
中存在一个.txt文档;以文本
zipv1.zip
命名的
Congrats.txt
。
You have cracked the .zip!
中也存在相同的.txt,但是这次将其放置在名为
zipv2.zip
的文件夹中,然后进行了压缩/密码保护。两个邮编的密码均为
ZIP Contents
。
dominique
压缩方法和7zip中的
Deflate
加密生成的。
ZipCrypto
(35/52行)
Line 35
中,在
John The Ripper Jr.txt
中用于
Line 1968
(1968/3106行)。
John The Ripper.txt
;它将创建一个名为
python Program.py -z zipv1 -f "John The Ripper Jr.txt"
的文件夹,并将
Extracted
放入我们先前设置的句子中。
Congrats.txt
也一样,但是
zipv2
将位于
Congrats.txt
文件夹内的
ZIP Contents
文件夹中。在这种情况下,解压缩.zip文件没有问题。
Extracted
即
John The Ripper.txt
尝试相同的操作,则会创建两个压缩文件的
python Program.py -z zipv1 -f "John The Ripper.txt"
文件夹。就像
Extracted
一样,但是由于某种未知的原因,这一次
John The Ripper Jr.txt
都将为空。
import argparse
import multiprocessing
import zipfile
parser = argparse.ArgumentParser(description="Unzips a password protected .zip by performing a brute-force attack.", usage="Program.py -z zip.zip -f file.txt")
# Creates -z arg
parser.add_argument("-z", "--zip", metavar="", required=True, help="Location and the name of the .zip file.")
# Creates -f arg
parser.add_argument("-f", "--file", metavar="", required=True, help="Location and the name of the word list/password list/dictionary.")
args = parser.parse_args()
def extract_zip(zip_filename, password):
try:
with zipfile.ZipFile(zip_filename, 'r') as zip_file:
zip_file.extractall('Extracted', pwd=password)
print(f"[+] Password for the .zip: {password.decode('utf-8')} \n")
except:
# If a password fails, it moves to the next password without notifying the user. If all passwords fail, it will print nothing in the command prompt.
pass
def main(zip, file):
if (zip == None) | (file == None):
# If the args are not used, it displays how to use them to the user.
print(parser.usage)
exit(0)
# Opens the word list/password list/dictionary in "read binary" mode.
txt_file = open(file, "rb")
# Allows 8 instances of Python to be ran simultaneously.
with multiprocessing.Pool(8) as pool:
# "starmap" expands the tuples as 2 separate arguments to fit "extract_zip"
pool.starmap(extract_zip, [(zip, line.strip()) for line in txt_file])
if __name__ == '__main__':
# Program.py - z zipname.zip -f filename.txt
main(args.zip, args.file)
Congrats.txt
但使用来自不同单词列表/密码列表/词典的不同密码的更多.zip文件。相同的方法;使用了.txt的较大版本和较小版本,并获得了与上述相同的结果。
Congrats.txt
中切出前2k个单词并创建一个新的.txt,说
John The Ripper.txt
; .zip文件被成功解压缩,出现
John The Ripper v2.txt
文件夹,并且其中出现
Extracted
以及其中的文本。因此,我认为它与密码输入后的行有关。因此,在这种情况下
Congrats.txt
;
Line 1968
之后脚本不会停止的地方?我不知道为什么会这样。我想这不是解决方案,而是迈向解决方案的一步...
import argparse
import multiprocessing
import zipfile
parser = argparse.ArgumentParser(description="Unzips a password protected .zip by performing a brute-force attack using", usage="Program.py -z zip.zip -f file.txt")
# Creates -z arg
parser.add_argument("-z", "--zip", metavar="", required=True, help="Location and the name of the .zip file.")
# Creates -f arg
parser.add_argument("-f", "--file", metavar="", required=True, help="Location and the name of the word list/password list/dictionary.")
args = parser.parse_args()
def extract_zip(zip_filename, password, queue):
try:
with zipfile.ZipFile(zip_filename, "r") as zip_file:
zip_file.extractall('Extracted', pwd=password)
print(f"[+] Password for the .zip: {password.decode('utf-8')} \n")
queue.put("Done") # Signal success
except:
# If a password fails, it moves to the next password without notifying the user. If all passwords fail, it will print nothing in the command prompt.
pass
def main(zip, file):
if (zip == None) | (file == None):
print(parser.usage) # If the args are not used, it displays how to use them to the user.
exit(0)
# Opens the word list/password list/dictionary in "read binary" mode.
txt_file = open(file, "rb")
# Create a Queue
manager = multiprocessing.Manager()
queue = manager.Queue()
with multiprocessing.Pool(8) as pool: # Allows 8 instances of Python to be ran simultaneously.
pool.starmap_async(extract_zip, [(zip, line.strip(), queue) for line in txt_file]) # "starmap" expands the tuples as 2 separate arguments to fit "extract_zip"
pool.close()
queue.get(True) # Wait for a process to signal success
pool.terminate() # Terminate the pool
pool.join()
if __name__ == '__main__':
main(args.zip, args.file) # Program.py -z zip.zip -f file.txt.
Line 1968
的
zipv1.zip
是完整的;里面有消息。但是关于
Congrats.txt
仍然是空的,不能说相同的话。
最佳答案
抱歉,长时间的停顿……看来您已陷入困境。
1.调查
场景很复杂(我想说,这个场景离MCVE太远了),很多原因可以归咎于这种行为。
从zipv1.zip / zipv2.zip不匹配开始。仔细观察,似乎zipv2也被弄乱了。如果对于zipv1来说很容易发现(Congrats.txt是唯一的文件),那么对于zipv2,“ ZIP Contents / Black-Large.png”的大小为0。它可以用任何文件复制,甚至更多:它适用于zf.namelist
返回的第一项(不是目录)。
因此,事情开始变得更加清晰:
由于密码文件中包含dominique,因此正在解压缩文件内容(不知道到那时为止会发生什么)
稍后,.zip的第一个条目被截断为0个字节
查看尝试使用错误密码提取文件时引发的异常,共有3种类型(其中最后2种可以组合在一起):
RuntimeError:文件密码错误...
其他:
zlib.error:解压缩数据时出现错误-3 ...
zipfile.BadZipFile:文件的CRC-32错误...
我创建了自己的存档文件。为了保持一致性,从现在开始我将使用它,但是所有内容也将适用于任何其他文件。
内容:
DummyFile0.zip(10字节)-包含:0123456789
DummyFile1.zip(10字节)-包含:0000000000
DummyFile2.zip(10字节)-包含:AAAAAAAAAA
使用Total Commander(9.21a)内部zip压缩程序归档了这3个文件,并使用dominique(zip2.0加密)对其进行了密码保护。生成的存档(命名为arc0.zip(但名称不相关))长392个字节
code.py:
#!/usr/bin/env python3
import sys
import os
import zipfile
def main():
arc_name = sys.argv[1] if len(sys.argv) > 1 else "./arc0.zip"
pwds = [
#b"dominique",
#b"dickhead",
b"coco",
]
pwds = [item.strip() for item in open("orig/John The Ripper.txt.orig", "rb").readlines()]
print("Unpacking (password protected: dominique) {:s},"
" using a list of predefined passwords ...".format(arc_name))
if not os.path.isfile(arc_name):
raise SystemExit("Archive file must exist!\nExiting.")
faulty_pwds = list()
good_pwds = list()
with zipfile.ZipFile(arc_name, "r") as zip_file:
print("Zip names: {:}\n".format(zip_file.namelist()))
for idx, pwd in enumerate(pwds):
try:
zip_file.extractall("Extracted", pwd=pwd)
except:
exc_cls, exc_inst, exc_tb = sys.exc_info()
if exc_cls != RuntimeError:
print("Exception caught when using password ({:d}): [{:}] ".format(idx, pwd))
print(" {:}: {:}".format(exc_cls, exc_inst))
faulty_pwds.append(pwd)
else:
print("Success using password ({:d}): [{:}] ".format(idx, pwd))
good_pwds.append(pwd)
print("\nFaulty passwords: {:}\nGood passwords: {:}".format(faulty_pwds, good_pwds))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q054532010]> "e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" code.py arc0.zip
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Unpacking (password protected: dominique) arc0.zip, using a list of predefined passwords ...
Zip names: ['DummyFile0.txt', 'DummyFile1.txt', 'DummyFile2.txt']
Exception caught when using password (1189): [b'mariah']
<class 'zlib.error'>: Error -3 while decompressing data: invalid code lengths set
Exception caught when using password (1446): [b'zebra']
<class 'zlib.error'>: Error -3 while decompressing data: invalid block type
Exception caught when using password (1477): [b'1977']
<class 'zlib.error'>: Error -3 while decompressing data: invalid block type
Success using password (1967): [b'dominique']
Exception caught when using password (2122): [b'hank']
<class 'zlib.error'>: Error -3 while decompressing data: invalid code lengths set
Exception caught when using password (2694): [b'solomon']
<class 'zlib.error'>: Error -3 while decompressing data: invalid distance code
Exception caught when using password (2768): [b'target']
<class 'zlib.error'>: Error -3 while decompressing data: invalid block type
Exception caught when using password (2816): [b'trish']
<class 'zlib.error'>: Error -3 while decompressing data: invalid code lengths set
Exception caught when using password (2989): [b'coco']
<class 'zlib.error'>: Error -3 while decompressing data: invalid stored block lengths
Faulty passwords: [b'mariah', b'zebra', b'1977', b'hank', b'solomon', b'target', b'trish', b'coco']
Good passwords: [b'dominique']
ZipFile.extractall
代码,它尝试提取所有成员。第一个引发异常,因此它开始变得更清楚为什么会如此。但是,当尝试使用2个错误的密码提取项目时,为什么会有行为上的差异?从对2种不同引发的异常类型的追溯中可以看出,答案在
ZipFile.open
的末尾。
>>> import zipfile
>>>
>>> zd_coco = zipfile._ZipDecrypter(b"coco")
>>> zd_dominique = zipfile._ZipDecrypter(b"dominique")
>>> zd_other = zipfile._ZipDecrypter(b"other")
>>> cipher = b'\xd1\x86y ^\xd77gRzZ\xee' # Member (1st) file cipher: 12 bytes starting from archive offset 44
>>>
>>> crc = 2793719750 # Member (1st) file CRC - archive bytes: 14 - 17
>>> hex(crc)
'0xa684c7c6'
>>> for zd in (zd_coco, zd_dominique, zd_other):
... print(zd, [hex(zd(c)) for c in cipher])
...
<zipfile._ZipDecrypter object at 0x0000021E8DA2E0F0> ['0x1f', '0x58', '0x89', '0x29', '0x89', '0xe', '0x32', '0xe7', '0x2', '0x31', '0x70', '0xa6']
<zipfile._ZipDecrypter object at 0x0000021E8DA2E160> ['0xa8', '0x3f', '0xa2', '0x56', '0x4c', '0x37', '0xbb', '0x60', '0xd3', '0x5e', '0x84', '0xa6']
<zipfile._ZipDecrypter object at 0x0000021E8DA2E128> ['0xeb', '0x64', '0x36', '0xa3', '0xca', '0x46', '0x17', '0x1a', '0xfb', '0x6d', '0x6c', '0x4e']
>>> # As seen, the last element of the first 2 arrays (coco and dominique) is 0xA6 (166), which is the same as the first byte of the CRC
关于python - zipfile.BadZipFile:提取受密码保护的.zip和.zip时,提取时损坏了CRC-32错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54532010/
如果 zip 文件已损坏,则需要处理,因此它只传递此文件并继续处理下一个。 在下面的代码示例中,我试图捕获异常,以便传递它。但是当 zip 文件损坏时我的脚本失败*,并给我“正常”回溯错误*而不是打印
前一分钟我的脚本连续工作几天,下一分钟我收到此错误。 File "", line 1, in runfile('F:/-/-/-/cleaner_games_appstore_babil
我有两个 zip 文件,它们都可以用 Windows Explorer 和 7-zip 很好地打开。 但是,当我使用 Python 的 zipfile 模块 [ zipfile.ZipFile("fi
此代码是 Django 应用程序中代码的简化,该应用程序通过 HTTP 多部分 POST 接收上传的 zip 文件,并对内部数据进行只读处理: #!/usr/bin/env python import
当我运行这段代码时,我得到了以下 error 。我尝试了不同的技术来摆脱这个问题,但不能。 我可以请求可以帮助我解决这个问题的人。同样的问题有一些解决方案,但我无法得到他们的积分来解决它。 impor
我正在尝试提取受密码保护的.zip文件,其中包含一个.txt文件(对于这种情况,请说Congrats.txt)。现在Congrats.txt中包含文本,因此其大小不为0kb。将其放置在.zip中(出于
我试图打开一个 xlsx 文件和一个 xlsm 文件都给我同样的错误 badzipfile: file is not a zip file 这是我输入的内容: import openpyxl wb=o
我试图做的是将数据框数据附加到现有的合法 excel 文件中。我使用了 openpyxl 中的 load_workbook() 函数,但它系统地返回了一个错误。这是一些在我的机器上崩溃的代码: fro
我有一个 1.4GB 的 zip 文件,正在尝试连续生成每个成员。 zipfile 模块不断抛出 BadZipfile 异常,指出 "zipfile.BadZipfile: zipfiles that
我正在尝试创建一个 Python 脚本来测试受密码保护的 zip 文件的密码。唯一的问题是,每当我尝试专门传递“D”作为密码时,我都会得到 zipfile.BadZipFile: Bad CRC-32
我是一名优秀的程序员,十分优秀!