Debian Codegraph @

src/core/ntproba.c

raw symbols SCIP
1 /*
2 *
3 * Ophcrack is a Lanmanager/NTLM hash cracker based on the faster time-memory
4 * trade-off using rainbow tables.
5 *
6 * Created with the help of: Maxime Mueller, Luca Wullschleger, Claude
7 * Hochreutiner, Andreas Huber and Etienne Dysli.
8 *
9 * Copyright (c) 2013 Philippe Oechslin, Cedric Tissieres,
10 * Bertrand Mesot, Pierre Lestringant
11 *
12 * Ophcrack 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.
16 *
17 * Ophcrack 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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Ophcrack; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * This program is released under the GPL with the additional exemption
27 * that compiling, linking, and/or using OpenSSL is allowed.
28 *
29 *
30 *
31 *
32 */
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <string.h>
36 #include <assert.h>
37
38 #include "ntproba.h"
39 #include "proba_info.h"
40 #include "hashToPwd.h"
41 #include "ophel.h"
42 #include "hash.h"
43 #include "ioMarkov.h"
44 #include "ioPattern.h"
45
46 typedef struct {
47 int refcount;
48 proba_info *proba;
49 } ntproba_global_t;
50
51 static ntproba_global_t globalfree = { 0, NULL };
52 static ntproba_global_t global10g = { 0, NULL };
53 static ntproba_global_t global60g = { 0, NULL };
54 /*-------------------------------------------------------------------------*/
55 ntproba_global_t *ntproba_global(table_t *tbl) {
56 switch (tbl->kind) {
57 case ntprobafree: return &globalfree;
58 case ntproba10g: return &global10g;
59 case ntproba60g: return &global60g;
60 default: assert(0);
61 }
62
63 return NULL;
64 }
65 /*-------------------------------------------------------------------------*/
66 int ntproba_setup(void *tbl_) {
67 table_t *tbl = tbl_;
68
69 tbl->ncols = 35000;
70 tbl->offset = 35000;
71 tbl->shared_init = 1;
72
73 switch (tbl->kind) {
74 case ntprobafree:
75 tbl->sizes = ntprobafree_sizes;
76 tbl->inisize = 152698328;
77 break;
78
79 case ntproba10g:
80 tbl->sizes = ntproba10g_sizes;
81 tbl->inisize = 154943200;
82 break;
83
84 case ntproba60g:
85 tbl->sizes = ntproba60g_sizes;
86 tbl->inisize = 151964072;
87 break;
88
89 default:
90 assert(0);
91 }
92
93 tbl->init = ntproba_init;
94 tbl->cleanup = ntproba_cleanup;
95
96 tbl->find = ntproba_find;
97 tbl->check = ntproba_check;
98 tbl->isvalid = ntproba_isvalid;
99
100 tbl->lookup_idx = ntproba_lookup_idx;
101 tbl->lookup_end = ntproba_lookup_end;
102 tbl->lookup_srt = ntproba_lookup_srt;
103
104 return 1;
105 }
106 /*-------------------------------------------------------------------------*/
107 proba_info *ntproba_proba_info_alloc(table_t *tbl) {
108 proba_info *proba = proba_info_alloc();
109
110 switch (tbl->kind) {
111 case ntprobafree:
112 proba->mtype = MARK2;
113 proba->power2 = 39;
114 proba->nbByteM0 = 4;
115 proba->nbByteM = 4;
116 proba->nbByteBin = 3;
117 proba->nbBitIndex = 15;
118 break;
119
120 case ntproba10g:
121 proba->mtype = MARK2;
122 proba->power2 = 43;
123 proba->nbByteM0 = 4;
124 proba->nbByteM = 4;
125 proba->nbByteBin = 3;
126 proba->nbBitIndex = 19;
127 break;
128
129 case ntproba60g:
130 proba->mtype = MARK2;
131 proba->power2 = 46;
132 proba->nbByteM0 = 5;
133 proba->nbByteM = 4;
134 proba->nbByteBin = 3;
135 proba->nbBitIndex = 22;
136 break;
137
138 default:
139 assert(0);
140 }
141
142 if ((!readImprobaStructFromBinFile(&(proba->mis), tbl->path, "markovI.dat"))
143 || (!initMarkovDynProgWrapper(proba->mdpw, proba->mis, proba->mtype))
144 || (!readPatternArrayFromXMLFile(proba->array, tbl->path, "pattern.xml"))
145 || (!checkPatternArrayCrackMarkov(proba->array, proba->mdpw, proba->mis))) {
146
147 ntproba_cleanup(tbl);
148
149 return NULL;
150 }
151
152 return proba;
153 }
154 /*-------------------------------------------------------------------------*/
155 void *ntproba_init(void *tbl_) {
156 table_t *tbl = tbl_;
157 ntproba_global_t *global = ntproba_global(tbl);
158
159 if (!global->proba) {
160 assert(global->refcount == 0);
161 global->proba = ntproba_proba_info_alloc(tbl);
162 global->refcount += global->proba ? 1 : 0;
163 }
164 else {
165 assert(global->refcount > 0);
166 global->refcount += 1;
167 }
168
169 return global->proba;
170 }
171 /*-------------------------------------------------------------------------*/
172 void ntproba_cleanup(void* tbl_) {
173 table_t *tbl = tbl_;
174 ntproba_global_t *global = ntproba_global(tbl);
175
176 assert(global->refcount > 0);
177 assert(global->proba == tbl->param);
178
179 global->refcount -= 1;
180
181 if (global->refcount == 0) {
182 proba_info *proba = global->proba;
183
184 deleteMarkovDynProgWrapper(proba->mdpw);
185 deletePatternArray(proba->array);
186 deleteImprobaStruct(proba->mis);
187 proba_info_free(proba);
188
189 global->proba = NULL;
190 }
191 }
192 /*-------------------------------------------------------------------------*/
193 void ntproba_find(void *hsh_, void *tbl_, void *el_) {
194 hash_t* hsh = hsh_;
195 table_t* tbl = tbl_;
196 ophel_t* el = el_;
197 proba_info *proba = tbl->param;
198
199 uchar_t* hash = hsh->hash;
200 ophstat_t* stat = el->stat;
201 uint64_t end;
202 uint32_t idx = tbl->idx;
203 uint32_t nbColumn = tbl->ncols;
204 uint32_t startColumn = el->col;
205 uint32_t power2 = proba->power2;
206
207 end = hashToIndex(proba->array, proba->mdpw, proba->mis, (unsigned int*)hash, startColumn, nbColumn, power2, idx, nbColumn);
208 stat->hredux += nbColumn-startColumn-1;
209
210 el->prefix = end >> (power2 - proba->nbBitIndex);
211 el->postfix = end & ((uint64_t)0xffffffffffffffff >> (64 + proba->nbBitIndex - power2));
212 }
213 /*-------------------------------------------------------------------------*/
214 int ntproba_lookup_idx(void *hsh_, void *tbl_, void *el_) {
215 table_t* tbl = tbl_;
216 ophel_t* el = el_;
217 uint32_t prefix = el->prefix;
218 ophstat_t* stat = el->stat;
219 proba_info *proba = tbl->param;
220 uint32_t nbByteM = proba->nbByteM;
221
222 stat->prefix++;
223
224 if (tbl->idxmem) {
225 char* mem = tbl->idxmem + prefix*nbByteM;
226 el->low = 0;
227 el->high = 0;
228 memcpy(&(el->low), mem, nbByteM);
229 memcpy(&(el->high), mem+nbByteM, nbByteM);
230 }
231 else {
232 FILE *idxfile = tbl->idxfile;
233 fseeko(idxfile, prefix*nbByteM, SEEK_SET);
234 el->low = 0;
235 el->high = 0;
236 fread(&(el->low), nbByteM, 1, idxfile);
237 fread(&(el->high), nbByteM, 1, idxfile);
238 stat->fseek_idx++;
239 }
240
241 return 1;
242 }
243 /*-------------------------------------------------------------------------*/
244 int ntproba_lookup_end(void *hsh_, void *tbl_, void *el_) {
245 table_t* tbl = tbl_;
246 ophel_t* el = el_;
247 ophstat_t* stat = el->stat;
248 proba_info *proba = tbl->param;
249 uint64_t postfix = el->postfix;
250 uint64_t range = el->high - el->low;
251 uint64_t i;
252 uint32_t nbByteBin = proba->nbByteBin;
253 uint64_t temp;
254 char* mem;
255
256 if (tbl->endmem) {
257 mem = tbl->endmem + el->low*nbByteBin;
258
259 for (i = 0; i < range; i++){
260 temp = 0;
261 memcpy(&temp, mem+i*nbByteBin, nbByteBin);
262 if (temp == postfix){
263 stat->postfix ++;
264 break;
265 }
266 }
267 }
268 else {
269 mem = (char*)malloc(range*nbByteBin);
270 FILE* endfile = tbl->endfile;
271
272 fseeko(endfile, el->low*nbByteBin, SEEK_SET);
273 fread(mem, nbByteBin, range, endfile);
274 stat->fseek_end++;
275
276 for (i = 0; i < range; i++){
277 temp = 0;
278 memcpy(&temp, mem+i*nbByteBin, nbByteBin);
279 if (temp == postfix){
280 stat->postfix ++;
281 break;
282 }
283 }
284 free(mem);
285 }
286
287 el->offset = el->low+i;
288
289 return (i == range) ? 0 : 1;
290 }
291 /*-------------------------------------------------------------------------*/
292 int ntproba_lookup_srt(void *hsh_, void *tbl_, void *el_) {
293 table_t* tbl = tbl_;
294 ophel_t* el = el_;
295 ophstat_t* stat = el->stat;
296 proba_info *proba = tbl->param;
297 uint32_t nbByteM0 = proba->nbByteM0;
298
299 stat->start ++;
300
301 if (tbl->srtmem) {
302 el->start = 0;
303 memcpy(&(el->start), tbl->srtmem + el->offset*nbByteM0, nbByteM0);
304 }
305 else {
306 FILE *srtfile = tbl->srtfile;
307
308 fseeko(srtfile, el->offset*nbByteM0, SEEK_SET);
309 el->start = 0;
310 fread(&(el->start), nbByteM0, 1, srtfile);
311 stat->fseek_srt ++;
312 }
313
314 return 1;
315 }
316 /*-------------------------------------------------------------------------*/
317 int ntproba_check(void *hsh_, void *tbl_, void *el_) {
318 hash_t* hsh = hsh_;
319 table_t* tbl = tbl_;
320 ophel_t* el = el_;
321 proba_info *proba = tbl->param;
322 ophstat_t* stat = el->stat;
323 uint32_t taille;
324 uint32_t idx = tbl->idx;
325 uint32_t nbColumn = tbl->ncols;
326 uint32_t stopColumn = el->col;
327 uint32_t power2 = proba->power2;
328 char pwdUTF[PWD_UTF_SIZE] = {0};
329
330 taille = indexToPwdUTF(proba->array, proba->mdpw, proba->mis, el->start, power2, stopColumn, idx, nbColumn, pwdUTF);
331
332 stat->hredux += stopColumn;
333 stat->falarm_hredux += stopColumn;
334
335 if (checkFoundPwd(pwdUTF, taille, (unsigned int*)(hsh->hash), el->pwd)){
336 stat->match_table++;
337 stat->falarm_hredux = 0;
338 return 1;
339 }
340 else{
341 stat->falarm ++;
342 return 0;
343 }
344 }
345 /*-------------------------------------------------------------------------*/
346 int ntproba_isvalid(void *hsh_, void *tbl_) {
347 hash_t *hsh = hsh_;
348
349 return (hsh->kind == nt)? 1 : 0;
350 }
351 /*-------------------------------------------------------------------------*/