【问题标题】:incrementally increase letters through the alphabet通过字母逐渐增加字母
【发布时间】:2019-03-31 03:30:35
【问题描述】:

所以我一直在尝试为 php 表单创建一个简单的暴力脚本(当然在我自己的本地网络服务器上),它本质上是通过 for 循环将数据输入到列表中来工作的。这是问题所在;如果我的列表是:list = ['a','b','c','d','e','f','g'] 并且我的 for 循环看起来像这样:

for i in list:
   r = request.post(url, values) #posts the values
   if 'id=correct' in open('output.txt').read(): #check if it works
      print("logged in") 
      #if it works, it would print this, if not, 
      #it will retry with the next element in the list

问题是,这仅适用于一个字母的密码(因为它只在列表中循环一次)。我的问题是;我将如何获得它,以便一旦它在列表中循环一次,它将再次启动 for 循环,但列表中的两个元素除外(即 aa、ab、ac、ad、ae、ef 等)

谢谢!

【问题讨论】:

  • 查看这个问题的答案。stackoverflow.com/questions/7074051/… 基本上您需要做的就是生成所有 1-n 组合,其中 n 是您的密码字段接受的最大长度,然后一一尝试。不要尝试打印地图的结果,如果 n 值很大,您会耗尽内存。
  • 您可能还想检查r.content 以获取服务器响应

标签: python list


【解决方案1】:

如果我理解正确,您可以使用itertools

import itertools

lst = ['a','b','c','d']
for x in range(1, len(lst)): 
    passwords = list(itertools.product(lst, repeat=x))
    for password in passwords:
        print(''.join(password))

【讨论】:

  • 我认为 OP 正在寻找类似itertools.product(lst, repeat=x)
  • @tobias_k 我最初是这么认为的,然后决定使用排列,因为这似乎是蛮力攻击最合乎逻辑的?
  • 但这永远不会找到最常见的密码password,因为有两个s
  • @tobias_k 我认为你可能是平衡的,编辑谢谢:)
  • @tobias_k 也是一个有效的观点,但我想知道 OP 是否知道这种蛮力方法将以多快的速度变得无法大规模测试。从他们的出发点来看,我不确定他们是否真的要全力以赴,如果他们真的想对此进行测试,他们应该使用其他方法。
【解决方案2】:

您可以通过增加一个数字并将其转换为以 7 为底(列表的长度)来将此视为基本转换问题:

def convert(n, lst):
    s = ''
    while True:
        s = lst[n % len(lst)] + s
        if n < len(lst):
            break
        n = n // len(lst) - 1
    return s

这样:

lst = ['a','b','c','d','e','f','g']
print(convert(0, lst))
print(convert(6, lst))
print(convert(7, lst))
print(convert(8, lst))
print(convert(55, lst))
print(convert(56, lst))
print(convert(57, lst))

输出:

a
g
aa
ab
gg
aaa
aab

【讨论】:

  • 完美运行!感谢您的快速响应和明确的回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 2016-03-02
  • 2010-10-29
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多