2 * freerainbowtables is a project for generating, distributing, and using
3 * perfect rainbow tables
5 * Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
6 * Copyright 2009, 2010, 2011 Martin Westergaard Jørgensen <martinwj2005@gmail.com>
7 * Copyright 2009, 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>
8 * Copyright 2009, 2010, 2011 James Nobis <frt@quelrod.net>
10 * This file is part of freerainbowtables.
12 * freerainbowtables is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 2 of the License, or
15 * (at your option) any later version.
17 * freerainbowtables is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with freerainbowtables. If not, see <http://www.gnu.org/licenses/>.
25 * Changes: not using OpenSSL routines the slow way anymore, as suggested by jci.
28 #include "HashAlgorithm.h"
34 //#include <openssl/md2.h>
39 #if defined(_WIN32) && !defined(__GNUC__)
40 #pragma comment(lib, "libeay32.lib")
47 #define MSCACHE_HASH_SIZE 16
48 void setup_des_key(unsigned char key_56[], des_key_schedule &ks)
53 key[1] = (key_56[0] << 7) | (key_56[1] >> 1);
54 key[2] = (key_56[1] << 6) | (key_56[2] >> 2);
55 key[3] = (key_56[2] << 5) | (key_56[3] >> 3);
56 key[4] = (key_56[3] << 4) | (key_56[4] >> 4);
57 key[5] = (key_56[4] << 3) | (key_56[5] >> 5);
58 key[6] = (key_56[5] << 2) | (key_56[6] >> 6);
59 key[7] = (key_56[6] << 1);
61 //des_set_odd_parity(&key);
62 des_set_key(&key, ks);
65 void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
68 unsigned char data[7] = {0};
69 memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen);
73 for (i = nPlainLen; i < 7; i++)
76 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
78 //setup_des_key(data, ks);
79 setup_des_key(pPlain, ks);
80 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pHash, ks, DES_ENCRYPT);
83 void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
85 unsigned char pass[14];
86 unsigned char pre_lmresp[21];
87 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
88 static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
91 memset (pass,0,sizeof(pass));
92 memset (pre_lmresp,0,sizeof(pre_lmresp));
94 memcpy (pass,pPlain, nPlainLen);
96 setup_des_key(pass, ks);
97 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
99 setup_des_key(&pass[7], ks);
100 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT);
102 setup_des_key(pre_lmresp, ks);
103 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
105 setup_des_key(&pre_lmresp[7], ks);
106 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
108 setup_des_key(&pre_lmresp[14], ks);
109 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
113 void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
115 unsigned char pre_lmresp[8];
116 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
117 static unsigned char salt[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
120 unsigned char plain[8] = {0};
121 memcpy(plain, pPlain, nPlainLen);
122 setup_des_key(plain, ks);
123 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
125 setup_des_key(pre_lmresp, ks);
126 des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT);
131 void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
133 unsigned char UnicodePlain[MAX_PLAIN_LEN];
134 static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
136 int len = (nPlainLen < 127) ? nPlainLen : 127;
139 for (i = 0; i < len; i++)
141 UnicodePlain[i * 2] = pPlain[i];
142 UnicodePlain[i * 2 + 1] = 0x00;
146 unsigned char lm[21];
150 MD4_Update(&ctx, UnicodePlain, len * 2);
151 MD4_Final(lm, &ctx); */
152 MD4_NEW(UnicodePlain, len * 2, lm);
154 //MD4(UnicodePlain, len * 2, lm);
155 lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0;
157 setup_des_key(lm, ks);
158 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
160 setup_des_key(&lm[7], ks);
161 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
163 setup_des_key(&lm[14], ks);
164 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
168 void HashORACLE(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
175 DES_key_schedule ks1,ks2;
176 unsigned char deskey_fixed[]={ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
178 #if defined(_WIN32) && !defined(__GNUC__)
179 strcpy_s(username, sizeof(username), "SYS");
181 strcpy(username, "SYS");
184 #if defined(_WIN32) && !defined(__GNUC__)
185 _strupr((char*) pPlain);
187 strupr((char*) pPlain);
189 memset (ToEncrypt,0,sizeof(ToEncrypt));
191 for (i=1,j=0; j<userlen; i++,j++)
193 ToEncrypt[i] = username[j];
197 for (j=0; j<nPlainLen; i++,j++)
199 ToEncrypt[i] = pPlain[j];
206 DES_set_key((DES_cblock*) deskey_fixed, &ks1);
207 DES_ncbc_encrypt((unsigned char*) ToEncrypt, (unsigned char*) temp, i, &ks1, &iv, DES_ENCRYPT);
208 DES_set_key((DES_cblock*) &iv, &ks2);
209 DES_ncbc_encrypt((unsigned char*) ToEncrypt, (unsigned char*) temp, i, &ks2, &iv2, DES_ENCRYPT);
210 memcpy (pHash,iv2,8);
214 void HashNTLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
216 unsigned char UnicodePlain[MAX_PLAIN_LEN * 2];
218 for (i = 0; i < nPlainLen; i++)
220 UnicodePlain[i * 2] = pPlain[i];
221 UnicodePlain[i * 2 + 1] = 0x00;
224 MD4_NEW(UnicodePlain, nPlainLen * 2, pHash);
228 void HashMD2(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
232 MD2_Update(&ctx, pPlain, nPlainLen);
233 MD2_Final(pHash, &ctx);
235 //MD2(pPlain, nPlainLen, pHash);
239 void HashMD4(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
241 MD4_NEW(pPlain, nPlainLen, pHash);
244 void HashMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
246 MD5_NEW(pPlain, nPlainLen, pHash);
248 void HashDoubleMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
250 MD5_NEW(pPlain, nPlainLen, pHash);
251 unsigned char hash[16];
252 memcpy(hash, pHash, 16);
253 MD5_NEW(hash, 16, pHash);
257 void HashSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
261 SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
262 SHA1_Final(pHash, &ctx);
265 void HashRIPEMD160(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
268 RIPEMD160_Init(&ctx);
269 RIPEMD160_Update(&ctx, pPlain, nPlainLen);
270 RIPEMD160_Final(pHash, &ctx);
272 //RIPEMD160(pPlain, nPlainLen, pHash);
275 void HashMSCACHE(unsigned char *pPlain, int nPlainLen, unsigned char* pHash)
277 char unicode_pwd[256];
278 char unicode_user[256];
279 static unsigned char username[] = "administrator";
280 static int userlen = 13;
281 unsigned char final1[MD4_DIGEST_LENGTH];
285 // strcpy (username, "administrator");
288 for (i=0; i<nPlainLen; i++)
290 unicode_pwd[i*2] = pPlain[i];
291 unicode_pwd[i*2+1] = 0x00;
294 for (i=0; i<userlen; i++)
296 unicode_user[i*2] = username[i];
297 unicode_user[i*2+1] = 0x00;
300 MD4_NEW( (unsigned char*)unicode_pwd, nPlainLen*2, final1 );
303 MD4_Update(&ctx,final1,MD4_DIGEST_LENGTH);
304 MD4_Update(&ctx,(unsigned char*) unicode_user,userlen*2);
305 MD4_Final(pHash,&ctx);
308 unsigned char unicode_pwd[256];
309 for (int i=0; i<nPlainLen; i++)
311 unicode_pwd[i*2] = pPlain[i];
312 unicode_pwd[i*2+1] = 0x00;
315 unsigned char *buf = (unsigned char*)calloc(MSCACHE_HASH_SIZE + nSaltLength, sizeof(unsigned char));
316 HashNTLM(pPlain, nPlainLen, buf, NULL);
317 //MD4(unicode_pwd, nPlainLen*2, buf);
318 memcpy(buf + MSCACHE_HASH_SIZE, pSalt, nSaltLength);
319 MD4(buf, MSCACHE_HASH_SIZE + nSaltLength, pHash);
324 //*********************************************************************************
325 // Code for MySQL password hashing
326 //*********************************************************************************
328 inline void mysql_hash_password_323(unsigned long *result, const char *password)
330 register unsigned long nr=1345345333L, add=7, nr2=0x12345671L;
332 for (; *password ; password++)
334 if (*password == ' ' || *password == '\t') continue;
335 tmp= (unsigned long) (unsigned char) *password;
336 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
337 nr2+=(nr2 << 8) ^ nr;
340 result[0]=nr & (((unsigned long) 1L << 31) -1L); // Don't use sign bit (str2int)
341 result[1]=nr2 & (((unsigned long) 1L << 31) -1L);
345 void HashMySQL323(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
347 unsigned long hash_pass[2];
348 unsigned char* f = (unsigned char*) hash_pass;
350 unsigned char* pass = (unsigned char*) calloc (nPlainLen+4,sizeof(unsigned char));
351 memcpy(pass,pPlain,nPlainLen);
353 mysql_hash_password_323(hash_pass, (char*) pass);
354 pHash[0]=*(f+3); pHash[1]=*(f+2); pHash[2]=*(f+1); pHash[3]=*(f+0);
355 pHash[4]=*(f+7); pHash[5]=*(f+6); pHash[6]=*(f+5); pHash[7]=*(f+4);
360 void HashMySQLSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
362 unsigned char hash_stage1[SHA_DIGEST_LENGTH];
366 SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
367 SHA1_Final(hash_stage1, &ctx);
369 SHA1_Update(&ctx, hash_stage1, SHA_DIGEST_LENGTH);
370 SHA1_Final(pHash, &ctx);
374 //*********************************************************************************
375 // Code for PIX password hashing
376 //*********************************************************************************
377 static char itoa64[] = /* 0 ... 63 => ascii - 64 */
378 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
380 void _crypt_to64(char *s, unsigned long v, int n)
383 *s++ = itoa64[v&0x3f];
388 void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
390 char temp[MD5_DIGEST_LENGTH+1];
391 unsigned char final[MD5_DIGEST_LENGTH];
392 char* pass = (char*) calloc (nPlainLen+MD5_DIGEST_LENGTH,sizeof(char));
394 memcpy (pass,pPlain,nPlainLen);
396 MD5_NEW((unsigned char *) pass, MD5_DIGEST_LENGTH, final);
398 char* p = (char*) temp;
399 _crypt_to64(p,*(unsigned long*) (final+0),4); p += 4;
400 _crypt_to64(p,*(unsigned long*) (final+4),4); p += 4;
401 _crypt_to64(p,*(unsigned long*) (final+8),4); p += 4;
402 _crypt_to64(p,*(unsigned long*) (final+12),4); p += 4;
405 memcpy(pHash,temp,MD5_DIGEST_LENGTH);
410 #if !defined(_WIN32) || defined(__GNUC__)
411 char *strupr(char *s1)
416 *p = (char) toupper(*p);