-1

So basically I am doing a brute-forcing script to guess a User's password, and I need to iterate through passwords and then use each password in the loop. So basically something like this:

for password in passwords_list:
    do_something_with_the_password({'Username': 'Joe', 'Password': password})

however since I will be getting the dictionary (e.g.: {'Username': 'Joe', 'Password': password}) from user input I will need to use the password variable before actually declaring it in my for loop. I tried using placeholders like password = None and then declaring it in the for loop like this:

password = None
data = {'Username': 'Joe', 'Password': password}  ## Got from user input
for password in passwords_list:
     data['Password'] = password
     do_something_with_the_password(data)

but this would be inconsistent because the for loop is actually a huge loop and I am also using multiprocessing so accessing the the dictionary would be troubling. I also tried using the eval() command but when I tested it, it significantly slowed my program. So is there anyway to do something like this successfully?

data = {'Username': 'Joe', 'Password': password}
for password in passwords_list:
    do_something_with_the_password(data)  ## With it actually using the password, for that specific loop, in the dictionary

  • Why are you collecting a password from the user only to then replace it with a password from the list? – jarmod Jul 14 '23 at 22:46
  • No, I am not collecting a password from the user I am collecting the data from user such as the Username (Joe in this example), and the keys to the username and the password variable used in the loop (Username and Password in this example) since the keys are going to be used in the script too, so they cannot be a hard-coded value in the code and the username must be provided from the user as well. I want the user to just specify password (without quotes) as a value to the key they want to guess its password. – LuckyCoder3607 Jul 14 '23 at 22:50
  • 1
    I'm lost. You might want to try and offer a more explicit, detailed explanation as it seems I'm not the only one. – AbeMonk Jul 14 '23 at 23:01
  • So basically the user is going to be asked for input of the dictionary, the user needs to specify the Username they wish to brute-force and the key value of the username and password since the key value will help with identifying where to actually put the username and password. Here is a few examples: The user wants to brute-force a login page running on the localhost which has two fields the username and the password, the username field's HTML name is 'uname' and the password field's name is 'pwd' so if the user wishes to guess the admin's password they would input --> – LuckyCoder3607 Jul 14 '23 at 23:08
  • ```{"uname": "admin", "pwd": password}``` . Another example the user wants to brute-force the website www.guessthepin.com to guess the pin on the after examining the webpage's HTML code they would need to provide: ```{"guess": password}``` as the dictionary and so on – LuckyCoder3607 Jul 14 '23 at 23:08
  • So my question is how could I allow the user to actually use password (without quotes) in the e.g. ```{"uname": "admin", "pwd": password}``` before declaring the password (using it in the for loop) so basically I want the user to be able to use the variable password before actually declaring that variable. I tried a few solutions as shown above but they did not work for me for the provided reasons. I hope that I explained my self well enough. – LuckyCoder3607 Jul 14 '23 at 23:11
  • Ah, that clarifies the "dictionary input" a lot. I do have a concern. When you said you were "multiprocessing", are you referring to running a concurrent for-loop? If so, you may want to check if Python dictionaries are thread safe, as that could be problematic (e.g. one thread could modify the dictionary before another is done). Also, where does the input come from. The terminal? This user input is collected as a string, and can't be used to declare variables. Last, if the password variable outside the loop and in the loop are entirely different, I would just use different names. – AbeMonk Jul 14 '23 at 23:15
  • When I referred to multiprocessing what I actually do is, so I have this one method that actually contains the for loop (I cannot provide it due to char limit) and this function calculates what passwords it needs to use with a mathematical process. So for example if I have two processes running and i have a password list of [1, 2, ,3 ,4] process one would try guessing 1 and 2, process 2 would try guessing 3 and 4, so the multiprocesses are not within the for loop they are the for loop (if that makes sense). and yes I guess it is problematic to modify python dicts especially while dealing --> – LuckyCoder3607 Jul 14 '23 at 23:23
  • with multiprocessing. and yes the input is getting taken from the terminal using ```input()``` however the password_list's path is getting read from the terminal (provided by user) and then the program reads that file. I can use the ```eval()``` function to make the user input a dictionary doing this one time would not hurt but doing this inside a for loop would slow down the process. however if i try to do this as soon as the user inputs the dictionary it would produce an error because the password is not defined. --> – LuckyCoder3607 Jul 14 '23 at 23:26

1 Answers1

1

If I understand what you're asking correctly, you're trying to dynamically set the key/value pairs of a dictionary in a for loop(?).

password_list = ["password", "password123"] # etc

# i.e. "Username"
key1 = input("First key")
# i.e. "Joe"
val1 = input("First val")
# i.e. "Password"
key2 = input("Second key")

for password in password_list:
    data = {key1: val1, key2: password}
    do_something(data)

Python will automatically insert "Username" for key1, "Joe" for val1, and "Password" for key2

Leshawn Rice
  • 617
  • 4
  • 13