Confused with Python and Need Help Debugging

Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).

Moderators: kgudger, bfinio, Moderators

BrendanStark
Posts: 14
Joined: Thu Nov 20, 2014 6:08 pm
Occupation: Student
Project Question: In my science project, I'm trying to make a password-guessing program but am having difficulties with Python.
Project Due Date: December 8th
Project Status: I am conducting my experiment

Confused with Python and Need Help Debugging

Post by BrendanStark »

Hi, for Science Fair I'm doing the "Password Security: How Secure is Your Password" project andtrying to make a password guessing program but I'm having difficulties with the programming. Can anyone help me with this? The following link is for the explination of the project and the code on bottom was from a template given from the project designer. If anyone could see anything weird in the code please help. Also, the main issue I had was that when I tried to run the program, it told me the passwords.txt file was not found (Error code shown below program code). Thanks a whole bunch for anyone who helps. I'd be good to finish this project in the next 2 weeks coming up.

-Brendan

Explination of Project: https://www.sciencebuddies.org/science- ... #procedure

Program Code

Code: Select all

#!/usr/bin/python

# This program is offered for use with the Science Buddies project idea
# "How Easily Can Your Password Be Hacked?" which lets you explore the
# makeup of a good password. This program will help you understand some
# methods that people use to guess other people's passwords.
#
# the program begins execution at 'main' after the helper programs and functions
# are defined. You'll also notice that several functions have places where 
# print() functions have been turned into comments. When debugging a program, it's
# helpful to add in more descriptive screen output than you might want later on.
# Once the program is working, you can remove the extra output os, as we've done here,
# 'comment it out' so that someone else can use it again later on if they decide to 
# modify your program.

#Tell Python we want to use some functions it doesn't always use
import sys, time, hashlib
from array import *

#--------------- global variables we expect will be used by any function -----------
#
# a number from 1 to 6 selects which password we'll be trying to guess from
# a selection below.
which_password = 0

# the user names and password we're trying to 'crack'. These will get written
password0 = ""
password1 = ""
password2 = ""
password3 = ""
password4 = ""
password5 = ""
password6 = ""


# total number of guesses we had to make to find it
totalguesses = 0


#--------------- extra helper functions -------------------
# These will be used by our search routines later on. We'll get these defined and out
# of the way. The actual search program is called "main" and will be the last one
# defined. Once it's defined, the last statement in the file runs it.
#
#


## Convert a string into MD5 hash
def MD5me(s):
    result = s.encode("utf-8")
    result = hashlib.md5(result).hexdigest()
    return result

# Takes a number from 0 on up and the number of digits we want it to have. It uses that
# number of digits to make a string like "0000" if we wanted 4 or "00000" if we wanted
# 5, converts our input number to a character string, sticks them together and then returns
# the number we started with, with extra zeroes stuck on the beginning. 
def leading_zeroes(n, zeroes):
    t=("0"*zeroes)+str(n)
    t=t[-zeroes:]
    return t

# check_userpass
def check_userpass(which_password, password):
    global password0, password1, password2, password3
    global password4, password5, password6
    
    result = False

    if (0 == which_password):
        if password == password0:
            result = True

    if (1 == which_password):
        if MD5me(password) == password1:
            result = True

    if (2 == which_password):
        if (MD5me(password) == password2):
            result = True

    if (3 == which_password):
        if (MD5me(password) == password3):
            result = True

    if (4 == which_password):
        if (MD5me(password) == password4):
            result = True
            
    if (5 == which_password):
        if (MD5me(password) == password5):
            result = True
            
    if (6 == which_password):
        if (MD5me(password) == password6):
            result = True
            
    return result

# This displays the results of a search including tests per second when possible
def report_search_time(tests, seconds):
    if (seconds > 0.000001):
        print ("The search took "+make_human_readable(seconds)+" seconds for "+make_human_readable(tests)+" tests or "+make_human_readable(tests/seconds)+" tests per second.")
    else:
        print ("The search took "+make_human_readable(seconds)+" seconds for "+make_human_readable(tests)+" tests.")
    return

