-
Notifications
You must be signed in to change notification settings - Fork 0
/
RainbowTable.cpp
299 lines (254 loc) · 6.5 KB
/
RainbowTable.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "RainbowTable.h"
#define keyspacesize 503611515004
string RainbowTable::reduce(unsigned char* hashVal, unsigned int size, int reductionNumber)
{
uint64_t n;
if(size >= 8)
{
n = *((uint64_t*) hashVal);
}
else
{
n = 0;
}
n += reductionNumber;
return numberToKey(n);
}
string RainbowTable::numberToKey(uint64_t n)
{
n %= keyspacesize;
string outString(1, FirstLetter[n % 52]);
int previous = 52;
while (n >= previous)
{
n /= previous;
n--;
outString += Domain[n % 46];
previous = 46;
}
return outString;
}
void RainbowTable::printHash(unsigned char* hash, unsigned int size)
{
for(int i=0; i<size; i++)
{
printf("%02x", hash[i]);
}
printf(" ");
}
unsigned int RainbowTable::applyHash(string &password, unsigned char* result)
{
const EVP_MD *md = EVP_sha1();
unsigned int size;
EVP_DigestInit_ex(mdctx, md, NULL);
EVP_DigestUpdate(mdctx, password.c_str(), password.length());
EVP_DigestFinal(mdctx, result, &size);
return size;
}
RainbowTable::RainbowTable(int chainLength, int numChains)
{
_chainLength = chainLength;
mdctx = EVP_MD_CTX_create();
std::random_device rd;
mt19937_64 randomNumber(rd());
int key = 1;
for(int i=0; i<numChains; i++)
{
string startKey = numberToKey(randomNumber());
string currKey = startKey;
unsigned char* hashVal = new unsigned char[EVP_MAX_MD_SIZE];
for(int i=0; i<chainLength-1; i++)
{
unsigned int size = applyHash(currKey, hashVal);
currKey=reduce(hashVal, size, i);
}
delete[] hashVal;
table[currKey]=startKey;
if(key % 10000 == 0)
cout << "processing key " << key << "\n";
key++;
}
}
RainbowTable::RainbowTable(int chainLength, string dictName)
{
_chainLength = chainLength;
ifstream fin(dictName);
mdctx = EVP_MD_CTX_create();
if(!fin)
{
cerr << "Couldn't open dictionary file\n";
exit(1);
}
//check errors
string startKey;
int key = 1;
while(fin >> startKey)
{
string currKey = startKey;
unsigned char* hashVal = new unsigned char[EVP_MAX_MD_SIZE];
for(int i=0; i<chainLength-1; i++)
{
unsigned int size = applyHash(currKey, hashVal);
currKey=reduce(hashVal, size, i);
}
delete[] hashVal;
table[currKey]=startKey;
if(key % 10000 == 0)
cout << "processing key " << key << "\n";
key++;
}
}
RainbowTable::RainbowTable(string fileName)
{
ifstream fin(fileName);
if(!fin)
{
cout << "Couldn't open rainbow table file\n";
exit(1);
}
string head, tail;
mdctx = EVP_MD_CTX_create();
fin >> _chainLength;
while(fin >> head >> tail)
{
table[tail] = head;
}
fin.close();
return;
}
void RainbowTable::outputToFile(string fileName)
{
ofstream fout(fileName);
fout << _chainLength << "\n";
for(auto iter = table.begin(); iter != table.end(); ++iter)
{
fout << iter->second << " " << iter->first << "\n";
}
fout.close();
return;
}
void RainbowTable::produceTable(int chainLength, int numChains, string outputFile)
{
ofstream fout(outputFile);
fout << chainLength << "\n";
std::random_device rd;
mt19937_64 randomNumber(rd());
int key = 1;
for(int i=0; i<numChains; i++)
{
string startKey = numberToKey(randomNumber());
string currKey = startKey;
unsigned char* hashVal = new unsigned char[EVP_MAX_MD_SIZE];
for(int i=0; i<chainLength-1; i++)
{
unsigned int size = applyHash(currKey, hashVal);
currKey=reduce(hashVal, size, i);
}
delete[] hashVal;
table[currKey]=startKey;
fout << startKey << " " << currKey << "\n";
if(key % 10000 == 0)
cout << "processing key " << key << "\n";
key++;
}
fout.close();
}
bool RainbowTable::equalHashes(unsigned char* hash1, unsigned char* hash2, unsigned size)
{
for(int k=0; k<size; k++)
{
if(hash1[k] != hash2[k])
return false;
}
return true;
}
string RainbowTable::walkChain(string currKey, unsigned char* lookupHash)
{
unsigned char* tmpHash = new unsigned char[EVP_MAX_MD_SIZE];
for(int reduceStep = 0; reduceStep < _chainLength - 1; reduceStep++)
{
unsigned size = applyHash(currKey, tmpHash);
if(equalHashes(lookupHash, tmpHash, size))
{
return currKey;
}
currKey = reduce(tmpHash, size, reduceStep);
}
return "";
}
void RainbowTable::generateReductions(list<std::pair<int, unsigned char*> >& hashes, unsigned int reductions, list< vector< string > >& precomputed)
{
precomputed.resize(hashes.size());
unsigned char* tmpHash = new unsigned char[EVP_MAX_MD_SIZE];
auto preiter = precomputed.begin();
for(auto hashiter = hashes.begin(); hashiter != hashes.end() && preiter != precomputed.end(); hashiter++,preiter++)
{
vector<string>& temporary = *preiter;
temporary.reserve(reductions);
for (int startReduceStep = reductions - 2;
startReduceStep >= 0; startReduceStep--)
{
//First reduction
string currReduction = reduce((*hashiter).second, 128, startReduceStep);
//Middle hash-reductions
for (int midReduceStep = startReduceStep + 1;
midReduceStep < reductions - 1; midReduceStep++)
{
unsigned size = applyHash(currReduction, tmpHash);
currReduction = reduce(tmpHash, size, midReduceStep);
}
temporary.push_back(currReduction);
}
}
delete tmpHash;
}
string RainbowTable::lookup(unsigned char* hashVal)
{
for (int startReduceStep = _chainLength - 2;
startReduceStep >= 0; startReduceStep--)
{
//First reduction
unsigned char* tmpHash = new unsigned char[EVP_MAX_MD_SIZE];
string currReduction = reduce(hashVal, 128, startReduceStep);
//Middle hash-reductions
for (int midReduceStep = startReduceStep + 1;
midReduceStep < _chainLength - 1; midReduceStep++)
{
unsigned size = applyHash(currReduction, tmpHash);
currReduction = reduce(tmpHash, size, midReduceStep);
}
delete tmpHash;
//Try to find the final reduction in the table
auto foundEndpoint = table.find(currReduction);
//We found it! Time to walk that chain and look for the password
string foundPass = "";
if(foundEndpoint != table.end())
{
foundPass = walkChain((*foundEndpoint).second, hashVal);
if(foundPass.length() > 0)
return foundPass;
}
}
return "";
}
string RainbowTable::batchLookup(vector<string>& endpoints, unsigned char * hashVal)
{
for (int endpoint=0; endpoint<endpoints.size(); endpoint++)
{
//Try to find the final reduction in the table
auto foundEndpoint = table.find(endpoints[endpoint]);
//We found it! Time to walk that chain and look for the password
string foundPass = "";
if(foundEndpoint != table.end())
{
foundPass = walkChain((*foundEndpoint).second, hashVal);
if(foundPass.length() > 0)
return foundPass;
}
}
return "";
}
RainbowTable::~RainbowTable()
{
EVP_MD_CTX_destroy(mdctx);
}