]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/Public.cpp
remove old deprecated rcracki
[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 racrcki_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 #ifdef _WIN32\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         #include <time.h>\r
34 \r
35         #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)\r
36                 #define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64\r
37         #else\r
38                 #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL\r
39         #endif\r
40  \r
41         struct timezone\r
42         {\r
43                 int  tz_minuteswest; /* minutes W of Greenwich */\r
44                 int  tz_dsttime;     /* type of dst correction */\r
45         };\r
46  \r
47         int gettimeofday(struct timeval *tv, struct timezone *tz)\r
48         {\r
49                 // Define a structure to receive the current Windows filetime\r
50                 FILETIME ft;\r
51  \r
52                 // Initialize the present time to 0 and the timezone to UTC\r
53                 unsigned __int64 tmpres = 0;\r
54                 static int tzflag = 0;\r
55  \r
56                 if (NULL != tv)\r
57                 {\r
58                         GetSystemTimeAsFileTime(&ft);\r
59  \r
60                         // The GetSystemTimeAsFileTime returns the number of 100 nanosecond \r
61                         // intervals since Jan 1, 1601 in a structure. Copy the high bits to \r
62                         // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.\r
63                         tmpres |= ft.dwHighDateTime;\r
64                         tmpres <<= 32;\r
65                         tmpres |= ft.dwLowDateTime;\r
66  \r
67                         // Convert to microseconds by dividing by 10\r
68                         tmpres /= 10;\r
69  \r
70                         // The Unix epoch starts on Jan 1 1970.  Need to subtract the difference \r
71                         // in seconds from Jan 1 1601.\r
72                         tmpres -= DELTA_EPOCH_IN_MICROSECS;\r
73          \r
74                         // Finally change microseconds to seconds and place in the seconds value. \r
75                         // The modulus picks up the microseconds.\r
76                         tv->tv_sec = (long)(tmpres / 1000000UL);\r
77                         tv->tv_usec = (long)(tmpres % 1000000UL);\r
78                 }\r
79          \r
80                 if (NULL != tz)\r
81                 {\r
82                         if (!tzflag)\r
83                         {\r
84                                 _tzset();\r
85                                 tzflag++;\r
86                         }\r
87           \r
88                         // Adjust for the timezone west of Greenwich\r
89                         tz->tz_minuteswest = _timezone / 60;\r
90                         tz->tz_dsttime = _daylight;\r
91                 }\r
92          \r
93                 return 0;\r
94         }\r
95 \r
96 #elif defined(__APPLE__) || \\r
97         ((defined(__unix__) || defined(unix)) && !defined(USG))\r
98 \r
99         #include <sys/param.h>\r
100 \r
101         #if defined(BSD)\r
102                 #include <sys/sysctl.h>\r
103         #elif defined(__linux__)\r
104                 #include <sys/sysinfo.h>\r
105         #else\r
106                 #error Unsupported Operating System\r
107         #endif\r
108 #endif\r
109 \r
110 //////////////////////////////////////////////////////////////////////\r
111 \r
112 timeval sub_timeofday( timeval tv2, timeval tv )\r
113 {\r
114         timeval final;\r
115 \r
116         final.tv_usec = tv2.tv_usec - tv.tv_usec;\r
117         final.tv_sec = tv2.tv_sec - tv.tv_sec;\r
118 \r
119         if ( final.tv_usec < 0 )\r
120         {\r
121                 final.tv_usec += 1000000;\r
122                 --final.tv_sec;\r
123         }\r
124 \r
125         return final;\r
126 }\r
127 \r
128 unsigned int GetFileLen(FILE* file)\r
129 {\r
130         long int pos = ftell(file);\r
131         fseek(file, 0, SEEK_END);\r
132         long int len = ftell(file);\r
133         fseek(file, pos, SEEK_SET);\r
134 \r
135         return len;\r
136 }\r
137 \r
138 string TrimString(string s)\r
139 {\r
140         while (s.size() > 0)\r
141         {\r
142                 if (s[0] == ' ' || s[0] == '\t')\r
143                         s = s.substr(1);\r
144                 else\r
145                         break;\r
146         }\r
147 \r
148         while (s.size() > 0)\r
149         {\r
150                 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')\r
151                         s = s.substr(0, s.size() - 1);\r
152                 else\r
153                         break;\r
154         }\r
155 \r
156         return s;\r
157 }\r
158 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)\r
159 {\r
160         // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)\r
161         if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset\r
162                 return false;\r
163 \r
164         UINT4 nEnd = (int) sCharset.rfind(')');\r
165         UINT4 nStart = (int) sCharset.rfind('(');\r
166         string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);\r
167         vector<string> vParts;\r
168         SeperateString(sChar, ",", vParts);\r
169         for(UINT4 i = 0; i < vParts.size(); i++)\r
170         {\r
171                 tCharset stCharset;\r
172                 vector<string> vParts2;\r
173                 SeperateString(vParts[i], "#", vParts2);\r
174                 stCharset.sName = vParts2[0];\r
175                 vector<string> vParts3;\r
176                 SeperateString(vParts2[1], "-", vParts3);\r
177                 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());\r
178                 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());\r
179                 vCharset.push_back(stCharset);\r
180         }\r
181         return true;\r
182 }\r
183 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)\r
184 {\r
185         vLine.clear();\r
186 \r
187         FILE* file = fopen(sPathName.c_str(), "rb");\r
188         if (file != NULL)\r
189         {\r
190                 unsigned int len = GetFileLen(file);\r
191                 char* data = new char[len + 1];\r
192                 fread(data, 1, len, file);\r
193                 data[len] = '\0';\r
194                 string content = data;\r
195                 content += "\n";\r
196                 delete [] data;\r
197 \r
198                 unsigned int i;\r
199                 for (i = 0; i < content.size(); i++)\r
200                 {\r
201                         if (content[i] == '\r')\r
202                                 content[i] = '\n';\r
203                 }\r
204 \r
205                 int n;\r
206                 while ((n = content.find("\n", 0)) != -1)\r
207                 {\r
208                         string line = content.substr(0, n);\r
209                         line = TrimString(line);\r
210                         if (line != "")\r
211                                 vLine.push_back(line);\r
212                         content = content.substr(n + 1);\r
213                 }\r
214 \r
215                 fclose(file);\r
216         }\r
217         else\r
218                 return false;\r
219 \r
220         return true;\r
221 }\r
222 \r
223 bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary)\r
224 {\r
225         FILE* file = fopen(sOutputFile.c_str(), "a");\r
226         if (file!=NULL)\r
227         {\r
228                 string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n";\r
229                 fputs (buffer.c_str(), file);\r
230                 fclose (file);\r
231                 return true;\r
232         }\r
233         else\r
234                 return false;\r
235 }\r
236 \r
237 bool SeperateString(string s, string sSeperator, vector<string>& vPart)\r
238 {\r
239         vPart.clear();\r
240 \r
241         unsigned int i;\r
242         for (i = 0; i < sSeperator.size(); i++)\r
243         {\r
244                 int n = s.find(sSeperator[i]);\r
245                 if (n != -1)\r
246                 {\r
247                         vPart.push_back(s.substr(0, n));\r
248                         s = s.substr(n + 1);\r
249                 }\r
250                 else\r
251                 {\r
252                         printf("not found: %c\n", sSeperator[i]);\r
253                         printf("s: %s\n", s.c_str());\r
254                         return false;\r
255                 }\r
256         }\r
257         vPart.push_back(s);\r
258 \r
259         return true;\r
260 }\r
261 \r
262 string uint64tostr(uint64 n)\r
263 {\r
264         char str[32];\r
265 \r
266 #ifdef _WIN32\r
267         sprintf(str, "%I64u", n);\r
268 #else\r
269         sprintf(str, "%llu", n);\r
270 #endif\r
271 \r
272         return str;\r
273 }\r
274 \r
275 string uint64tohexstr(uint64 n)\r
276 {\r
277         char str[32];\r
278 \r
279 #ifdef _WIN32\r
280         sprintf(str, "%016I64x", n);\r
281 #else\r
282         sprintf(str, "%016llx", n);\r
283 #endif\r
284 \r
285         return str;\r
286 }\r
287 \r
288 string HexToStr(const unsigned char* pData, int nLen)\r
289 {\r
290         string sRet;\r
291         int i;\r
292         for (i = 0; i < nLen; i++)\r
293         {\r
294                 char szByte[3];\r
295                 sprintf(szByte, "%02x", pData[i]);\r
296                 sRet += szByte;\r
297         }\r
298 \r
299         return sRet;\r
300 }\r
301 \r
302 uint64 GetAvailPhysMemorySize()\r
303 {\r
304 #ifdef _WIN32\r
305         MEMORYSTATUS ms;\r
306         GlobalMemoryStatus(&ms);\r
307         return ms.dwAvailPhys;\r
308 #elif defined(BSD)\r
309         int mib[2] = { CTL_HW, HW_PHYSMEM };\r
310         uint64 physMem;\r
311         //XXX warning size_t isn't portable\r
312         size_t len;\r
313         len = sizeof(physMem);\r
314         sysctl(mib, 2, &physMem, &len, NULL, 0);\r
315         return physMem;\r
316 #elif defined(__linux__)\r
317         struct sysinfo info;\r
318         sysinfo(&info);\r
319         return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit;\r
320 #else\r
321         return 0;\r
322         #error Unsupported Operating System\r
323 #endif\r
324 }\r
325 \r
326 string GetApplicationPath()\r
327 {\r
328         char fullPath[FILENAME_MAX];\r
329 \r
330 #ifdef _WIN32\r
331         GetModuleFileName(NULL, fullPath, FILENAME_MAX);\r
332 #else\r
333         char szTmp[32];\r
334         sprintf(szTmp, "/proc/%d/exe", getpid());\r
335         int bytes = readlink(szTmp, fullPath, FILENAME_MAX);\r
336         if(bytes >= 0)\r
337                 fullPath[bytes] = '\0';\r
338 #endif\r
339 \r
340         string sApplicationPath = fullPath;\r
341 #ifdef _WIN32\r
342         int nIndex = sApplicationPath.find_last_of('\\');\r
343 #else\r
344         int nIndex = sApplicationPath.find_last_of('/');\r
345 #endif\r
346 \r
347         if (nIndex != -1)\r
348                 sApplicationPath = sApplicationPath.substr(0, nIndex+1);\r
349 \r
350         //printf ("\n\nDebug: The application directory is %s\n", sApplicationPath.c_str());\r
351         return sApplicationPath;\r
352 }\r
353 \r
354 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)\r
355 {\r
356         UINT4 i;\r
357         for (i = 0; i < sHash.size() / 2; i++)\r
358         {\r
359                 string sSub = sHash.substr(i * 2, 2);\r
360                 int nValue;\r
361                 sscanf(sSub.c_str(), "%02x", &nValue);\r
362                 pHash[i] = (unsigned char)nValue;\r
363         }\r
364 \r
365         nHashLen = (int) sHash.size() / 2;\r
366 }\r
367 \r
368 void Logo()\r
369 {\r
370         printf("RainbowCrack (improved, multi-threaded) - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");\r
371         printf("by Martin Westergaard <martinwj2005@gmail.com>\n");\r
372         printf("multi-threaded and enhanced by neinbrucke (version 0.6.3)\n");\r
373         printf("http://www.freerainbowtables.com/\n");\r
374         printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");\r
375         printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");\r
376 }\r
377 \r
378 // XXX nmap is GPL2, will check newer releases regarding license\r
379 // Code comes from nmap, used for the linux implementation of kbhit()\r
380 #ifndef _WIN32\r
381 \r
382 static int tty_fd = 0;\r
383 struct termios saved_ti;\r
384 \r
385 int tty_getchar()\r
386 {\r
387         int c, numChars;\r
388 \r
389         if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {\r
390                 c = 0;\r
391                 numChars = read(tty_fd, &c, 1);\r
392                 if (numChars > 0) return c;\r
393         }\r
394 \r
395         return -1;\r
396 }\r
397 \r
398 void tty_done()\r
399 {\r
400         if (!tty_fd) return;\r
401 \r
402         tcsetattr(tty_fd, TCSANOW, &saved_ti);\r
403 \r
404         close(tty_fd);\r
405         tty_fd = 0;\r
406 }\r
407 \r
408 void tty_init()\r
409 {\r
410         struct termios ti;\r
411 \r
412         if (tty_fd)\r
413                 return;\r
414 \r
415         if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;\r
416 \r
417         tcgetattr(tty_fd, &ti);\r
418         saved_ti = ti;\r
419         ti.c_lflag &= ~(ICANON | ECHO);\r
420         ti.c_cc[VMIN] = 1;\r
421         ti.c_cc[VTIME] = 0;\r
422         tcsetattr(tty_fd, TCSANOW, &ti);\r
423 \r
424         atexit(tty_done);\r
425 }\r
426 \r
427 void tty_flush(void)\r
428 {\r
429         tcflush(tty_fd, TCIFLUSH);\r
430 }\r
431 // end nmap code\r
432 #endif\r