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 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 char input_path[512];
213 boinc_resolve_filename(sPathName.c_str(), input_path, sizeof(input_path));
214 FILE *file = boinc_fopen(input_path, "rb");
217 "Couldn't find input file, resolved name %s.\n", input_path
224 unsigned int len = GetFileLen(file);
225 char* data = new char[len + 1];
226 fread(data, 1, len, file);
228 string content = data;
233 for (i = 0; i < content.size(); i++)
235 if (content[i] == '\r')
240 while ((n = content.find("\n", 0)) != string::npos)
242 string line = content.substr(0, n);
243 line = TrimString(line);
245 vLine.push_back(line);
246 content = content.substr(n + 1);
257 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
261 FILE* file = fopen(sPathName.c_str(), "rb");
264 unsigned int len = GetFileLen(file);
265 char* data = new char[len + 1];
266 fread(data, 1, len, file);
268 string content = data;
273 for (i = 0; i < content.size(); i++)
275 if (content[i] == '\r')
280 while ((n = content.find("\n", 0)) != string::npos)
282 string line = content.substr(0, n);
283 line = TrimString(line);
285 vLine.push_back(line);
286 content = content.substr(n + 1);
297 bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary)
299 FILE* file = fopen(sOutputFile.c_str(), "a");
302 string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n";
303 fputs (buffer.c_str(), file);
311 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
316 for (i = 0; i < sSeperator.size(); i++)
319 if ( (n = s.find(sSeperator[i])) != string::npos)
321 vPart.push_back(s.substr(0, n));
326 printf("not found: %c\n", sSeperator[i]);
327 printf("s: %s\n", s.c_str());
336 string uint64tostr(uint64 n)
341 sprintf(str, "%I64u", n);
343 sprintf(str, "%llu", n);
349 string uint64tohexstr(uint64 n)
354 sprintf(str, "%016I64x", n);
356 sprintf(str, "%016llx", n);
362 string HexToStr(const unsigned char* pData, int nLen)
366 for (i = 0; i < nLen; i++)
369 sprintf(szByte, "%02x", pData[i]);
376 unsigned long GetAvailPhysMemorySize()
380 GlobalMemoryStatus(&ms);
381 return ms.dwAvailPhys;
383 int mib[2] = { CTL_HW, HW_PHYSMEM };
385 //XXX warning size_t isn't portable
387 len = sizeof(physMem);
388 sysctl(mib, 2, &physMem, &len, NULL, 0);
390 #elif defined(__linux__)
393 return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit;
396 #error Unsupported Operating System
400 string GetApplicationPath()
402 char fullPath[FILENAME_MAX];
405 GetModuleFileName(NULL, fullPath, FILENAME_MAX);
408 // XXX linux/proc file system dependent
409 sprintf(szTmp, "/proc/%d/exe", getpid());
410 int bytes = readlink(szTmp, fullPath, FILENAME_MAX);
413 fullPath[bytes] = '\0';
416 string sApplicationPath = fullPath;
418 string::size_type nIndex = sApplicationPath.find_last_of('\\');
420 string::size_type nIndex = sApplicationPath.find_last_of('/');
423 if ( nIndex != string::npos )
424 sApplicationPath = sApplicationPath.substr(0, nIndex+1);
426 return sApplicationPath;
429 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
432 for (i = 0; i < sHash.size() / 2; i++)
434 string sSub = sHash.substr(i * 2, 2);
436 sscanf(sSub.c_str(), "%02x", &nValue);
437 pHash[i] = (unsigned char)nValue;
440 nHashLen = (int) sHash.size() / 2;
445 printf("RainbowCrack (improved) 2.0 - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
446 printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
447 printf("http://www.freerainbowtables.com/\n");
448 printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
449 printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
452 // XXX nmap is GPL2, will check newer releases regarding license
453 // Code comes from nmap, used for the linux implementation of kbhit()
456 static int tty_fd = 0;
457 struct termios saved_ti;
463 if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {
465 numChars = read(tty_fd, &c, 1);
466 if (numChars > 0) return c;
476 tcsetattr(tty_fd, TCSANOW, &saved_ti);
489 if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;
491 tcgetattr(tty_fd, &ti);
493 ti.c_lflag &= ~(ICANON | ECHO);
496 tcsetattr(tty_fd, TCSANOW, &ti);
503 tcflush(tty_fd, TCIFLUSH);