2 * rcracki_mt is a multithreaded implementation and fork of the original
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 rcracki_mt.
12 * rcracki_mt 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 * rcracki_mt 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 rcracki_mt. 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"
32 #include <openssl/des.h>
33 //#include <openssl/md2.h>
34 #include <openssl/md4.h>
35 #include <openssl/sha.h>
36 //#include <openssl/ripemd.h>
40 #if defined(_WIN32) && !defined(__GNUC__)
41 #pragma comment(lib, "libeay32.lib")
48 #define MSCACHE_HASH_SIZE 16
49 void setup_des_key(unsigned char key_56[], des_key_schedule &ks)
54 key[1] = (key_56[0] << 7) | (key_56[1] >> 1);
55 key[2] = (key_56[1] << 6) | (key_56[2] >> 2);
56 key[3] = (key_56[2] << 5) | (key_56[3] >> 3);
57 key[4] = (key_56[3] << 4) | (key_56[4] >> 4);
58 key[5] = (key_56[4] << 3) | (key_56[5] >> 5);
59 key[6] = (key_56[5] << 2) | (key_56[6] >> 6);
60 key[7] = (key_56[6] << 1);
62 //des_set_odd_parity(&key);
63 des_set_key(&key, ks);
66 void HashLM(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
69 unsigned char data[7] = {0};
70 memcpy(data, pPlain, nPlainLen > 7 ? 7 : nPlainLen);
74 for (i = nPlainLen; i < 7; i++)
77 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
79 //setup_des_key(data, ks);
80 setup_des_key(pPlain, ks);
81 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pHash, ks, DES_ENCRYPT);
84 void HashLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
86 unsigned char pass[14];
87 unsigned char pre_lmresp[21];
88 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
89 static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
92 memset (pass,0,sizeof(pass));
93 memset (pre_lmresp,0,sizeof(pre_lmresp));
95 memcpy (pass,pPlain, nPlainLen);
97 setup_des_key(pass, ks);
98 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
100 setup_des_key(&pass[7], ks);
101 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)&pre_lmresp[8], ks, DES_ENCRYPT);
103 setup_des_key(pre_lmresp, ks);
104 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
106 setup_des_key(&pre_lmresp[7], ks);
107 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
109 setup_des_key(&pre_lmresp[14], ks);
110 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[16], ks, DES_ENCRYPT);
114 void HashHALFLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
116 unsigned char pre_lmresp[8];
117 static unsigned char magic[] = {0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
118 static unsigned char salt[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
121 unsigned char plain[8] = {0};
122 memcpy(plain, pPlain, nPlainLen);
123 setup_des_key(plain, ks);
124 des_ecb_encrypt((des_cblock*)magic, (des_cblock*)pre_lmresp, ks, DES_ENCRYPT);
126 setup_des_key(pre_lmresp, ks);
127 des_ecb_encrypt((des_cblock*)salt, (des_cblock*)pHash, ks, DES_ENCRYPT);
132 void HashNTLMCHALL(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
134 unsigned char UnicodePlain[MAX_PLAIN_LEN];
135 static unsigned char spoofed_challange[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
137 int len = (nPlainLen < 127) ? nPlainLen : 127;
140 for (i = 0; i < len; i++)
142 UnicodePlain[i * 2] = pPlain[i];
143 UnicodePlain[i * 2 + 1] = 0x00;
147 unsigned char lm[21];
151 MD4_Update(&ctx, UnicodePlain, len * 2);
152 MD4_Final(lm, &ctx); */
153 MD4_NEW(UnicodePlain, len * 2, lm);
155 //MD4(UnicodePlain, len * 2, lm);
156 lm[16] = lm[17] = lm[18] = lm[19] = lm[20] = 0;
158 setup_des_key(lm, ks);
159 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)pHash, ks, DES_ENCRYPT);
161 setup_des_key(&lm[7], ks);
162 des_ecb_encrypt((des_cblock*)spoofed_challange, (des_cblock*)&pHash[8], ks, DES_ENCRYPT);
164 setup_des_key(&lm[14], ks);
165 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);
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 fast_MD5(pPlain, nPlainLen, pHash);
247 void HashDoubleMD5(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
249 fast_MD5(pPlain, nPlainLen, pHash);
250 unsigned char hash[16];
251 memcpy(hash, pHash, 16);
252 fast_MD5(hash, 16, pHash);
255 void HashSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
259 SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
260 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);
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;
301 MD4_Update(&ctx,unicode_pwd,nPlainLen*2);
302 MD4_Final(final1,&ctx);
304 MD4_NEW( (unsigned char*)unicode_pwd, nPlainLen*2, final1 );
307 MD4_Update(&ctx,final1,MD4_DIGEST_LENGTH);
308 MD4_Update(&ctx,(unsigned char*) unicode_user,userlen*2);
309 MD4_Final(pHash,&ctx);
312 unsigned char unicode_pwd[256];
313 for (int i=0; i<nPlainLen; i++)
315 unicode_pwd[i*2] = pPlain[i];
316 unicode_pwd[i*2+1] = 0x00;
319 unsigned char *buf = (unsigned char*)calloc(MSCACHE_HASH_SIZE + nSaltLength, sizeof(unsigned char));
320 HashNTLM(pPlain, nPlainLen, buf, NULL);
321 //MD4(unicode_pwd, nPlainLen*2, buf);
322 memcpy(buf + MSCACHE_HASH_SIZE, pSalt, nSaltLength);
323 MD4(buf, MSCACHE_HASH_SIZE + nSaltLength, pHash);
328 //*********************************************************************************
329 // Code for MySQL password hashing
330 //*********************************************************************************
332 inline void mysql_hash_password_323(unsigned long *result, const char *password)
334 register unsigned long nr=1345345333L, add=7, nr2=0x12345671L;
336 for (; *password ; password++)
338 if (*password == ' ' || *password == '\t') continue;
339 tmp= (unsigned long) (unsigned char) *password;
340 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
341 nr2+=(nr2 << 8) ^ nr;
344 result[0]=nr & (((unsigned long) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
345 result[1]=nr2 & (((unsigned long) 1L << 31) -1L);
349 void HashMySQL323(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
351 unsigned long hash_pass[2];
352 unsigned char* f = (unsigned char*) hash_pass;
354 unsigned char* pass = (unsigned char*) calloc (nPlainLen+4,sizeof(unsigned char));
355 memcpy(pass,pPlain,nPlainLen);
357 mysql_hash_password_323(hash_pass, (char*) pass);
358 pHash[0]=*(f+3); pHash[1]=*(f+2); pHash[2]=*(f+1); pHash[3]=*(f+0);
359 pHash[4]=*(f+7); pHash[5]=*(f+6); pHash[6]=*(f+5); pHash[7]=*(f+4);
364 void HashMySQLSHA1(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
366 unsigned char hash_stage1[SHA_DIGEST_LENGTH];
370 SHA1_Update(&ctx, (unsigned char *) pPlain, nPlainLen);
371 SHA1_Final(hash_stage1, &ctx);
373 SHA1_Update(&ctx, hash_stage1, SHA_DIGEST_LENGTH);
374 SHA1_Final(pHash, &ctx);
377 //*********************************************************************************
378 // Code for PIX password hashing
379 //*********************************************************************************
380 static char itoa64[] = /* 0 ... 63 => ascii - 64 */
381 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
383 void _crypt_to64(char *s, unsigned long v, int n)
386 *s++ = itoa64[v&0x3f];
391 void HashPIX(unsigned char* pPlain, int nPlainLen, unsigned char* pHash)
393 char temp[MD5_DIGEST_LENGTH+1];
394 unsigned char final[MD5_DIGEST_LENGTH];
395 char* pass = (char*) calloc (nPlainLen+MD5_DIGEST_LENGTH,sizeof(char));
397 memcpy (pass,pPlain,nPlainLen);
401 MD5_Update(&ctx, (unsigned char *) pass, MD5_DIGEST_LENGTH);
402 MD5_Final(final, &ctx);*/
403 fast_MD5((unsigned char *) pass, MD5_DIGEST_LENGTH, final);
405 char* p = (char*) temp;
406 _crypt_to64(p,*(unsigned long*) (final+0),4); p += 4;
407 _crypt_to64(p,*(unsigned long*) (final+4),4); p += 4;
408 _crypt_to64(p,*(unsigned long*) (final+8),4); p += 4;
409 _crypt_to64(p,*(unsigned long*) (final+12),4); p += 4;
412 memcpy(pHash,temp,MD5_DIGEST_LENGTH);
417 #if !defined(_WIN32) || defined(__GNUC__)
418 char *strupr(char *s1)
423 *p = (char) toupper(*p);