Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Password Guesser Project
#11
(Nov-13-2017, 04:01 AM)metulburr Wrote: one is guest and the other is guess
LOL

But I have a new problem:
#!/usr/bin/env python3
#PasswordGuesser.py

AlphaNum = "abc"
passwordThatNeedsToBeGuessed = "cab"
guessMatches = False
guess = ""
guessList = list(guess)#make a list version of guess, so we can change the chars
guess = ''.join(guessList)#convert to a string, so we can print guess as a string
guessLength = len(guess)

##def iterateAlphaNum():
##    i = 0
##    while i < len(AlphaNum):
##        guess[0] == AlphaNum[i]
##        i += 1

guessLength = 1 #start with a guess length of just one char
char1 = 0 
for char1 in AlphaNum:    
    guessList[0] = AlphaNum[char1]
    char1 += 1    
    print(guess)
Error:
Traceback (most recent call last): File "I:\Python\Python36-32\SamsPrograms\PasswordGuesser.py", line 21, in <module> guessList[0] = AlphaNum[char1] TypeError: string indices must be integers
I also tried putting the int keyword before char1 on line 19, but an error said that was invalid syntax. This is a problem, because it's vital for this program to be able to dynamically reference chars from AlphaNum.
Reply
#12
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:

#!/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).
Reply
#13
Quote:
for char1 in AlphaNum:    
    guessList[0] = AlphaNum[char1]

What are you trying to do here?  char1 already is a character in AlphaNum.  So why not just guessList[0] = char1?
Reply
#14
(Nov-19-2017, 04:43 PM)nilamo Wrote:
Quote:
for char1 in AlphaNum:    
    guessList[0] = AlphaNum[char1]

What are you trying to do here?  char1 already is a character in AlphaNum.  So why not just guessList[0] = char1?
Maybe more comments in my code will make it easier for you to understand:

#!/usr/bin/env python3
#PasswordGuesser.py
import sys

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]
            currChar = guessList[n]#pointer to the current char
            
            nextChar = guessList[n+1]#pointer to next char (currChar must have reached last char in AlphaNum,
                                     #before the nextChar moves/points to the next char in AlphaNum)
            
            previousChar = guessList[n-1]#This is needed to check two things:
                                         #if the previous char is the first char (guessList[0]), and if so, then
                                         #guessList[0] will always reset to the first char in AlphaNum again before
                                         #guessList[1] moves/points to the next char in AlphaNum
                                         #OR, if the previous char is not guessList[0], then we just need to make sure
                                         #that the previous char has pointed to (has reached) the last char in AlphaNum
                                         #before it resets to AlphaNum[0] and makes the current char move/point to the
                                         #next char in AlphaNum.
            
            if currChar <= AN_size and currChar < lastChar and previousChar == pastEnd:#if the current char has not yet reached/pointed to the
                                                            #last char in AlphaNum, and the current char is not the last char of our current
                                                            #guessList, and the previous char has already reached the last char in AlphaNum,
                
                currChar = AlphaNum[i+1]#then make the current guessList char point to the next char in AlphaNum,
                previousChar = AlphaNum[0]#and reset the previous char to point to AlphaNum[0]
                
            elif currChar == pastEnd and currChar == lastChar:#otherwise, if the current guessList char has already pointed to every char
                                                              #in AlphaNum, and that current guessList char is the last (rightmost) char in
                                                              #our current guessList
                
                    replaceList.append(AlphaNum[0])#then increase the guessList length
                guessList = passwordGuesser(n, i)
Again, think about the old car odometer algorithm I spoke of in my previous post, as it's critical to understanding what I'm trying to do.
Reply
#15
I really sat down for a while and thought about the program. Here's my latest code:

#!/usr/bin/env python3
#SamsPasswordGuesser.py
import sys

