]> git.sesse.net Git - freerainbowtables/blob - Server Applications/rsearchi/rsearch.cpp
initial
[freerainbowtables] / Server Applications / rsearchi / rsearch.cpp
1 // distrrtgen_assembler.cpp : Defines the entry point for the console application.
2 //
3 #include "stdafx.h"
4
5 #include "MySQL.h"
6 #include <string>
7 #include <sstream>
8 #include <list>
9 #include <iostream>
10 #include <sstream>
11 #include <fstream>
12 #include <time.h>
13 #include <map>
14 #include <sys/types.h>
15 #include "dirent.h"
16 #include <errno.h>
17 #include <vector>
18
19 #include "ChainWalkContext.h"
20 #include "CrackEngine.h"
21 //#include "ServerConnector.h"
22 #ifdef WIN32
23         #include <direct.h>
24 #endif
25 typedef struct
26 {
27         std::string sHost;
28         std::string sUsername;
29         std::string sPassword;
30         std::string sDatabase;
31 } DB_SETTINGS;
32 using std::map;
33 using namespace std;
34 #define DIR_INDICES "/home/boincadm/indices/"
35 #define DIR_CHAINS "/home/boincadm/chains/"
36 //typedef __int64 uint64;
37 //typedef int uint32;
38
39 DB_SETTINGS dbSettings;
40
41 void GetCharset(std::string sCharsetName, char **pCharset);
42
43 void RunCracker(int nTableID, CHashSet &hs, vector<string> &vPathName)
44 {
45 //      vector<string> vPathName;
46 //      vPathName.push_back("md5_mixalpha-numeric-all-space#1-6_0_10000x9434153_0.rt");
47
48 }
49
50
51 int main(int argc, char* argv[])
52 {
53         
54 #ifdef WIN32
55         std::string sRTPath = "/rainbowtables_i";
56 #else
57         std::string sRTPath = "/home/rainbow/tables/indexed";
58 nice(19);
59 #endif
60         while(1)
61         {
62                 try
63                 {
64         //              chdir(sRTPath.c_str());
65
66                         dbSettings.sDatabase.assign(MYSQL_DATABASE);
67                         dbSettings.sHost.assign(MYSQL_HOST);
68                         dbSettings.sPassword.assign(MYSQL_PASSWORD);
69                         dbSettings.sUsername.assign(MYSQL_USERNAME);
70
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;
75                         while(1)
76                         {
77                                 vector<string> vFiles;
78 /*
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");
80                                 int nPrevSet = 0;
81                                 while(pDatabase->Fetch() == true)
82                                 {
83                                         if(pDatabase->GetInt(0) != nPrevSet)
84                                         {
85                                                 if(nPrevSet != 0)
86                                                 {
87                                                         mTableSets[nPrevSet] = vFiles;
88                                                         vFiles.erase(vFiles.begin(), vFiles.end());
89                                                 }
90                                                 nPrevSet = pDatabase->GetInt(0);
91                                         }
92                                         vFiles.push_back((const char *)pDatabase->GetText(1));                          
93                                 }
94                                 if(nPrevSet > 0)
95                                         mTableSets[nPrevSet] = vFiles;
96 */
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;
102                                         return errno;
103                                 }
104                                 while ((hrp = readdir(hr)) != NULL) 
105                                 {
106
107                                         string hrs = string(hrp->d_name);
108                                         if(hrs != "." && hrs != "..")
109                                         {
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;
114                                                         continue;
115                                                 }
116                                                 while ((tablesp = readdir(tables)) != NULL) 
117                                                 {
118                                                         string tablename = string(tablesp->d_name);
119                                                         if(tablename != "." && tablename != ".." && tablename.substr(tablename.length()-8) != ".torrent")
120                                                         {
121                                                                 string hashroutine;
122                                                                 string charsets;
123                                                                 string sminletters;
124                                                                 string smaxletters;
125                                                                 string indexs;
126
127                                                                 vector<string> vPart;
128                                                                 if(tablename.find("hybrid") != string::npos)
129                                                                 {// Special file handling of hybrid files
130                                                                         if (!SeperateString(tablename, "_()#-_", vPart))
131                                                                         {
132                                                                                 printf("filename %s not identified\n", tablename.c_str());
133                                                                                 continue;
134                                                                         }
135                                                                         
136                                                                         hashroutine = vPart[0];
137                                                                         charsets = string(vPart[1] + string("(") + vPart[2] + ")");
138                                                                         sminletters = vPart[4];
139                                                                         smaxletters = vPart[5];
140                                                                         indexs = vPart[6];
141                                                                         
142                                                                 }
143                                                                 else
144                                                                 {
145                                                                         if (!SeperateString(tablename, "_#-_", vPart))
146                                                                         {
147                                                                                 printf("filename %s not identified\n", tablename.c_str());
148                                                                                 continue;
149                                                                         }
150                                                                         hashroutine = vPart[0];
151                                                                         charsets = vPart[1];
152                                                                         sminletters = vPart[2];
153                                                                         smaxletters = vPart[3];
154                                                                         indexs = vPart[4];
155                                                                 }
156
157                                                                 string sDirFiles = string(sRTPath + string("/") + hrs + string("/") + tablename);
158                                                                 if((files  = opendir(sDirFiles.c_str())) == NULL) {
159                                                                         cout << "Error(" << errno << ") opening " << sDirFiles << endl;
160                                                                         continue;
161                                                                 }
162                                                                 vFiles.erase(vFiles.begin(), vFiles.end());                                                                                             
163                                                                 while ((filesp = readdir(files)) != NULL) 
164                                                                 {
165                                                                         string filess = string(filesp->d_name);
166                                                                         if(filess != "." && filess != "..")
167                                                                         {
168                                                                                 string extension = filess.substr(filess.length() - 4, 4);
169                                                                                 if(extension.compare(".rti") == 0)
170                                                                                         vFiles.push_back(filess);
171                                                                         }
172                                                                 }
173                                                                 closedir(files);                                                                                        
174                                                                 if(vFiles.size() > 0)
175                                                                 {
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)
180                                                                         {
181                                                                                 int nTableID = pDatabase->GetInt(0);
182                                                                                 mTableSets[nTableID] = vFiles;
183                                                                         }
184                                                                 }                                                                       
185                                                         }
186                                                 }
187                                         }                                                                                       
188                                 }
189                                 closedir(hr);
190
191                                 CCrackEngine ce;
192                                 
193                                 for(map<int, vector<string> >::iterator cur = mTableSets.begin(); cur != mTableSets.end(); cur++)
194                                 {
195
196                                         ostringstream sSQL;
197                                         vector<int> vRequestID;
198
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";
201
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)
205                                         {
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 (";
210                                                 int nRow = 0;                                           
211                                                 while(pDatabase->Fetch() == true)
212                                                 {
213                                                         if(nRow > 0)
214                                                         {
215                                                                 sToLook << ",";
216                                                         }
217                                                         else
218                                                         {
219                                                                 printf("Fetching indices");
220                                                         }
221                                                         nRow++;
222                                                         printf(".");
223                                                         sHashRoutine.assign((char *)pDatabase->GetText(2));
224                                                         sToLook << pDatabase->GetInt(0);
225                                                         ostringstream sPath;
226                                                         sPath << sRTPath << "/" << pDatabase->GetText(2) << "/" << pDatabase->GetText(2) << "_" << pDatabase->GetText(3) << "#" << pDatabase->GetInt(4) << "-" << pDatabase->GetInt(5) << "_" << pDatabase->GetInt(6);
227         #ifdef WIN32
228                                                         _chdir(sPath.str().c_str());                            
229         #else
230                                                         chdir(sPath.str().c_str());                             
231         #endif
232                                                         vRequestID.push_back(pDatabase->GetInt(0));
233                                                         
234
235
236
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");                                               
242                                                         if(fIndex == NULL)
243                                                         {
244                                                                 printf("Could not open file %s", indiceFilename.str().c_str());
245                                                                 continue;
246                                                         }
247
248
249                                                         uint64 *indices = new uint64[pDatabase->GetInt(8)];
250                                                         int i = 0;
251                                                         std::string sInt;
252                                                         if(fread(indices, 8, pDatabase->GetInt(8), fIndex) != pDatabase->GetInt(8))
253                                                         {
254                                                                 printf("Error reading index file %s\n", indiceFilename.str().c_str()); 
255                                                                 fclose(fIndex);
256                                                                 continue;
257                                                         }
258                                                         fclose(fIndex);
259                                                         hs->AddHash((const char *)pDatabase->GetText(1), indices, pDatabase->GetInt(0));
260                                                 }
261                                                 
262                                                 sToLook << ")";
263                                                 if(nRow > 0)
264                                                 {
265                                                         pDatabase->Query(sToLook.str());
266                                                         printf("ok!\n");
267                                                 }
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++)
273                                                 {
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");
280                                                         if(fChains == NULL)
281                                                         {
282                                                                 printf("Unable to open file %s", sChainFile.str().c_str());
283                                                                 continue;
284                                                         }
285                                                         for(int i = 0; i < curHash->second.size(); i++)
286                                                         {
287                                                                 if(fwrite(&curHash->second[i].nIndexS, sizeof(curHash->second[i].nIndexS), 1, fChains) != 1)
288                                                                 {
289                                                                         printf("Unable to write data to %s", sChainFile.str().c_str());
290                                                                         continue;
291                                                                 }
292                                                                 if(fwrite(&curHash->second[i].nGuessedPos, sizeof(curHash->second[i].nGuessedPos), 1, fChains) != 1)
293                                                                 {
294                                                                         printf("Unable to write data to %s", sChainFile.str().c_str());
295                                                                         continue;
296                                                                 }
297                                                         }                                                       
298                                                         fclose(fChains);                                                        
299                                                         sSQL.str("");
300                                                         sSQL << "UPDATE rainbowcrack_cracker_lookups SET status = 4, hasindices = 3 WHERE lookupid = " << hs->GetLookupID(curHash->first);
301                                                         try
302                                                         {
303                                                                 pDatabase->Query(sSQL.str());
304                                                                 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());
313
314                                                         }
315                                                         catch(DatabaseException *ex)
316                                                         {
317                                                                 if(ex->getErrorCode() == CR_SERVER_GONE_ERROR || ex->getErrorCode() == CR_SERVER_LOST)
318                                                                 {
319                                                                         std::cout << "Server has disconnected";
320                                                                 }
321                                                                 else throw ex;
322                                                         }
323                                                 }
324 //                                              delete sc;
325                                                 delete hs;
326                                         }
327                                         sSQL.str("");
328                                         for(int i = 0; i < vRequestID.size(); i++)
329                                         {
330                                                 stringstream sFile;
331                                                 sFile << DIR_INDICES << vRequestID[i] << ".index";
332                                                 unlink(sFile.str().c_str());
333                                         }                                       
334                 /*                      if(vRequestID.size() > 0)
335                                         {
336                                                 stringstream sList;
337                                                 for(int i = 0; i < vRequestID.size(); i++)
338                                                 {
339                                                         if(i != 0)
340                                                                 sList << ",";                                   
341                                                         sList << vRequestID[i];
342                                                 }
343                                                 sSQL << "UPDATE rainbowcrack_cracker_lookups SET status = 2, hasindices = 2 WHERE lookupid IN (" << sList.str() << ")";
344                                                 pDatabase->Query(sSQL.str());
345                                         }*/
346                                 }
347                                 Sleep(1000);
348                         }
349                 }
350                 catch(DatabaseException *ex)
351                 {
352                         std::cout << "Database exception " << " ( " << ex->getErrorCode() << " ): " << ex->GetErrorMessage()  << std::endl;
353                         delete ex;
354                         
355                 }
356         }
357         return 0;
358 }
359