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