【问题标题】:Dictionary attack on SHA1 hash对 SHA1 哈希的字典攻击
【发布时间】:2018-09-28 01:39:17
【问题描述】:

以下是我的代码。我一直在尝试修复代码以对哈希(SHA-1)执行字典攻击,我得到以下结果。 附言我是编码初学者。

import hashlib
import random
#plug in the hash that needs to be cracked
hash_to_crack = "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"
#direct the location of the dictionary file
dict_file = "C:/Users/kiran/AppData/Local/Programs/Python/Python37/dictionary.txt"

def main():
    with open(dict_file) as fileobj:
        for line in fileobj:
            line = line.strip()
            if hashlib.sha1(line.encode()).hexdigest() == hash_to_crack:
                print ("The password is %s") % (line);
                return ""
    print ("Failed to crack the hash!")
    return ""


if __name__ == "__main__":
    main()

结果:

RESTART: C:/Users/kiran/AppData/Local/Programs/Python/Python37/Codes/datest1.py
The password is %s
Traceback (most recent call last):
  File "C:/Users/kiran/AppData/Local/Programs/Python/Python37/Codes/datest1.py", line 20, in <module>
    main()
  File "C:/Users/kiran/AppData/Local/Programs/Python/Python37/Codes/datest1.py", line 13, in main
    print ("The password is %s") % (line);
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

【问题讨论】:

  • 请不要使用大写锁定来写问题。另外,请花一些时间来解释您要做什么。要了解如何提出更好的问题,请查看how to ask 和on-topic。这将帮助其他人帮助你。如果您有任何疑问,请将您的代码提供为mvce。
  • 您可以使用hashcat 而不是自己尝试编程。

标签: python hash sha1 dictionary-attack


【解决方案1】:

您使用的是 Python 3,其中 print 是一个函数。行:

print ("The password is %s") % (line)

使用参数"The password is %s" 调用函数print。该函数返回None。然后None % (line) 给出你看到的错误信息。

最惯用的方式是这样写:

print("The password is", line)

其他可行的方法:

print("The password is %s" % line)
print(("The password is %s") % (line))

【讨论】:

    【解决方案2】:

    这一行不是有效的 python3 语法:(编辑:这是一个有效的语法!但不是你想要的 :))

    print ("The password is %s") % (line);
    

    改用这个:

    print ("The password is %s" % line)
    

    另外,根据错误消息,行可能是 None。

    【讨论】:

    • 查看我的回答:Python 3 中的语法很好——它根本没有按照他们希望的那样做 ;-) 如果语法错误,他们会得到一个 SyntaxError 反而。问题是 print() 函数返回 None,而 then None % a_string 没有意义(这是他们得到的错误消息)。
    猜你喜欢
    • 2017-10-18
    • 1970-01-01
    • 2012-06-24
    • 2011-06-10
    • 2017-10-30
    • 1970-01-01
    • 2011-07-24
    • 2011-01-28
    • 2011-06-23
    相关资源
    最近更新 更多