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>
9 * Copyright 2010 Yngve AAdlandsvik
11 * This file is part of rcracki_mt.
13 * rcracki_mt is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 2 of the License, or
16 * (at your option) any later version.
18 * rcracki_mt is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with rcracki_mt. If not, see <http://www.gnu.org/licenses/>.
27 #if defined(_WIN32) && !defined(__GNUC__)
28 #pragma warning(disable : 4786 4267 4018)
31 #include "ChainWalkContext.h"
35 //////////////////////////////////////////////////////////////////////
37 string CChainWalkContext::m_sHashRoutineName;
38 HASHROUTINE CChainWalkContext::m_pHashRoutine;
39 int CChainWalkContext::m_nHashLen;
40 int CChainWalkContext::m_nPlainLenMinTotal = 0;
41 int CChainWalkContext::m_nPlainLenMaxTotal = 0;
42 int CChainWalkContext::m_nHybridCharset = 0;
43 bool CChainWalkContext::isOldRtFormat = false;
44 bool CChainWalkContext::isRti2RtFormat = false;
45 vector<stCharset> CChainWalkContext::m_vCharset;
46 uint64 CChainWalkContext::m_nPlainSpaceUpToX[MAX_PLAIN_LEN + 1];
47 uint64 CChainWalkContext::m_nPlainSpaceTotal;
48 unsigned char CChainWalkContext::m_Salt[MAX_SALT_LEN];
49 int CChainWalkContext::m_nSaltLen = 0;
50 int CChainWalkContext::m_nRainbowTableIndex;
51 uint64 CChainWalkContext::m_nReduceOffset;
53 //////////////////////////////////////////////////////////////////////
55 CChainWalkContext::CChainWalkContext()
59 CChainWalkContext::~CChainWalkContext()
63 bool CChainWalkContext::LoadCharset(string sName)
70 for (i = 0x00; i <= 0xff; i++)
71 tCharset.m_PlainCharset[i] = (unsigned char) i;
72 tCharset.m_nPlainCharsetLen = 256;
73 tCharset.m_sPlainCharsetName = sName;
74 tCharset.m_sPlainCharsetContent = "0x00, 0x01, ... 0xff";
75 m_vCharset.push_back(tCharset);
78 if(sName.substr(0, 6) == "hybrid") // Hybrid charset consisting of 2 charsets
83 bool readCharset = false;
86 if ( ReadLinesFromFile("charset.txt", vLine) )
88 else if ( ReadLinesFromFile(GetApplicationPath() + "charset.txt", vLine) )
94 for (i = 0; i < vLine.size(); i++)
97 if (vLine[i][0] == '#')
100 vector<string> vPart;
101 if (SeperateString(vLine[i], "=", vPart))
104 string sCharsetName = TrimString(vPart[0]);
105 if (sCharsetName == "")
108 // sCharsetName charset check
109 bool fCharsetNameCheckPass = true;
111 for (j = 0; j < sCharsetName.size(); j++)
113 if ( !isalpha(sCharsetName[j])
114 && !isdigit(sCharsetName[j])
115 && (sCharsetName[j] != '-'))
117 fCharsetNameCheckPass = false;
121 if (!fCharsetNameCheckPass)
123 printf("invalid charset name %s in charset configuration file\n", sCharsetName.c_str());
128 string sCharsetContent = TrimString(vPart[1]);
129 if (sCharsetContent == "" || sCharsetContent == "[]")
131 if (sCharsetContent[0] != '[' || sCharsetContent[sCharsetContent.size() - 1] != ']')
133 printf("invalid charset content %s in charset configuration file\n", sCharsetContent.c_str());
136 sCharsetContent = sCharsetContent.substr(1, sCharsetContent.size() - 2);
137 if (sCharsetContent.size() > 256)
139 printf("charset content %s too long\n", sCharsetContent.c_str());
143 //printf("%s = [%s]\n", sCharsetName.c_str(), sCharsetContent.c_str());
145 // Is it the wanted charset?
146 if(m_nHybridCharset == 1)
148 vector<tCharset> vCharsets;
149 GetHybridCharsets(sName, vCharsets);
150 if(sCharsetName == vCharsets[m_vCharset.size()].sName)
153 tCharset.m_nPlainCharsetLen = sCharsetContent.size();
154 memcpy(tCharset.m_PlainCharset, sCharsetContent.c_str(), tCharset.m_nPlainCharsetLen);
155 tCharset.m_sPlainCharsetName = sCharsetName;
156 tCharset.m_sPlainCharsetContent = sCharsetContent;
157 tCharset.m_nPlainLenMin = vCharsets[m_vCharset.size()].nPlainLenMin;
158 tCharset.m_nPlainLenMax = vCharsets[m_vCharset.size()].nPlainLenMax;
159 m_vCharset.push_back(tCharset);
160 if(vCharsets.size() == m_vCharset.size())
162 i = 0; // Start the lookup over again for the next charset
165 else if (sCharsetName == sName)
168 tCharset.m_nPlainCharsetLen = sCharsetContent.size();
169 memcpy(tCharset.m_PlainCharset, sCharsetContent.c_str(), tCharset.m_nPlainCharsetLen);
170 tCharset.m_sPlainCharsetName = sCharsetName;
171 tCharset.m_sPlainCharsetContent = sCharsetContent;
172 m_vCharset.push_back(tCharset);
177 printf("charset %s not found in charset.txt\n", sName.c_str());
180 printf("can't open charset configuration file\n");
184 //////////////////////////////////////////////////////////////////////
186 bool CChainWalkContext::SetHashRoutine(string sHashRoutineName)
189 hr.GetHashRoutine(sHashRoutineName, m_pHashRoutine, m_nHashLen);
190 if (m_pHashRoutine != NULL)
192 m_sHashRoutineName = sHashRoutineName;
199 bool CChainWalkContext::SetPlainCharset(string sCharsetName, int nPlainLenMin, int nPlainLenMax)
201 // m_PlainCharset, m_nPlainCharsetLen, m_sPlainCharsetName, m_sPlainCharsetContent
202 if (!LoadCharset(sCharsetName))
205 if(m_vCharset.size() == 1) // Not hybrid charset
207 // m_nPlainLenMin, m_nPlainLenMax
208 if (nPlainLenMin < 1 || nPlainLenMax > MAX_PLAIN_LEN || nPlainLenMin > nPlainLenMax)
210 printf("invalid plaintext length range: %d - %d\n", nPlainLenMin, nPlainLenMax);
213 m_vCharset[0].m_nPlainLenMin = nPlainLenMin;
214 m_vCharset[0].m_nPlainLenMax = nPlainLenMax;
216 // m_nPlainSpaceUpToX
217 m_nPlainSpaceUpToX[0] = 0;
218 m_nPlainLenMaxTotal = 0;
219 m_nPlainLenMinTotal = 0;
222 for(j = 0; j < m_vCharset.size(); j++)
225 m_nPlainLenMaxTotal += m_vCharset[j].m_nPlainLenMax;
226 m_nPlainLenMinTotal += m_vCharset[j].m_nPlainLenMin;
227 for (i = 1; i <= m_vCharset[j].m_nPlainLenMax; i++)
229 nTemp *= m_vCharset[j].m_nPlainCharsetLen;
230 if (i < m_vCharset[j].m_nPlainLenMin)
231 m_nPlainSpaceUpToX[k] = 0;
233 m_nPlainSpaceUpToX[k] = m_nPlainSpaceUpToX[k - 1] + nTemp;
237 // m_nPlainSpaceTotal
238 m_nPlainSpaceTotal = m_nPlainSpaceUpToX[m_nPlainLenMaxTotal];
243 bool CChainWalkContext::SetRainbowTableIndex(int nRainbowTableIndex)
245 if (nRainbowTableIndex < 0)
247 m_nRainbowTableIndex = nRainbowTableIndex;
248 m_nReduceOffset = 65536 * nRainbowTableIndex;
253 bool CChainWalkContext::SetSalt(unsigned char *Salt, int nSaltLength)
255 memcpy(&m_Salt[0], Salt, nSaltLength);
257 m_nSaltLen = nSaltLength;
262 bool CChainWalkContext::SetupWithPathName(string sPathName, int& nRainbowChainLen, int& nRainbowChainCount)
264 // something like lm_alpha#1-7_0_100x16_test.rt
267 string::size_type nIndex = sPathName.find_last_of('\\');
269 string::size_type nIndex = sPathName.find_last_of('/');
271 if (nIndex != string::npos)
272 sPathName = sPathName.substr(nIndex + 1);
274 if (sPathName.size() < 3)
276 printf("%s is not a rainbow table\n", sPathName.c_str());
279 if (sPathName.substr(sPathName.size() - 5) == ".rti2")
281 isRti2RtFormat = true;
283 else if (sPathName.substr(sPathName.size() - 4) == ".rti")
285 isOldRtFormat = false;
287 else if (sPathName.substr(sPathName.size() - 3) == ".rt")
289 isOldRtFormat = true;
293 printf("%s is not a rainbow table\n", sPathName.c_str());
298 vector<string> vPart;
299 if (!SeperateString(sPathName, "___x_", vPart))
301 printf("filename %s not identified\n", sPathName.c_str());
305 string sHashRoutineName = vPart[0];
306 int nRainbowTableIndex = atoi(vPart[2].c_str());
307 nRainbowChainLen = atoi(vPart[3].c_str());
308 nRainbowChainCount = atoi(vPart[4].c_str());
310 // Parse charset definition
311 string sCharsetDefinition = vPart[1];
313 int nPlainLenMin = 0, nPlainLenMax = 0;
315 // printf("Charset: %s", sCharsetDefinition.c_str());
317 if(sCharsetDefinition.substr(0, 6) == "hybrid") // Hybrid table
319 sCharsetName = sCharsetDefinition;
323 if ( sCharsetDefinition.find('#') == string::npos ) // For backward compatibility, "#1-7" is implied
325 sCharsetName = sCharsetDefinition;
331 vector<string> vCharsetDefinitionPart;
332 if (!SeperateString(sCharsetDefinition, "#-", vCharsetDefinitionPart))
334 printf("filename %s not identified\n", sPathName.c_str());
339 sCharsetName = vCharsetDefinitionPart[0];
340 nPlainLenMin = atoi(vCharsetDefinitionPart[1].c_str());
341 nPlainLenMax = atoi(vCharsetDefinitionPart[2].c_str());
346 if (!SetHashRoutine(sHashRoutineName))
348 printf("hash routine %s not supported\n", sHashRoutineName.c_str());
351 if (!SetPlainCharset(sCharsetName, nPlainLenMin, nPlainLenMax))
353 if (!SetRainbowTableIndex(nRainbowTableIndex))
355 printf("invalid rainbow table index %d\n", nRainbowTableIndex);
358 m_nPlainSpaceTotal = m_nPlainSpaceUpToX[m_nPlainLenMaxTotal];
362 string CChainWalkContext::GetHashRoutineName()
364 return m_sHashRoutineName;
367 int CChainWalkContext::GetHashLen()
372 string CChainWalkContext::GetPlainCharsetName()
374 return m_vCharset[0].m_sPlainCharsetName;
377 string CChainWalkContext::GetPlainCharsetContent()
379 return m_vCharset[0].m_sPlainCharsetContent;
382 int CChainWalkContext::GetPlainLenMin()
384 return m_vCharset[0].m_nPlainLenMin;
387 int CChainWalkContext::GetPlainLenMax()
389 return m_vCharset[0].m_nPlainLenMax;
392 uint64 CChainWalkContext::GetPlainSpaceTotal()
394 return m_nPlainSpaceTotal;
397 int CChainWalkContext::GetRainbowTableIndex()
399 return m_nRainbowTableIndex;
402 void CChainWalkContext::Dump()
404 printf("hash routine: %s\n", m_sHashRoutineName.c_str());
405 printf("hash length: %d\n", m_nHashLen);
407 printf("plain charset: ");
409 for (i = 0; i < m_vCharset[0].m_nPlainCharsetLen; i++)
411 if (isprint(m_vCharset[0].m_PlainCharset[i]))
412 printf("%c", m_vCharset[0].m_PlainCharset[i]);
418 printf("plain charset in hex: ");
419 for (i = 0; i < m_vCharset[0].m_nPlainCharsetLen; i++)
420 printf("%02x ", m_vCharset[0].m_PlainCharset[i]);
423 printf("plain length range: %d - %d\n", m_vCharset[0].m_nPlainLenMin, m_vCharset[0].m_nPlainLenMax);
424 printf("plain charset name: %s\n", m_vCharset[0].m_sPlainCharsetName.c_str());
425 //printf("plain charset content: %s\n", m_sPlainCharsetContent.c_str());
426 //for (i = 0; i <= m_nPlainLenMax; i++)
427 // printf("plain space up to %d: %s\n", i, uint64tostr(m_nPlainSpaceUpToX[i]).c_str());
428 printf("plain space total: %s\n", uint64tostr(m_nPlainSpaceTotal).c_str());
430 printf("rainbow table index: %d\n", m_nRainbowTableIndex);
431 printf("reduce offset: %s\n", uint64tostr(m_nReduceOffset).c_str());
435 void CChainWalkContext::SetIndex(uint64 nIndex)
440 void CChainWalkContext::SetHash(unsigned char* pHash)
442 memcpy(m_Hash, pHash, m_nHashLen);
445 void CChainWalkContext::IndexToPlain()
450 for (i = m_nPlainLenMaxTotal - 1; i >= m_nPlainLenMinTotal - 1; i--)
452 if (m_nIndex >= m_nPlainSpaceUpToX[i])
459 // this is an optimized version of the above
461 for (i = m_nPlainLenMaxTotal - 1; i >= m_nPlainLenMinTotal - 1
462 && m_nIndex < m_nPlainSpaceUpToX[i]; i--)
469 m_nPlainLen = m_nPlainLenMinTotal;
470 uint64 nIndexOfX = m_nIndex - m_nPlainSpaceUpToX[m_nPlainLen - 1];
472 // this is the generic code for non x86/x86_64 platforms
473 #if !defined(_M_X64) && !defined(_M_IX86) && !defined(__i386__) && !defined(__x86_64__)
475 // generic version (slow for non 64-bit platforms and gcc < 4.5.x)
476 for (i = m_nPlainLen - 1; i >= 0; i--)
479 for(UINT4 j = 0; j < m_vCharset.size(); j++)
481 nCharsetLen += m_vCharset[j].m_nPlainLenMax;
482 if(i < nCharsetLen) // We found the correct charset
484 m_Plain[i] = m_vCharset[j].m_PlainCharset[nIndexOfX % m_vCharset[j].m_nPlainCharsetLen];
485 nIndexOfX /= m_vCharset[j].m_nPlainCharsetLen;
491 #elif defined(_M_X64) || defined(_M_IX86) || defined(__i386__) || defined(__x86_64__)
494 for (i = m_nPlainLen - 1; i >= 0; i--)
496 // 0x100000000 = 2^32
498 if (nIndexOfX < 0x100000000I64)
501 if (nIndexOfX < 0x100000000llu)
506 for(UINT4 j = 0; j < m_vCharset.size(); j++)
508 nCharsetLen += m_vCharset[j].m_nPlainLenMax;
509 if(i < nCharsetLen) // We found the correct charset
511 m_Plain[i] = m_vCharset[j].m_PlainCharset[nIndexOfX % m_vCharset[j].m_nPlainCharsetLen];
512 nIndexOfX /= m_vCharset[j].m_nPlainCharsetLen;
518 UINT4 nIndexOfX32 = (UINT4)nIndexOfX;
522 for(UINT4 j = 0; j < m_vCharset.size(); j++)
524 nCharsetLen += m_vCharset[j].m_nPlainLenMax;
525 if(i < nCharsetLen) // We found the correct charset
528 // m_Plain[i] = m_vCharset[j].m_PlainCharset[nIndexOfX32 % m_vCharset[j].m_nPlainCharsetLen];
529 // nIndexOfX32 /= m_vCharset[j].m_nPlainCharsetLen;
532 // moving nPlainCharsetLen into the asm body and avoiding the extra temp
533 // variable results in a performance gain
534 // unsigned int nPlainCharsetLen = m_vCharset[j].m_nPlainCharsetLen;
537 #if defined(_WIN32) && !defined(__GNUC__)
539 // VC++ still needs this
540 unsigned int nPlainCharsetLen = m_vCharset[j].m_nPlainCharsetLen;
550 m_Plain[i] = m_vCharset[j].m_PlainCharset[nTemp];
552 __asm__ __volatile__ ( "mov %2, %%eax;"
557 : "=m"(nIndexOfX32), "=m"(nTemp)
558 : "m"(nIndexOfX32), "m"(m_vCharset[j].m_nPlainCharsetLen)
561 m_Plain[i] = m_vCharset[j].m_PlainCharset[nTemp];
570 void CChainWalkContext::PlainToHash()
572 m_pHashRoutine(m_Plain, m_nPlainLen, m_Hash);
575 void CChainWalkContext::HashToIndex(int nPos)
577 m_nIndex = (*(uint64*)m_Hash + m_nReduceOffset + nPos) % m_nPlainSpaceTotal;
580 uint64 CChainWalkContext::GetIndex()
584 const uint64 *CChainWalkContext::GetIndexPtr()
589 string CChainWalkContext::GetPlain()
593 for (i = 0; i < m_nPlainLen; i++)
602 string CChainWalkContext::GetBinary()
604 return HexToStr(m_Plain, m_nPlainLen);
607 string CChainWalkContext::GetHash()
609 return HexToStr(m_Hash, m_nHashLen);
612 bool CChainWalkContext::CheckHash(unsigned char* pHash)
614 if (memcmp(m_Hash, pHash, m_nHashLen) == 0)
620 bool CChainWalkContext::isOldFormat()
622 return isOldRtFormat;
625 bool CChainWalkContext::isRti2Format()
627 return isRti2RtFormat;