Python Brute Force algorithm [closed]
Asked Answered
C

10

28

I need to generate every possible combination from a given charset to a given range. Like,

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

And the out put should be,

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

I know I can do this using already in use libraries.But I need to know how they really works.If anyone can give me a commented code of this kind of algorithm in Python or any programming language readable,I would be very grateful.

Cirsoid answered 31/7, 2012 at 19:9 Comment(15)
What have you tried? And is this homework? Shouldn't be super difficult...Commitment
What determines the length of the string at the end of the list?Thymol
list(map(str, "abc...")) is the most useless piece of code ever.Beaulieu
I recommend you go take a look at permuations() from itertools.Rhymester
That's ~2PB of data. I don't think you want to bruteforce this.Verbal
Aside: list() returns a list. map() returns a list, too. If your input really needs to be a list (which I doubt), use charset=list(string.lowercase)Thymol
You are essentially enumerating all numbers of ≤ k digits in base n, where n is the number of characters in the set. That’s a lot of combinations: n ^ k, actually.Atalayah
This would take up about 1.6 petabytes of ram. Iterating over it at a rate of 10 per millisecond would take about 11000 years.Butterfly
@SebastianPaaskeTørholm don't you mean combinations?Thymol
@kojiro: map returns a list in Python 2, but a generator in Python 3.Metalloid
@sr2222 Yes I'v tried,And I'v done it with numbers.When it comes to strings.I'v no luck.Cirsoid
@Metalloid that's true, but doesn't make the given code make sense. (Note I eliminated map altogether from my suggestion.)Thymol
@Thymol It looks like order matters from the output given. If not, then yes, combinations.Rhymester
@wug Ok,I don't need to run exactly like this one,i just need to know how bruteforce algo would workCirsoid
The question is perfectly well focused. It's also a common duplicate: see stackoverflow.com/questions/3099987.Gigue
C
26

If you REALLY want to brute force it, try this, but it will take you a ridiculous amount of time:

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

On a smaller example, where list = 'ab' and we only go up to 5, this prints the following:

['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']
Cailly answered 31/7, 2012 at 19:17 Comment(3)
And by ridiculous amount of time you mean it is intractable and will die out of memory before it completes, right?Thymol
It will also crash unless your computer has well over 4410270 Gigabytes of memory.Verbal
Wouldn't it be less memory-eating with the del statement in python 3? I know this is old by the way.Lycurgus
K
60

Use itertools.product, combined with itertools.chain to put the various lengths together:

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)))

Demonstration:

>>> 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']

This will efficiently produce progressively larger words with the input sets, up to length maxlength.

Do not attempt to produce an in-memory list of 26 characters up to length 10; instead, iterate over the results produced:

for attempt in bruteforce(string.ascii_lowercase, 10):
    # match it against your password, or whatever
    if matched:
        break
Keel answered 31/7, 2012 at 19:19 Comment(2)
@Madushan: to be honest, the answer you picked is not code I'd show my teacher..Keel
@Madushan: The meat of my method is the itertools.product method, which has a implementation listed in the documentation, so you can understand how we generate all possible combinations easily. What I prefer? When generating this amount of data, you really need to use generators like this.Keel
C
26

If you REALLY want to brute force it, try this, but it will take you a ridiculous amount of time:

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

On a smaller example, where list = 'ab' and we only go up to 5, this prints the following:

['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']
Cailly answered 31/7, 2012 at 19:17 Comment(3)
And by ridiculous amount of time you mean it is intractable and will die out of memory before it completes, right?Thymol
It will also crash unless your computer has well over 4410270 Gigabytes of memory.Verbal
Wouldn't it be less memory-eating with the del statement in python 3? I know this is old by the way.Lycurgus
O
7

I found another very easy way to create dictionaries using itertools.

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

This will iterate through all combinations of 'a','b','c' and 'd' and create combinations with a total length of 1 to 4. ie. a,b,c,d,aa,ab.........,dddc,dddd. generator is an itertool object and you can loop through normally like this,

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

Each password is infact of type tuple and you can work on them as you normally do.

Overwhelm answered 26/12, 2016 at 18:18 Comment(1)
This is not suitable for password creation since itertools skips possible outcomes. Example: look for all combinations between 'ab' for example which should be 4. aa, ba, ab and bb. Now itertools will only return 3 possibilites with combinations_with_replacement('ab', 2) such as: aa, ab, bb !Shuster
A
4

If you really want a bruteforce algorithm, don't save any big list in the memory of your computer, unless you want a slow algorithm that crashes with a MemoryError.

You could try to use itertools.product like this :

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) returns the cartesian products of the iterables you entered.

[i for i in product('bar', (42,))] returns e.g. [('b', 42), ('a', 42), ('r', 42)]

The repeat parameter allows you to make exactly what you asked :

[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')]

Note:

You wanted a brute-force algorithm so I gave it to you. Now, it is a very long method when the password starts to get bigger because it grows exponentially (it took 62 sec to find the word 'solved').

Amerson answered 22/1, 2018 at 19:34 Comment(0)
A
3

itertools is ideally suited for this:

itertools.chain.from_iterable((''.join(l)
                               for l in itertools.product(charset, repeat=i))
                              for i in range(1, maxlen + 1))
Affiche answered 31/7, 2012 at 19:21 Comment(0)
T
3

A solution using recursion:

def brute(string, length, charset):
    if len(string) == length:
        return
    for char in charset:
        temp = string + char
        print(temp)
        brute(temp, length, charset)

Usage:

brute("", 4, "rce")
Tetracycline answered 8/5, 2016 at 23:56 Comment(0)
F
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
Fatness answered 14/2, 2018 at 15:55 Comment(0)
P
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()
Portal answered 10/3, 2017 at 15:46 Comment(1)
Please do not post answers with pure code, but also add text to clarify how your solutions solves the problem.Chili
C
1

Simple solution using the itertools and string modules

# 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() 

I piped the output to a file to save ram, and used the input function so you can set the character limit to something like "hiiworld". Below is the same script but with a more fluid character set using letters, numbers, symbols, and spaces.

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()
Calicle answered 25/4, 2018 at 1:25 Comment(0)
N
-2

Try this:

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)
Niven answered 12/1, 2015 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.