2 * freerainbowtables is a project for generating, distributing, and using
3 * perfect rainbow tables
5 * Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
6 * Copyright Martin Westergaard Jørgensen <martinwj2005@gmail.com>
7 * Copyright 2009, 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>
8 * Copyright 2009, 2010 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};
179 strcpy_s(username, sizeof(username), "SYS");
181 strcpy(username, "SYS");
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);
213 void HashNTLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
215 unsigned char UnicodePlain[MAX_PLAIN_LEN * 2];
217 for (i = 0; i < nPlainLen; i++)
219 UnicodePlain[i * 2] = pPlain[i];
220 UnicodePlain[i * 2 + 1] = 0x00;
223 MD4_NEW(UnicodePlain, nPlainLen * 2, pHash);
227 void HashMD2(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
231 MD2_Update(&ctx, pPlain, nPlainLen);
232 MD2_Final(pHash, &ctx);
234 //MD2(pPlain, nPlainLen, pHash);
238 void HashMD4(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
240 MD4_NEW(pPlain, nPlainLen, pHash);
243 void HashMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
245 MD5_NEW(pPlain, nPlainLen, pHash);
247 void HashDoubleMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
249 MD5_NEW(pPlain, nPlainLen, pHash);
250 unsigned char hash[16];
251 memcpy(hash, pHash, 16);
252 MD5_NEW(hash, 16, pHash);
256 void HashSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
260 SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
261 SHA1_Final(pHash, &ctx);
264 void HashRIPEMD160(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
267 RIPEMD160_Init(&ctx);
268 RIPEMD160_Update(&ctx, pPlain, nPlainLen);
269 RIPEMD160_Final(pHash, &ctx);
271 //RIPEMD160(pPlain, nPlainLen, pHash);
274 void HashMSCACHE(unsigned char *pPlain, int nPlainLen, unsigned char* pHash)
276 char unicode_pwd[256];
277 char unicode_user[256];
278 static unsigned char username[] = "administrator";
279 static int userlen = 13;
280 unsigned char final1[MD4_DIGEST_LENGTH];
284 // strcpy (username, "administrator");
287 for (i=0; i<nPlainLen; i++)
289 unicode_pwd[i*2] = pPlain[i];
290 unicode_pwd[i*2+1] = 0x00;
293 for (i=0; i<userlen; i++)
295 unicode_user[i*2] = username[i];
296 unicode_user[i*2+1] = 0x00;
299 MD4_NEW( (unsigned char*)unicode_pwd, nPlainLen*2, final1 );
302 MD4_Update(&ctx,final1,MD4_DIGEST_LENGTH);
303 MD4_Update(&ctx,(unsigned char*) unicode_user,userlen*2);
304 MD4_Final(pHash,&ctx);
307 unsigned char unicode_pwd[256];
308 for (int i=0; i<nPlainLen; i++)
310 unicode_pwd[i*2] = pPlain[i];
311 unicode_pwd[i*2+1] = 0x00;
314 unsigned char *buf = (unsigned char*)calloc(MSCACHE_HASH_SIZE + nSaltLength, sizeof(unsigned char));
315 HashNTLM(pPlain, nPlainLen, buf, NULL);
316 //MD4(unicode_pwd, nPlainLen*2, buf);
317 memcpy(buf + MSCACHE_HASH_SIZE, pSalt, nSaltLength);
318 MD4(buf, MSCACHE_HASH_SIZE + nSaltLength, pHash);
323 //*********************************************************************************
324 // Code for MySQL password hashing
325 //*********************************************************************************
327 inline void mysql_hash_password_323(unsigned long *result, const char *password)
329 register unsigned long nr=1345345333L, add=7, nr2=0x12345671L;
331 for (; *password ; password++)
333 if (*password == ' ' || *password == '\t') continue;
334 tmp= (unsigned long) (unsigned char) *password;
335 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
336 nr2+=(nr2 << 8) ^ nr;
339 result[0]=nr & (((unsigned long) 1L << 31) -1L); // Don't use sign bit (str2int)
340 result[1]=nr2 & (((unsigned long) 1L << 31) -1L);
344 void HashMySQL323(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
346 unsigned long hash_pass[2];
347 unsigned char* f = (unsigned char*) hash_pass;
349 unsigned char* pass = (unsigned char*) calloc (nPlainLen+4,sizeof(unsigned char));
350 memcpy(pass,pPlain,nPlainLen);
352 mysql_hash_password_323(hash_pass, (char*) pass);
353 pHash[0]=*(f+3); pHash[1]=*(f+2); pHash[2]=*(f+1); pHash[3]=*(f+0);
354 pHash[4]=*(f+7); pHash[5]=*(f+6); pHash[6]=*(f+5); pHash[7]=*(f+4);
359 void HashMySQLSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
361 unsigned char hash_stage1[SHA_DIGEST_LENGTH];
365 SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
366 SHA1_Final(hash_stage1, &ctx);
368 SHA1_Update(&ctx, hash_stage1, SHA_DIGEST_LENGTH);
369 SHA1_Final(pHash, &ctx);
373 //*********************************************************************************
374 // Code for PIX password hashing
375 //*********************************************************************************
376 static char itoa64[] = /* 0 ... 63 => ascii - 64 */
377 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
379 void _crypt_to64(char *s, unsigned long v, int n)
382 *s++ = itoa64[v&0x3f];
387 void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
389 char temp[MD5_DIGEST_LENGTH+1];
390 unsigned char final[MD5_DIGEST_LENGTH];
391 char* pass = (char*) calloc (nPlainLen+MD5_DIGEST_LENGTH,sizeof(char));
393 memcpy (pass,pPlain,nPlainLen);
397 MD5_Update(&ctx, (unsigned char *) pass, MD5_DIGEST_LENGTH);
398 MD5_Final(final, &ctx);
399 MD5_NEW((unsigned char *) pass, MD5_DIGEST_LENGTH, final);
401 char* p = (char*) temp;
402 _crypt_to64(p,*(unsigned long*) (final+0),4); p += 4;
403 _crypt_to64(p,*(unsigned long*) (final+4),4); p += 4;
404 _crypt_to64(p,*(unsigned long*) (final+8),4); p += 4;
405 _crypt_to64(p,*(unsigned long*) (final+12),4); p += 4;
408 memcpy(pHash,temp,MD5_DIGEST_LENGTH);
413 #if !defined(_WIN32) || defined(__GNUC__)
414 char *strupr(char *s1)
419 *p = (char) toupper(*p);