I am trying to build a brute force password cracker (for educational purposes) but my code keeps returning an indexerror that says list index out of range on line 19. I do not know why this is happening. I would suspect it has something to do with the list insert with variables in it but I don't know.
import random
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
username = ("")
driver = webdriver.Chrome()
driver.get("site")
chars = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
password = ["a","a","a","a","a","a","a","a"]
list_index1 = 0
list_index2 = 0
list_insert = 0
list_pos = 0
while(driver.current_url != "logged in site"):
password[list_index2] = chars[list_index1]
driver.find_element_by_id("j_username").send_keys(username)
driver.find_element_by_id("j_password").send_keys(password)
driver.find_element_by_name("login").click()
list_index1 = 1
print (password)
if (list_index1 == 61):
listinsert = 1
list_pos = 1
list_index2.insert(list_pos, list_insert)
list_index1 = 0
print (list_index1)
CodePudding user response:
Edit: Oops, I see how the url is supposed to change now. It's when it logs in successfully, I get it. I have also realized that you probably just need to indent the if-statement below the while loop to fix your issue, since it looks like that's where you were trying to implement safeguards against the index error. Right now the if-statement is never reached inside the loop.
Old, completely wrong answer: The most immediate problem is that your while loop is infinite. Unless the url changes automatically after a couple seconds, nothing ever manually changes the url you're on and nothing breaks from the loop. Because of this, you just keep incrementing list_index1 forever until it's eventually larger than chars, causing the index error.
CodePudding user response:
There are a few issues in your code.
Firstly, within your while loop, you keep incrementing the value of list_index1 and assign chars[list_index1] to password[list_index2]. This means as the loop continues, your password follows the following pattern:
["0","a","a","a","a","a","a","a"]
["1","a","a","a","a","a","a","a"]
.
.
["z","a","a","a","a","a","a","a"]
Once you hit z, list_index1 will go out of range in the next iteration unless the correct password lies in one of the strings above.
Your if condition is outside the loop, so it does not reset the value of list_index1 back to 0.
Secondly, you might also want to pay attention to the following line:
list_index2.insert(list_pos, list_insert)`.
list_index2 is an integer in your code. Here you are treating it as a list.
