hash_v.1.0.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python3
  2. import sys
  3. from argparse import ArgumentParser
  4. import hashlib
  5. import os.path
  6. import time
  7. from multiprocessing import Process, current_process, Queue
  8. # Several hash types(other existing hash types can be added)
  9. def hash_function(hash_type, object):
  10. hashed_file = {
  11. 'sha256': hashlib.sha256(object).hexdigest(),
  12. 'sha512': hashlib.sha512(object).hexdigest(),
  13. 'md5': hashlib.md5(object).hexdigest()
  14. }
  15. if hash_type.lower() in hashed_file.keys():
  16. return hashed_file[hash_type.lower()]
  17. else:
  18. print('[!] Wrong or non existing hash type')
  19. sys.exit(1)
  20. # Get the CheckSum of the whole file
  21. def hash_of_file(hash_type, path):
  22. with open(path, 'rb') as file:
  23. print('[+] Hash of file:\n'+hash_function(hash_type, file.read()))
  24. # Hashed passwords list generator
  25. def read_file_with_passwds(start_index, end_index, queue, hash_type, input_path):
  26. print('[+] Process: '+current_process().name+' Started')
  27. hash = []
  28. # Open file with passwords in plain text and read lines
  29. with open(input_path, 'r') as input_file:
  30. passwds = input_file.read().splitlines()
  31. # Making pair of Password:Hash(exmpl. qwerty:65e84be3...)
  32. for word in range(start_index, end_index):
  33. hash.append(passwds[word] + ':' + hash_function(hash_type, passwds[word].encode()) + '\n')
  34. # To read the file correctly while processing put our passwd:hash pair to queue
  35. queue.put(hash)
  36. print('[+] Process: '+current_process().name + ' Finished')
  37. # After creating list of passwd:hash pair write it in output file
  38. def write_hashes_to_file(hash_list, output_path):
  39. with open(output_path, 'a') as output_file:
  40. for word in hash_list:
  41. output_file.write(word)
  42. # Main function to create and start multiple processors to generate rainbow table
  43. def processing_rainbow_generator(hash_type, input_path, output_path, count):
  44. # Check if file exists before writing in
  45. if os.path.exists(output_path):
  46. print('[!] Output file cannot be used because it already exists')
  47. sys.exit(1)
  48. else:
  49. # Read the whole file to detect number of passwords
  50. with open(input_path, 'r') as file:
  51. file_len = len(file.readlines())
  52. # Arguments to get number of passwords for each process
  53. # For example, number of process: 5, number of passwords: 1000
  54. # 1000/5=200, 200 passwords for each process
  55. start_index, end_index = 0, 0
  56. # List of process
  57. procs = []
  58. q = Queue()
  59. # Creating number(count) of processes
  60. for i in range(count):
  61. end_index += file_len / count
  62. p = Process(target=read_file_with_passwds, args=(int(start_index), int(end_index), q, hash_type, input_path))
  63. procs.append(p)
  64. start_index += file_len / count
  65. # Starting processes
  66. for i in range(count):
  67. procs[i].start()
  68. # Writing passwd:hash pair to output file
  69. for i in range(count):
  70. # Get and write the value in queue
  71. write_hashes_to_file(q.get(), output_path)
  72. # Waiting for process to finish
  73. for i in range(count):
  74. procs[i].join()
  75. print('[+] Rainbow table generated successfully')
  76. if __name__ == '__main__':
  77. # Available arguments(in cmd type -h for help)
  78. parser = ArgumentParser()
  79. parser.add_argument('-s', '--hash', help="sha256/md5/sha512", default='', required=True)
  80. parser.add_argument('-w', '--word', help="Random string to get hash", default='')
  81. parser.add_argument('-f', '--file', help="/home/kali/somefile", default='')
  82. parser.add_argument('-i', '--input', help="Rainbow table generator input file: /home/kali/passwords.txt", default='')
  83. parser.add_argument('-o', '--output', help="Rainbow table generator output file: /home/kali/rainbow.txt(default rainbow_table.txt)", default='rainbow_table.txt')
  84. parser.add_argument('-p', '--procs', help="Rainbow table generator: Number of used processes(default 5)", type=int, default=5)
  85. args = parser.parse_args()
  86. # Hash of simple string
  87. if args.word is not '':
  88. print('[+] Hash of word:\n'+hash_function(args.hash, args.word.encode()))
  89. # Hash of file
  90. elif args.file is not '':
  91. try:
  92. hash_of_file(args.hash, args.file)
  93. except FileNotFoundError:
  94. print('[!] Not existing file or path')
  95. # Creating rainbow table(passwd:hash pair), options output and procs have default values
  96. elif args.input is not '' and args.output is not '':
  97. try:
  98. # Starting timer
  99. start = time.time()
  100. processing_rainbow_generator(args.hash, args.input, args.output, args.procs)
  101. stop = time.time()
  102. print('Time used: ', stop - start)
  103. except FileNotFoundError:
  104. print('[!] Not existing file or path')
  105. else:
  106. print('[!] No word, file or input file were given')
  107. sys.exit(1)