2 * rcracki_mt is a multithreaded implementation and fork of the original
\r
5 * Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
\r
6 * Copyright Martin Westergaard Jørgensen <martinwj2005@gmail.com>
\r
7 * Copyright 2009, 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>
\r
8 * Copyright 2009, 2010, 2011 James Nobis <frt@quelrod.net>
\r
10 * This file is part of rcracki_mt.
\r
12 * rcracki_mt is free software: you can redistribute it and/or modify
\r
13 * it under the terms of the GNU General Public License as published by
\r
14 * the Free Software Foundation, either version 2 of the License, or
\r
15 * (at your option) any later version.
\r
17 * rcracki_mt is distributed in the hope that it will be useful,
\r
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
20 * GNU General Public License for more details.
\r
22 * You should have received a copy of the GNU General Public License
\r
23 * along with rcracki_mt. If not, see <http://www.gnu.org/licenses/>.
\r
26 #if defined(_WIN32) && !defined(__GNUC__)
\r
27 #pragma warning(disable : 4786)
\r
30 #include "ChainWalkSet.h"
\r
32 CChainWalkSet::CChainWalkSet()
\r
34 m_sHashRoutineName = "";
\r
35 m_sPlainCharsetName = "";
\r
38 m_nRainbowTableIndex = 0;
\r
39 m_nRainbowChainLen = 0;
\r
41 sPrecalcPathName = "";
\r
45 CChainWalkSet::~CChainWalkSet()
\r
50 void CChainWalkSet::DiscardAll()
\r
52 //printf("debug: discarding all walk...\n");
\r
54 list<ChainWalk>::iterator it;
\r
55 for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++)
\r
56 delete [] it->pIndexE;
\r
57 m_lChainWalk.clear();
\r
60 string CChainWalkSet::CheckOrRotatePreCalcFile()
\r
62 char sPreCalcFileName[255];
\r
64 // 255 files limit to be sure
\r
65 for (; preCalcPart < 255; preCalcPart++)
\r
67 sprintf(sPreCalcFileName, "%s.%d", sPrecalcPathName.c_str(), preCalcPart);
\r
68 string sReturnPreCalcPath(sPreCalcFileName);
\r
72 FILE* file = fopen(sReturnPreCalcPath.c_str(), "ab");
\r
75 fileLen = GetFileLen(file);
\r
76 long unsigned int nextFileLen = fileLen + (sizeof(uint64) * (m_nRainbowChainLen-1));
\r
77 // Rotate to next file if we are going to pass 2GB filesize
\r
78 if (nextFileLen < ((unsigned)2 * 1024 * 1024 * 1024))
\r
80 // We might want to vPrecalcFiles.push_back(sReturnPreCalcPath) if we just created this file
\r
81 // We don't as only newly generated chainwalksets will be stored to this new file, so we don't have to look there
\r
82 if (debug) printf("Debug: Using for precalc: %s\n", sReturnPreCalcPath.c_str());
\r
84 return sReturnPreCalcPath;
\r
93 void CChainWalkSet::updateUsedPrecalcFiles()
\r
95 // we might also use this function to search a wildcard path of precalc files
\r
96 vPrecalcFiles.clear();
\r
97 char sPreCalcFileName[255];
\r
101 for (i = 0; i < 255; i++)
\r
103 sprintf(sPreCalcFileName, "%s.%d", sPrecalcPathName.c_str(), i);
\r
104 string sTryPreCalcPath(sPreCalcFileName);
\r
105 FILE* file = fopen(sTryPreCalcPath.c_str(), "rb");
\r
107 vPrecalcFiles.push_back(sTryPreCalcPath);
\r
116 void CChainWalkSet::removePrecalcFiles()
\r
118 if (debug) printf("Debug: Removing precalc files.\n");
\r
119 updateUsedPrecalcFiles();
\r
120 string sCurrentPrecalcPathName = "";
\r
121 string sCurrentPrecalcIndexPathName = "";
\r
124 for (i = 0; i < (int)vPrecalcFiles.size(); i++)
\r
126 sCurrentPrecalcPathName = vPrecalcFiles[i];
\r
127 sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index";
\r
129 if (debug) printf("Debug: Removing precalc file: %s\n", sCurrentPrecalcPathName.c_str());
\r
131 if (remove(sCurrentPrecalcPathName.c_str()) != 0)
\r
132 if (debug) printf("Debug: Failed removing precalc file: %s\n", sCurrentPrecalcPathName.c_str());
\r
134 if (debug) printf("Debug: Removing precalc index file: %s\n", sCurrentPrecalcIndexPathName.c_str());
\r
136 if (remove(sCurrentPrecalcIndexPathName.c_str()) != 0)
\r
137 if (debug) printf("Debug: Failed removing precalc index file: %s\n", sCurrentPrecalcIndexPathName.c_str());
\r
142 bool CChainWalkSet::FindInFile(uint64* pIndexE, unsigned char* pHash, int nHashLen)
\r
144 int gotPrecalcOnLine = -1;
\r
145 char precalculationLine[255];
\r
146 sprintf(precalculationLine, "%s_%s#%d-%d_%d_%d:%s\n", m_sHashRoutineName.c_str(), m_sPlainCharsetName.c_str(), m_nPlainLenMin, m_nPlainLenMax, m_nRainbowTableIndex, m_nRainbowChainLen, HexToStr(pHash, nHashLen).c_str() );
\r
147 string precalcString(precalculationLine);
\r
149 string sCurrentPrecalcPathName = "";
\r
150 string sCurrentPrecalcIndexPathName = "";
\r
151 long unsigned int offset;
\r
154 for (i = 0; i < (int)vPrecalcFiles.size() && gotPrecalcOnLine == -1; i++)
\r
156 sCurrentPrecalcPathName = vPrecalcFiles[i];
\r
157 sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index";
\r
161 vector<string> precalcLines;
\r
162 if (ReadLinesFromFile(sCurrentPrecalcIndexPathName.c_str(), precalcLines))
\r
165 for (j = 0; j < (int)precalcLines.size(); j++)
\r
167 if (precalcString.compare(0, precalcString.size()-1, precalcLines[j]) == 0)
\r
169 gotPrecalcOnLine = j;
\r
174 vector<string> vPart;
\r
175 if (SeparateString(precalcLines[j], "___:", vPart))
\r
178 offset += ((atoi(vPart[3].c_str())-1) * sizeof(uint64));
\r
182 printf("Corrupted precalculation file!\n");
\r
183 gotPrecalcOnLine = -1;
\r
190 if (gotPrecalcOnLine > -1)
\r
192 if (debug) printf("Debug: Reading pre calculations from file, line %d offset %lu\n", gotPrecalcOnLine, offset);
\r
194 FILE* fp = fopen(sCurrentPrecalcPathName.c_str(), "rb");
\r
197 fseek(fp, offset, SEEK_SET);
\r
199 // We should do some verification here, for example by recalculating the middle chain, to catch corrupted files
\r
200 if(fread(pIndexE, sizeof(uint64), (unsigned long)m_nRainbowChainLen-1, fp) != (unsigned long)m_nRainbowChainLen-1)
\r
201 printf("File read error.");
\r
205 printf("Cannot open precalculation file %s.\n", sCurrentPrecalcPathName.c_str());
\r
207 //printf("\npIndexE[0]: %s\n", uint64tostr(pIndexE[0]).c_str());
\r
208 //printf("\npIndexE[nRainbowChainLen-2]: %s\n", uint64tostr(pIndexE[m_nRainbowChainLen-2]).c_str());
\r
216 void CChainWalkSet::StoreToFile(uint64* pIndexE, unsigned char* pHash, int nHashLen)
\r
218 if (debug) printf("\nDebug: Storing precalc\n");
\r
220 string sCurrentPrecalcPathName = CheckOrRotatePreCalcFile();
\r
221 string sCurrentPrecalcIndexPathName = sCurrentPrecalcPathName + ".index";
\r
223 FILE* fp = fopen(sCurrentPrecalcPathName.c_str(), "ab");
\r
226 if(fwrite(pIndexE, sizeof(uint64), (unsigned long)m_nRainbowChainLen-1, fp) != (unsigned long)m_nRainbowChainLen-1)
\r
227 printf("File write error.");
\r
230 FILE* file = fopen(sCurrentPrecalcIndexPathName.c_str(), "a");
\r
233 char precalculationLine[255];
\r
234 sprintf(precalculationLine, "%s_%s#%d-%d_%d_%d:%s\n", m_sHashRoutineName.c_str(), m_sPlainCharsetName.c_str(), m_nPlainLenMin, m_nPlainLenMax, m_nRainbowTableIndex, m_nRainbowChainLen, HexToStr(pHash, nHashLen).c_str() );
\r
235 fputs (precalculationLine, file);
\r
242 printf("Cannot open precalculation file %s\n", sCurrentPrecalcPathName.c_str());
\r
245 uint64* CChainWalkSet::RequestWalk(unsigned char* pHash, int nHashLen,
\r
246 string sHashRoutineName,
\r
247 string sPlainCharsetName, int nPlainLenMin, int nPlainLenMax,
\r
248 int nRainbowTableIndex,
\r
249 int nRainbowChainLen,
\r
250 bool& fNewlyGenerated,
\r
255 sPrecalcPathName = sPrecalc;
\r
257 if ( m_sHashRoutineName != sHashRoutineName
\r
258 || m_sPlainCharsetName != sPlainCharsetName
\r
259 || m_nPlainLenMin != nPlainLenMin
\r
260 || m_nPlainLenMax != nPlainLenMax
\r
261 || m_nRainbowTableIndex != nRainbowTableIndex
\r
262 || m_nRainbowChainLen != nRainbowChainLen)
\r
266 m_sHashRoutineName = sHashRoutineName;
\r
267 m_sPlainCharsetName = sPlainCharsetName;
\r
268 m_nPlainLenMin = nPlainLenMin;
\r
269 m_nPlainLenMax = nPlainLenMax;
\r
270 m_nRainbowTableIndex = nRainbowTableIndex;
\r
271 m_nRainbowChainLen = nRainbowChainLen;
\r
274 memcpy(cw.Hash, pHash, nHashLen);
\r
275 cw.pIndexE = new uint64[nRainbowChainLen - 1];
\r
276 m_lChainWalk.push_back(cw);
\r
278 // Only update this list when we search through another rainbow table
\r
279 updateUsedPrecalcFiles();
\r
281 if (!FindInFile(cw.pIndexE, pHash, nHashLen))
\r
282 fNewlyGenerated = true;
\r
284 fNewlyGenerated = false;
\r
288 list<ChainWalk>::iterator it;
\r
289 for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++)
\r
291 if (memcmp(it->Hash, pHash, nHashLen) == 0)
\r
293 fNewlyGenerated = false;
\r
294 return it->pIndexE;
\r
299 memcpy(cw.Hash, pHash, nHashLen);
\r
300 cw.pIndexE = new uint64[nRainbowChainLen - 1];
\r
301 m_lChainWalk.push_back(cw);
\r
303 if (!FindInFile(cw.pIndexE, pHash, nHashLen))
\r
304 fNewlyGenerated = true;
\r
306 fNewlyGenerated = false;
\r
310 void CChainWalkSet::DiscardWalk(uint64* pIndexE)
\r
312 list<ChainWalk>::iterator it;
\r
313 for (it = m_lChainWalk.begin(); it != m_lChainWalk.end(); it++)
\r
315 if (it->pIndexE == pIndexE)
\r
317 delete it->pIndexE;
\r
318 m_lChainWalk.erase(it);
\r
323 printf("debug: pIndexE not found\n");
\r