AlphaNum = "abc"#temporary AlphaNum. Will add more characters after program works.
password = "cab"
guess = ""#default length is 0
AN_size = AlphaNum[len(AlphaNum)-1]
def passwordGuesser(n,i):#guessList[n], AlphaNum[i]
    guessList = list(guess)
    if len(guessList) < 1:
        guessList.append(AlphaNum[0])

    print(guess)#output current guess

    guess = ''.join(guessList)
    if guess == password:
        print("The password is: " + guess)
        sys.exit

    else:
        guessList = list(guess)
        guessList[n] = AlphaNum[i]#point current guessList char to the char in AlphaNum

        if guessList[n] < AlphaNum[-1]:
            i += 1
            guessList[n] = AlphaNum[i]

        elif i == (len(AlphaNum)-1):#if we are pointing to the last char in the AlphaNum string,
            guessList[n] = AlphaNum[i]#then apply it to the current guessList char.

        else:#if we have exhausted all possible chars in AlphaNum to be pointed to for the current guessList char,
            for n in range(0, guessList[n]):#then reset all characters (guessList elements) on the left to AlphaNum[0].
                guessList[n] = AlphaNum[0]

            #However, before we reset the current guessList char to AlphaNum[0], we need to check if we are already at
            #the end of guessList. If we are not, then the current guessList char also needs to be reset to AlphaNum[0],
            #and the next guessList index value to the right needs to point to the next char in AlphaNum. If we ARE already
            #at the end of guessList, then we need to make sure that we reset all chars in guessList to AlphaNum[0], and
            #increase the length of guessList by one for a new password guess length. This needs to be done with the following:

            if guessList[n] == guessList[-1] and guessList[n] == AlphaNum[-1]:#if we are at the last index of guessList,
                #and it is pointing to the last char in AlphaNum,
                guessList[n] = AlphaNum[i]#then apply it to the current guessList char. For example, a guess string of "ccc"
                #must be tested against the password before we test "aaaa" against the password.

            elif guessList[n] == guessList[-1] and guessList[n] < AlphaNum[-1]:#if we are at the last index of guessList,
                #but it has not yet pointed to the last char in AlphaNum,
                i += 1#then point to the next char in AlphaNum,
                guessList[n] = AlphaNum[i]#apply it to the current guessList index,
                guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
                #guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])

            elif guessList[n] < guessList[-1] and guessList[n] < AlphaNum[-1]:#if we are NOT at the last index of guessList,
                #and it has not yet pointed to the last char in AlphaNum,
                i += 1#then point to the next char in AlphaNum,
                guessList[n] = AlphaNum[i]#apply it to the current guessList index,
                guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
                #guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])

            elif guessList[n] < guessList[-1] and guessList[n] == AlphaNum[-1]:#if we are NOT at the last index of guessList,
                #but it IS pointing to the last char in AlphaNum,
                guessList[n] = AlphaNum[i]#then apply it to the current guessList char. For example, a guess string of "bcc"
                #must be tested against the password before we test "ccc" against the password.
                guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
                #guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])

            else:#if we are NOT at the last index of guessList, but we have exhausted all possible chars in AlphaNum to be pointed
                #to for the current guessList char,
                i += 1#then make a pointer to the next char in AlphaNum,
                n += 1#move to the next guessList char,
                guessList[n] = AlphaNum[i]#apply it to the current guessList char,
                for n in range(0, guessList[n]):#reset all characters (guessList elements) on the left to AlphaNum[0]
                    #(for example, "ccca" would become "aaab"),
                    guessList[n] = AlphaNum[0]
                guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
                #guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])

            passwordGuesser(n,i)#and the function begins again until the password is guessed!

def main():
    passwordGuesser(n,i)

main()
The current errors are:

Error:
Traceback (most recent call last): File "I:/Python/Python36-32/SamsPrograms/SamsPasswordGuesser.py", line 84, in <module> main() File "I:/Python/Python36-32/SamsPrograms/SamsPasswordGuesser.py", line 82, in main passwordGuesser(n,i) NameError: name 'n' is not defined
The way the program is planned out, the correct output needs to be:

a
b
c
aa
ba
ca
ab
bb
cb
ac
bc
cc
aaa
baa
caa
aba
bba
cba
aca
bca
cca
aab
bab
cab
the password is cab
Reply
#16
Quote:
def main():
    passwordGuesser(n,i)
 
main()

You call a function, passwordGuesser, and give it two parameters, n and i, neither of which are defined anywhere.
Reply
#17
Okay, first of all, what is wrong with line 86? Am I not allowed to call the main function where ever I want?

Second, initializing i and n to 0 on lines 9 and 10 doesn't make them global constants, does it?

Finally, the third error says that I'm referencing a local variable before assignment, and yet I've initialized the guess string to "" on line 7 before converting it to guessList.

#!/usr/bin/env python3
#SamsPasswordGuesser.py
import sys