# search method 1 will try using digits as the password.
def search_method_1(num_digits):
    global totalguesses
    result = False
    a=0
    #num_digits = 3    # How many digits to try. 1 = 0 to 9, 2 = 00 to 99, etc.
    starttime = time.time()
    tests = 0
    still_searching = True
    print("Using method 1 and searching for "+str(num_digits)+" digit numbers.")
    while still_searching and a<(10**num_digits):
        ourguess = leading_zeroes(a,num_digits)
        tests = tests + 1
        totalguesses = totalguesses + 1
        if (check_userpass(which_password, ourguess)):
            print ("Success! Password "+str(which_password)+" is " + ourguess)
            still_searching = False   # we can stop now - we found it!
            result = True
        #else:
            #print ("Darn. " + ourguess + " is NOT the password.")
        a=a+1

    seconds = time.time()-starttime
    report_search_time(tests, seconds)
    return result

# search method 2 is a simulation of a letter-style combination lock. Each'wheel' has the
# letters A-Z, a-z and 0-9 on it as well as a blank. The idea is that we have a number of
# wheels for a user name and password and we try each possible combination.
def search_method_2(num_pass_wheels):
    global totalguesses
    result = False
    starttime = time.time()
    tests = 0
    still_searching = True
    print("Using method 2 and searching with "+str(num_pass_wheels)+" password wheels.")
    wheel = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    # we only allow up to 8 wheels for each password for now
    if (num_pass_wheels > 8):
        print("Unable to handle the request. No more than 8 characters for a password")
        still_searching = False
    # set all of the wheels to the first position
    pass_wheel_array=array('i',[1,0,0,0,0,0,0,0,0])
        
    while still_searching:
        ourguess_pass = ""
        for i in range(0,num_pass_wheels):  # once for each wheel
            if pass_wheel_array[i] > 0:
                ourguess_pass = wheel[pass_wheel_array[i]] + ourguess_pass
        #print ("trying ["+ourguess_pass+"]")
        if (check_userpass(which_password, ourguess_pass)):
            print ("Success! Password  "+str(which_password)+" is " + ourguess_pass)
            still_searching = False   # we can stop now - we found it!
            result = True
        #else:
            #print ("Darn. " + ourguess + " is NOT the password.")
        tests = tests + 1
        totalguesses = totalguesses + 1
# spin the rightmost wheel and if it changes, spin the next one over and so on
        carry = 1
        for i in range(0,num_pass_wheels): # once for each wheel
            pass_wheel_array[i] = pass_wheel_array[i] + carry
            carry = 0
            if pass_wheel_array[i] > 62:
                pass_wheel_array[i] = 1
                carry = 1
                if i == (num_pass_wheels-1):
                    still_searching = False

    seconds = time.time()-starttime
    report_search_time(tests, seconds)
    return result

# This function takes in numbers, rounds them to the nearest integer and puts
# commas in to make it more easily read by humans
def make_human_readable(n):
    if n>=1:
        result = ""
        temp=str(int(n+0.5))
        while temp != "":
            result = temp[-3:] + result
            temp = temp[:-3]
            if temp != "":
                result = "," + result
    else:
        temp = int(n*100)
        temp = temp /100
        result = str(temp)
    return result
        
## A little helper program to remove any weird formatting in the file
def cleanup (s):
    s = s.strip()
    return s

## A little helper program that capitalizes the first letter of a word
def Cap (s):
    s = s.upper()[0]+s[1:]
    return s


# search method 3 uses a list of dictionary words. In this case, we have a list
# of the 500 most commonly used passwords in 2005 as collected by Mark Burnett
# for his book "Perfect Passwords" (ISBN 978-1597490412). Because the list comes
# from so many people around the world, we had to remove some of the passwords.
# People like to use passwords that they think will shock other people, so
# sometimes they're not fit for polite company.
def search_method_3(file_name):
    global totalguesses
    result = False
    
    # Start by reading the list of words into a Python list
    f = open(file_name)
    words = f.readlines()
    f.close
    # We need to know how many there are
    number_of_words = len(words)
    print("Using method 3 with "+str(number_of_words)+" in the list")
    
    ## Depending on the file system, there may be extra characters before
    ## or after the words. 
    for i in range(0, number_of_words):
        words[i] = cleanup(words[i])

    # Let's try each one as the password and see what happens
    starttime = time.time()
    tests = 0
    still_searching = True
    word1count = 0           # Which word we'll try next

    while still_searching:
        ourguess_pass = words[word1count]
        #print("Guessing: "+ourguess_pass)
        # Try it the way it is in the word list
        if (check_userpass(which_password, ourguess_pass)):
            print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
            still_searching = False   # we can stop now - we found it!
            result = True
        #else:
            #print ("Darn. " + ourguess_pass + " is NOT the password.")
        tests = tests + 1
        totalguesses = totalguesses + 1
        # Now let's try it with the first letter capitalized
        if still_searching:
            ourguess_pass = Cap(ourguess_pass)
            #print("Guessing: "+ourguess_pass)
            if (check_userpass(which_password, ourguess_pass)):
                print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
                still_searching = False   # we can stop now - we found it!
                result = True
            #else:
                #print ("Darn. " + ourguess_pass + " is NOT the password.")
            tests = tests + 1
            totalguesses = totalguesses + 1

        word1count = word1count + 1
        if (word1count >=  number_of_words):
            still_searching = False

    seconds = time.time()-starttime
    report_search_time(tests, seconds)
    return result
            
