Python暴力算法

28

我需要根据给定的字符集和范围生成所有可能的组合。

charset=list(map(str,"abcdefghijklmnopqrstuvwxyz"))
range=10

输出应该是这样的:

[a,b,c,d..................,zzzzzzzzzy,zzzzzzzzzz]

我知道我可以使用已经存在的库来完成这个任务。但我需要知道它们是如何工作的。如果有人可以给我一个带注释的Python或任何可读的编程语言的代码段,我将不胜感激。


6
你尝试过什么?这是作业吗?它不应该太难... - Silas Ray
17
“list(map(str, "abc..."))”是最毫无意义的代码。 - JBernardo
19
那是大约2PB的数据。我觉得你不想使用暴力破解这个。 - Mooing Duck
2
附注:list()返回列表。map()也返回一个列表。如果您的输入确实需要是一个列表(我怀疑),请使用charset = list(string.lowercase) - kojiro
6
иҝҷе°ҶеҚ з”ЁеӨ§зәҰ1.6 PBзҡ„еҶ…еӯҳгҖӮд»ҘжҜҸжҜ«з§’10дёӘзҡ„йҖҹеәҰиҝӯд»Је®ғе°ҶйңҖиҰҒзәҰ11000е№ҙзҡ„ж—¶й—ҙгҖӮ - Wug
显示剩余10条评论
10个回答

60

使用itertools.productitertools.chain组合,将各个长度拼接在一起:

from itertools import chain, product
def bruteforce(charset, maxlength):
    return (''.join(candidate)
        for candidate in chain.from_iterable(product(charset, repeat=i)
        for i in range(1, maxlength + 1)))

演示:

>>> list(bruteforce('abcde', 2))
['a', 'b', 'c', 'd', 'e', 'aa', 'ab', 'ac', 'ad', 'ae', 'ba', 'bb', 'bc', 'bd', 'be', 'ca', 'cb', 'cc', 'cd', 'ce', 'da', 'db', 'dc', 'dd', 'de', 'ea', 'eb', 'ec', 'ed', 'ee']

这将高效地使用输入集生成逐渐增长的单词,直到长度达到最大值maxlength。

不要尝试生成一个内存中包含26个字符的列表,直到长度为10;相反,应迭代生成的结果:

for attempt in bruteforce(string.ascii_lowercase, 10):
    # match it against your password, or whatever
    if matched:
        break

5
说实话,你选择的答案不是我会向老师展示的代码。 - Martijn Pieters
@Madushan:我的方法核心是itertools.product方法,文档中有其实现列表,因此您可以轻松理解我们如何生成所有可能的组合。我更喜欢什么?在生成这么多数据时,确实需要使用像这样的生成器。 - Martijn Pieters

26

如果您真的想要暴力破解它,请尝试这个方法,但是这将需要您花费荒谬的时间:

your_list = 'abcdefghijklmnopqrstuvwxyz'
complete_list = []
for current in xrange(10):
    a = [i for i in your_list]
    for y in xrange(current):
        a = [x+i for i in your_list for x in a]
    complete_list = complete_list+a

在一个更小的例子中,如果列表为'ab',并且我们只处理到5,则会打印以下内容:

['a', 'b', 'aa', 'ba', 'ab', 'bb', 'aaa', 'baa', 'aba', 'bba', 'aab', 'bab', 'abb', 'bbb', 'aaaa', 'baaa', 'abaa', 'bbaa', 'aaba', 'baba', 'abba', 'bbba', 'aaab', 'baab', 'abab', 'bbab', 'aabb', 'babb', 'abbb', 'bbbb', 'aaaaa', 'baaaa', 'abaaa', 'bbaaa', 'aabaa', 'babaa', 'abbaa', 'bbbaa', 'aaaba','baaba', 'ababa', 'bbaba', 'aabba', 'babba', 'abbba', 'bbbba', 'aaaab', 'baaab', 'abaab', 'bbaab', 'aabab', 'babab', 'abbab', 'bbbab', 'aaabb', 'baabb', 'ababb', 'bbabb', 'aabbb', 'babbb', 'abbbb', 'bbbbb']

17
“ridiculous amount of time” 指的是时间太长,导致任务无法完成并且在它完成之前就已经被遗忘了,对吗? - kojiro
17
除非您的计算机拥有超过4410270GB的内存,否则它将崩溃。 - Mooing Duck
1
用 Python 3 的 del 语句会不会更节省内存呢?顺便说一下,我知道这已经是老话题了。 - Remigiusz Schoida

7

我发现使用itertools创建字典的另一种非常简单的方法。

generator=itertools.combinations_with_replacement('abcd', 4 )

这将迭代所有由'a'、'b'、'c'和'd'组成的组合,并创建总长度为1到4的组合,即a、b、c、d、aa、ab……dddc、dddd。生成器是一个itertools对象,您可以像这样正常循环遍历:

for password in generator:
        ''.join(password)

实际上,每个密码都是元组类型,您可以像通常一样处理它们。


