Posts: 26
Threads: 10
Joined: Jun 2018
Im trying to make a password Brute Force, it works to an extent but there is a problem.
Its goes from 'az' to 'bb' instead of 'ba'
Same with 'aaz' to 'abb' instead of 'aba'
Im not sure if im doing this password guesser completely wrong because it seems to simple, if so tell me please. i asked on another post how to do it, looked around for a few hours and still could not figure it out.
from itertools import combinations_with_replacement
for item in combinations_with_replacement('abcdefghijklmnopqrstuvwxyz', 1):
print(''.join(item))
for item in combinations_with_replacement('abcdefghijklmnopqrstuvwxyz', 2):
print(''.join(item))
for item in combinations_with_replacement('abcdefghijklmnopqrstuvwxyz', 3):
print(''.join(item))
Posts: 8,160
Threads: 160
Joined: Sep 2016
Oct-17-2018, 04:02 PM
(This post was last modified: Oct-17-2018, 04:02 PM by buran.)
you need itertools.product()
>>> import itertools
>>> list(itertools.product('abc', repeat=2))
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]
>>> list(itertools.combinations_with_replacement('abc', 2))
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]
>>>
Posts: 26
Threads: 10
Joined: Jun 2018
Thanks, but will i have to do that with every single possible combination?
Posts: 8,160
Threads: 160
Joined: Sep 2016
(Oct-17-2018, 04:03 PM)2skywalkers Wrote: Thanks, but will i have to do that with every single possible combination? no. do you know about loops, e.g. for loops?
Posts: 26
Threads: 10
Joined: Jun 2018
Yes, this is just the first time I've used itertools, I also want to be able to do it for combinations that are 1 char long, then go to two chars long, then 3 and so on, If you don't mind could you maybe link me to some tutorial that you think would be helpful for this certain scenario please? Thanks.
Posts: 8,160
Threads: 160
Joined: Sep 2016
any basic tutorial on loops would do. you want to change the number of repeats. do this using a loop.
Posts: 26
Threads: 10
Joined: Jun 2018
Oct-17-2018, 07:18 PM
(This post was last modified: Oct-17-2018, 07:18 PM by 2skywalkers.)
import itertools
for item in list(itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=1)):
print(''.join(item))
if item == 'e':
break I want it to stop printing when it gets to the letter 'e', but it keeps going. Not really sure how to fix this.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Oct-17-2018, 07:57 PM
(This post was last modified: Oct-17-2018, 07:57 PM by buran.)
import itertools
for item in itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=1):
print(''.join(item))
if item[0] == 'e':
break
Posts: 26
Threads: 10
Joined: Jun 2018
Oct-18-2018, 01:02 PM
(This post was last modified: Oct-18-2018, 01:03 PM by 2skywalkers.)
Thanks, this makes A LOT of sense. I would like some feedback on this code if you don't mind please, thanks. I feel like there is a better way of doing this using a loop or something like that, but im still trying to figure that out.
import itertools
password = 'dog'
for item in list(itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=1)):
print(''.join(item))
if(''.join(item)) == password:
break
elif(''.join(item)) == 'z':
for item in list(itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=2)):
print(''.join(item))
if(''.join(item)) == password:
break
elif(''.join(item)) == 'zz':
for item in list(itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=3)):
print(''.join(item))
if(''.join(item)) == password:
break
elif(''.join(item)) == 'zzz':
for item in list(itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=4)):
print(''.join(item))
if(''.join(item)) == password:
break
Posts: 8,160
Threads: 160
Joined: Sep 2016
Oct-18-2018, 02:35 PM
(This post was last modified: Oct-18-2018, 02:35 PM by buran.)
when you see repetitive code, you should think something is not right.
As I already said - use loop to change the number of repeats. It can be for loop or while loop. e.g. if you want to check only 1, 2, 3, 4 letter words (finite length of the password), you may use for loop. You may use while loop if you don't want to limit max length of the password you check.
from itertools import product
my_pass = 'sdxa' # that is my password. i.e. the word you are looking for
found_it = False
for pass_length in range(1, 5): # check only for password with length 1, 2, 3, 4
for item in product('abcdefghijklmnopqrstuvwxyz', repeat=pass_length):
my_guess = ''.join(item)
if my_guess == my_pass:
print 'Password is {}'.format(my_guess)
found_it = True
break
if found_it:
break Now, this is not best approach. In real life I would do it a bit differently. e.g. one will separate the password generation in a function/generator, etc. I show it like this in order to be easier for you to understand what is going on. Also in this case you know the length of passwords, so it does not make sense to check for 1 letter guess_word, when you know the password is 4 letter word. In real life you will not know the length of password
|