2 * freerainbowtables is a project for generating, distributing, and using
3 * perfect rainbow tables
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, 2011 James Nobis <frt@quelrod.net>
10 * This file is part of freerainbowtables.
12 * freerainbowtables 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, either version 2 of the License.
16 * freerainbowtables 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 freerainbowtables. If not, see <http://www.gnu.org/licenses/>.
25 #if defined(_WIN32) && !defined(__GNUC__)
26 #pragma warning(disable : 4786)
31 #include "boinc_win.h"
46 #include "boinc_api.h"
54 #if defined(_WIN32) && !defined(__GNUC__)
58 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
59 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
61 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
66 int tz_minuteswest; /* minutes W of Greenwich */
67 int tz_dsttime; /* type of dst correction */
70 int gettimeofday(struct timeval *tv, struct timezone *tz)
72 // Define a structure to receive the current Windows filetime
75 // Initialize the present time to 0 and the timezone to UTC
76 unsigned __int64 tmpres = 0;
77 static int tzflag = 0;
81 GetSystemTimeAsFileTime(&ft);
83 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond
84 // intervals since Jan 1, 1601 in a structure. Copy the high bits to
85 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
86 tmpres |= ft.dwHighDateTime;
88 tmpres |= ft.dwLowDateTime;
90 // Convert to microseconds by dividing by 10
93 // The Unix epoch starts on Jan 1 1970. Need to subtract the difference
94 // in seconds from Jan 1 1601.
95 tmpres -= DELTA_EPOCH_IN_MICROSECS;
97 // Finally change microseconds to seconds and place in the seconds value.
98 // The modulus picks up the microseconds.
99 tv->tv_sec = (long)(tmpres / 1000000UL);
100 tv->tv_usec = (long)(tmpres % 1000000UL);
111 // Adjust for the timezone west of Greenwich
112 tz->tz_minuteswest = _timezone / 60;
113 tz->tz_dsttime = _daylight;
119 #elif defined(__APPLE__) || \
120 ((defined(__unix__) || defined(unix)) && !defined(USG))
122 #include <sys/param.h>
125 #include <sys/sysctl.h>
126 #elif defined(__linux__)
127 #include <sys/sysinfo.h>
129 #error Unsupported Operating System
133 //////////////////////////////////////////////////////////////////////
135 timeval sub_timeofday( timeval tv2, timeval tv )
139 final.tv_usec = tv2.tv_usec - tv.tv_usec;
140 final.tv_sec = tv2.tv_sec - tv.tv_sec;
142 if ( final.tv_usec < 0 )
144 final.tv_usec += 1000000;
151 long GetFileLen(FILE* file)
153 // XXX on x86/x86_64 linux returns long
154 // 32-bit this is a problem if the file is > (2^31-1) bytes
155 long pos = ftell(file);
156 fseek(file, 0, SEEK_END);
157 long len = ftell(file);
158 fseek(file, pos, SEEK_SET);
163 string TrimString(string s)
167 if (s[0] == ' ' || s[0] == '\t')
175 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
176 s = s.substr(0, s.size() - 1);
183 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
185 // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
186 if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
189 string::size_type nEnd = sCharset.rfind(')');
190 string::size_type nStart = (int) sCharset.rfind('(');
191 string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);
192 vector<string> vParts;
193 SeperateString(sChar, ",", vParts);
194 for(uint32 i = 0; i < vParts.size(); i++)
197 vector<string> vParts2;
198 SeperateString(vParts[i], "#", vParts2);
199 stCharset.sName = vParts2[0];
200 vector<string> vParts3;
201 SeperateString(vParts2[1], "-", vParts3);
202 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());
203 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());
204 vCharset.push_back(stCharset);
209 bool boinc_ReadLinesFromFile(string sPathName, vector<string>& vLine)
212 #ifdef USE_INTEGRATED_CHARSET
213 vLine.push_back("byte = []");
214 vLine.push_back("alpha = [ABCDEFGHIJKLMNOPQRSTUVWXYZ]");
215 vLine.push_back("alpha-space = [ABCDEFGHIJKLMNOPQRSTUVWXYZ ]");
216 vLine.push_back("alpha-numeric = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]");
217 vLine.push_back("alpha-numeric-space = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ]");
218 vLine.push_back("alpha-numeric-symbol14 = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D]");
219 vLine.push_back("alpha-numeric-symbol14-space= [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x20]");
220 vLine.push_back("all = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F]");
221 vLine.push_back("all-space = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
222 vLine.push_back("alpha-numeric-symbol32-space = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
223 vLine.push_back("lm-frt-cp437 = [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7D\x7E\x80\x8E\x8F\x90\x92\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA5\xE0\xE1\xE2\xE3\xE4\xE6\xE7\xE8\xE9\xEA\xEB\xEE]");
224 vLine.push_back("lm-frt-cp850 = [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7D\x7E\x80\x8E\x8F\x90\x92\x99\x9A\x9C\x9D\x9F\xA5\xB5\xB6\xB7\xBD\xBE\xC7\xCF\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xDE\xE0\xE1\xE2\xE3\xE5\xE6\xE8\xE9\xEA\xEB\xED\xEF]");
225 vLine.push_back("lm-frt-cp437-850 = [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7D\x7E\x80\x8E\x8F\x90\x92\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA5\xB5\xB6\xB7\xBD\xBE\xC7\xCF\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xDE\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xED\xEE\xEF]");
226 vLine.push_back("numeric = [0123456789]");
227 vLine.push_back("numeric-space = [0123456789 ]");
228 vLine.push_back("loweralpha = [abcdefghijklmnopqrstuvwxyz]");
229 vLine.push_back("loweralpha-space = [abcdefghijklmnopqrstuvwxyz ]");
230 vLine.push_back("loweralpha-numeric = [abcdefghijklmnopqrstuvwxyz0123456789]");
231 vLine.push_back("loweralpha-numeric-space = [abcdefghijklmnopqrstuvwxyz0123456789 ]");
232 vLine.push_back("loweralpha-numeric-symbol14 = [abcdefghijklmnopqrstuvwxyz0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D]");
233 vLine.push_back("loweralpha-numeric-all = [abcdefghijklmnopqrstuvwxyz0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F]");
234 vLine.push_back("loweralpha-numeric-symbol32-space= [abcdefghijklmnopqrstuvwxyz0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
235 vLine.push_back("mixalpha = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]");
236 vLine.push_back("mixalpha-space = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ]");
237 vLine.push_back("mixalpha-numeric = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]");
238 vLine.push_back("mixalpha-numeric-space = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ]");
239 vLine.push_back("mixalpha-numeric-symbol14 = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D]");
240 vLine.push_back("mixalpha-numeric-all = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F]");
241 vLine.push_back("mixalpha-numeric-symbol32-space = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
242 vLine.push_back("mixalpha-numeric-all-space = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
245 char input_path[512];
246 boinc_resolve_filename(sPathName.c_str(), input_path, sizeof(input_path));
247 FILE *file = boinc_fopen(input_path, "rb");
250 "Couldn't find input file, resolved name %s.\n", input_path
257 unsigned int len = GetFileLen(file);
258 char* data = new char[len + 1];
259 fread(data, 1, len, file);
261 string content = data;
266 for (i = 0; i < content.size(); i++)
268 if (content[i] == '\r')
273 while ((n = content.find("\n", 0)) != string::npos)
275 string line = content.substr(0, n);
276 line = TrimString(line);
278 vLine.push_back(line);
279 content = content.substr(n + 1);
290 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
294 FILE* file = fopen(sPathName.c_str(), "rb");
297 unsigned int len = GetFileLen(file);
298 char* data = new char[len + 1];
299 fread(data, 1, len, file);
301 string content = data;
306 for (i = 0; i < content.size(); i++)
308 if (content[i] == '\r')
313 while ((n = content.find("\n", 0)) != string::npos)
315 string line = content.substr(0, n);
316 line = TrimString(line);
318 vLine.push_back(line);
319 content = content.substr(n + 1);
330 bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary)
332 FILE* file = fopen(sOutputFile.c_str(), "a");
335 string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n";
336 fputs (buffer.c_str(), file);
344 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
349 for (i = 0; i < sSeperator.size(); i++)
352 if ( (n = s.find(sSeperator[i])) != string::npos)
354 vPart.push_back(s.substr(0, n));
359 printf("not found: %c\n", sSeperator[i]);
360 printf("s: %s\n", s.c_str());
369 string uint64tostr(uint64 n)
374 sprintf(str, "%I64u", n);
376 sprintf(str, "%llu", n);
382 string uint64tohexstr(uint64 n)
387 sprintf(str, "%016I64x", n);
389 sprintf(str, "%016llx", n);
395 string HexToStr(const unsigned char* pData, int nLen)
399 for (i = 0; i < nLen; i++)
402 sprintf(szByte, "%02x", pData[i]);
409 unsigned long GetAvailPhysMemorySize()
413 GlobalMemoryStatus(&ms);
414 return ms.dwAvailPhys;
416 int mib[2] = { CTL_HW, HW_PHYSMEM };
418 //XXX warning size_t isn't portable
420 len = sizeof(physMem);
421 sysctl(mib, 2, &physMem, &len, NULL, 0);
423 #elif defined(__linux__)
426 return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit;
429 #error Unsupported Operating System
433 string GetApplicationPath()
435 char fullPath[FILENAME_MAX];
438 GetModuleFileName(NULL, fullPath, FILENAME_MAX);
441 // XXX linux/proc file system dependent
442 sprintf(szTmp, "/proc/%d/exe", getpid());
443 int bytes = readlink(szTmp, fullPath, FILENAME_MAX);
446 fullPath[bytes] = '\0';
449 string sApplicationPath = fullPath;
451 string::size_type nIndex = sApplicationPath.find_last_of('\\');
453 string::size_type nIndex = sApplicationPath.find_last_of('/');
456 if ( nIndex != string::npos )
457 sApplicationPath = sApplicationPath.substr(0, nIndex+1);
459 return sApplicationPath;
462 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
465 for (i = 0; i < sHash.size() / 2; i++)
467 string sSub = sHash.substr(i * 2, 2);
469 sscanf(sSub.c_str(), "%02x", &nValue);
470 pHash[i] = (unsigned char)nValue;
473 nHashLen = (int) sHash.size() / 2;
478 printf("RainbowCrack (improved) 2.0 - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
479 printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
480 printf("http://www.freerainbowtables.com/\n");
481 printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
482 printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
485 // XXX nmap is GPL2, will check newer releases regarding license
486 // Code comes from nmap, used for the linux implementation of kbhit()
489 static int tty_fd = 0;
490 struct termios saved_ti;
496 if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {
498 numChars = read(tty_fd, &c, 1);
499 if (numChars > 0) return c;
509 tcsetattr(tty_fd, TCSANOW, &saved_ti);
522 if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;
524 tcgetattr(tty_fd, &ti);
526 ti.c_lflag &= ~(ICANON | ECHO);
529 tcsetattr(tty_fd, TCSANOW, &ti);
536 tcflush(tty_fd, TCIFLUSH);