1 // distrrtgen_assembler.cpp : Defines the entry point for the console application.
14 #include <sys/types.h>
19 #include "ChainWalkContext.h"
20 #include "CrackEngine.h"
21 //#include "ServerConnector.h"
28 std::string sUsername;
29 std::string sPassword;
30 std::string sDatabase;
34 #define DIR_INDICES "/home/boincadm/indices/"
35 #define DIR_CHAINS "/home/boincadm/chains/"
36 //typedef __int64 uint64;
39 DB_SETTINGS dbSettings;
41 void GetCharset(std::string sCharsetName, char **pCharset);
43 void RunCracker(int nTableID, CHashSet &hs, vector<string> &vPathName)
45 // vector<string> vPathName;
46 // vPathName.push_back("md5_mixalpha-numeric-all-space#1-6_0_10000x9434153_0.rt");
51 int main(int argc, char* argv[])
55 std::string sRTPath = "/rainbowtables_i";
57 std::string sRTPath = "/home/rainbow/tables/indexed";
64 // chdir(sRTPath.c_str());
66 dbSettings.sDatabase.assign(MYSQL_DATABASE);
67 dbSettings.sHost.assign(MYSQL_HOST);
68 dbSettings.sPassword.assign(MYSQL_PASSWORD);
69 dbSettings.sUsername.assign(MYSQL_USERNAME);
71 map<int, vector<string> > mTableSets;
72 std::cout << "Connecting to database" << std::endl;
73 CDatabase *pDatabase = (CDatabase*)new MySQL(dbSettings.sHost, dbSettings.sUsername, dbSettings.sPassword, dbSettings.sDatabase);
74 std::cout << "Connected!" << std::endl;
77 vector<string> vFiles;
79 pDatabase->Select("SELECT rct.tableid, rctf.filename, rcts.hashroutine, rcts.charset, rcts.minletters, rcts.maxletters, rct.`index` FROM rainbowcrack_cracker_tables rct INNER JOIN rainbowcrack_cracker_tablesets rcts ON rcts.tablesetid = rct.tablesetid INNER JOIN rainbowcrack_cracker_tsfiles rctf ON rctf.tableid = rct.tableid");
81 while(pDatabase->Fetch() == true)
83 if(pDatabase->GetInt(0) != nPrevSet)
87 mTableSets[nPrevSet] = vFiles;
88 vFiles.erase(vFiles.begin(), vFiles.end());
90 nPrevSet = pDatabase->GetInt(0);
92 vFiles.push_back((const char *)pDatabase->GetText(1));
95 mTableSets[nPrevSet] = vFiles;
97 DIR *hr, *files, *tables;//, *letters, *index, ;
98 struct dirent *hrp, *filesp, *tablesp;//, *charsetp, *lettersp, *indexp;
99 cout << "Opening dir " << sRTPath << endl;
100 if((hr = opendir(sRTPath.c_str())) == NULL) {
101 cout << "Error(" << errno << ") opening dir " << sRTPath << endl;
104 while ((hrp = readdir(hr)) != NULL)
107 string hrs = string(hrp->d_name);
108 if(hrs != "." && hrs != "..")
110 string sDirTables = string(sRTPath + string("/") + hrs);
111 cout << "Opening dir " << sDirTables << endl;
112 if((tables = opendir(sDirTables.c_str())) == NULL) {
113 cout << "Error(" << errno << ") opening dir " << sDirTables << endl;
116 while ((tablesp = readdir(tables)) != NULL)
118 string tablename = string(tablesp->d_name);
119 if(tablename != "." && tablename != ".." && tablename.substr(tablename.length()-8) != ".torrent")
127 vector<string> vPart;
128 if(tablename.find("hybrid") != string::npos)
129 {// Special file handling of hybrid files
130 if (!SeperateString(tablename, "_()#-_", vPart))
132 printf("filename %s not identified\n", tablename.c_str());
136 hashroutine = vPart[0];
137 charsets = string(vPart[1] + string("(") + vPart[2] + ")");
138 sminletters = vPart[4];
139 smaxletters = vPart[5];
145 if (!SeperateString(tablename, "_#-_", vPart))
147 printf("filename %s not identified\n", tablename.c_str());
150 hashroutine = vPart[0];
152 sminletters = vPart[2];
153 smaxletters = vPart[3];
157 string sDirFiles = string(sRTPath + string("/") + hrs + string("/") + tablename);
158 if((files = opendir(sDirFiles.c_str())) == NULL) {
159 cout << "Error(" << errno << ") opening " << sDirFiles << endl;
162 vFiles.erase(vFiles.begin(), vFiles.end());
163 while ((filesp = readdir(files)) != NULL)
165 string filess = string(filesp->d_name);
166 if(filess != "." && filess != "..")
168 string extension = filess.substr(filess.length() - 4, 4);
169 if(extension.compare(".rti") == 0)
170 vFiles.push_back(filess);
174 if(vFiles.size() > 0)
176 std::stringstream sSQL;
177 sSQL << "SELECT t.tableid FROM rainbowcrack_cracker_tables t INNER JOIN rainbowcrack_cracker_tablesets ts ON ts.tablesetid = t.tablesetid WHERE ts.hashroutine = '" << hashroutine << "' AND ts.charset = '" << charsets << "' AND ts.minletters = " << sminletters<< " AND ts.maxletters = " << smaxletters << " AND t.`index` = " << indexs;
178 pDatabase->Select(sSQL.str());
179 if(pDatabase->Fetch() == true)
181 int nTableID = pDatabase->GetInt(0);
182 mTableSets[nTableID] = vFiles;
193 for(map<int, vector<string> >::iterator cur = mTableSets.begin(); cur != mTableSets.end(); cur++)
197 vector<int> vRequestID;
199 // sSQL << "SELECT workid, hash, cl.salt, indicelist, ts.charset, ts.chainlength FROM rainbowcrack_cracker_hashlist cl INNER JOIN rainbowcrack_cracker_tablesets ts ON (ts.tableid = cl.tableid) WHERE processed = 1 AND indicelist IS NOT NULL AND ts.tableid = " << cur->first;
200 // sSQL << "SELECT cl.lookupid, ch.hash, ts.salt, cl.indices, ts.charset, ts.chainlength FROM rainbowcrack_cracker_hashlist ch INNER JOIN rainbowcrack_cracker_lookups cl ON (ch.hashid = cl.hashid) INNER JOIN rainbowcrack_cracker_tables ct ON (ct.tableid = cl.tableid) INNER JOIN rainbowcrack_cracker_tablesets ts ON (ts.tablesetid = ct.tablesetid) WHERE cl.indices IS NOT NULL AND ch.password IS NULL AND status = 0 AND ct.tableid = " << cur->first << " LIMIT 100";
202 cout << "Querying for " << cur->first << std::endl;
203 sSQL << "SELECT cl.lookupid, ch.hash, ts.hashroutine, ts.charset, ts.minletters, ts.maxletters, ct.index, ts.salt, ts.chainlength FROM rainbowcrack_cracker_hashlist ch INNER JOIN rainbowcrack_cracker_lookups cl ON (ch.hashid = cl.hashid) INNER JOIN rainbowcrack_cracker_tables ct ON (ct.tableid = cl.tableid) INNER JOIN rainbowcrack_cracker_tablesets ts ON (ts.tablesetid = ct.tablesetid) WHERE cl.hasindices = 2 AND ch.password IS NULL AND (status = 0 OR (status = 1 AND DATE_SUB(NOW(), INTERVAL 1 HOUR) > time_serverassignment)) AND ct.tableid = " << cur->first << " LIMIT 1000";
204 if(pDatabase->Select(sSQL.str()) > 0)
206 std::string sHashRoutine;
207 CHashSet *hs = new CHashSet();
208 std::ostringstream sToLook;
209 sToLook << "UPDATE rainbowcrack_cracker_lookups SET status = 1, time_serverassignment = NOW() WHERE lookupid IN (";
211 while(pDatabase->Fetch() == true)
219 printf("Fetching indices");
223 sHashRoutine.assign((char *)pDatabase->GetText(2));
224 sToLook << pDatabase->GetInt(0);
226 sPath << sRTPath << "/" << pDatabase->GetText(2) << "/" << pDatabase->GetText(2) << "_" << pDatabase->GetText(3) << "#" << pDatabase->GetInt(4) << "-" << pDatabase->GetInt(5) << "_" << pDatabase->GetInt(6);
228 _chdir(sPath.str().c_str());
230 chdir(sPath.str().c_str());
232 vRequestID.push_back(pDatabase->GetInt(0));
237 // New code, loads from the local disk
238 std::string sIndexDir = DIR_INDICES;
239 std::stringstream indiceFilename;
240 indiceFilename << sIndexDir.c_str() << pDatabase->GetText(0) << ".index";
241 FILE *fIndex = fopen(indiceFilename.str().c_str(), "rb");
244 printf("Could not open file %s", indiceFilename.str().c_str());
249 uint64 *indices = new uint64[pDatabase->GetInt(8)];
252 if(fread(indices, 8, pDatabase->GetInt(8), fIndex) != pDatabase->GetInt(8))
254 printf("Error reading index file %s\n", indiceFilename.str().c_str());
259 hs->AddHash((const char *)pDatabase->GetText(1), indices, pDatabase->GetInt(0));
265 pDatabase->Query(sToLook.str());
268 ce.Run(cur->second, *hs);
269 map<string, vector<FoundRainbowChain> > mHashes;
270 hs->GetFoundChains(mHashes);
271 // ServerConnector *sc = new ServerConnector();
272 for(map<string, vector<FoundRainbowChain> >::iterator curHash = mHashes.begin(); curHash != mHashes.end(); curHash++)
274 std::stringstream sChains;
275 // Old code - Upload the chains through HTTP
276 // New code. Write it directly to the file and update the DB
277 std::stringstream sChainFile;
278 sChainFile << DIR_CHAINS << hs->GetLookupID(curHash->first) << ".chains";
279 FILE *fChains = fopen(sChainFile.str().c_str(), "wb");
282 printf("Unable to open file %s", sChainFile.str().c_str());
285 for(int i = 0; i < curHash->second.size(); i++)
287 if(fwrite(&curHash->second[i].nIndexS, sizeof(curHash->second[i].nIndexS), 1, fChains) != 1)
289 printf("Unable to write data to %s", sChainFile.str().c_str());
292 if(fwrite(&curHash->second[i].nGuessedPos, sizeof(curHash->second[i].nGuessedPos), 1, fChains) != 1)
294 printf("Unable to write data to %s", sChainFile.str().c_str());
300 sSQL << "UPDATE rainbowcrack_cracker_lookups SET status = 4, hasindices = 3 WHERE lookupid = " << hs->GetLookupID(curHash->first);
303 pDatabase->Query(sSQL.str());
305 sSQL << "INSERT IGNORE INTO rainbowcrack_cracker_verificationqueue (lookupid, hash, hashroutine, tablesetid, charset, minletters, maxletters, chainlength, salt, `index`) ";
306 sSQL << "SELECT l.lookupid, h.hash, h.hashroutine, t.tablesetid, ts.charset, ts.minletters, ts.maxletters, ts.chainlength, ts.salt, t.`index` ";
307 sSQL << " FROM rainbowcrack_cracker_lookups l ";
308 sSQL << " INNER JOIN rainbowcrack_cracker_hashlist h ON h.hashid = l.hashid ";
309 sSQL << " INNER JOIN rainbowcrack_cracker_tables t ON t.tableid = l.tableid ";
310 sSQL << " INNER JOIN rainbowcrack_cracker_tablesets ts ON ts.tablesetid = t.tablesetid ";
311 sSQL << " WHERE l.lookupid = " << hs->GetLookupID(curHash->first);
312 pDatabase->Query(sSQL.str());
315 catch(DatabaseException *ex)
317 if(ex->getErrorCode() == CR_SERVER_GONE_ERROR || ex->getErrorCode() == CR_SERVER_LOST)
319 std::cout << "Server has disconnected";
328 for(int i = 0; i < vRequestID.size(); i++)
331 sFile << DIR_INDICES << vRequestID[i] << ".index";
332 unlink(sFile.str().c_str());
334 /* if(vRequestID.size() > 0)
337 for(int i = 0; i < vRequestID.size(); i++)
341 sList << vRequestID[i];
343 sSQL << "UPDATE rainbowcrack_cracker_lookups SET status = 2, hasindices = 2 WHERE lookupid IN (" << sList.str() << ")";
344 pDatabase->Query(sSQL.str());
350 catch(DatabaseException *ex)
352 std::cout << "Database exception " << " ( " << ex->getErrorCode() << " ): " << ex->GetErrorMessage() << std::endl;