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