A more efficient Bruteforce

Asked

Viewed 321 times

-1

I have a code I made in a few minutes that was supposed to be a "Bruteforce". works, but after 3 characters, it takes too long.

I had some ideas to make it more efficient, such as saving in a variable the password that has already been tested, or in a file. but that wouldn’t make much difference (besides spending too much machine resource). Is there any way to make it more efficient? even a simple password of 3 characters, all minusculas, facilitating in all forms, takes about 25 seconds

from time import time
start = time()
import random
#random.choice(alphabet)

alphabet = 'abcdefghijklmnopqrstuvwxyz'
maxLeng = 5
password = input('Password\n>')

possible = ''


while possible != password:
    possible = possible + random.choice(alphabet)

    if len(possible)>=maxLeng:
        possible=''

    if possible == password:
        print('The password is: '+str(possible))
        end=time()
        timet=end-start
        print('Found in '+str(int(timet))+' seconds') #  "str(int(timet))" para arrendondar o tempo
        break
    else:
        print(possible)
input()

An example of how inefficient it is: my script took 290,518 attempts to find "abc", a well-written script, by someone who knows something about it, took 704 attempts.

1 answer

0


Are you trying to randomly kick passwords using the random.choice(alphabet). This way you don’t keep any control over the passwords already tried, or the ordering of the kicks. I’m not sure about the technical term, but for me, kicking completely randomly wouldn’t even be called brute force.

Why don’t you try, kick the passwords in an orderly fashion? That is, first try "a", then "b" until "z". Then kick "aa", "ab" until "az". If you have difficulty understanding this logic, you can think as if you were counting: First we count from 0 to 9, then we count from 10 to 19, after 20 to 29, etc. To verify the password, we could also do if (password in respostasPossiveis): and we wouldn’t need this additional variable, nor this "finding", but in a didactic way, I preferred to compare one to one manually. To make the answers possible, I used list comprehension, but you can do it the way you want. Follow the example:

from time import time
start = time()
achado=0
alphabet = 'abcdefghijklmnopqrstuvwxyz'
maxLeng = 5
password = input('Password\n>')
respostasPossiveis=alphabet

for i in range(1,maxLeng+1):
    #print (respostasPossiveis)
    for x in respostasPossiveis:
        if (password == x):
            print('The password is: '+str(x))
            end=time()
            timet=end-start
            print('Found in '+str(int(timet))+' seconds') #  "str(int(timet))" para arrendondar o tempo
            achado=1;
            break
    if (achado==1 or maxLeng==i):
        break
    respostasPossiveis=[a+b for a in respostasPossiveis for b in alphabet]
input()

Of course it varies from computer to computer, but the longest password of all, "zzzzz," took 34 seconds here:

The password is: zzzzz
Found in 34 seconds

Browser other questions tagged

You are not signed in. Login or sign up in order to post.