这不适合用于密码创建,因为itertools会跳过可能的结果。例如:查找'ab'之间的所有组合,应该是4个。aabaabbb。现在,使用combinations_with_replacement('ab', 2),itertools只会返回3种可能性,如下:aaabbb - Daniel Biegler

4

如果您真的想要一个暴力算法,不要在计算机内存中保存任何大型列表,除非您想要一个速度慢且会因MemoryError而崩溃的算法。

您可以尝试使用itertools.product,像这样:

from string import ascii_lowercase
from itertools import product

charset = ascii_lowercase  # abcdefghijklmnopqrstuvwxyz
maxrange = 10


def solve_password(password, maxrange):
    for i in range(maxrange+1):
        for attempt in product(charset, repeat=i):
            if ''.join(attempt) == password:
                return ''.join(attempt)


solved = solve_password('solve', maxrange)  # This worked for me in 2.51 sec

itertools.product(*iterables)函数返回输入的可迭代对象的笛卡尔积。

[i for i in product('bar', (42,))]返回例如[('b', 42), ('a', 42), ('r', 42)]

repeat参数允许您精确地制作所需的内容:

[i for i in product('abc', repeat=2)]

返回结果:Returns
[('a', 'a'),
 ('a', 'b'),
 ('a', 'c'),
 ('b', 'a'),
 ('b', 'b'),
 ('b', 'c'),
 ('c', 'a'),
 ('c', 'b'),
 ('c', 'c')]

注意:

你需要一个暴力算法,所以我给了你。但是,当密码变得更长时,它会呈指数增长,因此这是一种非常耗时的方法(找到单词“solved”花费了62秒)。


3
使用递归的解决方案:
def brute(string, length, charset):
    if len(string) == length:
        return
    for char in charset:
        temp = string + char
        print(temp)
        brute(temp, length, charset)

使用方法:

brute("", 4, "rce")

3

itertools非常适合这种情况:

itertools.chain.from_iterable((''.join(l)
                               for l in itertools.product(charset, repeat=i))
                              for i in range(1, maxlen + 1))

2
import string, itertools

    #password = input("Enter password: ")

    password = "abc"

    characters = string.printable

    def iter_all_strings():
        length = 1
        while True:
            for s in itertools.product(characters, repeat=length):
                yield "".join(s)
            length +=1

    for s in iter_all_strings():
        print(s)
        if s == password:
            print('Password is {}'.format(s))
            break

1

使用itertools和string模块的简单解决方案

# modules to easily set characters and iterate over them
import itertools, string 

# character limit so you don't run out of ram
maxChar = int(input('Character limit for password: '))  

# file to save output to, so you can look over the output without using so much ram
output_file = open('insert filepath here', 'a+') 

# this is the part that actually iterates over the valid characters, and stops at the 
# character limit.
x = list(map(''.join, itertools.permutations(string.ascii_lowercase, maxChar))) 

# writes the output of the above line to a file 
output_file.write(str(x)) 

# saves the output to the file and closes it to preserve ram
output_file.close() 

我将输出导入文件以节省内存,并使用输入函数,因此您可以将字符限制设置为类似于“hiiworld”的内容。下面是相同的脚本,但使用字母、数字、符号和空格等更流畅的字符集。
import itertools, string

maxChar = int(input('Character limit for password: '))
output_file = open('insert filepath here', 'a+')

x = list(map(''.join, itertools.permutations(string.printable, maxChar)))
x.write(str(x))
x.close()

1
from random import choice

sl = 4  #start length
ml = 8 #max length 
ls = '9876543210qwertyuiopasdfghjklzxcvbnm' # list
g = 0
tries = 0

file = open("file.txt",'w') #your file

for j in range(0,len(ls)**4):
    while sl <= ml:
        i = 0
        while i < sl:
            file.write(choice(ls))
            i += 1
        sl += 1
        file.write('\n')
        g += 1
    sl -= g
    g = 0
    print(tries)
    tries += 1


file.close()

3
请勿仅发布纯代码答案,还需添加文字以说明您的解决方案如何解决问题。 - Lukas Körfer

-2

试试这个:

import os
import sys

Zeichen=["a","b","c","d","e","f","g","h"­,"i","j","k","l","m","n","o","p","q­","r","s","­;t","u","v","w","x","y","z"]
def start(): input("Enter to start")
def Gen(stellen): if stellen==1: for i in Zeichen: print(i) elif stellen==2: for i in Zeichen:    for r in Zeichen: print(i+r) elif stellen==3: for i in Zeichen: for r in Zeichen: for t in Zeichen:     print(i+r+t) elif stellen==4: for i in Zeichen: for r in Zeichen: for t in Zeichen: for u in Zeichen:    print(i+r+t+u) elif stellen==5: for i in Zeichen: for r in Zeichen: for t in Zeichen: for u in    Zeichen: for o in Zeichen: print(i+r+t+u+o) else: print("done")

#*********************
start()
Gen(1)
Gen(2)
Gen(3)
Gen(4)
Gen(5)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接