2 RainbowCrack - a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
4 Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
7 #include "HashAlgorithm.h"
14 #define MSCACHE_HASH_SIZE 16
15 void setup_des_key(unsigned char key_56[], des_key_schedule &ks)
20 key[1] = (key_56[0] << 7) | (key_56[1] >> 1);
21 key[2] = (key_56[1] << 6) | (key_56[2] >> 2);
22 key[3] = (key_56[2] << 5) | (key_56[3] >> 3);
23 key[4] = (key_56[3] << 4) | (key_56[4] >> 4);
24 key[5] = (key_56[4] << 3) | (key_56[5] >> 5);
25 key[6] = (key_56[5] << 2) | (key_56[6] >> 6);
26 key[7] = (key_56[6] << 1);
28 //des_set_odd_parity(&key);
29 des_set_key(&key, ks);
32 void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
35 unsigned char data[7] = {0};
36 memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen);
40 for (i = nPlainLen; i < 7; i++)
43 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
45 //setup_des_key(data, ks);
46 setup_des_key(pPlain, ks);
47 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pHash, ks, DES_ENCRYPT);
50 void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
52 unsigned char pass[14];
53 unsigned char pre_lmresp[21];
54 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
55 static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
58 memset (pass,0,sizeof(pass));
59 memset (pre_lmresp,0,sizeof(pre_lmresp));
61 memcpy (pass,pPlain, nPlainLen);
63 setup_des_key(pass, ks);
64 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
66 setup_des_key(&pass[7], ks);
67 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT);
69 setup_des_key(pre_lmresp, ks);
70 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
72 setup_des_key(&pre_lmresp[7], ks);
73 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
75 setup_des_key(&pre_lmresp[14], ks);
76 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
80 void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
82 unsigned char pre_lmresp[8];
83 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
84 static unsigned char salt[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
87 unsigned char plain[8] = {0};
88 memcpy(plain, pPlain, nPlainLen);
89 setup_des_key(plain, ks);
90 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
92 setup_des_key(pre_lmresp, ks);
93 des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT);
98 void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
100 unsigned char UnicodePlain[MAX_PLAIN_LEN];
101 static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
103 int len = (nPlainLen < 127) ? nPlainLen : 127;
106 for (i = 0; i < len; i++)
108 UnicodePlain[i * 2] = pPlain[i];
109 UnicodePlain[i * 2 + 1] = 0x00;
113 unsigned char lm[21];
115 MD4_NEW(UnicodePlain, len * 2, lm);
116 lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0;
118 setup_des_key(lm, ks);
119 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
121 setup_des_key(&lm[7], ks);
122 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
124 setup_des_key(&lm[14], ks);
125 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
129 void HashORACLE(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
136 DES_key_schedule ks1,ks2;
137 unsigned char deskey_fixed[]={ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
140 strcpy_s(username, sizeof(username), "SYS");
142 strcpy(username, "SYS");
146 _strupr((char*) pPlain);
148 strupr((char*) pPlain);
150 memset (ToEncrypt,0,sizeof(ToEncrypt));
152 for (i=1,j=0; j<userlen; i++,j++)
154 ToEncrypt[i] = username[j];
158 for (j=0; j<nPlainLen; i++,j++)
160 ToEncrypt[i] = pPlain[j];
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);
174 void HashNTLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
176 unsigned char UnicodePlain[MAX_PLAIN_LEN * 2];
178 for (i = 0; i < nPlainLen; i++)
180 UnicodePlain[i * 2] = pPlain[i];
181 UnicodePlain[i * 2 + 1] = 0x00;
184 MD4_NEW(UnicodePlain, nPlainLen * 2, pHash);
187 void HashMD2(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
189 MD2(pPlain, nPlainLen, pHash);
192 void HashMD4(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
194 MD4_NEW(pPlain, nPlainLen, pHash);
197 void HashMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
199 MD5_NEW(pPlain, nPlainLen, pHash);
201 void HashDoubleMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
203 MD5_NEW(pPlain, nPlainLen, pHash);
204 unsigned char hash[16];
205 memcpy(hash, pHash, 16);
206 MD5_NEW(hash, 16, pHash);
209 void HashSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
211 SHA1(pPlain, nPlainLen, pHash);
214 void HashRIPEMD160(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
216 RIPEMD160(pPlain, nPlainLen, pHash);
219 void HashMSCACHE(unsigned char *pPlain, int nPlainLen, unsigned char* pHash)
221 char unicode_pwd[256];
222 char unicode_user[256];
223 static unsigned char username[] = "administrator";
224 static int userlen = 13;
225 unsigned char final1[MD4_DIGEST_LENGTH];
229 // strcpy (username, "administrator");
232 for (i=0; i<nPlainLen; i++)
234 unicode_pwd[i*2] = pPlain[i];
235 unicode_pwd[i*2+1] = 0x00;
238 for (i=0; i<userlen; i++)
240 unicode_user[i*2] = username[i];
241 unicode_user[i*2+1] = 0x00;
245 MD4_Update(&ctx,unicode_pwd,nPlainLen*2);
246 MD4_Final(final1,&ctx);
249 MD4_Update(&ctx,final1,MD4_DIGEST_LENGTH);
250 MD4_Update(&ctx,(unsigned char*) unicode_user,userlen*2);
251 MD4_Final(pHash,&ctx);
254 unsigned char unicode_pwd[256];
255 for (int i=0; i<nPlainLen; i++)
257 unicode_pwd[i*2] = pPlain[i];
258 unicode_pwd[i*2+1] = 0x00;
261 unsigned char *buf = (unsigned char*)calloc(MSCACHE_HASH_SIZE + nSaltLength, sizeof(unsigned char));
262 HashNTLM(pPlain, nPlainLen, buf, NULL);
263 //MD4(unicode_pwd, nPlainLen*2, buf);
264 memcpy(buf + MSCACHE_HASH_SIZE, pSalt, nSaltLength);
265 MD4(buf, MSCACHE_HASH_SIZE + nSaltLength, pHash);
270 //*********************************************************************************
271 // Code for MySQL password hashing
272 //*********************************************************************************
274 inline void mysql_hash_password_323(unsigned long *result, const char *password)
276 register unsigned long nr=1345345333L, add=7, nr2=0x12345671L;
278 for (; *password ; password++)
280 if (*password == ' ' || *password == '\t') continue;
281 tmp= (unsigned long) (unsigned char) *password;
282 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
283 nr2+=(nr2 << 8) ^ nr;
286 result[0]=nr & (((unsigned long) 1L << 31) -1L); ;
287 result[1]=nr2 & (((unsigned long) 1L << 31) -1L);
291 void HashMySQL323(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
293 unsigned long hash_pass[2];
294 unsigned char* f = (unsigned char*) hash_pass;
296 unsigned char* pass = (unsigned char*) calloc (nPlainLen+4,sizeof(unsigned char));
297 memcpy(pass,pPlain,nPlainLen);
299 mysql_hash_password_323(hash_pass, (char*) pass);
300 pHash[0]=*(f+3); pHash[1]=*(f+2); pHash[2]=*(f+1); pHash[3]=*(f+0);
301 pHash[4]=*(f+7); pHash[5]=*(f+6); pHash[6]=*(f+5); pHash[7]=*(f+4);
306 void HashMySQLSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
308 unsigned char hash_stage1[SHA_DIGEST_LENGTH];
312 SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
313 SHA1_Final(hash_stage1, &ctx);
315 SHA1_Update(&ctx, hash_stage1, SHA_DIGEST_LENGTH);
316 SHA1_Final(pHash, &ctx);
319 //*********************************************************************************
320 // Code for PIX password hashing
321 //*********************************************************************************
322 static char itoa64[] = /* 0 ... 63 => ascii - 64 */
323 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
325 void _crypt_to64(char *s, unsigned long v, int n)
328 *s++ = itoa64[v&0x3f];
333 void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
335 char temp[MD5_DIGEST_LENGTH+1];
336 unsigned char final[MD5_DIGEST_LENGTH];
337 char* pass = (char*) calloc (nPlainLen+MD5_DIGEST_LENGTH,sizeof(char));
339 memcpy (pass,pPlain,nPlainLen);
343 MD5_Update(&ctx, (unsigned char *) pass, MD5_DIGEST_LENGTH);
344 MD5_Final(final, &ctx);
346 char* p = (char*) temp;
347 _crypt_to64(p,*(unsigned long*) (final+0),4); p += 4;
348 _crypt_to64(p,*(unsigned long*) (final+4),4); p += 4;
349 _crypt_to64(p,*(unsigned long*) (final+8),4); p += 4;
350 _crypt_to64(p,*(unsigned long*) (final+12),4); p += 4;
353 memcpy(pHash,temp,MD5_DIGEST_LENGTH);