【问题标题】:AttributeError With `argparse`带有`argparse`的AttributeError
【发布时间】:2019-06-23 04:56:58
【问题描述】:

所以我正在重写 TJ O'Connor 在 Violent Python 上发布的 ZIP Cracker,它是用 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 时,没有响应/输出等。

我该如何解决这个问题?

【问题讨论】:

  • 您将参数命名为--file,并且尝试访问args.dictionary。当然没有args.dictionary
  • 所以我应该使用args.file 而不是args.dictionary?但是话又说回来,这并不能解释当我使用 args.file 而不是 args.dictionary 时运行程序得到的空响应
  • 解密失败。
  • 看来你是对的@user2357112。但是我不明白为什么当我从raw.githubusercontent.com/ncorbuk/… 的密码中选择一个密码然后用它来制作一个简单的受密码保护的.zip 时它会失败。我将该粘贴为 .txt,另存为“最常见的 10k 密码”(如图所示)。不知道出了什么问题,因为我使用随机选择的几个词创建了一个类似的 zip 并且它有效。所以不太清楚为什么我不能使用更大的字典/密码列表/单词列表来暴力破解。

标签: python python-3.x argparse optparse


【解决方案1】:

使用代码的 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

但由于这 2 个参数是必需的,因此它们永远不会是 None

从那里的问题是zip 的值是否是有效zip 文件的名称,以及您是否可以打开dictionary

【讨论】:

  • 是的,我假设这触及了@user2347112 所说的内容,我使用args.dictionary 而不是args.file 对吗?但这仍然不能解释为什么我不能破译/暴力破解我使用来自 raw.githubusercontent.com/ncorbuk/… 的密码创建的 .zip,然后使用 .txt 暴力破解它
猜你喜欢
  • 2015-08-13
  • 2015-04-17
  • 2020-10-26
  • 2021-02-02
  • 2011-05-26
  • 2022-08-16
  • 2018-09-25
  • 2013-08-19
  • 2016-07-12
相关资源
最近更新 更多