-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrainbowtool.py
183 lines (162 loc) · 7.74 KB
/
rainbowtool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import argparse
from os.path import isfile
from os import remove
import hashlib
import binascii
import zlib
import zipfile
from io import BytesIO
import random
class hashes():
def __init__(self):
self.supported = ["ntlm","md5","sha256"]
def list_hashes(self):
print("Supported hash types : ")
for alg in self.supported:
print("$ " + alg)
def ntlm(self,text):
resultHash = hashlib.new('md4', text.encode()).hexdigest()
return resultHash
def md5(self,text):
resultHash = hashlib.md5(text.encode()).hexdigest()
return resultHash
def sha256(self,text):
resultHash = hashlib.sha256(text.encode()).hexdigest()
return resultHash
def get_random_filename(length):
k = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
filename = ""
for i in range(length):
filename += random.choice(k)
return filename
class rainbowtool():
def __init__(self):
pass
def crack_with_rainbowtable(self,hashtocrack,rainbowtable,hashformat,verbose=False,compressed=False):
hashtocrack = hashtocrack.lower()
if not compressed:
for line in open(rainbowtable,'r'):
line = line.strip()
if hashtocrack in line:
if line.count(":") == 0 and verbose:
print("[?] Encountered Invalid Line : " + line)
else:
parsed = line.lower().split(":")
computed_hash = parsed[0]
if computed_hash == hashtocrack:
password = parsed[1]
if verbose:
print("[!!!] CRACKED : " + password)
return password
else:
inzipfile = zipfile.ZipFile(rainbowtable)
parts = inzipfile.namelist()
print("[+] Compressed Parts : " + str(len(parts)))
for part in parts:
content = inzipfile.read(part)
content = zlib.decompress(content)
for line in content.split(b"\n"):
try:
line = line.decode()
parsed = line.lower().split(":")
computed_hash = parsed[0]
if computed_hash == hashtocrack:
password = parsed[1]
if verbose:
print("[!!!] CRACKED : " + password)
return password
except UnicodeDecodeError:
pass
print("[---] Hash not in rainbow table") # If we reach here, it means that the hash could not be cracked
def make_rainbowtable(self,file,hashfunction,outfile=None,compress=False):
# makes the output file if it doesn't exist
if compress:
if outfile != None:
if not isfile(outfile):
open(outfile,'x').close()
outputzipfile = zipfile.ZipFile(outfile,'w')
counter = 0
buf = ""
compressed = b""
bufsize = 1024
for line in open(file,'r',errors='ignore'):
line = line.strip()
computed_hash = hashfunction(line)
new_line = computed_hash + ":" + line + "\n"
counter += 1
buf += new_line
if counter == bufsize or counter > bufsize:
compressed = zlib.compress(buf.encode())
if outfile != None:
filename = get_random_filename(15)
open(filename,'x').close()
open(filename,'wb').write(compressed)
outputzipfile.write(filename)
remove(filename)
else:
print(compressed)
counter = 0
buf = ""
# Deposits the remaining 1024 bytes
compressed = zlib.compress(buf.encode())
if outfile != None:
filename = get_random_filename(15)
open(filename,'x').close()
open(filename,'wb').write(compressed)
outputzipfile.write(filename)
remove(filename)
else:
print(compressed)
else:
if outfile != None:
if not isfile(outfile):
open(outfile,'x').close()
fileHandler = open(outfile,'a')
for line in open(file,'r',errors='ignore'): # Ignores invalid characters because they're a headache to deal with, and the vast majority of passwords don't contain them anyway
line = line.strip()
computed_hash = hashfunction(line)
new_line = computed_hash + ":" + line + "\n"
if outfile != None:
fileHandler.write(new_line)
else:
print(new_line,end="")
if outfile != None:
fileHandler.close()
def main():
parser = argparse.ArgumentParser(description="Rainbowtool V1")
misc = parser.add_argument_group('Miscellaneous')
misc.add_argument("--listhashes",dest="listhashes",default=False,help="List supported hashing algorithms",action="store_true")
misc.add_argument("--compress",dest="compress",help="If the rainbow table is/should be compressed",default=False,action="store_true")
cracking = parser.add_argument_group("Cracking")
cracking.add_argument('--hash',dest='hashtocrack',default=False,help='The hash to crack')
cracking.add_argument('--rainbowtable',dest='rainbowtable',help='The rainbow table to use to crack the hash',default=False)
make_rainbow = parser.add_argument_group("Make Rainbow Tables")
make_rainbow.add_argument("--wordlist",dest="wordlist",help="The wordlist to use for the rainbow table",default=False)
make_rainbow.add_argument("--outfile",dest="outfile",help="The file to save the rainbow table to, defaults to stdout",default=None)
make_rainbow.add_argument('--format',dest="format",help="The hash format to use",default=False)
arguments = parser.parse_args()
hashesObject = hashes()
rainbowtoolObject = rainbowtool()
functionDict = {"ntlm" : hashesObject.ntlm, "md5" : hashesObject.md5, "sha256" : hashesObject.sha256} # A dictionary of hashing functions
if arguments.listhashes:
hashesObject.list_hashes()
exit(0)
elif arguments.hashtocrack and arguments.rainbowtable:
hashtocrack = arguments.hashtocrack
rainbowtable = arguments.rainbowtable
print("[+] Hash length : " + str(len(arguments.hashtocrack)))
if isfile(rainbowtable):
print("[+] Rainbow table exists")
else:
print("[-] Rainbow table does not exist; Try checking the file name")
exit(1)
rainbowtoolObject.crack_with_rainbowtable(hashtocrack,rainbowtable,format,verbose=True,compressed=arguments.compress)
elif arguments.wordlist and arguments.format:
hash_function = functionDict[arguments.format]
if not isfile(arguments.wordlist):
print("[-] File does not exist")
exit(1)
rainbowtoolObject.make_rainbowtable(arguments.wordlist,hash_function,outfile=arguments.outfile,compress=arguments.compress)
else:
parser.print_help()
main()