]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rcracki_mt/rcrackiThread.cpp
test
[freerainbowtables] / Client Applications / rcracki_mt / rcrackiThread.cpp
1 /*
2  * rcracki_mt is a multithreaded implementation and fork of the original 
3  * RainbowCrack
4  *
5  * Copyright 2009, 2010 DaniĆ«l Niggebrugge <niggebrugge@fox-it.com>
6  * Copyright 2009, 2010 James Nobis <frt@quelrod.net>
7  *
8  * This file is part of rcracki_mt.
9  *
10  * rcracki_mt is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * rcracki_mt is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with rcracki_mt.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #if defined(_WIN32) && !defined(__GNUC__)
25         #pragma warning(disable : 4786 4267 4018)
26 #endif
27
28 #include "rcrackiThread.h"
29
30 // create job for pre-calculation
31 rcrackiThread::rcrackiThread(unsigned char* TargetHash, int thread_id, int nRainbowChainLen, int thread_count, uint64* pStartPosIndexE)
32 {
33         t_TargetHash = TargetHash;
34         t_nRainbowChainLen = nRainbowChainLen;
35         t_ID = thread_id;
36         t_count = thread_count;
37         t_pStartPosIndexE = pStartPosIndexE;
38         t_nChainWalkStep = 0;
39         falseAlarmChecker = false;
40         falseAlarmCheckerO = false;
41 }
42
43 // create job for false alarm checking
44 rcrackiThread::rcrackiThread(unsigned char* pHash, bool oldFormat)
45 {
46         falseAlarmChecker = true;
47         falseAlarmCheckerO = oldFormat;
48         t_pChainsFound.clear();
49         t_nGuessedPoss.clear();
50         t_pHash = pHash;
51         t_nChainWalkStepDueToFalseAlarm = 0;
52         t_nFalseAlarm = 0;
53         foundHash = false;
54 }
55
56 void rcrackiThread::AddAlarmCheck(RainbowChain* pChain, int nGuessedPos)
57 {
58         t_pChainsFound.push_back(pChain);
59         t_nGuessedPoss.push_back(nGuessedPos);
60 }
61
62 void rcrackiThread::AddAlarmCheckO(RainbowChainO* pChain, int nGuessedPos)
63 {
64         t_pChainsFoundO.push_back(pChain);
65         t_nGuessedPoss.push_back(nGuessedPos);
66 }
67
68 // Windows (beginthreadex) way of threads
69 //unsigned __stdcall rcrackiThread::rcrackiThreadStaticEntryPoint(void * pThis)
70 //{
71 //      rcrackiThread* pTT = (rcrackiThread*)pThis;
72 //      pTT->rcrackiThreadEntryPoint();
73 //      _endthreadex( 2 );
74 //      return 2;
75 //}
76
77 // entry point for the posix thread
78 void * rcrackiThread::rcrackiThreadStaticEntryPointPthread(void * pThis)
79 {
80         rcrackiThread* pTT = (rcrackiThread*)pThis;
81         pTT->rcrackiThreadEntryPoint();
82         pthread_exit(NULL);
83         return NULL;
84 }
85
86 // start processing of jobs
87 void rcrackiThread::rcrackiThreadEntryPoint()
88 {
89         if (falseAlarmChecker) {
90                 if (falseAlarmCheckerO) {
91                         CheckAlarmO();
92                 }
93                 else {
94                         CheckAlarm();
95                 }
96         }
97         else {
98                 PreCalculate();
99         }
100 }
101
102 uint64 rcrackiThread::GetIndex(int nPos)
103 {
104         uint64 t_index = t_vStartPosIndexE[nPos - t_ID];
105         return t_index;
106 }
107
108 int rcrackiThread::GetChainWalkStep()
109 {
110         return t_nChainWalkStep;
111 }
112
113 int rcrackiThread::GetIndexCount()
114 {
115         return t_vStartPosIndexE.size();
116 }
117
118 rcrackiThread::~rcrackiThread(void)
119 {
120 }
121
122 void rcrackiThread::PreCalculate()
123 {
124         for (t_nPos = t_nRainbowChainLen - 2 - t_ID; t_nPos >= 0; t_nPos -= t_count)
125         {
126                 t_cwc.SetHash(t_TargetHash);
127                 t_cwc.HashToIndex(t_nPos);
128                 int i;
129                 for (i = t_nPos + 1; i <= t_nRainbowChainLen - 2; i++)
130                 {
131                         t_cwc.IndexToPlain();
132                         t_cwc.PlainToHash();
133                         t_cwc.HashToIndex(i);
134                 }
135                 t_pStartPosIndexE[t_nPos] = t_cwc.GetIndex();
136                 t_nChainWalkStep += t_nRainbowChainLen - 2 - t_nPos;
137         }
138 }
139
140 void rcrackiThread::CheckAlarm()
141 {
142         UINT4 i;
143         for (i = 0; i < t_pChainsFound.size(); i++)
144         {
145                 RainbowChain* t_pChain = t_pChainsFound[i];
146                 int t_nGuessedPos = t_nGuessedPoss[i];          
147                 
148                 CChainWalkContext cwc;
149                 //uint64 nIndexS = t_pChain->nIndexS & 0x0000FFFFFFFFFFFF; // for first 6 bytes
150                 //uint64 nIndexS = t_pChain->nIndexS >> 16;
151                 uint64 nIndexS = t_pChain->nIndexS & 0x0000FFFFFFFFFFFFULL; // for first 6 bytes
152                 cwc.SetIndex(nIndexS);
153                 //cwc.SetIndex(t_pChain->nIndexS);      
154                 int nPos;
155                 for (nPos = 0; nPos < t_nGuessedPos; nPos++)
156                 {
157                         cwc.IndexToPlain();
158                         cwc.PlainToHash();
159                         cwc.HashToIndex(nPos);
160                 }
161                 cwc.IndexToPlain();
162                 cwc.PlainToHash();
163                 if (cwc.CheckHash(t_pHash))
164                 {
165                         t_Hash = cwc.GetHash();
166                         t_Plain = cwc.GetPlain();
167                         t_Binary = cwc.GetBinary();
168
169                         foundHash = true;
170                         break;
171                 }
172                 else {
173                         foundHash = false;
174                         t_nChainWalkStepDueToFalseAlarm += t_nGuessedPos + 1;
175                         t_nFalseAlarm++;
176                 }
177         }
178 }
179
180 void rcrackiThread::CheckAlarmO()
181 {
182         UINT4 i;
183         for (i = 0; i < t_pChainsFoundO.size(); i++)
184         {
185                 RainbowChainO* t_pChain = t_pChainsFoundO[i];
186                 int t_nGuessedPos = t_nGuessedPoss[i];          
187                 
188                 CChainWalkContext cwc;
189
190                 uint64 nIndexS = t_pChain->nIndexS;
191                 cwc.SetIndex(nIndexS);
192
193                 int nPos;
194                 for (nPos = 0; nPos < t_nGuessedPos; nPos++)
195                 {
196                         cwc.IndexToPlain();
197                         cwc.PlainToHash();
198                         cwc.HashToIndex(nPos);
199                 }
200                 cwc.IndexToPlain();
201                 cwc.PlainToHash();
202                 if (cwc.CheckHash(t_pHash))
203                 {
204                         t_Hash = cwc.GetHash();
205                         t_Plain = cwc.GetPlain();
206                         t_Binary = cwc.GetBinary();
207
208                         foundHash = true;
209                         break;
210                 }
211                 else {
212                         foundHash = false;
213                         t_nChainWalkStepDueToFalseAlarm += t_nGuessedPos + 1;
214                         t_nFalseAlarm++;
215                 }
216         }
217 }
218
219 bool rcrackiThread::FoundHash()
220 {
221         return foundHash;
222 }
223
224 int rcrackiThread::GetChainWalkStepDueToFalseAlarm()
225 {
226         return t_nChainWalkStepDueToFalseAlarm;
227 }
228
229 int rcrackiThread::GetnFalseAlarm()
230 {
231         return t_nFalseAlarm;
232 }
233
234 string rcrackiThread::GetHash()
235 {
236         return t_Hash;
237 }
238
239 string rcrackiThread::GetPlain()
240 {
241         return t_Plain;
242 }
243
244 string rcrackiThread::GetBinary()
245 {
246         return t_Binary;
247 }