## Search method 4 is similar to 3 in that it uses the dictionary, but it tries two
## two words separated by a punctuation character
def search_method_4(file_name):
    global totalguesses
    result = False
    
    # Start by reading the list of words into a Python list
    f = open(file_name)
    words = f.readlines()
    f.close
    # We need to know how many there are
    number_of_words = len(words)
    
    ## Depending on the file system, there may be extra characters before
    ## or after the words. 
    for i in range(0, number_of_words):
        words[i] = cleanup(words[i])

    # Let's try each one as the password and see what happens
    starttime = time.time()
    tests = 0
    still_searching = True
    word1count = 0           # Which word we'll try next
    punc_count = 0
    word2count = 0

    punctuation="~!@#$%^&*()_-+={}[]:<>,./X"  # X is a special case where we omit
                                              # the punctuation to run the words together

    number_of_puncs = len(punctuation)
    print("Using method 4 with "+str(number_of_puncs)+" punc chars and "+str(number_of_words)+" in the list")

    while still_searching:
        if ("X" == punctuation[punc_count]):
            # If we're at the end of the string and found the 'X', leave it out
            ourguess_pass = words[word1count] + words[word2count]
        else:
            ourguess_pass = words[word1count] + punctuation[punc_count] + words[word2count]
        #print("Guessing: "+ourguess_pass)
        # Try it the way they are in the word list
        if (check_userpass(which_password, ourguess_pass)):
            print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
            still_searching = False   # we can stop now - we found it!
            result = True
        #else:
            #print ("Darn. " + ourguess_pass + " is NOT the password.")
        tests = tests + 1
        totalguesses = totalguesses + 1
        # Now let's try it with the first letter of the first word capitalized
        if still_searching:
            ourguess_pass = Cap(words[word1count]) + punctuation[punc_count] + words[word2count]
            #print("Guessing: "+ourguess_pass)
            if (check_userpass(which_password, ourguess_pass)):
                print ("Success! Passwword "+str(which_password)+" is " + ourguess_pass)
                still_searching = False   # we can stop now - we found it!
                result = True
            #else:
                #print ("Darn. " + ourguess_pass + " is NOT the password.")
            tests = tests + 1
            totalguesses = totalguesses + 1
        # Now let's try it with the first letter of the second word capitalized
        if still_searching:
            ourguess_pass = words[word1count] + punctuation[punc_count] + Cap(words[word2count])
            #print("Guessing: "+ourguess_pass)
            if (check_userpass(which_password, ourguess_pass)):
                print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
                still_searching = False   # we can stop now - we found it!
                result = True
            #else:
                #print ("Darn. " + ourguess_pass + " is NOT the password.")
            tests = tests + 1
            totalguesses = totalguesses + 1
        # Now let's try it with the both words capitalized
        if still_searching:
            ourguess_pass = Cap(words[word1count]) + punctuation[punc_count] + Cap(words[word2count])
            #print("Guessing: "+ourguess_pass)
            if (check_userpass(which_password, ourguess_pass)):
                print ("Success! Password "+str(which_password)+" is " + ourguess_pass)
                still_searching = False   # we can stop now - we found it!
                result = True
            #else:
                #print ("Darn. " + ourguess_pass + " is NOT the password.")
            tests = tests + 1
            totalguesses = totalguesses + 1

        word1count = word1count + 1
        if (word1count >=  number_of_words):
            word1count = 0
            punc_count = punc_count + 1
            if (punc_count >= number_of_puncs):
                punc_count = 0
                word2count = word2count + 1
                if (word2count >= number_of_words):
                    still_searching = False

    seconds = time.time()-starttime
    report_search_time(tests, seconds)
    return result


