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 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, version 2 of the License.
\r
16 * rcracki_mt is distributed in the hope that it will be useful,
\r
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
19 * GNU General Public License for more details.
\r
21 * You should have received a copy of the GNU General Public License
\r
22 * along with rcracki_mt. If not, see <http://www.gnu.org/licenses/>.
\r
25 #if defined(_WIN32) && !defined(__GNUC__)
\r
26 #pragma warning(disable : 4786 4267 4018)
\r
32 #include <windows.h>
\r
35 #if defined(_WIN32) && !defined(__GNUC__)
\r
36 #include <windows.h>
\r
39 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
\r
40 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
\r
42 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
\r
47 int tz_minuteswest; /* minutes W of Greenwich */
\r
48 int tz_dsttime; /* type of dst correction */
\r
51 int gettimeofday(struct timeval *tv, struct timezone *tz)
\r
53 // Define a structure to receive the current Windows filetime
\r
56 // Initialize the present time to 0 and the timezone to UTC
\r
57 unsigned __int64 tmpres = 0;
\r
58 static int tzflag = 0;
\r
62 GetSystemTimeAsFileTime(&ft);
\r
64 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond
\r
65 // intervals since Jan 1, 1601 in a structure. Copy the high bits to
\r
66 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
\r
67 tmpres |= ft.dwHighDateTime;
\r
69 tmpres |= ft.dwLowDateTime;
\r
71 // Convert to microseconds by dividing by 10
\r
74 // The Unix epoch starts on Jan 1 1970. Need to subtract the difference
\r
75 // in seconds from Jan 1 1601.
\r
76 tmpres -= DELTA_EPOCH_IN_MICROSECS;
\r
78 // Finally change microseconds to seconds and place in the seconds value.
\r
79 // The modulus picks up the microseconds.
\r
80 tv->tv_sec = (long)(tmpres / 1000000UL);
\r
81 tv->tv_usec = (long)(tmpres % 1000000UL);
\r
92 // Adjust for the timezone west of Greenwich
\r
93 tz->tz_minuteswest = _timezone / 60;
\r
94 tz->tz_dsttime = _daylight;
\r
100 #elif defined(__APPLE__) || \
\r
101 ((defined(__unix__) || defined(unix)) && !defined(USG))
\r
103 #include <sys/param.h>
\r
106 #include <sys/sysctl.h>
\r
107 #elif defined(__linux__)
\r
108 #include <sys/sysinfo.h>
\r
110 #error Unsupported Operating System
\r
114 //////////////////////////////////////////////////////////////////////
\r
116 timeval sub_timeofday( timeval tv2, timeval tv )
\r
120 final.tv_usec = tv2.tv_usec - tv.tv_usec;
\r
121 final.tv_sec = tv2.tv_sec - tv.tv_sec;
\r
123 if ( final.tv_usec < 0 )
\r
125 final.tv_usec += 1000000;
\r
132 unsigned int GetFileLen(FILE* file)
\r
134 long int pos = ftell(file);
\r
135 fseek(file, 0, SEEK_END);
\r
136 long int len = ftell(file);
\r
137 fseek(file, pos, SEEK_SET);
\r
142 string TrimString(string s)
\r
144 while (s.size() > 0)
\r
146 if (s[0] == ' ' || s[0] == '\t')
\r
152 while (s.size() > 0)
\r
154 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
\r
155 s = s.substr(0, s.size() - 1);
\r
162 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
\r
164 // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
\r
165 if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
\r
168 string::size_type nEnd = sCharset.rfind(')');
\r
169 string::size_type nStart = (int) sCharset.rfind('(');
\r
170 string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);
\r
171 vector<string> vParts;
\r
172 SeperateString(sChar, ",", vParts);
\r
173 for(UINT4 i = 0; i < vParts.size(); i++)
\r
175 tCharset stCharset;
\r
176 vector<string> vParts2;
\r
177 SeperateString(vParts[i], "#", vParts2);
\r
178 stCharset.sName = vParts2[0];
\r
179 vector<string> vParts3;
\r
180 SeperateString(vParts2[1], "-", vParts3);
\r
181 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());
\r
182 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());
\r
183 vCharset.push_back(stCharset);
\r
187 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
\r
191 FILE* file = fopen(sPathName.c_str(), "rb");
\r
194 unsigned int len = GetFileLen(file);
\r
195 char* data = new char[len + 1];
\r
196 fread(data, 1, len, file);
\r
198 string content = data;
\r
203 for (i = 0; i < content.size(); i++)
\r
205 if (content[i] == '\r')
\r
209 string::size_type n;
\r
210 while ((n = content.find("\n", 0)) != string::npos)
\r
212 string line = content.substr(0, n);
\r
213 line = TrimString(line);
\r
215 vLine.push_back(line);
\r
216 content = content.substr(n + 1);
\r
227 bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary)
\r
229 FILE* file = fopen(sOutputFile.c_str(), "a");
\r
232 string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n";
\r
233 fputs (buffer.c_str(), file);
\r
241 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
\r
246 for (i = 0; i < sSeperator.size(); i++)
\r
248 string::size_type n;
\r
249 if ( (n = s.find(sSeperator[i])) != string::npos)
\r
251 vPart.push_back(s.substr(0, n));
\r
252 s = s.substr(n + 1);
\r
256 printf("not found: %c\n", sSeperator[i]);
\r
257 printf("s: %s\n", s.c_str());
\r
261 vPart.push_back(s);
\r
266 string uint64tostr(uint64 n)
\r
271 sprintf(str, "%I64u", n);
\r
273 sprintf(str, "%llu", n);
\r
279 string uint64tohexstr(uint64 n)
\r
284 sprintf(str, "%016I64x", n);
\r
286 sprintf(str, "%016llx", n);
\r
292 string HexToStr(const unsigned char* pData, int nLen)
\r
296 for (i = 0; i < nLen; i++)
\r
299 sprintf(szByte, "%02x", pData[i]);
\r
306 uint64 GetAvailPhysMemorySize()
\r
308 #if defined(_WIN32)
\r
310 GlobalMemoryStatus(&ms);
\r
311 return ms.dwAvailPhys;
\r
313 int mib[2] = { CTL_HW, HW_PHYSMEM };
\r
315 //XXX warning size_t isn't portable
\r
317 len = sizeof(physMem);
\r
318 sysctl(mib, 2, &physMem, &len, NULL, 0);
\r
320 #elif defined(__linux__)
\r
321 struct sysinfo info;
\r
323 return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit;
\r
326 #error Unsupported Operating System
\r
330 string GetApplicationPath()
\r
332 char fullPath[FILENAME_MAX];
\r
335 GetModuleFileName(NULL, fullPath, FILENAME_MAX);
\r
338 // XXX linux/proc file system dependen
\r
339 sprintf(szTmp, "/proc/%d/exe", getpid());
\r
340 int bytes = readlink(szTmp, fullPath, FILENAME_MAX);
\r
342 fullPath[bytes] = '\0';
\r
345 string sApplicationPath = fullPath;
\r
347 string::size_type nIndex = sApplicationPath.find_last_of('\\');
\r
349 string::size_type nIndex = sApplicationPath.find_last_of('/');
\r
352 if ( nIndex != string::npos )
\r
353 sApplicationPath = sApplicationPath.substr(0, nIndex+1);
\r
355 //printf ("\n\nDebug: The application directory is %s\n", sApplicationPath.c_str());
\r
356 return sApplicationPath;
\r
359 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
\r
362 for (i = 0; i < sHash.size() / 2; i++)
\r
364 string sSub = sHash.substr(i * 2, 2);
\r
366 sscanf(sSub.c_str(), "%02x", &nValue);
\r
367 pHash[i] = (unsigned char)nValue;
\r
370 nHashLen = (int) sHash.size() / 2;
\r
375 printf("RainbowCrack (improved, multi-threaded) - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
\r
376 printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
\r
377 printf("multi-threaded and enhanced by neinbrucke (version 0.6.3)\n");
\r
378 printf("http://www.freerainbowtables.com/\n");
\r
379 printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
\r
380 printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
\r
383 // XXX nmap is GPL2, will check newer releases regarding license
\r
384 // Code comes from nmap, used for the linux implementation of kbhit()
\r
387 static int tty_fd = 0;
\r
388 struct termios saved_ti;
\r
394 if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {
\r
396 numChars = read(tty_fd, &c, 1);
\r
397 if (numChars > 0) return c;
\r
405 if (!tty_fd) return;
\r
407 tcsetattr(tty_fd, TCSANOW, &saved_ti);
\r
420 if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;
\r
422 tcgetattr(tty_fd, &ti);
\r
424 ti.c_lflag &= ~(ICANON | ECHO);
\r
426 ti.c_cc[VTIME] = 0;
\r
427 tcsetattr(tty_fd, TCSANOW, &ti);
\r
432 void tty_flush(void)
\r
434 tcflush(tty_fd, TCIFLUSH);
\r