Posts: 4
Threads: 1
Joined: Apr 2017
Hello Everyone, I only just signed up to post this question which will be the first of many.
I've only just started using python so I don't have much experience at all, I'm hoping you guys are friendly.
So..I've been playing with a piece of code and i've been researching and modifying it to do what I'd like to do, but for some reason it's not working.
I'm hoping someone might be able to figure this out. Here is the code:
import itertools
import string
def guess_password(real):
chars = string.ascii_lowercase + string.digits
#WORDLIST = "passwords.txt"
#inFile = open(WORDLIST, 'r')
#chars = inFile
attempts = 0
for password_length in range(1, 9):
for guess in itertools.product(chars, repeat=password_length):
attempts += 1
guess = ''.join(guess)
if guess == real:
return input('password is {}. found in {} guesses.'.format(guess, attempts))
print(guess, attempts)
print(guess_password(input("Enter password to crack"))) The part which has been commented out is what I'm trying to implement but for some reason it runs though all the words in the list without realizing that it has matched.
The passwords in the list are seperated by each line and are being read correctly from what I can see in the output. but when it reaches "abc123" it doesn't stop and say "Password found" or anything, it just keeps going. I have put return input to halt the program when it finishes so it doesn't automatically close.
Posts: 8,157
Threads: 160
Joined: Sep 2016
Apr-03-2017, 01:34 PM
(This post was last modified: Apr-03-2017, 01:35 PM by buran.)
well, it's not clear what you try to implement. If that is homework - say so and post the full assignment.
note that commented part, if not commented would overwrite chars
Posts: 4
Threads: 1
Joined: Apr 2017
(Apr-03-2017, 01:34 PM)buran Wrote: well, it's not clear what you try to implement. If that is homework - say so and post the full assignment.
note that commented part, if not commented would overwrite chars
Hi Buran,
Not homework at all. This is actually a bit more advance to what I have been taught currently but since I'm on holidays I'd like to study myself and try and make some programs.
the chars line was actually replaced with the chars line that I have commented out. I wanted to post both versions so you could see what I was trying to do.
The program currently runs by trying possible combinations like aa,ab,ac,ad etc. until it has guessed the password that the user inputted. What I want to do is modify it so it uses the external list of passwords in a text file and try and match it to what the user has inputted. but when the program runs, it doesn't recognise that it has made a match.
Let's say I input the password as "abc123"
The top of the list is "abc" then the next line is "abcd" then "abc123" but instead of stopping at "abc123" and saying that the password is found, it will continue checking passwords, I think this might have something to do with the (repeat=password_length) line, but I'm unsure.
Posts: 8,157
Threads: 160
Joined: Sep 2016
Apr-03-2017, 02:02 PM
(This post was last modified: Apr-03-2017, 02:03 PM by buran.)
1. open the file.
2. Read the file line by line and compare with user input. Also note that when reading the lines from the file they will have new line (cartridge return) at the end. So you need to strip this before comparing with the user input. e.f. \n
2a. As an alternative read the file's full content in the memory. Note that if the file is too big this could cause memory issues. cartridge return still issue in this case.
I would say your problem is because of the cartridge return at the end of each line
Posts: 2,953
Threads: 48
Joined: Sep 2016
If he loads the file in memory using file_object.readlines(), isn't this create a list from all the lines in the file, stripped?
Posts: 8,157
Threads: 160
Joined: Sep 2016
(Apr-03-2017, 05:28 PM)wavic Wrote: If he loads the file in memory using file_object.readlines(), isn't this create a list from all the lines in the file, stripped? no
with open('test.txt', 'r') as f:
print f.readlines() Output: ['line1\n', 'line2\n', 'line3\n']
Posts: 2,953
Threads: 48
Joined: Sep 2016
Yes, I forgot it. Didn't deal with files for a while
Posts: 4
Threads: 1
Joined: Apr 2017
(Apr-03-2017, 07:02 PM)buran Wrote: (Apr-03-2017, 05:28 PM)wavic Wrote: If he loads the file in memory using file_object.readlines(), isn't this create a list from all the lines in the file, stripped? no
with open('test.txt', 'r') as f:
print f.readlines() Output: ['line1\n', 'line2\n', 'line3\n']
Sorry buran for the late reply.
I have been trying to work with your strategy but I can't seem to figure it out.
What I have done is wrote a new txt file with just one password written in it, and the program works as expected. It just doesn't work with multiple passwords in the txt file.
I also tried using \n at the end of each password but that didn't work. and using as f: gave me some syntax error.
Could you modify my code to show your method? and how should my txt file look like? I have just opened Notepad++ and write a new password on each line which does seem to work but as I said it has issues with 2 or more lines.
Thanks
Posts: 8,157
Threads: 160
Joined: Sep 2016
Apr-04-2017, 12:12 PM
(This post was last modified: Apr-04-2017, 12:12 PM by buran.)
my example in the previous post was for wavic.
Here is one for you, where the passwords.txt file is read line by line and once a match is found it returns the number (of attempts)
assumes python3
def guess_password(real):
WORDLIST = "passwords.txt"
with open(WORDLIST, 'r') as in_file: # open the file for read
for attempt, line in enumerate(in_file, start=1): # read file line by line
if line.strip() == real: # compare line, stripped of the cartridge return, with real
return attempt # if match is found - return the number of attempts
return None # no match after the file is exhausted - return None. You can skip this line, but for clarity let explicitly return None.
if __name__ == '__main__':
my_guess = input("Enter password to crack: ") # take user input
is_found = guess_password(my_guess) # call the function and get the result
if is_found: # if function returned number, i.e. there is match and that evaluates to True
print('password is {}. found in {} guesses.'.format(my_guess, is_found))
else: # function return None (that evaluates to False)
print('passowrd {} not found in the file.'.format(my_guess))
Posts: 4
Threads: 1
Joined: Apr 2017
Hi Buran,
That code seems to work really well, I made some adjustments and managed to pull it off the way I wanted it to. But I have one more question.
What you wrote was very different to what I wrote, I actually couldn't replicate it by myself, So I just wanted to ask, was there something I could've done to my code which may have resolved my issue? if so, what changes should I have made?
Thank you again for all your help.
|