AlphaNum = "abc"#temporary AlphaNum. Will add more characters after program works.
password = "cab"
guess = ""#default length is 0
AN_size = AlphaNum[len(AlphaNum)-1]
i = 0
n = 0
def passwordGuesser(n,i):#guessList[n], AlphaNum[i]
    guessList = list(guess)
    if len(guessList) < 1:
        guessList.append(AlphaNum[0])

    print(guess)#output current guess

    guess = ''.join(guessList)
    if guess == password:
        print("The password is: " + guess)
        sys.exit

    else:
        guessList = list(guess)
        guessList[n] = AlphaNum[i]#point current guessList char to the char in AlphaNum

        if guessList[n] < AlphaNum[-1]:
            i += 1
            guessList[n] = AlphaNum[i]

        elif i == (len(AlphaNum)-1):#if we are pointing to the last char in the AlphaNum string,
            guessList[n] = AlphaNum[i]#then apply it to the current guessList char.

        else:#if we have exhausted all possible chars in AlphaNum to be pointed to for the current guessList char,
            for n in range(0, guessList[n]):#then reset all characters (guessList elements) on the left to AlphaNum[0].
                guessList[n] = AlphaNum[0]

            #However, before we reset the current guessList char to AlphaNum[0], we need to check if we are already at
            #the end of guessList. If we are not, then the current guessList char also needs to be reset to AlphaNum[0],
            #and the next guessList index value to the right needs to point to the next char in AlphaNum. If we ARE already
            #at the end of guessList, then we need to make sure that we reset all chars in guessList to AlphaNum[0], and
            #increase the length of guessList by one for a new password guess length. This needs to be done with the following:

            if guessList[n] == guessList[-1] and guessList[n] == AlphaNum[-1]:#if we are at the last index of guessList,
                #and it is pointing to the last char in AlphaNum,
                guessList[n] = AlphaNum[i]#then apply it to the current guessList char. For example, a guess string of "ccc"
                #must be tested against the password before we test "aaaa" against the password.

            elif guessList[n] == guessList[-1] and guessList[n] < AlphaNum[-1]:#if we are at the last index of guessList,
                #but it has not yet pointed to the last char in AlphaNum,
                i += 1#then point to the next char in AlphaNum,
                guessList[n] = AlphaNum[i]#apply it to the current guessList index,
                guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
                #guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])

            elif guessList[n] < guessList[-1] and guessList[n] < AlphaNum[-1]:#if we are NOT at the last index of guessList,
                #and it has not yet pointed to the last char in AlphaNum,
                i += 1#then point to the next char in AlphaNum,
                guessList[n] = AlphaNum[i]#apply it to the current guessList index,
                guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
                #guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])

            elif guessList[n] < guessList[-1] and guessList[n] == AlphaNum[-1]:#if we are NOT at the last index of guessList,
                #but it IS pointing to the last char in AlphaNum,
                guessList[n] = AlphaNum[i]#then apply it to the current guessList char. For example, a guess string of "bcc"
                #must be tested against the password before we test "ccc" against the password.
                guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
                #guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])

            else:#if we are NOT at the last index of guessList, but we have exhausted all possible chars in AlphaNum to be pointed
                #to for the current guessList char,
                i += 1#then make a pointer to the next char in AlphaNum,
                n += 1#move to the next guessList char,
                guessList[n] = AlphaNum[i]#apply it to the current guessList char,
                for n in range(0, guessList[n]):#reset all characters (guessList elements) on the left to AlphaNum[0]
                    #(for example, "ccca" would become "aaab"),
                    guessList[n] = AlphaNum[0]
                guessList[n] = guessList[0]#and move back to the first index of guessList (without overwritting the current
                #guessList char we just got done making point to the char in AlphaNum with what's currently at guessList[0])

            passwordGuesser(n,i)#and the function begins again until the password is guessed!

def main():
    passwordGuesser(n,i)

main()
Error:
Traceback (most recent call last): File "I:\Python\Python36-32\SamsPrograms\SamsPasswordGuesser.py", line 86, in <module> main() File "I:\Python\Python36-32\SamsPrograms\SamsPasswordGuesser.py", line 84, in main passwordGuesser(n,i) File "I:\Python\Python36-32\SamsPrograms\SamsPasswordGuesser.py", line 12, in passwordGuesser guessList = list(guess) UnboundLocalError: local variable 'guess' referenced before assignment
Reply
#18
1) There's nothing wrong with that.  As long as the function you're calling exists, then you can call it.  And since you're calling it immediately after defining it, that's fine.

2) Defining them in the global scope makes them globals, but it doesn't make them constants.

3) You defined it in the global scope, but not within the function's scope.  There's a couple ways to fix this.  The easiest would be to simply move the definition so it's within the function, since you never use it outside the function, anyway.  The second, would be to re-define it within the function, thus hiding whatever value is in the global scope.  The third is to use the global keyword, to make it clear that you're using a variable from a higher scope level.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Brute Force Password Guesser 2skywalkers 1 3,140 Oct-05-2018, 08:04 PM
Last Post: ichabod801
  Brute Force Pad Lock Guesser RedSkeleton007 4 3,883 Mar-03-2018, 07:42 AM
Last Post: RedSkeleton007
  Speeding up Brute force password guesser Gamervote 5 6,746 Jul-20-2017, 02:52 PM
Last Post: nilamo
  Password Hacker Science Project BenjC 2 4,865 Apr-17-2017, 07:36 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

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