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