Python Forum
brute force password from list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
brute force password from list
#1
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.
Reply
#2
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
Reply
#3
(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.
Reply
#4
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
Reply
#5
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?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(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']
Reply
#7
Yes, I forgot it. Didn't deal with files for a while
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(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
Reply
#9
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))
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need an alternative to brute force optimization loop jmbonni 5 1,185 Dec-07-2023, 12:28 PM
Last Post: RockBlok
  help me to make my password list in python >>> Oktay34riza 0 587 Dec-23-2022, 12:38 PM
Last Post: Oktay34riza
  Solving an equation by brute force within a range alexfrol86 3 2,812 Aug-09-2022, 09:44 AM
Last Post: Gribouillis
  force a program to exit ? Armandito 3 2,544 May-12-2022, 04:03 PM
Last Post: Gribouillis
  How to use scipy.optimization.brute for multivariable function Shiladitya 9 6,248 Oct-28-2020, 10:40 PM
Last Post: scidam
  best way to force an exception Skaperen 2 2,070 Oct-21-2020, 05:59 AM
Last Post: Gribouillis
  I need advise with developing a brute forcing script fatjuicypython 11 5,085 Aug-21-2020, 09:20 PM
Last Post: Marbelous
  Force calculation result as decimal vercetty92 4 2,873 Mar-20-2019, 02:27 PM
Last Post: vercetty92
  Password Brute Force 2skywalkers 9 5,385 Oct-18-2018, 02:35 PM
Last Post: buran
  Brute Force Password Guesser 2skywalkers 1 3,190 Oct-05-2018, 08:04 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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