]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/Public.cpp
41a95155ec9f47c41bf25630d5bf70aa69148481
[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 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 4267 4018)\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 unsigned int GetFileLen(FILE* file)\r
133 {\r
134         long int pos = ftell(file);\r
135         fseek(file, 0, SEEK_END);\r
136         long int len = ftell(file);\r
137         fseek(file, pos, SEEK_SET);\r
138 \r
139         return len;\r
140 }\r
141 \r
142 string TrimString(string s)\r
143 {\r
144         while (s.size() > 0)\r
145         {\r
146                 if (s[0] == ' ' || s[0] == '\t')\r
147                         s = s.substr(1);\r
148                 else\r
149                         break;\r
150         }\r
151 \r
152         while (s.size() > 0)\r
153         {\r
154                 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')\r
155                         s = s.substr(0, s.size() - 1);\r
156                 else\r
157                         break;\r
158         }\r
159 \r
160         return s;\r
161 }\r
162 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)\r
163 {\r
164         // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)\r
165         if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset\r
166                 return false;\r
167 \r
168         string::size_type nEnd = sCharset.rfind(')');\r
169         string::size_type nStart = (int) sCharset.rfind('(');\r
170         string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);\r
171         vector<string> vParts;\r
172         SeperateString(sChar, ",", vParts);\r
173         for(UINT4 i = 0; i < vParts.size(); i++)\r
174         {\r
175                 tCharset stCharset;\r
176                 vector<string> vParts2;\r
177                 SeperateString(vParts[i], "#", vParts2);\r
178                 stCharset.sName = vParts2[0];\r
179                 vector<string> vParts3;\r
180                 SeperateString(vParts2[1], "-", vParts3);\r
181                 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());\r
182                 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());\r
183                 vCharset.push_back(stCharset);\r
184         }\r
185         return true;\r
186 }\r
187 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)\r
188 {\r
189         vLine.clear();\r
190 \r
191         FILE* file = fopen(sPathName.c_str(), "rb");\r
192         if (file != NULL)\r
193         {\r
194                 unsigned int len = GetFileLen(file);\r
195                 char* data = new char[len + 1];\r
196                 fread(data, 1, len, file);\r
197                 data[len] = '\0';\r
198                 string content = data;\r
199                 content += "\n";\r
200                 delete [] data;\r
201 \r
202                 unsigned int i;\r
203                 for (i = 0; i < content.size(); i++)\r
204                 {\r
205                         if (content[i] == '\r')\r
206                                 content[i] = '\n';\r
207                 }\r
208 \r
209                 string::size_type n;\r
210                 while ((n = content.find("\n", 0)) != string::npos)\r
211                 {\r
212                         string line = content.substr(0, n);\r
213                         line = TrimString(line);\r
214                         if (line != "")\r
215                                 vLine.push_back(line);\r
216                         content = content.substr(n + 1);\r
217                 }\r
218 \r
219                 fclose(file);\r
220         }\r
221         else\r
222                 return false;\r
223 \r
224         return true;\r
225 }\r
226 \r
227 bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary)\r
228 {\r
229         FILE* file = fopen(sOutputFile.c_str(), "a");\r
230         if (file!=NULL)\r
231         {\r
232                 string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n";\r
233                 fputs (buffer.c_str(), file);\r
234                 fclose (file);\r
235                 return true;\r
236         }\r
237         else\r
238                 return false;\r
239 }\r
240 \r
241 bool SeperateString(string s, string sSeperator, vector<string>& vPart)\r
242 {\r
243         vPart.clear();\r
244 \r
245         unsigned int i;\r
246         for (i = 0; i < sSeperator.size(); i++)\r
247         {\r
248                 string::size_type n;\r
249                 if ( (n = s.find(sSeperator[i])) != string::npos)\r
250                 {\r
251                         vPart.push_back(s.substr(0, n));\r
252                         s = s.substr(n + 1);\r
253                 }\r
254                 else\r
255                 {\r
256                         printf("not found: %c\n", sSeperator[i]);\r
257                         printf("s: %s\n", s.c_str());\r
258                         return false;\r
259                 }\r
260         }\r
261         vPart.push_back(s);\r
262 \r
263         return true;\r
264 }\r
265 \r
266 string uint64tostr(uint64 n)\r
267 {\r
268         char str[32];\r
269 \r
270 #ifdef _WIN32\r
271         sprintf(str, "%I64u", n);\r
272 #else\r
273         sprintf(str, "%llu", n);\r
274 #endif\r
275 \r
276         return str;\r
277 }\r
278 \r
279 string uint64tohexstr(uint64 n)\r
280 {\r
281         char str[32];\r
282 \r
283 #ifdef _WIN32\r
284         sprintf(str, "%016I64x", n);\r
285 #else\r
286         sprintf(str, "%016llx", n);\r
287 #endif\r
288 \r
289         return str;\r
290 }\r
291 \r
292 string HexToStr(const unsigned char* pData, int nLen)\r
293 {\r
294         string sRet;\r
295         int i;\r
296         for (i = 0; i < nLen; i++)\r
297         {\r
298                 char szByte[3];\r
299                 sprintf(szByte, "%02x", pData[i]);\r
300                 sRet += szByte;\r
301         }\r
302 \r
303         return sRet;\r
304 }\r
305 \r
306 uint64 GetAvailPhysMemorySize()\r
307 {\r
308 #if defined(_WIN32)\r
309         MEMORYSTATUS ms;\r
310         GlobalMemoryStatus(&ms);\r
311         return ms.dwAvailPhys;\r
312 #elif defined(BSD)\r
313         int mib[2] = { CTL_HW, HW_PHYSMEM };\r
314         uint64 physMem;\r
315         //XXX warning size_t isn't portable\r
316         size_t len;\r
317         len = sizeof(physMem);\r
318         sysctl(mib, 2, &physMem, &len, NULL, 0);\r
319         return physMem;\r
320 #elif defined(__linux__)\r
321         struct sysinfo info;\r
322         sysinfo(&info);\r
323         return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit;\r
324 #else\r
325         return 0;\r
326         #error Unsupported Operating System\r
327 #endif\r
328 }\r
329 \r
330 string GetApplicationPath()\r
331 {\r
332         char fullPath[FILENAME_MAX];\r
333 \r
334 #ifdef _WIN32\r
335         GetModuleFileName(NULL, fullPath, FILENAME_MAX);\r
336 #else\r
337         char szTmp[32];\r
338         // XXX linux/proc file system dependen\r
339         sprintf(szTmp, "/proc/%d/exe", getpid());\r
340         int bytes = readlink(szTmp, fullPath, FILENAME_MAX);\r
341         if(bytes >= 0)\r
342                 fullPath[bytes] = '\0';\r
343 #endif\r
344 \r
345         string sApplicationPath = fullPath;\r
346 #ifdef _WIN32\r
347         string::size_type nIndex = sApplicationPath.find_last_of('\\');\r
348 #else\r
349         string::size_type nIndex = sApplicationPath.find_last_of('/');\r
350 #endif\r
351 \r
352         if ( nIndex != string::npos )\r
353                 sApplicationPath = sApplicationPath.substr(0, nIndex+1);\r
354 \r
355         //printf ("\n\nDebug: The application directory is %s\n", sApplicationPath.c_str());\r
356         return sApplicationPath;\r
357 }\r
358 \r
359 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)\r
360 {\r
361         UINT4 i;\r
362         for (i = 0; i < sHash.size() / 2; i++)\r
363         {\r
364                 string sSub = sHash.substr(i * 2, 2);\r
365                 int nValue;\r
366                 sscanf(sSub.c_str(), "%02x", &nValue);\r
367                 pHash[i] = (unsigned char)nValue;\r
368         }\r
369 \r
370         nHashLen = (int) sHash.size() / 2;\r
371 }\r
372 \r
373 void Logo()\r
374 {\r
375         printf("RainbowCrack (improved, multi-threaded) - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");\r
376         printf("by Martin Westergaard <martinwj2005@gmail.com>\n");\r
377         printf("multi-threaded and enhanced by neinbrucke (version 0.6.3)\n");\r
378         printf("http://www.freerainbowtables.com/\n");\r
379         printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");\r
380         printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");\r
381 }\r
382 \r
383 // XXX nmap is GPL2, will check newer releases regarding license\r
384 // Code comes from nmap, used for the linux implementation of kbhit()\r
385 #ifndef _WIN32\r
386 \r
387 static int tty_fd = 0;\r
388 struct termios saved_ti;\r
389 \r
390 int tty_getchar()\r
391 {\r
392         int c, numChars;\r
393 \r
394         if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {\r
395                 c = 0;\r
396                 numChars = read(tty_fd, &c, 1);\r
397                 if (numChars > 0) return c;\r
398         }\r
399 \r
400         return -1;\r
401 }\r
402 \r
403 void tty_done()\r
404 {\r
405         if (!tty_fd) return;\r
406 \r
407         tcsetattr(tty_fd, TCSANOW, &saved_ti);\r
408 \r
409         close(tty_fd);\r
410         tty_fd = 0;\r
411 }\r
412 \r
413 void tty_init()\r
414 {\r
415         struct termios ti;\r
416 \r
417         if (tty_fd)\r
418                 return;\r
419 \r
420         if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;\r
421 \r
422         tcgetattr(tty_fd, &ti);\r
423         saved_ti = ti;\r
424         ti.c_lflag &= ~(ICANON | ECHO);\r
425         ti.c_cc[VMIN] = 1;\r
426         ti.c_cc[VTIME] = 0;\r
427         tcsetattr(tty_fd, TCSANOW, &ti);\r
428 \r
429         atexit(tty_done);\r
430 }\r
431 \r
432 void tty_flush(void)\r
433 {\r
434         tcflush(tty_fd, TCIFLUSH);\r
435 }\r
436 // end nmap code\r
437 #endif\r