]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/Public.cpp
rcracki_mt updated to rti2
[freerainbowtables] / Client Applications / rcracki_mt / Public.cpp
1 /*
2    RainbowCrack - a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
3
4    Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
5 */
6
7 #ifdef _WIN32
8         #pragma warning(disable : 4786 4267 4018)
9 #endif
10
11 #include "Public.h"
12
13 #ifdef _WIN32
14         #include <windows.h>
15 #else
16         #include <sys/sysinfo.h>
17 #endif
18
19 //////////////////////////////////////////////////////////////////////
20
21 unsigned int GetFileLen(FILE* file)
22 {
23         unsigned int pos = ftell(file);
24         fseek(file, 0, SEEK_END);
25         unsigned int len = ftell(file);
26         fseek(file, pos, SEEK_SET);
27
28         return len;
29 }
30
31 string TrimString(string s)
32 {
33         while (s.size() > 0)
34         {
35                 if (s[0] == ' ' || s[0] == '\t')
36                         s = s.substr(1);
37                 else
38                         break;
39         }
40
41         while (s.size() > 0)
42         {
43                 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
44                         s = s.substr(0, s.size() - 1);
45                 else
46                         break;
47         }
48
49         return s;
50 }
51 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
52 {
53         // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
54         if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
55                 return false;
56         size_t nEnd = sCharset.rfind(')');
57         size_t nStart = sCharset.rfind('(');
58         string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);
59         vector<string> vParts;
60         SeperateString(sChar, ",", vParts);
61         for(int i = 0; i < vParts.size(); i++)
62         {
63                 tCharset stCharset;
64                 vector<string> vParts2;
65                 SeperateString(vParts[i], "#", vParts2);
66                 stCharset.sName = vParts2[0];
67                 vector<string> vParts3;
68                 SeperateString(vParts2[1], "-", vParts3);
69                 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());
70                 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());
71                 vCharset.push_back(stCharset);
72         }
73         return true;
74 }
75 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
76 {
77         vLine.clear();
78
79         FILE* file = fopen(sPathName.c_str(), "rb");
80         if (file != NULL)
81         {
82                 unsigned int len = GetFileLen(file);
83                 char* data = new char[len + 1];
84                 fread(data, 1, len, file);
85                 data[len] = '\0';
86                 string content = data;
87                 content += "\n";
88                 delete data;
89
90                 unsigned int i;
91                 for (i = 0; i < content.size(); i++)
92                 {
93                         if (content[i] == '\r')
94                                 content[i] = '\n';
95                 }
96
97                 int n;
98                 while ((n = content.find("\n", 0)) != -1)
99                 {
100                         string line = content.substr(0, n);
101                         line = TrimString(line);
102                         if (line != "")
103                                 vLine.push_back(line);
104                         content = content.substr(n + 1);
105                 }
106
107                 fclose(file);
108         }
109         else
110                 return false;
111
112         return true;
113 }
114
115 bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary)
116 {
117         FILE* file = fopen(sOutputFile.c_str(), "a");
118         if (file!=NULL)
119         {
120                 string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n";
121                 fputs (buffer.c_str(), file);
122                 fclose (file);
123                 return true;
124         }
125         else
126                 return false;
127 }
128
129 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
130 {
131         vPart.clear();
132
133         unsigned int i;
134         for (i = 0; i < sSeperator.size(); i++)
135         {
136                 int n = s.find(sSeperator[i]);
137                 if (n != -1)
138                 {
139                         vPart.push_back(s.substr(0, n));
140                         s = s.substr(n + 1);
141                 }
142                 else
143                 {
144                         printf("not found: %c\n", sSeperator[i]);
145                         printf("s: %s\n", s.c_str());
146                         return false;
147                 }
148         }
149         vPart.push_back(s);
150
151         return true;
152 }
153
154 string uint64tostr(uint64 n)
155 {
156         char str[32];
157
158 #ifdef _WIN32
159         sprintf(str, "%I64u", n);
160 #else
161         sprintf(str, "%llu", n);
162 #endif
163
164         return str;
165 }
166
167 string uint64tohexstr(uint64 n)
168 {
169         char str[32];
170
171 #ifdef _WIN32
172         sprintf(str, "%016I64x", n);
173 #else
174         sprintf(str, "%016llx", n);
175 #endif
176
177         return str;
178 }
179
180 string HexToStr(const unsigned char* pData, int nLen)
181 {
182         string sRet;
183         int i;
184         for (i = 0; i < nLen; i++)
185         {
186                 char szByte[3];
187                 sprintf(szByte, "%02x", pData[i]);
188                 sRet += szByte;
189         }
190
191         return sRet;
192 }
193
194 unsigned int GetAvailPhysMemorySize()
195 {
196 #ifdef _WIN32
197                 MEMORYSTATUS ms;
198                 GlobalMemoryStatus(&ms);
199                 return ms.dwAvailPhys;
200 #else
201         struct sysinfo info;
202         sysinfo(&info);                 // This function is Linux-specific
203         return info.freeram;
204 #endif
205 }
206
207 string GetApplicationPath()
208 {
209         char fullPath[FILENAME_MAX];
210
211 #ifdef _WIN32
212         GetModuleFileName(NULL, fullPath, FILENAME_MAX);
213 #else
214         char szTmp[32];
215         sprintf(szTmp, "/proc/%d/exe", getpid());
216         int bytes = readlink(szTmp, fullPath, FILENAME_MAX);
217         if(bytes >= 0)
218                 fullPath[bytes] = '\0';
219 #endif
220
221         string sApplicationPath = fullPath;
222 #ifdef _WIN32
223         int nIndex = sApplicationPath.find_last_of('\\');
224 #else
225         int nIndex = sApplicationPath.find_last_of('/');
226 #endif
227
228         if (nIndex != -1)
229                 sApplicationPath = sApplicationPath.substr(0, nIndex+1);
230
231         //printf ("\n\nDebug: The application directory is %s\n", sApplicationPath.c_str());
232         return sApplicationPath;
233 }
234
235 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
236 {
237         int i;
238         for (i = 0; i < sHash.size() / 2; i++)
239         {
240                 string sSub = sHash.substr(i * 2, 2);
241                 int nValue;
242                 sscanf(sSub.c_str(), "%02x", &nValue);
243                 pHash[i] = (unsigned char)nValue;
244         }
245
246         nHashLen = sHash.size() / 2;
247 }
248
249 void Logo()
250 {
251         printf("RainbowCrack (improved, multi-threaded) - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
252         printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
253         printf("multi-threaded and enhanced by neinbrucke (version 0.6-svn)\n");
254         printf("http://www.freerainbowtables.com/\n");
255         printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
256         printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
257 }
258
259 // Code comes from nmap, used for the linux implementation of kbhit()
260 #ifndef _WIN32
261
262 static int tty_fd = 0;
263 struct termios saved_ti;
264
265 int tty_getchar()
266 {
267         int c, numChars;
268
269         if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {
270                 c = 0;
271                 numChars = read(tty_fd, &c, 1);
272                 if (numChars > 0) return c;
273         }
274
275         return -1;
276 }
277
278 void tty_done()
279 {
280         if (!tty_fd) return;
281
282         tcsetattr(tty_fd, TCSANOW, &saved_ti);
283
284         close(tty_fd);
285         tty_fd = 0;
286 }
287
288 void tty_init()
289 {
290         struct termios ti;
291
292         if (tty_fd)
293                 return;
294
295         if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;
296
297         tcgetattr(tty_fd, &ti);
298         saved_ti = ti;
299         ti.c_lflag &= ~(ICANON | ECHO);
300         ti.c_cc[VMIN] = 1;
301         ti.c_cc[VTIME] = 0;
302         tcsetattr(tty_fd, TCSANOW, &ti);
303
304         atexit(tty_done);
305 }
306
307 void tty_flush(void)
308 {
309         tcflush(tty_fd, TCIFLUSH);
310 }
311 #endif