2 RainbowCrack - a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
4 Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
6 Changes: not using OpenSSL routines the slow way anymore, as suggested by jci.
9 #include "HashAlgorithm.h"
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>
25 #pragma comment(lib, "libeay32.lib")
27 #define MSCACHE_HASH_SIZE 16
28 void setup_des_key(unsigned char key_56[], des_key_schedule &ks)
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);
41 //des_set_odd_parity(&key);
42 des_set_key(&key, ks);
45 void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
48 unsigned char data[7] = {0};
49 memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen);
53 for (i = nPlainLen; i < 7; i++)
56 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
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);
63 void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
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};
71 memset (pass,0,sizeof(pass));
72 memset (pre_lmresp,0,sizeof(pre_lmresp));
74 memcpy (pass,pPlain, nPlainLen);
76 setup_des_key(pass, ks);
77 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
79 setup_des_key(&pass[7], ks);
80 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT);
82 setup_des_key(pre_lmresp, ks);
83 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
85 setup_des_key(&pre_lmresp[7], ks);
86 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
88 setup_des_key(&pre_lmresp[14], ks);
89 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
93 void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
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};
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);
105 setup_des_key(pre_lmresp, ks);
106 des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT);
111 void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
113 unsigned char UnicodePlain[MAX_PLAIN_LEN];
114 static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
116 int len = (nPlainLen < 127) ? nPlainLen : 127;
119 for (i = 0; i < len; i++)
121 UnicodePlain[i * 2] = pPlain[i];
122 UnicodePlain[i * 2 + 1] = 0x00;
126 unsigned char lm[21];
130 MD4_Update(&ctx, UnicodePlain, len * 2);
131 MD4_Final(lm, &ctx); */
132 MD4_NEW(UnicodePlain, len * 2, lm);
134 //MD4(UnicodePlain, len * 2, lm);
135 lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0;
137 setup_des_key(lm, ks);
138 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
140 setup_des_key(&lm[7], ks);
141 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
143 setup_des_key(&lm[14], ks);
144 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
148 void HashORACLE(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
155 DES_key_schedule ks1,ks2;
156 unsigned char deskey_fixed[]={ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
159 strcpy (username, "SYS");
162 strupr ((char*) pPlain);
163 memset (ToEncrypt,0,sizeof(ToEncrypt));
165 for (i=1,j=0; j<userlen; i++,j++)
167 ToEncrypt[i] = username[j];
171 for (j=0; j<nPlainLen; i++,j++)
173 ToEncrypt[i] = pPlain[j];
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);
187 void HashNTLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
189 unsigned char UnicodePlain[MAX_PLAIN_LEN * 2];
191 for (i = 0; i < nPlainLen; i++)
193 UnicodePlain[i * 2] = pPlain[i];
194 UnicodePlain[i * 2 + 1] = 0x00;
199 MD4_Update(&ctx, UnicodePlain, nPlainLen * 2);
200 MD4_Final(pHash, &ctx);*/
202 //MD4(UnicodePlain, nPlainLen * 2, pHash);
203 MD4_NEW(UnicodePlain, nPlainLen * 2, pHash);
206 void HashMD2(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
210 MD2_Update(&ctx, pPlain, nPlainLen);
211 MD2_Final(pHash, &ctx);
213 //MD2(pPlain, nPlainLen, pHash);
216 void HashMD4(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
220 MD4_Update(&ctx, pPlain, nPlainLen);
221 MD4_Final(pHash, &ctx);*/
223 MD4_NEW(pPlain, nPlainLen, pHash);
224 //MD4(pPlain, nPlainLen, pHash);
227 void HashMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
229 //MD5_NEW(pPlain, nPlainLen, pHash);
230 fast_MD5(pPlain, nPlainLen, pHash);
232 void HashDoubleMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
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);
242 void HashSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
246 SHA1_Update(&ctx, pPlain, nPlainLen);
247 SHA1_Final(pHash, &ctx);*/
249 //SHA1(pPlain, nPlainLen, pHash);
250 SHA1_NEW(pPlain, nPlainLen, pHash);
253 void HashRIPEMD160(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
256 RIPEMD160_Init(&ctx);
257 RIPEMD160_Update(&ctx, pPlain, nPlainLen);
258 RIPEMD160_Final(pHash, &ctx);
260 //RIPEMD160(pPlain, nPlainLen, pHash);
263 void HashMSCACHE(unsigned char *pPlain, int nPlainLen, unsigned char* pHash)
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];
273 // strcpy (username, "administrator");
276 for (i=0; i<nPlainLen; i++)
278 unicode_pwd[i*2] = pPlain[i];
279 unicode_pwd[i*2+1] = 0x00;
282 for (i=0; i<userlen; i++)
284 unicode_user[i*2] = username[i];
285 unicode_user[i*2+1] = 0x00;
289 MD4_Update(&ctx,unicode_pwd,nPlainLen*2);
290 MD4_Final(final1,&ctx);
293 MD4_Update(&ctx,final1,MD4_DIGEST_LENGTH);
294 MD4_Update(&ctx,(unsigned char*) unicode_user,userlen*2);
295 MD4_Final(pHash,&ctx);
298 unsigned char unicode_pwd[256];
299 for (int i=0; i<nPlainLen; i++)
301 unicode_pwd[i*2] = pPlain[i];
302 unicode_pwd[i*2+1] = 0x00;
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);
314 //*********************************************************************************
315 // Code for MySQL password hashing
316 //*********************************************************************************
318 inline void mysql_hash_password_323(unsigned long *result, const char *password)
320 register unsigned long nr=1345345333L, add=7, nr2=0x12345671L;
322 for (; *password ; password++)
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;
330 result[0]=nr & (((unsigned long) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
331 result[1]=nr2 & (((unsigned long) 1L << 31) -1L);
335 void HashMySQL323(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
337 unsigned long hash_pass[2];
338 unsigned char* f = (unsigned char*) hash_pass;
340 unsigned char* pass = (unsigned char*) calloc (nPlainLen+4,sizeof(unsigned char));
341 memcpy(pass,pPlain,nPlainLen);
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);
350 void HashMySQLSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
352 unsigned char hash_stage1[SHA_DIGEST_LENGTH];
356 SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
357 SHA1_Final(hash_stage1, &ctx);
359 SHA1_Update(&ctx, hash_stage1, SHA_DIGEST_LENGTH);
360 SHA1_Final(pHash, &ctx);
363 //*********************************************************************************
364 // Code for PIX password hashing
365 //*********************************************************************************
366 static char itoa64[] = /* 0 ... 63 => ascii - 64 */
367 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
369 void _crypt_to64(char *s, unsigned long v, int n)
372 *s++ = itoa64[v&0x3f];
377 void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
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));
383 memcpy (pass,pPlain,nPlainLen);
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);
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;
398 memcpy(pHash,temp,MD5_DIGEST_LENGTH);
404 char *strupr(char *s1)