Advertisement
skip420

password_analyzer

Jan 14th, 2021
1,676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.25 KB | None | 0 0
  1. #python_password_analyzer.py
  2. #words.txt
  3. #/bin/bash
  4. # Brute-force and dictionary password analyzer
  5. # Dictionary mode uses the word list created with the word_grabber.py script in:
  6.  
  7. import random
  8. import time
  9. import json
  10.  
  11. # Choose which characters to use in password guesses, or use dictionary
  12. mode = "string_full"
  13.  
  14. #### Character lists to create passwords based on mode ####
  15. # Upper and lower case letters with numbers
  16. if mode == "string_full":
  17.     characters = ["s", "a", "t", "c", "b", "d", "e", "f", "w", "g", "h", "i", \
  18.                   "l", "p", "r", "m", "u", "n", "o", "j", "k", "x", "v", "y", \
  19.                   "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "q", "z", \
  20.                   "S", "A", "T", "C", "B", "D", "E", "F", "W", "G", "H", "I", \
  21.                   "L", "P", "R", "M", "U", "N", "O", "J", "K", "X", "V", "Y", \
  22.                   "Q", "Z"]
  23. # Lower case letters with numbers
  24. elif mode == "string_lower":
  25.     characters = ["s", "a", "t", "c", "b", "d", "e", "f", "w", "g", "h", "i", \
  26.                   "l", "p", "r", "m", "u", "n", "o", "j", "k", "x", "v", "y", \
  27.                   "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "q", "z"]
  28. # Numeric characters only for PIN-type passwords    
  29. elif mode == "numeric":
  30.     characters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  31.  
  32. elif mode == "dictionary": # Single run only, no bulk passwords
  33.     # Try words starting with each letter in this order. These are all sections in the word list
  34.     word_order = ["common", "s", "a", "t", "c", "b", "d", "e", "f", "w", "g", "h", "i", \
  35.                   "l", "p", "r", "m", "u", "n", "o", "j", "k", "x", "v", "y", "q", "z"]
  36.    
  37.     # Load the word file into words_list array
  38.     with open("words.txt") as word_holder:
  39.         words_list = json.load(word_holder)[0]
  40.         print(len(words_list))
  41.        
  42.     # Currently trying words starting with this letter in word list    
  43.     current_letter = word_order[0]
  44.     # Index of current word withing its letter's section
  45.     current_letter_index = 0
  46.  
  47. # Number of guesses for current password
  48. guesses = 0
  49. # Current guess from generate() function
  50. current_guess = ""
  51. # Number of guesses for all passwords
  52. all_guesses = 0
  53. # Number of passwords to crack (bulk password runs only work with brute force, not dictionary)
  54. reps = [0, 1]
  55. # Flag to stop guessing when attempt is sucessfull
  56. status = "ongoing"
  57.  
  58. # Length to start guessing, will increment as all combinations are tried
  59. password_length_to_start = 1
  60. # Current guessing length
  61. password_length = password_length_to_start
  62. # Length of target passwords to create for bulk runs
  63. password_length_to_generate = 3
  64.  
  65. # String to hold the target password
  66. target_password = ""
  67.  
  68. # Indexes for each character in a password
  69. digits = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}
  70.  
  71.  
  72. # Create a brute-force password guess using the values from digits array
  73. def generate_brute_force():
  74.     global target_password, current_guess, password_length, digits, status
  75.    
  76.     # Number of characters filled so far
  77.     char_count = 0
  78.     # Characters filled so far
  79.     current_guess = ""
  80.     # The current index in the digits array
  81.     index_counter = password_length
  82.    
  83.     # Create the guess according to current digits array    
  84.     while char_count < password_length:
  85.         char_count += 1
  86.         current_guess += characters[digits[char_count]]
  87.  
  88.     # Incerement the relative key in the index array for next run
  89.     digits_modded = "no"
  90.     while digits_modded != "yes":
  91.         # Increment end index value if not equal to length of character array
  92.         if digits[index_counter] < (len(characters) - 1):
  93.             digits[index_counter] += 1
  94.             digits_modded = "yes"
  95.  
  96.         # Otherwise move focus left if not already on first index in digits array    
  97.         else:
  98.             if index_counter > 1:
  99.                 # Reset value fo existing index
  100.                 digits[index_counter] = 0
  101.                 # Move index left
  102.                 index_counter -= 1
  103.                 # Increment the value for the new index
  104.                 if digits[index_counter] < (len(characters) - 1):
  105.                     digits[index_counter] += 1
  106.                     digits_modded = "yes"
  107.             else:
  108.                 # All combinations tried. Increase password length
  109.                 #  and reset counters
  110.                 password_length += 1
  111.                 for pos in digits:
  112.                     digits[pos] = 0                  
  113.                 digits_modded = "yes"
  114.                 print(status + ": " + current_guess)
  115.  
  116.                
  117. # Select a password guess from the word list
  118. def generate_dictionary_word():
  119.     global target_password, current_guess, status, current_letter, current_letter_index, guesses, all_guesses
  120.     # Retrieve current word from word list array
  121.     if len(words_list[current_letter]) > 0:
  122.         current_guess = words_list[current_letter][current_letter_index]
  123.     # Prepare for next guess
  124.     # Move to next index in the current letter array if not at end
  125.     if current_letter_index < (len(words_list[current_letter]) - 1):
  126.         current_letter_index += 1
  127.  
  128.     # If all words from that letter were tried    
  129.     else:
  130.         # Change to the next letter if any remaining
  131.         if word_order.index(current_letter) < (len(word_order) - 1):
  132.             current_letter = word_order[word_order.index(current_letter) + 1]
  133.             # Start from beginning of new letter section
  134.             current_letter_index = 0
  135.             print(current_letter)
  136.            
  137.         else:
  138.             #All words from all letters tried, bad luck
  139.             status = "not_found"
  140.             print("DONE: " + current_letter)
  141.             all_guesses += (guesses + 1)
  142.             guesses = 0
  143.  
  144. ### MAIN LOOP
  145. # Get the starting time to compare to end time for bulk runs  
  146. total_time = 0.0
  147.  
  148. # Until the quota of passwords have been cracked (Bulk runs)
  149. ## or the user enters an empty password (single runs)
  150. while reps[0] < reps[1]:
  151.     status = "ongoing"
  152.  
  153.    
  154.     if mode == "dictionary":
  155.         # Reset the indexes for the all_words array to zero
  156.         current_letter = word_order[0]
  157.         current_letter_index = 0
  158.     else:
  159.         # Reset the digits array indexes to zero
  160.         for pos in digits:
  161.             digits[pos] = 0
  162.         password_length = password_length_to_start
  163.     # If multiple runs, create random target_password for this run
  164.     if reps[1] > 1:
  165.         # Create a string from randomly chosen characters
  166.         target_password = ""
  167.         while len(target_password) < password_length_to_generate:
  168.             # Append a random index from the characters list to target password
  169.             target_password += random.choice(characters)
  170.         reps[0] += 1
  171.        
  172.     # For single runs, prompt for target password each time instead
  173.     else:
  174.         target_password = str(input("Enter a password to test\n"))
  175.        
  176.         # Leaving empty will exit main loop
  177.         if target_password == "":
  178.             reps[0] += 1
  179.             status = "stopped"
  180.             print(status)
  181.  
  182.     # Get the starting time for this run  
  183.     this_time = time.time()
  184.     guesses = 0
  185.     # Start guessing until correct
  186.     while status == "ongoing":
  187.         # Generate a new guess
  188.         guesses += 1
  189.         if mode == "dictionary":
  190.             generate_dictionary_word()
  191.         else:
  192.             generate_brute_force()
  193.         if current_guess == target_password:
  194.             elapsed = time.time() - this_time
  195.             status = "Cracked"
  196.             print(status + ": " + current_guess)
  197.             print(str(guesses) + " guesses, " + str(elapsed) + " seconds.\n___________\n")
  198.             all_guesses += guesses
  199.             total_time += elapsed
  200.  
  201.  
  202. # Calculate and display overall stats for bulk runs
  203. if reps[1] > 1:
  204.     print(str(all_guesses / reps[1]) + " guesses per password")
  205.     print(str(total_time / reps[1]) + " seconds per password")
  206.     print(str(all_guesses / float(total_time)) + " guesses per second")
  207.  
  208. ### MAIN LOOP
  209. ### Get the starting time to compare to end time    
  210. ##start_time = time.time()
  211. ##
  212. ##
  213. ##
  214. ##
  215. ##
  216. ### Until the quota of passwords have been cracked
  217. ##while reps[0] < reps[1]:
  218. ##    status = "ongoing"
  219. ##
  220. ##    # Brute-force
  221. ##    if mode != "dictionary":
  222. ##        # Reset the digits array to default
  223. ##        for pos in digits:
  224. ##            digits[pos] = 0
  225. ##
  226. ##        # If multiple runs, create random target_password for this run      
  227. ##        if reps[1] != 1:
  228. ##            # Generate target password to guess
  229. ##            target_password = ""
  230. ##            # Reset current password length to default starting length
  231. ##            password_length = password_length_to_start
  232. ##            # Number of characters generated so far
  233. ##            chars = 0
  234. ##            # Create a string from randomly chosen characters
  235. ##            while chars < password_length_to_generate:
  236. ##                # Append a random index from the characters list to target password
  237. ##                target_password += random.choice(characters)
  238. ##                chars += 1
  239. ##                
  240. ##    # Dictionary, single run, program has already prompted for target password    
  241. ##    else:
  242. ##        print(current_letter)
  243. ##        current_letter = word_order[0]
  244. ##        current_letter_index = 0
  245. ##            
  246. ##    # Start guessing
  247. ##    while status == "ongoing":
  248. ##        # Generate next password guess
  249. ##        if mode != "dictionary":
  250. ##            generate_brute_force()
  251. ##        else:
  252. ##            generate_dictionary_word()
  253. ##        if target_password == current_guess:
  254. ##            status = "Cracked"
  255. ##            print(status + ": " + current_guess + " in " + str(guesses + 1) + " guesses")
  256. ##            all_guesses += (guesses + 1)
  257. ##            guesses = 0
  258. ##        else:
  259. ##            guesses += 1
  260. ##    if status != "Cracked":
  261. ##        print(status)
  262. ##        print(str(all_guesses / reps[1]) + " guesses")
  263. ##    reps[0] += 1
  264. ##
  265. ### Calculate and display stats    
  266. ##average_time = (time.time() - start_time) / reps[1]
  267. ##print("Mode: " + mode)
  268. ##if reps[1] != 1:
  269. ##    print(str(all_guesses / reps[1]) + " guesses per password")
  270. ##    print(str(average_time) + " seconds per password")
  271. ##else:
  272. ##    print(str(average_time) + " seconds")
  273. ##
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement