Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Password Brute Force
#1
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))
Reply
#2
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')]
>>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks, but will i have to do that with every single possible combination?
Reply
#4
(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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Reply
#6
any basic tutorial on loops would do. you want to change the number of repeats. do this using a loop.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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.
Reply
#8
import itertools
 
for item in itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=1):
    print(''.join(item))
    if item[0] == 'e':
        break
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
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
Reply
#10
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need an alternative to brute force optimization loop jmbonni 5 1,176 Dec-07-2023, 12:28 PM
Last Post: RockBlok
  Solving an equation by brute force within a range alexfrol86 3 2,783 Aug-09-2022, 09:44 AM
Last Post: Gribouillis
  force a program to exit ? Armandito 3 2,522 May-12-2022, 04:03 PM
Last Post: Gribouillis
  How to use scipy.optimization.brute for multivariable function Shiladitya 9 6,203 Oct-28-2020, 10:40 PM
Last Post: scidam
  best way to force an exception Skaperen 2 2,064 Oct-21-2020, 05:59 AM
Last Post: Gribouillis
  I need advise with developing a brute forcing script fatjuicypython 11 5,070 Aug-21-2020, 09:20 PM
Last Post: Marbelous
  Force calculation result as decimal vercetty92 4 2,865 Mar-20-2019, 02:27 PM
Last Post: vercetty92
  Brute Force Password Guesser 2skywalkers 1 3,181 Oct-05-2018, 08:04 PM
Last Post: ichabod801
  Brute Force Pad Lock Guesser RedSkeleton007 4 3,935 Mar-03-2018, 07:42 AM
Last Post: RedSkeleton007
  Speeding up Brute force password guesser Gamervote 5 6,816 Jul-20-2017, 02:52 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020