Nov-18-2017, 10:04 AM
For a school project, I'm working on brute force password guesser, but not just any brute force password guesser. The traditional way to make a brute force password guesser is to hard code a bunch of nested for loops:
Again, please observe the output from the nested for loop password guesser program carefully, and notice how the chars increment in each generated guess (just like an old car's odometer, the next char on the right points to the next char in AlphaNum each time the char on its left has incremented through every char in AlphaNum).
#!/usr/bin/env python3 #PasswordGuesser.py import sys AlphaNum = "abc" password = "cab" guessMatches = False guess = "" for char1 in AlphaNum: guess = char1 print(guess) if(guess == password): print("the password is " + guess) sys.exit() for char1 in AlphaNum: for char2 in AlphaNum: guess = char2 + char1 print(guess) if(guess == password): print("the password is " + guess) sys.exit() for char1 in AlphaNum: for char2 in AlphaNum: for char3 in AlphaNum: guess = char3 + char2 + char1 print(guess) if(guess == password): print("the password is " + guess) sys.exit() for char1 in AlphaNum: for char2 in AlphaNum: for char3 in AlphaNum: for char4 in AlphaNum: guess = char4 + char3 + char2 + char1 print(guess) if(guess == password): print("the password is " + guess) sys.exit()That code works (test it and see for yourself, so you can see the exact output I want to generate). However, I want to create a brute force password guesser that's recursive, and thus will have the power to generate all possible guesses of any length without the mess of for loops. Here's what I've come up with so far:
AlphaNum = "abc" password = "cab" guess = "" AN_size = AlphaNum[len(AlphaNum)-1] pastEnd = AlphaNum[len(AlphaNum)] def passwordGuesser(n, i):#guessList[n], AlphaNum[i] #while guess != password: guess = ''.join(guessList)#convert guessList back to a string to be tested if guess == password: print("The password is " + guess) sys.exit() else: guessList = list(guess)#convert from immutable string to changable list firstChar = guessList[0] lastChar = guessList[-1] nextChar = guessList[n+1] if guessList[n] <= AN_size and guessList[n] <= lastChar: guessList[n] = AlphaNum[i+1] elif guessList[n] == pastEnd and guessList[n] > lastChar: guessList[0] = AlphaNum[0] nextChar = AlphaNum[i+1] #Where do I go from here, if guessList is to become #more than 2 chars long? if lastChar == AN_size:#if last char in guess finally increments to match last char in AlphaNum replaceList.append(AlphaNum[0])#increase list length guessList = passwordGuesser(n, i)That code is untested, since I'm currently hitting a wall as described on lines 22 and 23.
Again, please observe the output from the nested for loop password guesser program carefully, and notice how the chars increment in each generated guess (just like an old car's odometer, the next char on the right points to the next char in AlphaNum each time the char on its left has incremented through every char in AlphaNum).