因此,我正在重写由TJ O'Connor在 Violent Python 上发布的ZIP破解程序,该程序是在Python 2.7上编写的。作者使用了optparse
,但我使用了argparse
。
我的代码如下:
import argparse
from threading import Thread
import zipfile
import io
parser = argparse.ArgumentParser(description="Unzips selected .zip using a dictionary attack", usage="CRARk.py -z zipname.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/dictionary-list/password-list.")
args = parser.parse_args()
def extract_zip(zipFile, password):
try:
zipFile.extractall(pwd=password.encode())
print("[+] Password for the .zip: {0}".format(password) + "\n")
except:
pass
def main(zip, dictionary):
if (zip == None) | (dictionary == None):
print(parser.usage)
exit(0)
zip_file = zipfile.ZipFile(zip)
pass_file = io.open(dictionary, mode="r", encoding="utf-8")
for line in pass_file.readlines():
password = line.strip("\n")
t = Thread(target=extract_zip, args=(zip_file, password))
t.start()
if __name__ == '__main__':
# USAGE - Project.py -z zipname.zip -f file.txt
main(args.zip, args.dictionary)
我得到的错误是:
Traceback (most recent call last):
File "C:\Users\User\Documents\Jetbrains\PyCharm\Project\Project.py", line 39, in <module>
main(args.zip, args.dictionary)
AttributeError: 'Namespace' object has no attribute 'dictionary'
现在我不确定这意味着什么。我尝试将args.dictionary
重命名为args.file
或类似名称,但最终在运行代码时在终端上返回了空响应。如下图所示,当我正确运行.py时,没有响应/输出等。
我该如何解决?
答案 0 :(得分:0)
使用代码的argparse部分:
import argparse
parser = argparse.ArgumentParser(description="Unzips selected .zip using a dictionary attack", usage="CRARk.py -z zipname.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/dictionary-list/password-list.")
args = parser.parse_args()
print(args)
示例运行:
2033:~/mypy$ python3 stack54431649.py -h
usage: CRARk.py -z zipname.zip -f file.txt
Unzips selected .zip using a dictionary attack
optional arguments:
-h, --help show this help message and exit
-z , --zip Location and the name of the .zip file.
-f , --file Location and the name of the word-list/dictionary-
list/password-list.
1726:~/mypy$ python3 stack54431649.py
usage: CRARk.py -z zipname.zip -f file.txt
stack54431649.py: error: the following arguments are required: -z/--zip, -f/--file
1726:~/mypy$ python3 stack54431649.py -z zippy -f afile
Namespace(file='afile', zip='zippy')
这意味着我可以使用
访问值args.file # 'afile'
args.zip # 'zippy'
main(args.zip, args.file)
在main
if zip is None: # better than zip == None
但是由于这两个参数是必需的,因此它们永远不会是None
。
从那里开始的问题是zip
的值是否是有效zip
文件的名称,以及是否可以打开dictionary
。