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