123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import sys
- from argparse import ArgumentParser
- import hashlib
- import os.path
- import time
- from multiprocessing import Process, current_process, Queue
- def hash_function(hash_type, object):
- hashed_file = {
- 'sha256': hashlib.sha256(object).hexdigest(),
- 'sha512': hashlib.sha512(object).hexdigest(),
- 'md5': hashlib.md5(object).hexdigest()
- }
- if hash_type.lower() in hashed_file.keys():
- return hashed_file[hash_type.lower()]
- else:
- print('[!] Wrong or non existing hash type')
- sys.exit(1)
- def hash_of_file(hash_type, path):
- with open(path, 'rb') as file:
- print('[+] Hash of file:\n'+hash_function(hash_type, file.read()))
- def read_file_with_passwds(start_index, end_index, queue, hash_type, input_path):
- print('[+] Process: '+current_process().name+' Started')
- hash = []
-
- with open(input_path, 'r') as input_file:
- passwds = input_file.read().splitlines()
-
- for word in range(start_index, end_index):
- hash.append(passwds[word] + ':' + hash_function(hash_type, passwds[word].encode()) + '\n')
-
- queue.put(hash)
- print('[+] Process: '+current_process().name + ' Finished')
- def write_hashes_to_file(hash_list, output_path):
- with open(output_path, 'a') as output_file:
- for word in hash_list:
- output_file.write(word)
- def processing_rainbow_generator(hash_type, input_path, output_path, count):
-
- if os.path.exists(output_path):
- print('[!] Output file cannot be used because it already exists')
- sys.exit(1)
- else:
-
- with open(input_path, 'r') as file:
- file_len = len(file.readlines())
-
-
-
- start_index, end_index = 0, 0
-
- procs = []
- q = Queue()
-
- for i in range(count):
- end_index += file_len / count
- p = Process(target=read_file_with_passwds, args=(int(start_index), int(end_index), q, hash_type, input_path))
- procs.append(p)
- start_index += file_len / count
-
- for i in range(count):
- procs[i].start()
-
- for i in range(count):
-
- write_hashes_to_file(q.get(), output_path)
-
- for i in range(count):
- procs[i].join()
- print('[+] Rainbow table generated successfully')
- if __name__ == '__main__':
-
- parser = ArgumentParser()
- parser.add_argument('-s', '--hash', help="sha256/md5/sha512", default='', required=True)
- parser.add_argument('-w', '--word', help="Random string to get hash", default='')
- parser.add_argument('-f', '--file', help="/home/kali/somefile", default='')
- parser.add_argument('-i', '--input', help="Rainbow table generator input file: /home/kali/passwords.txt", default='')
- parser.add_argument('-o', '--output', help="Rainbow table generator output file: /home/kali/rainbow.txt(default rainbow_table.txt)", default='rainbow_table.txt')
- parser.add_argument('-p', '--procs', help="Rainbow table generator: Number of used processes(default 5)", type=int, default=5)
- args = parser.parse_args()
-
- if args.word is not '':
- print('[+] Hash of word:\n'+hash_function(args.hash, args.word.encode()))
-
- elif args.file is not '':
- try:
- hash_of_file(args.hash, args.file)
- except FileNotFoundError:
- print('[!] Not existing file or path')
-
- elif args.input is not '' and args.output is not '':
- try:
-
- start = time.time()
- processing_rainbow_generator(args.hash, args.input, args.output, args.procs)
- stop = time.time()
- print('Time used: ', stop - start)
- except FileNotFoundError:
- print('[!] Not existing file or path')
- else:
- print('[!] No word, file or input file were given')
- sys.exit(1)
|