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"
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>
18 #pragma comment(lib, "libeay32.lib")
20 #define MSCACHE_HASH_SIZE 16
21 void setup_des_key(unsigned char key_56[], des_key_schedule &ks)
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);
34 //des_set_odd_parity(&key);
35 des_set_key(&key, ks);
38 void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
41 unsigned char data[7] = {0};
42 memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen);
46 for (i = nPlainLen; i < 7; i++)
49 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
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);
56 void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
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};
64 memset (pass,0,sizeof(pass));
65 memset (pre_lmresp,0,sizeof(pre_lmresp));
67 memcpy (pass,pPlain, nPlainLen);
69 setup_des_key(pass, ks);
70 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
72 setup_des_key(&pass[7], ks);
73 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT);
75 setup_des_key(pre_lmresp, ks);
76 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
78 setup_des_key(&pre_lmresp[7], ks);
79 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
81 setup_des_key(&pre_lmresp[14], ks);
82 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
86 void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
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};
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);
98 setup_des_key(pre_lmresp, ks);
99 des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT);
104 void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
106 unsigned char UnicodePlain[MAX_PLAIN_LEN];
107 static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
109 int len = (nPlainLen < 127) ? nPlainLen : 127;
112 for (i = 0; i < len; i++)
114 UnicodePlain[i * 2] = pPlain[i];
115 UnicodePlain[i * 2 + 1] = 0x00;
119 unsigned char lm[21];
121 MD4(UnicodePlain, len * 2, lm);
122 lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0;
124 setup_des_key(lm, ks);
125 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
127 setup_des_key(&lm[7], ks);
128 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
130 setup_des_key(&lm[14], ks);
131 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
135 void HashORACLE(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
142 DES_key_schedule ks1,ks2;
143 unsigned char deskey_fixed[]={ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
146 strcpy (username, "SYS");
149 // 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(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(pPlain, nPlainLen, pHash);
197 void HashMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
199 MD5(pPlain, nPlainLen, pHash);
202 void HashSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
204 SHA1(pPlain, nPlainLen, pHash);
207 void HashRIPEMD160(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
209 RIPEMD160(pPlain, nPlainLen, pHash);
212 void HashMSCACHE(unsigned char *pPlain, int nPlainLen, unsigned char* pHash)
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];
222 // strcpy (username, "administrator");
225 for (i=0; i<nPlainLen; i++)
227 unicode_pwd[i*2] = pPlain[i];
228 unicode_pwd[i*2+1] = 0x00;
231 for (i=0; i<userlen; i++)
233 unicode_user[i*2] = username[i];
234 unicode_user[i*2+1] = 0x00;
238 MD4_Update(&ctx,unicode_pwd,nPlainLen*2);
239 MD4_Final(final1,&ctx);
242 MD4_Update(&ctx,final1,MD4_DIGEST_LENGTH);
243 MD4_Update(&ctx,(unsigned char*) unicode_user,userlen*2);
244 MD4_Final(pHash,&ctx);
247 unsigned char unicode_pwd[256];
248 for (int i=0; i<nPlainLen; i++)
250 unicode_pwd[i*2] = pPlain[i];
251 unicode_pwd[i*2+1] = 0x00;
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);
263 //*********************************************************************************
264 // Code for MySQL password hashing
265 //*********************************************************************************
267 inline void mysql_hash_password_323(unsigned long *result, const char *password)
269 register unsigned long nr=1345345333L, add=7, nr2=0x12345671L;
271 for (; *password ; password++)
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;
279 result[0]=nr & (((unsigned long) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
280 result[1]=nr2 & (((unsigned long) 1L << 31) -1L);
284 void HashMySQL323(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
286 unsigned long hash_pass[2];
287 unsigned char* f = (unsigned char*) hash_pass;
289 unsigned char* pass = (unsigned char*) calloc (nPlainLen+4,sizeof(unsigned char));
290 memcpy(pass,pPlain,nPlainLen);
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);
299 void HashMySQLSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
301 unsigned char hash_stage1[SHA_DIGEST_LENGTH];
305 SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
306 SHA1_Final(hash_stage1, &ctx);
308 SHA1_Update(&ctx, hash_stage1, SHA_DIGEST_LENGTH);
309 SHA1_Final(pHash, &ctx);
312 //*********************************************************************************
313 // Code for PIX password hashing
314 //*********************************************************************************
315 static char itoa64[] = /* 0 ... 63 => ascii - 64 */
316 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
318 void _crypt_to64(char *s, unsigned long v, int n)
321 *s++ = itoa64[v&0x3f];
326 void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
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));
332 memcpy (pass,pPlain,nPlainLen);
336 MD5_Update(&ctx, (unsigned char *) pass, MD5_DIGEST_LENGTH);
337 MD5_Final(final, &ctx);
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;
346 memcpy(pHash,temp,MD5_DIGEST_LENGTH);