2 RainbowCrack - a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
4 Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
8 #pragma warning(disable : 4786 4267 4018)
16 #include <sys/sysinfo.h>
19 //////////////////////////////////////////////////////////////////////
21 unsigned int GetFileLen(FILE* file)
23 unsigned int pos = ftell(file);
24 fseek(file, 0, SEEK_END);
25 unsigned int len = ftell(file);
26 fseek(file, pos, SEEK_SET);
31 string TrimString(string s)
35 if (s[0] == ' ' || s[0] == '\t')
43 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
44 s = s.substr(0, s.size() - 1);
51 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
53 // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
54 if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
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++)
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);
75 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
79 FILE* file = fopen(sPathName.c_str(), "rb");
82 unsigned int len = GetFileLen(file);
83 char* data = new char[len + 1];
84 fread(data, 1, len, file);
86 string content = data;
91 for (i = 0; i < content.size(); i++)
93 if (content[i] == '\r')
98 while ((n = content.find("\n", 0)) != -1)
100 string line = content.substr(0, n);
101 line = TrimString(line);
103 vLine.push_back(line);
104 content = content.substr(n + 1);
115 bool writeResultLineToFile(string sOutputFile, string sHash, string sPlain, string sBinary)
117 FILE* file = fopen(sOutputFile.c_str(), "a");
120 string buffer = sHash + ":" + sPlain + ":" + sBinary + "\n";
121 fputs (buffer.c_str(), file);
129 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
134 for (i = 0; i < sSeperator.size(); i++)
136 int n = s.find(sSeperator[i]);
139 vPart.push_back(s.substr(0, n));
144 printf("not found: %c\n", sSeperator[i]);
145 printf("s: %s\n", s.c_str());
154 string uint64tostr(uint64 n)
159 sprintf(str, "%I64u", n);
161 sprintf(str, "%llu", n);
167 string uint64tohexstr(uint64 n)
172 sprintf(str, "%016I64x", n);
174 sprintf(str, "%016llx", n);
180 string HexToStr(const unsigned char* pData, int nLen)
184 for (i = 0; i < nLen; i++)
187 sprintf(szByte, "%02x", pData[i]);
194 unsigned int GetAvailPhysMemorySize()
198 GlobalMemoryStatus(&ms);
199 return ms.dwAvailPhys;
202 sysinfo(&info); // This function is Linux-specific
207 string GetApplicationPath()
209 char fullPath[FILENAME_MAX];
212 GetModuleFileName(NULL, fullPath, FILENAME_MAX);
215 sprintf(szTmp, "/proc/%d/exe", getpid());
216 int bytes = readlink(szTmp, fullPath, FILENAME_MAX);
218 fullPath[bytes] = '\0';
221 string sApplicationPath = fullPath;
223 int nIndex = sApplicationPath.find_last_of('\\');
225 int nIndex = sApplicationPath.find_last_of('/');
229 sApplicationPath = sApplicationPath.substr(0, nIndex+1);
231 //printf ("\n\nDebug: The application directory is %s\n", sApplicationPath.c_str());
232 return sApplicationPath;
235 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
238 for (i = 0; i < sHash.size() / 2; i++)
240 string sSub = sHash.substr(i * 2, 2);
242 sscanf(sSub.c_str(), "%02x", &nValue);
243 pHash[i] = (unsigned char)nValue;
246 nHashLen = sHash.size() / 2;
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");
259 // Code comes from nmap, used for the linux implementation of kbhit()
262 static int tty_fd = 0;
263 struct termios saved_ti;
269 if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {
271 numChars = read(tty_fd, &c, 1);
272 if (numChars > 0) return c;
282 tcsetattr(tty_fd, TCSANOW, &saved_ti);
295 if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;
297 tcgetattr(tty_fd, &ti);
299 ti.c_lflag &= ~(ICANON | ECHO);
302 tcsetattr(tty_fd, TCSANOW, &ti);
309 tcflush(tty_fd, TCIFLUSH);