def main(argv=None):
    global password0, password1, password2, password3
    global password4, password5, password6, totalguesses
    global which_password

    # This is a place for you to set a password of your own
    password0 = "314"
    # Set up the passwords we want to crack. These must be MD5 hash
    # data blocks. Set them up using MD5me like:
    #     'password1=MD5me("ScienceBuddies")'
    #
    password1="202cb962ac59075b964b07152d234b70"
    password2="570a90bfbf8c7eab5dc5d4e26832d5b1"
    password3="f78f2477e949bee2d12a2c540fb6084f"
    password4="09408af74a7178e95b8ddd4e92ea4b0e"
    password5="2034f6e32958647fdff75d265b455ebf"
    password6="9b3af42d61cde121f40b96097fb77d3e"

    # start searching
    which_password = 1
    which_password = int(input("Which password (0-6)? "))
    overallstart = time.time()
    foundit = False
    print("Trying to guess password "+str(which_password))
    # Look through our list of common passwords first
    if not foundit:
        foundit = search_method_3("passwords.txt")
    # Still looking? Let's combine the common passwords 2 at a time
    if not foundit:
        foundit = search_method_4("passwords.txt")
    # Still looking? See if it's a single digit
    if not foundit:
        foundit = search_method_1(1)
    # Still looking? See if it's a 2 digit number
    if not foundit:
        foundit = search_method_1(2)
    # Still looking? See if it's a 3 digit number
    if not foundit:
        foundit = search_method_1(3)
    # Still looking? See if it's a 4 digit number
    if not foundit:
        foundit = search_method_1(4)
    # Still looking? Use our rotary wheel simulation up to 6 wheels.
    # This should take care of any 5 digit number as well as letter
    # combinations up to 6 characters
    if not foundit:
        foundit = search_method_2(6)
    # Still looking? Try 7 digit numbers
    if not foundit:
        foundit = search_method_1(7)
    # Still looking? Try 8 digit numbers
    if not foundit:
        foundit = search_method_1(8)
    seconds = time.time()-overallstart
    # When testing this project, some users reported that the next lines of code reported
    # an error when Python tried to divide by zero. On those machines, the clock seems
    # to think that the seconds calculation just above gave us "zero" seconds which doesn't
    # make any sense. To avoid the crash though, we'll test for that case and avoid the
    # problem.
    if (seconds < 0.00001):
        print ("The total search for all methods took "+make_human_readable(seconds)+" seconds and "+make_human_readable(totalguesses)+" guesses.")
        print ("(on some machines, Python doesn't know how long things actually took)")
    else:
        print ("The total search for all methods took "+make_human_readable(seconds)+" seconds and "+make_human_readable(totalguesses)+" guesses.("+make_human_readable(totalguesses/seconds)+" guesses per second)")

    if foundit:
        if (6 == which_password):
            print("Wow! Be sure to confirm your find at https://www.sciencebuddies.org/science-fair-projects/project_ideas/CompSci_p046/PasswordCrack.shtml")
        elif (0 == which_password):  # The Science Buddies website can't confirm passwords you added yourself
            print ("Your algorithm correctly guessed the password you entered. Try some others or see if you can make it guess faster.")
        else:
            print("You can confirm your find at https://www.sciencebuddies.org/science-fair-projects/project_ideas/CompSci_p046/PasswordCrack.shtml")

print ("Science Buddies: How Easily Can Your password Be Hacked?")
if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))


Error Code:

Code: Select all

Traceback (most recent call last):
  File "C:\Users\Chuchin\Documents\Programming\crack2 (1).py", line 446, in <module>
    sys.exit(main(sys.argv[1:]))
  File "C:\Users\Chuchin\Documents\Programming\crack2 (1).py", line 397, in main
    foundit = search_method_3("passwords.txt")
  File "C:\Users\Chuchin\Documents\Programming\crack2 (1).py", line 220, in search_method_3
    f = open(file_name)
FileNotFoundError: [Errno 2] No such file or directory: 'passwords.txt'
>>> 
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Confused with Python and Need Help Debugging

Post by HowardE »

Hi Brendan. The error message is telling you that when the program tried to open up the file 'passwords.txt', it couldn't find it. On the Materials tab of the project, in addition to the 'crack2.py' file that you downloaded, you should also have downloaded the 'passwords.txt' file. Simply put the two files together in the same directory. When you load the program into Python it should find the file as long as they're together. If you don't like the words in that file, feel free to make up your own.

