BRUTE FORCE password hacker- How to add a minimum length to the brute force guesses?

Question:

I am trying to write code for an ethical hacking challenge. So far I have the following (see below). To speed it up I would like to set a minimum password length that it guesses based on the idea the hacker could seek the information from the website. How do I add this? I have made a start but I’m lost as to how to put it in the while guess function.

I have limited the characters it the code is working with whilst trialing aspects to speed it up.

import random
character = "0123456789abcdefgABCDEFG!?£#" #option to add capital letters and special characters ##only partial use of characters available to allow for speed when testing
character_list = list(character)
#website_pwd_length = "xxxx"
#min_length = len(website_pwd_length)
password= input('Enter password (Must contain 4 or more characters): ')
hack = ''
attempts = 0
while (hack != password):
    hack = random.choices(character_list,k=len(password))
    attempts += 1
    hack = "".join(hack)

print("Success! The password is {}. nThis was found in {} attempts.".format(hack, attempts))

I have commented out some of my initial attempts at establishing the length variable

Asked By: JoannaD

||

Answers:

To add a minimum password length to your password guessing program, you can modify the while loop to only generate passwords that are at least a certain length. Here’s one way to do it:

import random

# Define the characters that can be used in the password
character = "0123456789abcdefgABCDEFG!?£#"
character_list = list(character)

# Get the target password from the user
password = input('Enter password (must contain 4 or more characters): ')

# Define the minimum password length
min_length = 6

# Keep generating passwords until the correct one is found
hack = ''
attempts = 0
while (hack != password):
    # Generate a new password of at least min_length characters
    hack = random.choices(character_list, k=random.randint(min_length, len(password)))
    attempts += 1
    hack = "".join(hack)

print("Success! The password is {}. nThis was found in {} attempts.".format(hack, attempts))

In this modified code, we’ve added a min_length variable that specifies the minimum length of the generated passwords. Inside the while loop, we generate a new password by choosing characters at random from character_list using the random.choices() function, and we use random.randint() to determine the length of the password (which is at least min_length but no longer than the length of the target password password).

With this modification, the program will only generate passwords that meet the minimum length requirement, which should speed up the password guessing process. Feel free to adjust the min_length value to suit your needs.

Answered By: Bryan Carvalho