]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/Public.cpp
(C)
[freerainbowtables] / Client Applications / rcracki_mt / Public.cpp
1 /*\r
2  * rcracki_mt is a multithreaded implementation and fork of the original \r
3  * RainbowCrack\r
4  *\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, 2011 James Nobis <frt@quelrod.net>\r
9  *\r
10  * This file is part of rcracki_mt.\r
11  *\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
15  *\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
20  *\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
23  */\r
24 \r
25 #if defined(_WIN32) && !defined(__GNUC__)\r
26         #pragma warning(disable : 4786)\r
27 #endif\r
28 \r
29 #include "Public.h"\r
30 \r
31 #ifdef _WIN32\r
32         #include <windows.h>\r
33 #endif\r
34 \r
35 #if defined(_WIN32) && !defined(__GNUC__)\r
36         #include <windows.h>\r
37         #include <time.h>\r
38 \r
39         #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)\r
40                 #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64\r
41         #else\r
42                 #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL\r
43         #endif\r
44  \r
45         struct timezone\r
46         {\r
47                 int tz_minuteswest; /* minutes W of Greenwich */\r
48                 int tz_dsttime;     /* type of dst correction */\r
49         };\r
50  \r
51         int gettimeofday(struct timeval *tv, struct timezone *tz)\r
52         {\r
53                 // Define a structure to receive the current Windows filetime\r
54                 FILETIME ft;\r
55  \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
59  \r
60                 if (NULL != tv)\r
61                 {\r
62                         GetSystemTimeAsFileTime(&ft);\r
63  \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
68                         tmpres <<= 32;\r
69                         tmpres |= ft.dwLowDateTime;\r
70  \r
71                         // Convert to microseconds by dividing by 10\r
72                         tmpres /= 10;\r
73  \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
77          \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
82                 }\r
83          \r
84                 if (NULL != tz)\r
85                 {\r
86                         if (!tzflag)\r
87                         {\r
88                                 _tzset();\r
89                                 tzflag++;\r
90                         }\r
91           \r
92                         // Adjust for the timezone west of Greenwich\r
93                         tz->tz_minuteswest = _timezone / 60;\r
94                         tz->tz_dsttime = _daylight;\r
95                 }\r
96          \r
97                 return 0;\r
98         }\r
99 \r
100 #elif defined(__APPLE__) || \\r
101         ((defined(__unix__) || defined(unix)) && !defined(USG))\r
102 \r
103         #include <sys/param.h>\r
104 \r
105         #if defined(BSD)\r
106                 #include <sys/sysctl.h>\r
107         #elif defined(__linux__)\r
108                 #include <sys/sysinfo.h>\r
109         #else\r
110                 #error Unsupported Operating System\r
111         #endif\r
112 #endif\r
113 \r
114 //////////////////////////////////////////////////////////////////////\r
115 \r
116 timeval sub_timeofday( timeval tv2, timeval tv )\r
117 {\r
118         timeval final;\r
119 \r
120         final.tv_usec = tv2.tv_usec - tv.tv_usec;\r
121         final.tv_sec = tv2.tv_sec - tv.tv_sec;\r
122 \r
123         if ( final.tv_usec < 0 )\r
124         {\r
125                 final.tv_usec += 1000000;\r
126                 --final.tv_sec;\r
127         }\r
128 \r
129         return final;\r
130 }\r
131 \r
132 long GetFileLen(FILE* file)\r
133 {\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
140 \r
141         return len;\r
142 }\r
143 \r
144 string TrimString(string s)\r
145 {\r
146         while (s.size() > 0)\r
147         {\r
148                 if (s[0] == ' ' || s[0] == '\t')\r
149                         s = s.substr(1);\r
150                 else\r
151                         break;\r
152         }\r
153 \r
154         while (s.size() > 0)\r
155         {\r
156                 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')\r
157                         s = s.substr(0, s.size() - 1);\r
158                 else\r
159                         break;\r
160         }\r
161 \r
162         return s;\r
163 }\r
164 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)\r
165 {\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
168                 return false;\r
169 \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
176         {\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
186         }\r
187         return true;\r
188 }\r
189 #ifdef BOINC\r
190 bool boinc_ReadLinesFromFile(string sPathName, vector<string>& vLine)\r
191 {\r
192         vLine.clear();\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
196         if (!file) {\r
197                 fprintf(stderr,\r
198                         "Couldn't find input file, resolved name %s.\n", input_path\r
199                 );\r
200                 exit(-1);\r
201         }\r
202 \r
203         if (file != NULL)\r
204         {\r
205                 long len = GetFileLen(file);\r
206                 char* data = new char[len + 1];\r
207                 fread(data, 1, len, file);\r
208                 data[len] = '\0';\r
209                 string content = data;\r
210                 content += "\n";\r
211                 delete [] data;\r
212 \r
213                 unsigned int i;\r
214                 for (i = 0; i < content.size(); i++)\r
215                 {\r
216                         if (content[i] == '\r')\r
217                                 content[i] = '\n';\r
218                 }\r
219 \r
220                 string::size_type n;\r
221                 while ((n = content.find("\n", 0)) != string::npos)\r
222                 {\r
223                         string line = content.substr(0, n);\r
224                         line = TrimString(line);\r
225                         if (line != "")\r
226                                 vLine.push_back(line);\r
227                         content = content.substr(n + 1);\r
228                 }\r
229 \r
230                 fclose(file);\r
231         }\r
232         else\r
233                 return false;\r
234 \r
235         return true;\r
236 }\r
237 #endif\r
238 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)\r
239 {\r
240         vLine.clear();\r
241 \r
242         FILE* file = fopen(sPathName.c_str(), "rb");\r
243         if (file != NULL)\r
244         {\r
245                 long len = GetFileLen(file);\r
246                 char* data = new char[len + 1];\r
247                 fread(data, 1, len, file);\r
248                 data[len] = '\0';\r
249                 string content = data;\r
250                 content += "\n";\r
251                 delete [] data;\r
252 \r
253                 unsigned int i;\r
254                 for (i = 0; i < content.size(); i++)\r
255                 {\r
256                         if (content[i] == '\r')\r
257                                 content[i] = '\n';\r
258                 }\r
259 \r
260                 string::size_type n;\r
261                 while ((n = content.find("\n", 0)) != string::npos)\r
262                 {\r
263                         string line = content.substr(0, n);\r
264                         line = TrimString(line);\r
265                         if (line != "")\r
266                                 vLine.push_back(line);\r
267                         content = content.substr(n + 1);\r
268                 }\r
269 \r
270                 fclose(file);\r
271         }\r
272         else\r
273                 return false;\r
274 \r
275         return true;\r
276 }\r
277 \r
278 bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary)\r
279 {\r
280         FILE* file = fopen(sOutputFile.c_str(), "a");\r
281         if (file!=NULL)\r
282         {\r
283                 string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n";\r
284                 fputs (buffer.c_str(), file);\r
285                 fclose (file);\r
286                 return true;\r
287         }\r
288         else\r
289                 return false;\r
290 }\r
291 \r
292 bool SeperateString(string s, string sSeperator, vector<string>& vPart)\r
293 {\r
294         vPart.clear();\r
295 \r
296         unsigned int i;\r
297         for (i = 0; i < sSeperator.size(); i++)\r
298         {\r
299                 string::size_type n;\r
300                 if ( (n = s.find(sSeperator[i])) != string::npos)\r
301                 {\r
302                         vPart.push_back(s.substr(0, n));\r
303                         s = s.substr(n + 1);\r
304                 }\r
305                 else\r
306                 {\r
307                         printf("not found: %c\n", sSeperator[i]);\r
308                         printf("s: %s\n", s.c_str());\r
309                         return false;\r
310                 }\r
311         }\r
312         vPart.push_back(s);\r
313 \r
314         return true;\r
315 }\r
316 \r
317 string uint64tostr(uint64 n)\r
318 {\r
319         char str[32];\r
320 \r
321 #ifdef _WIN32\r
322         sprintf(str, "%I64u", n);\r
323 #else\r
324         sprintf(str, "%llu", n);\r
325 #endif\r
326 \r
327         return str;\r
328 }\r
329 \r
330 string uint64tohexstr(uint64 n)\r
331 {\r
332         char str[32];\r
333 \r
334 #ifdef _WIN32\r
335         sprintf(str, "%016I64x", n);\r
336 #else\r
337         sprintf(str, "%016llx", n);\r
338 #endif\r
339 \r
340         return str;\r
341 }\r
342 \r
343 string HexToStr(const unsigned char* pData, int nLen)\r
344 {\r
345         string sRet;\r
346         int i;\r
347         for (i = 0; i < nLen; i++)\r
348         {\r
349                 char szByte[3];\r
350                 sprintf(szByte, "%02x", pData[i]);\r
351                 sRet += szByte;\r
352         }\r
353 \r
354         return sRet;\r
355 }\r
356 \r
357 unsigned long GetAvailPhysMemorySize()\r
358 {\r
359 #ifdef _WIN32\r
360         MEMORYSTATUS ms;\r
361         GlobalMemoryStatus(&ms);\r
362         return ms.dwAvailPhys;\r
363 #elif defined(BSD)\r
364         int mib[2] = { CTL_HW, HW_PHYSMEM };\r
365         uint64 physMem;\r
366         //XXX warning size_t isn't portable\r
367         size_t len;\r
368         len = sizeof(physMem);\r
369         sysctl(mib, 2, &physMem, &len, NULL, 0);\r
370         return physMem;\r
371 #elif defined(__linux__)\r
372         struct sysinfo info;\r
373         sysinfo(&info);\r
374         return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit;\r
375 #else\r
376         return 0;\r
377         #error Unsupported Operating System\r
378 #endif\r
379 }\r
380 \r
381 string GetApplicationPath()\r
382 {\r
383         char fullPath[FILENAME_MAX];\r
384 \r
385 #ifdef _WIN32\r
386         GetModuleFileName(NULL, fullPath, FILENAME_MAX);\r
387 #else\r
388         char szTmp[32];\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
392 \r
393         if( bytes >= 0 )\r
394                 fullPath[bytes] = '\0';\r
395 #endif\r
396 \r
397         string sApplicationPath = fullPath;\r
398 #ifdef _WIN32\r
399         string::size_type nIndex = sApplicationPath.find_last_of('\\');\r
400 #else\r
401         string::size_type nIndex = sApplicationPath.find_last_of('/');\r
402 #endif\r
403 \r
404         if ( nIndex != string::npos )\r
405                 sApplicationPath = sApplicationPath.substr(0, nIndex+1);\r
406 \r
407         return sApplicationPath;\r
408 }\r
409 \r
410 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)\r
411 {\r
412         uint32 i;\r
413         for (i = 0; i < sHash.size() / 2; i++)\r
414         {\r
415                 string sSub = sHash.substr(i * 2, 2);\r
416                 int nValue;\r
417                 sscanf(sSub.c_str(), "%02x", &nValue);\r
418                 pHash[i] = (unsigned char)nValue;\r
419         }\r
420 \r
421         nHashLen = (int) sHash.size() / 2;\r
422 }\r
423 \r
424 void Logo()\r
425 {\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
432 }\r
433 \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
436 #ifndef _WIN32\r
437 \r
438 static int tty_fd = 0;\r
439 struct termios saved_ti;\r
440 \r
441 int tty_getchar()\r
442 {\r
443         int c, numChars;\r
444 \r
445         if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {\r
446                 c = 0;\r
447                 numChars = read(tty_fd, &c, 1);\r
448                 if (numChars > 0) return c;\r
449         }\r
450 \r
451         return -1;\r
452 }\r
453 \r
454 void tty_done()\r
455 {\r
456         if (!tty_fd) return;\r
457 \r
458         tcsetattr(tty_fd, TCSANOW, &saved_ti);\r
459 \r
460         close(tty_fd);\r
461         tty_fd = 0;\r
462 }\r
463 \r
464 void tty_init()\r
465 {\r
466         struct termios ti;\r
467 \r
468         if (tty_fd)\r
469                 return;\r
470 \r
471         if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;\r
472 \r
473         tcgetattr(tty_fd, &ti);\r
474         saved_ti = ti;\r
475         ti.c_lflag &= ~(ICANON | ECHO);\r
476         ti.c_cc[VMIN] = 1;\r
477         ti.c_cc[VTIME] = 0;\r
478         tcsetattr(tty_fd, TCSANOW, &ti);\r
479 \r
480         atexit(tty_done);\r
481 }\r
482 \r
483 void tty_flush(void)\r
484 {\r
485         tcflush(tty_fd, TCIFLUSH);\r
486 }\r
487 // end nmap code\r
488 #endif\r