Advertisement
skip420

article_two_brute_force

Jan 14th, 2021
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.23 KB | None | 0 0
  1. # Use brute-force method to crack passwords containing lower-case letters and numbers.
  2. # From the second article in the Python Password Analizer  
  3. #article_two_brute_force.py
  4.  
  5. import random
  6. import time
  7.  
  8. # Characters to create random passwords
  9. characters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", \
  10.               "m", "n", "o", "p", "r", "s", "t", "u", "x", "y", "0", "1", \
  11.               "2", "3", "4", "5", "6", "7", "8", "9", "q", "v", "z"]
  12.  
  13. # The password to guess
  14. target_password = ""
  15.  
  16. # Set the length of the passwords to generate and guess for bulk runs
  17. password_length = 3
  18.  
  19. # Number of passwords to crack
  20. reps = [0, 1]
  21.  
  22. # Flag to stop guessing when attempt is sucessfull
  23. status = "ongoing"
  24.  
  25. # Number of guesses for all passwords
  26. all_guesses = 0
  27.  
  28. # Indexes for each character in a password up to eight characters
  29. digits = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}
  30.  
  31.  
  32. # Generate a password guess with values from digits array as index in the characters array
  33. def generate():
  34.     global password_length, digits, status
  35.     # Number of characters filled so far
  36.     char_count = 0
  37.     current_guess = ""
  38.     # The current key in the digits array
  39.     index_counter = password_length
  40.    
  41.     # Create the guess according to current digits array
  42.     while char_count < password_length:
  43.         char_count += 1
  44.         current_guess += characters[digits[char_count]]
  45.  
  46.     # Incerement the relative key in the digits array for next run
  47.     digits_modded = "no"
  48.     while digits_modded != "yes":
  49.         # Increment end character value if not equal to length of character array
  50.         if digits[index_counter] < (len(characters) - 1):
  51.             digits[index_counter] += 1
  52.             digits_modded = "yes"
  53.         # Otherwise move focus left if not already on first key in digits array
  54.         else:        
  55.             if index_counter > 1:
  56.                 # Reset value fo existing key
  57.                 digits[index_counter] = 0
  58.                 # Move left to next key
  59.                 index_counter -= 1
  60.                 # Increment the value for the new key
  61.                 if digits[index_counter] < (len(characters) - 1):
  62.                     digits[index_counter] += 1
  63.                     digits_modded = "yes"
  64.             else:
  65.                 # All combinations tried, give up
  66.                 digits_modded = "yes"
  67.                 status = "not_found"
  68.                 print(status, current_guess)
  69.     return current_guess
  70.  
  71.  
  72. # Get the starting time to compare to end time for bulk runs  
  73. total_time = 0.0
  74.  
  75. # Until the quota of passwords have been cracked
  76. while reps[0] < reps[1]:
  77.     status = "ongoing"
  78.     # Reset the digits array to default
  79.     for pos in digits:
  80.         digits[pos] = 0  
  81.        
  82.     # If multiple runs, create random target_password for this run
  83.     if reps[1] > 1:
  84.         # Create a string from randomly chosen characters
  85.         target_password = ""
  86.         while len(target_password) < password_length:
  87.             # Append a random index from the characters list to target password
  88.             target_password += random.choice(characters)
  89.         reps[0] += 1
  90.        
  91.     # For single runs, prompt for target password each time instead
  92.     else:
  93.         target_password = str(input("Enter a password to test\n"))
  94.         password_length = len(target_password)
  95.         # Leaving empty will exit main loop
  96.         if target_password == "":
  97.             reps[0] += 1
  98.             status = "stopped"
  99.  
  100.     # Get the starting time for this run  
  101.     this_time = time.time()
  102.     guesses = 0
  103.     # Start guessing until correct
  104.     while status == "ongoing":
  105.         # Generate a new guess
  106.         guesses += 1
  107.         if generate() == target_password:
  108.             elapsed = time.time() - this_time
  109.             status = "Cracked"
  110.             print(status + ": " + target_password)
  111.             print(str(guesses) + " guesses, " + str(elapsed) + " seconds.\n___________\n")
  112.             all_guesses += guesses
  113.             total_time += elapsed
  114.  
  115. # Calculate and display overall stats for bulk runs
  116. if reps[1] > 1:
  117.     average_time = total_time / reps[1]
  118.     print(str(all_guesses / reps[1]) + " guesses per password")
  119.     print(str(total_time / reps[1]) + " seconds per password")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement