]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/HashAlgorithm.cpp
merged paths
[freerainbowtables] / Client Applications / rcracki_mt / HashAlgorithm.cpp
1 /*
2  * rcracki_mt is a multithreaded implementation and fork of the original 
3  * RainbowCrack
4  *
5  * Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
6  * Copyright Martin Westergaard Jørgensen <martinwj2005@gmail.com>
7  * Copyright 2009, 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>
8  * Copyright 2009, 2010 James Nobis <frt@quelrod.net>
9  *
10  * This file is part of rcracki_mt.
11  *
12  * rcracki_mt is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * rcracki_mt is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with rcracki_mt.  If not, see <http://www.gnu.org/licenses/>.
24  *
25  * Changes: not using OpenSSL routines the slow way anymore, as suggested by jci.
26  */
27
28 #include "HashAlgorithm.h"
29
30 #include "Public.h"
31
32 #include <openssl/des.h>
33 //#include <openssl/md2.h>
34 #include <openssl/md4.h>
35 #include <openssl/sha.h>
36 //#include <openssl/ripemd.h>
37 #include "fast_md5.h"
38 #include "md4.h"
39 //#include "sha1.h"
40 #if defined(_WIN32) && !defined(__GNUC__)
41         #pragma comment(lib, "libeay32.lib")
42 #endif
43
44 #ifdef __NetBSD__
45         #include <des.h>
46 #endif
47
48 #define MSCACHE_HASH_SIZE 16
49 void setup_des_key(unsigned char key_56[], des_key_schedule &ks)
50 {
51         des_cblock key;
52
53         key[0] = key_56[0];
54         key[1] = (key_56[0] << 7) | (key_56[1] >> 1);
55         key[2] = (key_56[1] << 6) | (key_56[2] >> 2);
56         key[3] = (key_56[2] << 5) | (key_56[3] >> 3);
57         key[4] = (key_56[3] << 4) | (key_56[4] >> 4);
58         key[5] = (key_56[4] << 3) | (key_56[5] >> 5);
59         key[6] = (key_56[5] << 2) | (key_56[6] >> 6);
60         key[7] = (key_56[6] << 1);
61
62         //des_set_odd_parity(&key);
63         des_set_key(&key, ks);
64 }
65
66 void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
67 {
68         /*
69         unsigned char data[7] = {0};
70         memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen);
71         */
72
73         int i;
74         for (i = nPlainLen; i < 7; i++)
75                 pPlain[i] = 0;
76
77         static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
78         des_key_schedule ks;
79         //setup_des_key(data, ks);
80         setup_des_key(pPlain, ks);
81         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pHash, ks, DES_ENCRYPT);
82 }
83
84 void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
85 {
86         unsigned char pass[14];
87         unsigned char pre_lmresp[21];
88         static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
89         static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; 
90         des_key_schedule ks;
91
92         memset (pass,0,sizeof(pass));
93         memset (pre_lmresp,0,sizeof(pre_lmresp));
94
95         memcpy (pass,pPlain, nPlainLen);
96
97         setup_des_key(pass, ks);
98         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
99
100         setup_des_key(&pass[7], ks);
101         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT);
102
103         setup_des_key(pre_lmresp, ks);
104         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
105
106         setup_des_key(&pre_lmresp[7], ks);
107         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
108
109         setup_des_key(&pre_lmresp[14], ks);
110         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
111
112
113
114 void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
115 {       
116         unsigned char pre_lmresp[8];
117         static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
118         static unsigned char salt[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
119
120         des_key_schedule ks;
121         unsigned char plain[8] = {0};   
122         memcpy(plain, pPlain, nPlainLen);
123         setup_des_key(plain, ks);
124         des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
125
126         setup_des_key(pre_lmresp, ks);
127         des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT);
128
129
130
131
132 void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
133 {
134         unsigned char UnicodePlain[MAX_PLAIN_LEN];
135         static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; 
136         
137         int len = (nPlainLen < 127) ? nPlainLen : 127;
138         int i;
139         
140         for (i = 0; i < len; i++)
141         {
142         UnicodePlain[i * 2] = pPlain[i];
143         UnicodePlain[i * 2 + 1] = 0x00;
144         }
145         
146         des_key_schedule ks;
147         unsigned char lm[21];
148         
149         /*MD4_CTX ctx;
150         MD4_Init(&ctx);
151         MD4_Update(&ctx, UnicodePlain, len * 2);
152         MD4_Final(lm, &ctx);  */
153         MD4_NEW(UnicodePlain, len * 2, lm);
154         
155         //MD4(UnicodePlain, len * 2, lm);
156         lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0;
157         
158         setup_des_key(lm, ks);
159         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
160         
161         setup_des_key(&lm[7], ks);
162         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
163         
164         setup_des_key(&lm[14], ks);
165         des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
166 }
167
168 void HashORACLE(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
169 {
170         char ToEncrypt[256];
171         char temp[256];
172         char username[256];
173
174         DES_cblock iv,iv2;
175         DES_key_schedule ks1,ks2;
176         unsigned char deskey_fixed[]={ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
177         int i,j;
178 #if defined(_WIN32) && !defined(__GNUC__)
179         strcpy_s(username, sizeof(username), "SYS");
180 #else
181         strcpy(username, "SYS");
182 #endif
183         int userlen = 3;
184 #if defined(_WIN32) && !defined(__GNUC__)
185         _strupr((char*) pPlain);
186 #else
187         strupr((char*) pPlain);
188 #endif
189         memset (ToEncrypt,0,sizeof(ToEncrypt));
190
191         for (i=1,j=0; j<userlen; i++,j++)
192         {
193                 ToEncrypt[i] = username[j];
194                 i++;
195         }
196
197         for (j=0; j<nPlainLen; i++,j++)
198         {
199                 ToEncrypt[i] = pPlain[j];
200                 i++;
201         }
202
203         i=i-1;
204         memset (iv,0,8);
205         memset (iv2,0,8);
206         DES_set_key((DES_cblock*) deskey_fixed, &ks1);
207         DES_ncbc_encrypt((unsigned char*) ToEncrypt, (unsigned char*) temp, i, &ks1, &iv, DES_ENCRYPT);
208         DES_set_key((DES_cblock*) &iv, &ks2);
209         DES_ncbc_encrypt((unsigned char*) ToEncrypt, (unsigned char*) temp, i, &ks2, &iv2, DES_ENCRYPT);
210         memcpy (pHash,iv2,8);
211 }
212
213 void HashNTLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
214 {
215         unsigned char UnicodePlain[MAX_PLAIN_LEN * 2];
216         int i;
217         for (i = 0; i < nPlainLen; i++)
218         {
219                 UnicodePlain[i * 2] = pPlain[i];
220                 UnicodePlain[i * 2 + 1] = 0x00;
221         }
222
223         MD4_NEW(UnicodePlain, nPlainLen * 2, pHash);
224 }
225
226 /*
227 void HashMD2(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
228 {
229         MD2_CTX ctx;
230         MD2_Init(&ctx);
231         MD2_Update(&ctx, pPlain, nPlainLen);
232         MD2_Final(pHash, &ctx);
233
234         //MD2(pPlain, nPlainLen, pHash);
235 }
236 */
237
238 void HashMD4(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
239 {
240         MD4_NEW(pPlain, nPlainLen, pHash);
241 }
242
243 void HashMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
244 {
245         fast_MD5(pPlain, nPlainLen, pHash);
246 }
247 void HashDoubleMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
248 {
249         fast_MD5(pPlain, nPlainLen, pHash);
250         unsigned char hash[16];
251         memcpy(hash, pHash, 16);
252         fast_MD5(hash, 16, pHash);
253 }
254
255 void HashSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
256 {
257         SHA_CTX ctx;
258         SHA1_Init(&ctx);
259         SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
260         SHA1_Final(pHash, &ctx);
261 }
262
263 /*
264 void HashRIPEMD160(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
265 {
266         RIPEMD160_CTX ctx;
267         RIPEMD160_Init(&ctx);
268         RIPEMD160_Update(&ctx, pPlain, nPlainLen);
269         RIPEMD160_Final(pHash, &ctx);  
270
271         //RIPEMD160(pPlain, nPlainLen, pHash);
272 }
273 */
274
275 void HashMSCACHE(unsigned char *pPlain, int nPlainLen, unsigned char* pHash)
276 {
277         char unicode_pwd[256];
278         char unicode_user[256];
279         static unsigned char username[] = "administrator";
280         static int userlen = 13;
281         unsigned char   final1[MD4_DIGEST_LENGTH];
282         MD4_CTX ctx;
283         int i;
284
285 //      strcpy (username, "administrator");
286 //      userlen = 13;
287
288         for (i=0; i<nPlainLen; i++)
289         {
290                 unicode_pwd[i*2] = pPlain[i];
291                 unicode_pwd[i*2+1] = 0x00;
292         }
293
294         for (i=0; i<userlen; i++)
295         {
296                 unicode_user[i*2] = username[i];
297                 unicode_user[i*2+1] = 0x00;
298         }
299         /*
300         MD4_Init(&ctx);
301         MD4_Update(&ctx,unicode_pwd,nPlainLen*2);
302         MD4_Final(final1,&ctx);
303         */
304         MD4_NEW( (unsigned char*)unicode_pwd, nPlainLen*2, final1 );
305
306         MD4_Init(&ctx);
307         MD4_Update(&ctx,final1,MD4_DIGEST_LENGTH);
308         MD4_Update(&ctx,(unsigned char*) unicode_user,userlen*2);
309         MD4_Final(pHash,&ctx);
310
311         /*
312         unsigned char unicode_pwd[256];
313         for (int i=0; i<nPlainLen; i++)
314         {
315                 unicode_pwd[i*2] = pPlain[i];
316                 unicode_pwd[i*2+1] = 0x00;
317         }*/     
318         /*
319         unsigned char *buf = (unsigned char*)calloc(MSCACHE_HASH_SIZE + nSaltLength, sizeof(unsigned char));    
320         HashNTLM(pPlain, nPlainLen, buf, NULL);
321         //MD4(unicode_pwd, nPlainLen*2, buf);
322         memcpy(buf + MSCACHE_HASH_SIZE, pSalt, nSaltLength);
323         MD4(buf, MSCACHE_HASH_SIZE + nSaltLength, pHash); 
324         free(buf);
325         */
326 }
327
328 //*********************************************************************************
329 // Code for MySQL password hashing
330 //*********************************************************************************
331
332 inline void mysql_hash_password_323(unsigned long *result, const char *password) 
333 {
334         register unsigned long nr=1345345333L, add=7, nr2=0x12345671L;
335         unsigned long tmp;
336         for (; *password ; password++) 
337         {
338                 if (*password == ' ' || *password == '\t') continue;
339                 tmp= (unsigned long) (unsigned char) *password;
340                 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
341                 nr2+=(nr2 << 8) ^ nr;
342                 add+=tmp;
343         }
344         result[0]=nr & (((unsigned long) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
345         result[1]=nr2 & (((unsigned long) 1L << 31) -1L);
346         return;
347 }
348
349 void HashMySQL323(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
350 {
351         unsigned long hash_pass[2];     
352         unsigned char* f = (unsigned char*) hash_pass;
353
354         unsigned char* pass = (unsigned char*) calloc (nPlainLen+4,sizeof(unsigned char));
355         memcpy(pass,pPlain,nPlainLen);
356
357         mysql_hash_password_323(hash_pass, (char*) pass);
358         pHash[0]=*(f+3); pHash[1]=*(f+2); pHash[2]=*(f+1); pHash[3]=*(f+0);
359         pHash[4]=*(f+7); pHash[5]=*(f+6); pHash[6]=*(f+5); pHash[7]=*(f+4);
360
361         free (pass);
362 }
363
364 void HashMySQLSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
365 {
366         unsigned char hash_stage1[SHA_DIGEST_LENGTH];
367         SHA_CTX ctx;
368
369         SHA1_Init(&ctx);
370         SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
371         SHA1_Final(hash_stage1, &ctx);
372         SHA1_Init(&ctx);
373         SHA1_Update(&ctx, hash_stage1, SHA_DIGEST_LENGTH);
374         SHA1_Final(pHash, &ctx);
375 }
376
377 //*********************************************************************************
378 // Code for PIX password hashing
379 //*********************************************************************************
380 static char itoa64[] =          /* 0 ... 63 => ascii - 64 */
381         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
382
383 void _crypt_to64(char *s, unsigned long v, int n)
384 {
385         while (--n >= 0) {
386                 *s++ = itoa64[v&0x3f];
387                 v >>= 6;
388         }
389 }
390
391 void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
392 {
393         char temp[MD5_DIGEST_LENGTH+1];
394         unsigned char final[MD5_DIGEST_LENGTH];
395         char* pass = (char*) calloc (nPlainLen+MD5_DIGEST_LENGTH,sizeof(char));
396
397         memcpy (pass,pPlain,nPlainLen);
398
399         /*MD5_CTX ctx;
400         MD5_Init(&ctx);
401         MD5_Update(&ctx, (unsigned char *) pass, MD5_DIGEST_LENGTH);
402         MD5_Final(final, &ctx);*/
403         fast_MD5((unsigned char *) pass, MD5_DIGEST_LENGTH, final);
404
405         char* p = (char*) temp;
406         _crypt_to64(p,*(unsigned long*) (final+0),4); p += 4;
407         _crypt_to64(p,*(unsigned long*) (final+4),4); p += 4;
408         _crypt_to64(p,*(unsigned long*) (final+8),4); p += 4;
409         _crypt_to64(p,*(unsigned long*) (final+12),4); p += 4;
410         *p=0;
411
412         memcpy(pHash,temp,MD5_DIGEST_LENGTH);
413
414         free (pass);
415 }
416
417 #if !defined(_WIN32) || defined(__GNUC__)
418 char *strupr(char *s1)
419 {
420         char *p = s1;
421         while(*p)
422         {
423                 *p = (char) toupper(*p);
424                 p++;
425         }
426         return s1;
427 }
428 #endif