And as a quick reminder, the passwords in the 'passwordN' variables are the passwords you want the program to try and guess. Passwords 1 through 6 as the code is written have to be encoded as MD5 hashed values. I did that so that I could offer you some sample passwords to crack without your knowing what they are. In the example you posted, you had removed all of the encoded ones. If you simply put your own passwords in those quotes, the program won't guess them unless you also change the code that does the comparison. As the code was provided, 'password0' is an unencoded password for you to test with. Putting an unencoded password in password1, for example, will always fail unless you change
if (1 == which_password):
if MD5me(password) == password1:
result = True

to

if (1 == which_password):
if password == password1:
result = True

Good luck with your project!

Howard
BrendanStark
Posts: 14
Joined: Thu Nov 20, 2014 6:08 pm
Occupation: Student
Project Question: In my science project, I'm trying to make a password-guessing program but am having difficulties with Python.
Project Due Date: December 8th
Project Status: I am conducting my experiment

Re: Confused with Python and Need Help Debugging

Post by BrendanStark »

Thanks a lot for the help! I'll try this tomorrow and see how it goes but the help is much appreciated!!!

-Brendan
BrendanStark
Posts: 14
Joined: Thu Nov 20, 2014 6:08 pm
Occupation: Student
Project Question: In my science project, I'm trying to make a password-guessing program but am having difficulties with Python.
Project Due Date: December 8th
Project Status: I am conducting my experiment

Re: Confused with Python and Need Help Debugging

Post by BrendanStark »

Okay.... The entire time I did have passwords.txt downloaded so, i put passwords.txt and the crack in the same folder but it still said it wasn't found... Any ideas on what to do? Also, I don't quite understand what you mean in your second paragraph... Because the code I had was the exact one I downloaded.
BrendanStark
Posts: 14
Joined: Thu Nov 20, 2014 6:08 pm
Occupation: Student
Project Question: In my science project, I'm trying to make a password-guessing program but am having difficulties with Python.
Project Due Date: December 8th
Project Status: I am conducting my experiment

Re: Confused with Python and Need Help Debugging

Post by BrendanStark »

Also... I have taken out and re-downloaded both files but it didn't work
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Confused with Python and Need Help Debugging

Post by HowardE »

I misread your posted code and thought you had removed the stored passwords from the program. They get set later on like this:
password1="202cb962ac59075b964b07152d234b70"
password2="570a90bfbf8c7eab5dc5d4e26832d5b1"
password3="f78f2477e949bee2d12a2c540fb6084f"
password4="09408af74a7178e95b8ddd4e92ea4b0e"
password5="2034f6e32958647fdff75d265b455ebf"
password6="9b3af42d61cde121f40b96097fb77d3e"
and these are the encrypted passwords you can guess.

Are you using IDLE3 to start Python? I've confirmed on a Mac, a Windows 8 machine and on a Linux machine that if I put the two files together I can start IDLE3 (the launcher for Python), open up 'crack2.py' and it properly finds the file. What might help in your particular configuration is if you start with a command prompt ('Terminal' in OSX or 'Command Prompt' in Windows) and go to the folder that contains the two files by using the 'cd' command. Once you can see the two files present, use that prompt to start Python or IDLE and see if it works better.

What sort of machine do you have and what version of the operating system? My machines are OSX 10.9, Windows 8.1 and Debian 6.0.
BrendanStark
Posts: 14
Joined: Thu Nov 20, 2014 6:08 pm
Occupation: Student
Project Question: In my science project, I'm trying to make a password-guessing program but am having difficulties with Python.
Project Due Date: December 8th
Project Status: I am conducting my experiment

Re: Confused with Python and Need Help Debugging

Post by BrendanStark »

I have a Windows 7 computer and yes, I am using Python 3
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Confused with Python and Need Help Debugging

Post by HowardE »

I just asked someone to try this on a Windows 7 machine and they had no problem, so there's something odd about the way you're loading your program or perhaps you renamed the file somehow. In any case, the important thing is to get you going, so here's a fix that will work. It's generally poor practice to hardcode in file locations to a program but that's what we'll do.

