2 * freerainbowtables is a project for generating, distributing, and using
3 * perfect rainbow tables
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>
10 * This file is part of freerainbowtables.
12 * freerainbowtables 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, either version 2 of the License, or
15 * (at your option) any later version.
17 * freerainbowtables is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with freerainbowtables. If not, see <http://www.gnu.org/licenses/>.
27 #pragma warning(disable : 4786)
47 #elif defined(__APPLE__) || \
48 ((defined(__unix__) || defined(unix)) && !defined(USG))
50 #include <sys/param.h>
53 #include <sys/sysctl.h>
54 #elif defined(__linux__)
55 #include <sys/sysinfo.h>
57 #error Unsupported Operating system
61 //////////////////////////////////////////////////////////////////////
63 unsigned int GetFileLen(FILE* file)
65 unsigned int pos = ftell(file);
66 fseek(file, 0, SEEK_END);
67 unsigned int len = ftell(file);
68 fseek(file, pos, SEEK_SET);
73 string TrimString(string s)
77 if (s[0] == ' ' || s[0] == '\t')
85 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
86 s = s.substr(0, s.size() - 1);
93 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
95 // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
96 if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
99 UINT4 nEnd = (int) sCharset.rfind(')');
100 UINT4 nStart = (int) sCharset.rfind('(');
101 string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);
102 vector<string> vParts;
103 SeperateString(sChar, ",", vParts);
104 for(UINT4 i = 0; i < vParts.size(); i++)
107 vector<string> vParts2;
108 SeperateString(vParts[i], "#", vParts2);
109 stCharset.sName = vParts2[0];
110 vector<string> vParts3;
111 SeperateString(vParts2[1], "-", vParts3);
112 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());
113 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());
114 vCharset.push_back(stCharset);
118 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
121 FILE *file = fopen(sPathName.c_str(), "rb");
124 unsigned int len = GetFileLen(file);
125 char* data = new char[len + 1];
126 fread(data, 1, len, file);
128 string content = data;
133 for (i = 0; i < content.size(); i++)
135 if (content[i] == '\r')
140 while ((n = content.find("\n", 0)) != -1)
142 string line = content.substr(0, n);
143 line = TrimString(line);
145 vLine.push_back(line);
146 content = content.substr(n + 1);
157 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
162 for (i = 0; i < sSeperator.size(); i++)
164 int n = s.find(sSeperator[i]);
167 vPart.push_back(s.substr(0, n));
178 string uint64tostr(uint64 n)
183 sprintf(str, "%I64u", n);
185 sprintf(str, "%llu", n);
191 string uint64tohexstr(uint64 n)
196 sprintf(str, "%016I64x", n);
198 sprintf(str, "%016llx", n);
204 string HexToStr(const unsigned char* pData, int nLen)
208 for (i = 0; i < nLen; i++)
211 sprintf(szByte, "%02x", pData[i]);
218 uint64 GetAvailPhysMemorySize()
222 GlobalMemoryStatus(&ms);
223 return ms.dwAvailPhys;
225 int mib[2] = { CTL_HW, HW_PHYSMEM };
227 //XXX warning size_t isn't portable
229 len = sizeof(physMem);
230 sysctl(mib, 2, &physMem, &len, NULL, 0);
232 #elif defined(__linux__)
235 return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit;
238 #error Unsupported Operating System
242 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
245 for (i = 0; i < sHash.size() / 2; i++)
247 string sSub = sHash.substr(i * 2, 2);
249 sscanf(sSub.c_str(), "%02x", &nValue);
250 pHash[i] = (unsigned char)nValue;
253 nHashLen = sHash.size() / 2;
258 printf("RainbowCrack (improved) 2.0 - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
259 printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
260 printf("http://www.freerainbowtables.com/\n");
261 printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
262 printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
265 // XXX nmap is GPL2, will check newer releases regarding license
266 // Code comes from nmap, used for the linux implementation of kbhit()
269 static int tty_fd = 0;
270 struct termios saved_ti;
276 if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {
278 numChars = read(tty_fd, &c, 1);
279 if (numChars > 0) return c;
289 tcsetattr(tty_fd, TCSANOW, &saved_ti);
302 if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;
304 tcgetattr(tty_fd, &ti);
306 ti.c_lflag &= ~(ICANON | ECHO);
309 tcsetattr(tty_fd, TCSANOW, &ti);
316 tcflush(tty_fd, TCIFLUSH);