有人给我一个使用 python 解决的问题,其中指出:
We've started organising the files to try and make sense of them, but they're all locked with a numerical three-digit passcode.
See if you can write a script to get into this example file
alien-zip-2092.zip
and read the text file inside which we think is named whatever the zip is (so in this casealien-zip-2092.txt
).The files should be extracted to the
/tmp/
directory.
我的代码到目前为止还没有完成。它获取 zip 文件,并使用 0-999 之间的所有数字来提取它,但每次运行代码时我最终只得到 999
作为结果。无法检查文件夹是否已正确提取,因此我添加了 Print (password)
进行检查。
这是我的代码:
import zipfile
zf = zipfile.ZipFile("/tmp/alien-zip-2092.zip")
for password in range(0,1000):
try:
zf.extract(member="/tmp", pwd = str(i).encode()
password = 'Password found: %s' % password
except:
pass
print(password)
似乎我的程序正在用同名的空白文件覆盖正确的文件,因为在提取文件后它没有打破循环,但我可能是错的。
打印出应该位于压缩文件夹中的 txt 文件的内容是一个好主意,但如果其余代码不工作,这是无法完成的。
为什么没有打印正确的密码?有人还有其他想法来解决主要问题吗?
最佳答案
一些建议:
- 不要覆盖变量
password
,它是您的循环变量。而且i
似乎没有被定义,所以你可能想改变它。 - 我建议用前导零格式化较小的数字,以符合问题陈述(也许使用
'{:03d}'.format()
) - 使用空白 except 不是一个好主意,因为它可能隐藏可能发生的其他错误。
- 我建议使用
.extractall()
或者至少在.extract()
中指定正确的成员
和路径
。
这是代码的修改版本:
import zipfile
filename = 'alien-zip-2092'
zf = zipfile.ZipFile('{}.zip'.format(filename))
password = None
for i in range(1000):
try:
temp_password = '{:03d}'.format(i)
zf.extract(
member='{}.txt'.format(filename),
path='/tmp/',
pwd=temp_password.encode())
# extraction was successful
password = temp_password
break # exit the for loop
except zipfile.BadZipFile as err:
# print(err)
pass
if password is None:
print('No valid password found.')
else:
print('Password found: {}'.format(password))
关于python - 如何使用 Python 通过暴力破解提取 .zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55524169/