1) First, bring up the Command Prompt in Windows (the equivalent would be Terminal in OSX). Move around in the file system until you find the two files, 'crack2.py' and 'passwords.txt' in one place. Look at the prompt. It may say something like "C:\Users\howardeglowstein\Documents\CompSci_p046" in Windows or "/Users/howardeglowstein/CompSci_p046" in OSX. Write down that path. That's the location on your drive where the files live. If you don't see a path in the prompt, in Windows you can type the command 'cd' or in OSX the command 'pwd' to see the current directory/location/path.

2) Open up 'crack2.py' and locate the section where the password variables are initialized. It looks like this:

# the user names and password we're trying to 'crack'. These will get written
password0 = ""
password1 = ""
password2 = ""
password3 = ""
password4 = ""
password5 = ""
password6 = ""

3) We're going to add a few lines of code below this. Substitute in the appropriate path that you located and wrote down a minute ago and paste this into the file:

# Some users are reporting that Python can't find the file. Here's a hardcoded path
# to help the interpreter locate the file. MAKE SURE TO ONLY HAVE ONE OF THESE
# ACTIVE! COMMENT OUT ALL BUT ONE filePath!!
#
# Use this next ONE line for OSX on a Macintosh
#filePath = "/Users/howardeglowstein/CompSci_p046/passwords.txt" ## OSx path
#
# Use the next ONE line for Windows on a PC. Use a double backslash to mean a single. Backslashes have special meaning in Python
filePath = "C:\\Users\\howardeglowstein\\Documents\\CompSci_p046\\passwords.txt" ## Windows 8 path

As the comments remind you, you only want the path that's appropriate for your machine and the location will vary depending on how your machine is set up. Double check that the the path you've uncommented has the correct information in it. Edit it so that the path matches the one you found. In Windows, you have to use two backslashes to represent one.

4) Finally, find the section of code that looks like this: (the indentation is likely to be bruised in the forum message but follow the format in the program)

print("Trying to guess password "+str(which_password))
# Look through our list of common passwords first
if not foundit:
foundit = search_method_3("passwords.txt")
# Still looking? Let's combine the common passwords 2 at a time
if not foundit:
foundit = search_method_4("passwords.txt")

and replace the file name 'passwords.txt' with the filePath variable you filled in earlier. Like this:

print("Trying to guess password "+str(which_password))
# Look through our list of common passwords first
if not foundit:
foundit = search_method_3(filePath)
# Still looking? Let's combine the common passwords 2 at a time
if not foundit:
foundit = search_method_4(filePath)


This is a bandaid to be sure, but without seeing your machine or how you're starting up Python, I can't tell you what the problem is. This will fix it but it's not a very elegant solution. Part of computer program design is making code easy to reuse and move from place to place. What we're doing here isn't very pretty.
BrendanStark
Posts: 14
Joined: Thu Nov 20, 2014 6:08 pm
Occupation: Student
Project Question: In my science project, I'm trying to make a password-guessing program but am having difficulties with Python.
Project Due Date: December 8th
Project Status: I am conducting my experiment

Re: Confused with Python and Need Help Debugging

Post by BrendanStark »

Wait a sec! I just re downloaded but instead of renaming to passwords.txt I left it at passwords! Just a pretty stupid mistake I make but thanks for all the help!!!!! I'll look at this over once more tomorrow because I'm tired now but I think I've fixed it!
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Confused with Python and Need Help Debugging

Post by HowardE »

Welcome to Debugging 101 Brendan! :D

I'm still glad you brought this up because others may have similar questions and at least now the discussion is out there.

When something basic like the example doesn't work, it's always good to go back and CAREFULLY read through the directions, look at the spellings of things, learn how to interpret the error messages, etc. When it said the file was missing and you are pretty sure it's not, there will be a good explanation for it. I'm glad it was something as simple as the file extension. Please let me know if you bump into anything else.

Obviously, don't hardcode in the file location if you don't need to. It should work fine if you don't.

Howard
BrendanStark
Posts: 14
Joined: Thu Nov 20, 2014 6:08 pm
Occupation: Student
Project Question: In my science project, I'm trying to make a password-guessing program but am having difficulties with Python.
Project Due Date: December 8th
Project Status: I am conducting my experiment

Re: Confused with Python and Need Help Debugging

Post by BrendanStark »

