#Mr Ryan's Brute Force demo
import string #used for listing characters used in attack
import time #used for measuring time
import itertools #used for generating

#######################################################################################
def getuserpassword():
#Take input from user
  flag = False
  password = ""
  while flag == False:
    password = input("Please enter a password to perform an attack on: ")
    if len(password) > 0: #prevents empty password from being accepted
      flag = True
      return password

#######################################################################################
def commonpasswordchecker(password):
### common password attack
  print("\n")
  print("MESSAGE: Checking for commonly used passwords...")
  commonpasswords = ["password", "pa55word", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789", "1234567890", "12345678910", "admin", "UNKNOWN", "123123", "111111", "Password", "Aa123456", "000000", "Admin123", "********", "user"]

  for i in range(0,len(commonpasswords)):
    if password == commonpasswords[i]:
      print ("Common password found is " + commonpasswords[i])
      return True

  return False

#######################################################################################
def bruteforcechecker(password):
  print("\n")
  print("MESSAGE: Now performing a Brute-Force Attack...")
  ### Brute-force attack
  #create list of characters to use in Brute-Force attack
  characterlibrary = string.ascii_letters + string.digits + string.punctuation

  #perform Brute-Force attack on unencrypted password of unknown length
  found = False
  possiblelength = 1
  overallattempts = 0
  while found == False:
    print("\n")
    print("Trying brute-force attack on password length of " + str(possiblelength))
    print("Attack library consists of " + str(characterlibrary))
    possibilities = len(characterlibrary) ** possiblelength
    print("There are " + str(possibilities) + " possible combinations")
    print("Starting brute-force attack... ")

    #create all possibilities
    attacklist = itertools.product(characterlibrary, repeat=possiblelength)
    attackcomplete = False
    starttime = time.time()
    attempts = 0
    overallattempts = overallattempts + attempts

    while attackcomplete == False:
      attempts = attempts + 1

      if attempts > possibilities:
        attackcomplete = True
        print("Password is not length = " + str(possiblelength))
        possiblelength = possiblelength + 1
      elif attempts <= possibilities:
        guess = ''.join(next(attacklist))
        print ("Attempt " + str(attempts) + " of " + str(possibilities) + " is checking " + guess)
        if password == guess:
          endtime = time.time()
          attackcomplete = True
          found = True
          print("Password is " + guess +  " and was found in " + str(endtime-starttime) + " seconds and " + str(attempts) + " attempts")

#######################################################################################
def advice(password):
  print("\n")
  print("MESSAGE: Generating password advice...")
  length_flag  = False
  if len(password) >= 8:
    length_flag  = True

  if length_flag == False:
    print("Your password should be at least 8 characters long")

  #check complexity of password (special, numbers, uppercase, lowercase) of password.

  #check for numbers
  number_flag = any(chr.isdigit() for chr in password)

  if number_flag == False:
    print("Your password should contain at least 1 number")

  #Check for uppercase
  upper_flag = any(chr.isupper() for chr in password)
  if upper_flag == False:
    print("Your password should contain at least 1 upper case character")

  #Check for lowercase
  lower_flag = any(chr.islower() for chr in password)
  if lower_flag == False:
    print("Your password should contain at least 1 lower case character")

  #Check for lowercase
  special_flag = any(not chr.isalnum() for chr in password)
  if special_flag == False:
    print("Your password should contain at least 1 special character")

#######################################################################################
password = getuserpassword()
mainmenu = True
while mainmenu == True:
  print("\n")
  print("###### Main Menu ######")
  response = input("Would you like to perform a (B)rute-force attack, (C)ommon Password Attack, get password (A)dvice, (S)et new password, or (Q)uit?")
  if response.lower() == "b":
    bruteforcechecker(password)
  elif response.lower() == "c":
    if commonpasswordchecker(password) == False:
      print("Common password not detected")
  elif response.lower() == "a":
    advice(password)
  elif response.lower() == "q":
    mainmenu = False
  elif response.lower() == "s":
    password = getuserpassword()
