]> git.sesse.net Git - freerainbowtables/blob - BOINC software/BOINC client apps/chain_checker/chain_checker.cpp
UINT4 -> uint32
[freerainbowtables] / BOINC software / BOINC client apps / chain_checker / chain_checker.cpp
1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
18 // Modified by Martin Westergaard for the chain check usage
19 #ifdef _WIN32
20 #include "boinc_win.h"
21 #else
22 #include "config.h"
23 #include <cstdio>
24 #include <cctype>
25 #include <ctime>
26 #include <cstring>
27 #include <cstdlib>
28 #include <csignal>
29 #include <unistd.h>
30 #endif
31
32 #include <string>
33 #include <fstream>
34
35 #include "str_util.h"
36 #include "util.h"
37 #include "filesys.h"
38 #include "boinc_api.h"
39 #include "Public.h"
40 // Rainbowcrack code
41 #include "ChainWalkContext.h"
42 #include "chain_checker.h"
43
44 typedef unsigned int uint32;
45
46
47 using std::string;
48
49 int main(int argc, char **argv) {
50     int retval;
51     char output_path[512], chkpt_path[512];
52         string sHashRoutineName, sCharsetName, sHash;
53         uint32 nPlainLenMin, nPlainLenMax, nRainbowTableIndex;
54     FILE* state, *infile, *outfile;     
55     retval = boinc_init();
56     if (retval) {
57         fprintf(stderr, "boinc_init returned %d\n", retval);
58         exit(retval);
59     }
60         
61         if(argc < 7)
62         {
63                 std::cerr << "Not enough parameters";
64                 return -1;
65         }
66
67         // Initialize the args
68         sHashRoutineName = argv[1];
69         sCharsetName = argv[2];
70         nPlainLenMin = atoi(argv[3]);
71         nPlainLenMax = atoi(argv[4]);
72         nRainbowTableIndex = atoi(argv[5]);
73         sHash = argv[6];
74
75
76         // Setup the ChainWalkContext
77         if (!CChainWalkContext::SetHashRoutine(sHashRoutineName))
78         {
79                 std::cerr << "hash routine " << sHashRoutineName << " not supported" << std::endl;
80                 return 1;
81         }
82
83         if (!CChainWalkContext::SetPlainCharset(sCharsetName, nPlainLenMin, nPlainLenMax))
84         {       
85                 std::cerr << "charset " << sCharsetName << " not supported" << std::endl;
86                 return 2;
87         }
88         
89         if (!CChainWalkContext::SetRainbowTableIndex(nRainbowTableIndex))
90         {
91                 std::cerr << "invalid rainbow table index " << nRainbowTableIndex << std::endl;
92                 return 3;
93         }
94
95         
96         // Open the file containing the chains we have to check.
97     boinc_resolve_filename("chains", output_path, sizeof(output_path));
98         infile = boinc_fopen(output_path, "rb");    
99         if (infile == NULL)
100         {
101                 std::cerr << "failed to open " << output_path << std::endl;
102                 return 4;
103         }
104
105         // Open the resulting file. This will *maybe* contain the password (if found)
106         boinc_resolve_filename("result", output_path, sizeof(output_path));
107         outfile = boinc_fopen(output_path, "wb");       
108         if (outfile == NULL)
109         {
110                 std::cerr << "failed to create " << output_path << std::endl;
111                 return 5;
112         }
113
114         // Read the chains from the input file
115         int len = GetFileLen(infile); 
116         int numchains = len / 12;  // Each chain is 12 bytes. 8 bytes startpoint and 4 bytes for the guessed position
117         fseek(infile, 0, SEEK_SET);
118         ChainCheckChain *pChain = new ChainCheckChain[numchains];
119         int totalread = 0, read;
120         for(int i = 0; i < numchains; i++)
121         {
122                 int read = fread(&pChain[i].nIndexS, sizeof(pChain[i].nIndexS), 1, infile);
123                 if(read != 1)
124                 {
125                         std::cerr << "Error reading chaincheck file" << std::endl;
126                         return 6;
127                 }
128                 read = fread(&pChain[i].nGuessedPos, sizeof(pChain[i].nGuessedPos), 1, infile);
129                 if(read != 1)
130                 {
131                         std::cerr << "Error reading chaincheck file" << std::endl;
132                         return 7;
133                 }
134
135         }
136         fclose(infile);
137
138
139         // Start checking the chains for false alarms
140         CChainWalkContext cwc;  
141         int found = 0;
142         for(int i = 0; i < numchains; i++)
143         {
144                 cwc.SetIndex(pChain[i].nIndexS);        
145                 int nPos;
146                 for (nPos = 0; nPos < pChain[i].nGuessedPos; nPos++)
147                 {
148                         cwc.IndexToPlain();
149                         cwc.PlainToHash();
150                         cwc.HashToIndex(nPos);
151                 }
152                 cwc.IndexToPlain();
153                 cwc.PlainToHash();
154                 std::string sHashme = cwc.GetHash();
155                 // Check if this is a matching chain
156                 if(sHashme.compare(sHash) == 0)
157                 {
158                         fwrite(cwc.GetPlain().c_str(), 1, cwc.GetPlain().length(), outfile);
159                         std::cout << "Password is " << cwc.GetPlain() << std::endl;
160                         found = 1;
161                         break; // Password is found. Lets break out.
162                 }
163                 // This chain didn't contain the password, so we update the progress
164                 boinc_fraction_done((double)i / (double)numchains); 
165                 
166         }
167         if(found == 0)
168                 fwrite("0x00", 4, 1, outfile);
169         // Last step: Clean up
170         fclose(outfile);    
171     boinc_fraction_done(1);
172     boinc_finish(0);
173 }
174
175 #ifdef _WIN32
176 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
177     LPSTR command_line;
178     char* argv[100];
179     int argc;
180
181     command_line = GetCommandLine();
182     argc = parse_command_line( command_line, argv );
183     return main(argc, argv);
184 }
185 #endif
186
187 const char *BOINC_RCSID_33ac47a071 = "$Id: chain_checker.cpp 2008-11-28 Martin Westergaard $";
188