OKay. one last issue... When I ran the code, something happened where when I put the passwords, there were no errors and everything worked, but it never told me where the password was. I know this is supposed to happen given I didn't identify the password so it basically doesn't know what it's cracking. Can you explain to me how this program works please? I literally JUST started programming and am a bit confused as to what I'm supposed to change and do so that I can make passwords and it guesses. In other words, can you help me comprehend the program? Because I'd like to have fun with it and see if I can change anything.
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Confused with Python and Need Help Debugging

Post by HowardE »

I'm glad things are working.

The easiest place to start is with password 0. It's defined in the program as:

# This is a place for you to set a password of your own
password0 = "314"

So you know what it is to start with. Run the program and answer '0' when it asks which password. You get back:

Science Buddies: How Easily Can Your password Be Hacked?
Which password (0-6)? 0
Trying to guess password 0
Using method 3 with 435 in the list [since there aren't any numbers in the password text file, this will fail for sure]
The search took 0.0 seconds for 870 tests or 953,999 tests per second.
Using method 4 with 26 punc chars and 435 in the list [combining multiple words together will still fail]
The search took 23 seconds for 19,679,400 tests or 872,705 tests per second.
Using method 1 and searching for 1 digit numbers. [it's not a single digit number]
The search took 0.03 seconds for 10 tests or 301 tests per second.
Using method 1 and searching for 2 digit numbers. [it's not a two digit number]
The search took 0.03 seconds for 100 tests or 2,998 tests per second.
Using method 1 and searching for 3 digit numbers. [it is a 3-digit number, so this test will find it]
Success! Password 0 is 314
The search took 0.06 seconds for 315 tests or 4,760 tests per second.
The total search for all methods took 23 seconds and 19,680,695 guesses.(856,763 guesses per second)
Your algorithm correctly guessed the password you entered. Try some others or see if you can make it guess faster.

What it told you in the last 4 lines is that it found the password, it only took 315 tests and it managed to find it in 23 seconds. So to experiment with your own passwords, put them in as password0 and try the program as is - OR - modify the program so that it searches differently. You will probably find that it has trouble with some passwords you think up. Why? Figure that out, write some code of your own as possibly a new method and see if your new code can find it.

I gave you 6 others as well that you can try to crack. The program will get 5 of the 6 without your help. It needs your creativity to help it find them faster and figure out that one last one...

Have fun with the project. Python is a fun language to learn as a first one. A hint - before you start writing a new algorithm to guess a password, think of how you'd do it yourself. If I asked you to guess my Science Buddies login password how might you do that? Arrange the steps in your head, write them out in order (possibly as a flowchart) and then use the examples provided and what you can find on the internet to write out the procedure in Python. Have your program print a lot of what it's doing at first to make sure it's doing what you want, then remove the print statements to speed things up. You'll see a number of commented print statements in the example.

Good luck and keep us posted!

Howard
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Confused with Python and Need Help Debugging

Post by HowardE »

I should clarify. It only took 315 tests once it tried the wheel dial method. Prior to that it tried the dictionary methods and failed, so that was the 19 million tests it ran for most of the 23 seconds. If it had tried the wheel dial method first it would have returned results in well under a second. I discounted the dictionary tests since in this case, those will never find a numerical password.
BrendanStark
Posts: 14
Joined: Thu Nov 20, 2014 6:08 pm
Occupation: Student
Project Question: In my science project, I'm trying to make a password-guessing program but am having difficulties with Python.
Project Due Date: December 8th
Project Status: I am conducting my experiment

Re: Confused with Python and Need Help Debugging

Post by BrendanStark »

Okay... Same thing happened when I put zero so everything works now. Now, I'm just confused with what an md5 hash is. I looked it up but was written in a pretty complicated manner.
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Confused with Python and Need Help Debugging

Post by HowardE »

I'm glad things are working. I think you'll find what you need with a bit of hunting. Here are two web pages i found that do a decent job of explaining it:

http://www.experts-exchange.com/Web_Dev ... 07231.html
http://www.unixwiz.net/techtips/iguide- ... ashes.html

I think you'll find there's a lot of fascinating stuff to learn on this project and you'll enjoy it more if you dig around and find it yourself. The short answer to your question is that an MD5 hash is essentially a fingerprint of sorts for a piece of data. The program has the fingerprints of 6 passwords. It will come up with a guess, make a fingerprint of it and compare the fingerprints. I did that so that you wouldn't know the answer before writing your program. I hope that makes sense.
Locked

Return to “Grades 9-12: Math and Computer Science”