-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.hpp
More file actions
442 lines (330 loc) · 9.26 KB
/
database.hpp
File metadata and controls
442 lines (330 loc) · 9.26 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//Created by Mitchell Barker
//3-12-16
//Password breaking tools
#ifndef DATABASE
#define DATABASE
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <string>
#include <string.h>
#include <openssl/md5.h>
#include <iomanip>
#include <map>
using namespace std;
typedef unsigned int uint;
#define START 33
#define END 126
#define START_CHAR (char) START
#define END_CHAR (char) END
bool is_munged(char c);
char munge_char(char c);
string next_munge(string original, string cur);
bool munged_string(string s);
string mungest_string(string s);
string next_string(string cur);
char next_char(char cur);
string my_compute_hash(string passwd);
// //lengthy munge check
// map<char, char> munge{{'A','@'},{'B','8'},{'C','('},{'D','6'},{'E','3'},{'F','#'},{'G','9'},{'H','#'},{'I','1'},{'K','<'},{'L','I'},
// {'N','^'},{'O','0'},{'Q','9'},{'S','$'},{'T','7'},{'V','>'},{'X','%'},{'Y','?'},{'@','4'},{'#','4'},{'$','5'},{'>','<'},
// {'1','!'},{'4','^'},{'7','+'},{'8','3'}};
//shortened munge check
map<char, char> munge{{'A','@'},{'B','3'},{'E','3'},{'G','9'},{'I','1'},{'L','1'},
{'O','0'},{'S','$'},{'T','7'},{'$','5'},{'1','!'}};
//*******************************Class declarations************************///
//User class for holding invididual info
class User {
public:
string last_name, first_name, user_name, hash_val, salt;
uint uin;
User(string last="",string first="", string user="",uint u=0, string hash="",string s="");
bool authenticate(string user, string passwd);
bool operator< (const User &u1);
bool operator== (const User &u1);
friend istream &operator>>(istream &input, User &U);
friend ostream &operator<<(ostream &output, const User &U);
};
//Database is collection of users
class Database{
public:
vector<User> db;
ofstream out;
Database(string input_file);
bool authenticate(string user, string passwd);
void pass_match(string hash);
void brute_find(string out_file);
void dict_find(string dict, string out_file);
void expanded_dict_find(string dict, string out_file);
void print_user_hash();
friend ostream &operator<<(ostream &output, const Database &d);
};
//**********************End class declarations***********************//
//********************User member functions**************************************//
User::User(string last,string first, string user,uint u, string hash,string s){
last_name = last;
first_name = first;
user_name = user;
uin = u;
hash_val = hash;
salt = s;
}
bool User::authenticate(string user, string passwd){
string temp = my_compute_hash(passwd+salt);
return temp == hash_val;
}
bool User::operator< (const User &u1){
return user_name < u1.user_name;
}
bool User::operator== (const User &u1){
return user_name == u1.user_name;
}
istream &operator>>(istream &input, User &U){
string line;
if(!getline(input, line))
return input;
for (int i = 0; i < line.length(); i++){
if (line[i] == ',')
line[i] = ' ';
}
istringstream iss(line);
iss>>U.last_name>>U.first_name>>U.user_name>>U.uin>>U.hash_val;
if(U.hash_val.size() == 3){
U.salt = U.hash_val;
iss>>U.hash_val;
}
return input;
}
ostream &operator<<(ostream &output, const User &U){
output<<U.last_name<<","<<U.first_name<<","<<U.user_name<<","
<<U.uin<<",";
if(U.salt.length() > 0)
output<<U.salt;
output<<","<<U.hash_val;
return output;
}
//**************************End User functions****************************************//
//**************************Database functions****************************************//
Database::Database(string input_file){
ifstream file(input_file.c_str());
while(file.good()){
User tmp = User();
file>>tmp;
db.push_back(tmp);
}
if(db.back().user_name.size() < 1)
db.pop_back();
sort(db.begin(), db.end());
file.close();
}
void Database::print_user_hash(){
ofstream os("john_use.txt");
for(auto it = db.begin(); it != db.end(); ++it)
os<<it->user_name<<":"<<it->hash_val<<endl;
}
void Database::pass_match(string passwd){
string hash = my_compute_hash(passwd);
string r_hash = hash;
for(auto it = db.begin(); it != db.end(); ++it){
if(it->salt.length()>0){
hash = my_compute_hash(passwd+it->salt);
if(it->hash_val == hash)
out<<it->user_name<<":"<<passwd<<endl;
hash = r_hash;
}
else if(it->hash_val == hash)
out<<it->user_name<<":"<<passwd<<endl;
}
}
void Database::brute_find(string out_file=""){
string current;
current = START_CHAR;
out.open(out_file.c_str());
while(true){
pass_match(current);
current = next_string(current);
}
out.close();
}
void Database::dict_find(string dict, string out_file=""){
ifstream ifile(dict.c_str());
string temp;
out.open(out_file.c_str());
while(ifile.good()){
getline(ifile, temp);
pass_match(temp);
}
ifile.close();
out.close();
}
void Database::expanded_dict_find(string dict, string out_file=""){
ifstream ifile(dict.c_str());
string temp, cur, m;
out.open(out_file.c_str());
while(ifile.good()){
getline(ifile, temp);
pass_match(temp);
cur = temp;
m = mungest_string(temp);
while(cur != m){
cur = next_munge(temp, cur);
pass_match(cur);
}
}
ifile.close();
out.close();
}
bool Database::authenticate(string user, string passwd){
User searching = User("temp","temp",user);
vector<User>::iterator it;
it = find(db.begin(), db.end(), searching);
if (it!=db.end())
return it->authenticate(user,passwd);
else
return false;
}
ostream &operator<<(ostream &output, const Database &d){
for(auto it = d.db.begin(); it != d.db.end(); ++it)
output<<*it<<endl;
return output;
}
//************************************Global functions*****************************//
//**************munge functions: find permutations of passwords********///
string next_munge(string original, string cur){
int i = cur.length() -1;
do{
if(is_munged(cur[i]))
cur[i] = original[i];
else
cur[i] = munge_char(cur[i]);
--i;
}while(cur[i+1] == original[i+1] && i >= 0);
return cur;
}
bool munged_string(string s){
bool final = true;
for(int i =0; i < s.length(); ++i){
if(!is_munged(s[i]))
final = false;
}
return final;
}
string mungest_string(string s){
char c;
for(int i = 0; i < s.size(); ++i){
c = munge_char(s[i]);
while(s[i] != c){
s[i] = c;
c = munge_char(s[i]);
}
}
return s;
}
bool is_munged(char c){
if (munge_char(c) == c)
return true;
else
return false;
}
char munge_char(char c){
if(isalpha(c) && islower(c))
c = toupper(c);
else{
if(munge.count(c) > 0)
c = munge[c];
}
return c;
}
//*************functions for brute search****************///
bool same_chars(const std::string& s) {
return s.find_first_not_of(s[0]) == std::string::npos;
}
string next_string(string cur){
int i = cur.length() -1;
do{
cur[i] = next_char(cur[i]);
--i;
}while(cur[i+1] == START_CHAR && i >= 0);
if (same_chars(cur) && cur[0] == '!')
cur = cur+START_CHAR;
return cur;
}
char next_char(char cur){
if(cur == END_CHAR)
cur = START_CHAR;
else
++cur;
return cur;
}
string rand_word(string file, int length){
//srand(time(NULL));
vector<string> dict;
ifstream is(file.c_str());
string line, word, temp;
int chars_left = length;
while (getline(is , line))
dict.push_back(line);
while(chars_left > 0){
temp = dict[rand()%dict.size()];
if(chars_left == temp.length()){
chars_left -= temp.length();
word += temp;
}
else if (chars_left > temp.length() && chars_left > 6){
chars_left -= temp.length();
word += temp;
}
}
return word;
}
string add_special_num(string s){
string next = s;
int r, count = 0;
while(s == next && count != s.length()*5){
++count;
r = rand() % s.length();
if(islower(next[r]) && !is_munged(toupper(next[r])) ){
while(!is_munged(next[r]) )
next[r] = munge_char(next[r]);
}
}
return next;
}
string add_capital(string s){
string next = s;
int r;
while(s == next){
r = rand() % s.length();
if( islower(next[r]))
next[r] = toupper(next[r]);
}
return next;
}
//adds at least one capital and one special char
string gen_password(string dict, int length, int capitals=1, int nonalpha=1){
srand(time(NULL));
string word = rand_word(dict, length);
for(int i = 0; i < capitals; ++i)
word = add_capital(word);
for(int i = 0; i < nonalpha; ++i)
word = add_special_num(word);
return word;
}
//MD5 Hashing functions////////////////////////
string my_compute_hash(string passwd) {
MD5_CTX context;
unsigned char digest[16];
const char* string = reinterpret_cast<const char*>(passwd.c_str());
MD5_Init(&context);
MD5_Update(&context, string, strlen(string));
MD5_Final(digest, &context);
char md5string[33];
for(int i = 0; i < 16; ++i)
sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
return md5string;
}
//**************************************End Global functions*****************************//
#endif