【问题标题】:Make password/hash verification algorithm more efficient使密码/哈希验证算法更高效
【发布时间】:2018-11-24 08:29:53
【问题描述】:

我试图从 etc/shadow 文件中猜测密码(它有 43 个用户/密码)。我得到了一些关于密码的提示:

  • 长度介于 4 到 8 个字符之间
  • 只能有 2 个数字,并且只能在末尾​​li>
  • 开头只有大写字母

所以我从一个由 4 个字符和 2 个数字组成的小组开始。但是处理起来需要很多时间:

import crypt
import string
import itertools
import datetime

dir = "shadow3"
file = open(dir, 'r').readlines() #Read all the 43 hashes

username = []
hashed = []
c = 0
cc = 0

for x in file: #Split the hash and the username
    usr, hshd, wtf, iss, this, thing, here, doing, example = x.split(':')
    username.append(usr)
    hashed.append(hshd)
#GRUPO1 4 caracteres 2 numeros
letters = string.ascii_lowercase
digits = string.digits
grupo1=[]
group1=itertools.product(letters,repeat=2)
group1b=itertools.product(digits,repeat=2)
for x in itertools.product(group1,group1b):  #Join the possible iterations
  string=''.join([''.join(k) for k in x])
  grupo1.append(string)
print(len(grupo1))
for y in grupo1:#Get the one of the iterations and try it 
  prueba=y
  for x in hashed: #Verify if that iteration is the password to any of the 43 users
    rehashed = crypt.crypt(prueba, x)
    if rehashed == x: #Password is found
        print('La contraseña del usuario ' + username[c] + ' es ' + prueba)
        cc = 1
    c = c + 1
if cc == 0: #after all iterations password is not found
    print('Lo sentimos "' + prueba + '" no es la contraseña de ningun usuario')

如何提高效率?如果它有助于任何类型的 GPU 处理,我有一个 GTX 1070。

【问题讨论】:

  • 如果您的代码可以正常工作,Code Review 可能是一个更好的询问地点。 Stack Overflow 在不工作的代码中社会化。

标签: python hash coding-efficiency


【解决方案1】:

我相信您的代码有错误。您的问题可以重新建模,以便更快:

根据您的条件生成所有可能的密码组合 - 长度在 4 到 8 个字符之间 - 只能有 2 个数字,并且只能在末尾 - 仅在开头大写字母

在每个组合上生成crypt() 比较密码与加密值。

【讨论】:

    【解决方案2】:

    幸运的是,Python 附带了一个分析器来帮助解决此类问题。查看 cProfile 文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      相关资源
      最近更新 更多