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 long GetFileLen(FILE* file)
\r
134 // XXX on x86/x86_64 linux returns long
\r
135 // 32-bit this is a problem if the file is > (2^31-1) bytes
\r
136 long pos = ftell(file);
\r
137 fseek(file, 0, SEEK_END);
\r
138 long len = ftell(file);
\r
139 fseek(file, pos, SEEK_SET);
\r
144 string TrimString(string s)
\r
146 while (s.size() > 0)
\r
148 if (s[0] == ' ' || s[0] == '\t')
\r
154 while (s.size() > 0)
\r
156 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
\r
157 s = s.substr(0, s.size() - 1);
\r
164 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
\r
166 // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
\r
167 if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
\r
170 string::size_type nEnd = sCharset.rfind(')');
\r
171 string::size_type nStart = (int) sCharset.rfind('(');
\r
172 string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);
\r
173 vector<string> vParts;
\r
174 SeperateString(sChar, ",", vParts);
\r
175 for(uint32 i = 0; i < vParts.size(); i++)
\r
177 tCharset stCharset;
\r
178 vector<string> vParts2;
\r
179 SeperateString(vParts[i], "#", vParts2);
\r
180 stCharset.sName = vParts2[0];
\r
181 vector<string> vParts3;
\r
182 SeperateString(vParts2[1], "-", vParts3);
\r
183 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());
\r
184 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());
\r
185 vCharset.push_back(stCharset);
\r
190 bool boinc_ReadLinesFromFile(string sPathName, vector<string>& vLine)
\r
193 char input_path[512];
\r
194 boinc_resolve_filename(sPathName.c_str(), input_path, sizeof(input_path));
\r
195 FILE *file = boinc_fopen(input_path, "rb");
\r
198 "Couldn't find input file, resolved name %s.\n", input_path
\r
205 long len = GetFileLen(file);
\r
206 char* data = new char[len + 1];
\r
207 fread(data, 1, len, file);
\r
209 string content = data;
\r
214 for (i = 0; i < content.size(); i++)
\r
216 if (content[i] == '\r')
\r
220 string::size_type n;
\r
221 while ((n = content.find("\n", 0)) != string::npos)
\r
223 string line = content.substr(0, n);
\r
224 line = TrimString(line);
\r
226 vLine.push_back(line);
\r
227 content = content.substr(n + 1);
\r
238 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
\r
242 FILE* file = fopen(sPathName.c_str(), "rb");
\r
245 long len = GetFileLen(file);
\r
246 char* data = new char[len + 1];
\r
247 fread(data, 1, len, file);
\r
249 string content = data;
\r
254 for (i = 0; i < content.size(); i++)
\r
256 if (content[i] == '\r')
\r
260 string::size_type n;
\r
261 while ((n = content.find("\n", 0)) != string::npos)
\r
263 string line = content.substr(0, n);
\r
264 line = TrimString(line);
\r
266 vLine.push_back(line);
\r
267 content = content.substr(n + 1);
\r
278 bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary)
\r
280 FILE* file = fopen(sOutputFile.c_str(), "a");
\r
283 string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n";
\r
284 fputs (buffer.c_str(), file);
\r
292 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
\r
297 for (i = 0; i < sSeperator.size(); i++)
\r
299 string::size_type n;
\r
300 if ( (n = s.find(sSeperator[i])) != string::npos)
\r
302 vPart.push_back(s.substr(0, n));
\r
303 s = s.substr(n + 1);
\r
307 printf("not found: %c\n", sSeperator[i]);
\r
308 printf("s: %s\n", s.c_str());
\r
312 vPart.push_back(s);
\r
317 string uint64tostr(uint64 n)
\r
322 sprintf(str, "%I64u", n);
\r
324 sprintf(str, "%llu", n);
\r
330 string uint64tohexstr(uint64 n)
\r
335 sprintf(str, "%016I64x", n);
\r
337 sprintf(str, "%016llx", n);
\r
343 string HexToStr(const unsigned char* pData, int nLen)
\r
347 for (i = 0; i < nLen; i++)
\r
350 sprintf(szByte, "%02x", pData[i]);
\r
357 unsigned long GetAvailPhysMemorySize()
\r
361 GlobalMemoryStatus(&ms);
\r
362 return ms.dwAvailPhys;
\r
364 int mib[2] = { CTL_HW, HW_PHYSMEM };
\r
366 //XXX warning size_t isn't portable
\r
368 len = sizeof(physMem);
\r
369 sysctl(mib, 2, &physMem, &len, NULL, 0);
\r
371 #elif defined(__linux__)
\r
372 struct sysinfo info;
\r
374 return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit;
\r
377 #error Unsupported Operating System
\r
381 string GetApplicationPath()
\r
383 char fullPath[FILENAME_MAX];
\r
386 GetModuleFileName(NULL, fullPath, FILENAME_MAX);
\r
389 // XXX linux/proc file system dependent
\r
390 sprintf(szTmp, "/proc/%d/exe", getpid());
\r
391 int bytes = readlink(szTmp, fullPath, FILENAME_MAX);
\r
394 fullPath[bytes] = '\0';
\r
397 string sApplicationPath = fullPath;
\r
399 string::size_type nIndex = sApplicationPath.find_last_of('\\');
\r
401 string::size_type nIndex = sApplicationPath.find_last_of('/');
\r
404 if ( nIndex != string::npos )
\r
405 sApplicationPath = sApplicationPath.substr(0, nIndex+1);
\r
407 return sApplicationPath;
\r
410 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
\r
413 for (i = 0; i < sHash.size() / 2; i++)
\r
415 string sSub = sHash.substr(i * 2, 2);
\r
417 sscanf(sSub.c_str(), "%02x", &nValue);
\r
418 pHash[i] = (unsigned char)nValue;
\r
421 nHashLen = (int) sHash.size() / 2;
\r
426 printf("RainbowCrack (improved, multi-threaded) - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
\r
427 printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
\r
428 printf("multi-threaded and enhanced by neinbrucke (version 0.6.3)\n");
\r
429 printf("http://www.freerainbowtables.com/\n");
\r
430 printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
\r
431 printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
\r
434 // XXX nmap is GPL2, will check newer releases regarding license
\r
435 // Code comes from nmap, used for the linux implementation of kbhit()
\r
438 static int tty_fd = 0;
\r
439 struct termios saved_ti;
\r
445 if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {
\r
447 numChars = read(tty_fd, &c, 1);
\r
448 if (numChars > 0) return c;
\r
456 if (!tty_fd) return;
\r
458 tcsetattr(tty_fd, TCSANOW, &saved_ti);
\r
471 if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;
\r
473 tcgetattr(tty_fd, &ti);
\r
475 ti.c_lflag &= ~(ICANON | ECHO);
\r
477 ti.c_cc[VTIME] = 0;
\r
478 tcsetattr(tty_fd, TCSANOW, &ti);
\r
483 void tty_flush(void)
\r
485 tcflush(tty_fd, TCIFLUSH);
\r