Debian Codegraph @

src/core/table.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) 2008 Philippe Oechslin, Cedric Tissieres, Bertrand Mesot
10 *
11 * Ophcrack is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Ophcrack is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Ophcrack; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * This program is released under the GPL with the additional exemption
26 * that compiling, linking, and/or using OpenSSL is allowed.
27 *
28 *
29 *
30 *
31 */
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #ifndef WIN32
38 #include <sys/mman.h>
39 #endif
40 #include <unistd.h>
41 #include <string.h>
42
43 #include "misc.h"
44 #include "table.h"
45 /*-------------------------------------------------------------------------*/
46 table_t *table_alloc(uint32_t code, char *path, int idx) {
47 table_t *tbl = (table_t*)malloc(sizeof(table_t));
48
49 tbl->kind = table_kind(code);
50 tbl->code = code;
51
52 tbl->path = strdup(path);
53 tbl->name = strdup(table_string(tbl->kind));
54
55 tbl->id = 0;
56 tbl->idx = idx;
57 tbl->cmin = 0;
58 tbl->cmax = 0;
59 tbl->ncols = 0;
60 tbl->offset = 0;
61 tbl->enabled = 0;
62 tbl->active = 0;
63
64 tbl->idxfile = 0;
65 tbl->endfile = 0;
66 tbl->srtfile = 0;
67
68 tbl->shared_init = 0;
69
70 tbl->inisize = 0;
71 tbl->idxsize = 0;
72 tbl->endsize = 0;
73 tbl->srtsize = 0;
74 tbl->sizes = NULL;
75
76 tbl->param = NULL;
77 tbl->idxmem = 0;
78 tbl->endmem = 0;
79 tbl->srtmem = 0;
80
81 tbl->init = NULL;
82 tbl->cleanup = NULL;
83 tbl->find = NULL;
84
85 tbl->lookup_idx = NULL;
86 tbl->lookup_end = NULL;
87 tbl->lookup_srt = NULL;
88
89 tbl->check = NULL;
90 tbl->isvalid = NULL;
91
92 return tbl;
93 }
94 /*-------------------------------------------------------------------------*/
95 void table_free(table_t *tbl) {
96 if (tbl->path) free(tbl->path);
97 if (tbl->name) free(tbl->name);
98
99 if (tbl->srtfile) fclose(tbl->srtfile);
100 if (tbl->idxfile) fclose(tbl->idxfile);
101 if (tbl->endfile) fclose(tbl->endfile);
102
103 #ifndef WIN32
104 if (tbl->srtmem) munmap(tbl->srtmem, tbl->srtsize);
105 if (tbl->idxmem) munmap(tbl->idxmem, tbl->idxsize);
106 if (tbl->endmem) munmap(tbl->endmem, tbl->endsize);
107 #else
108 if (tbl->srtmem) free(tbl->srtmem);
109 if (tbl->idxmem) free(tbl->idxmem);
110 if (tbl->endmem) free(tbl->endmem);
111 #endif
112
113 free(tbl);
114 }
115 /*-------------------------------------------------------------------------*/
116 int table_load(table_t *tbl) {
117 char fname[STR_BUFF_SIZE];
118 char *path = tbl->path;
119 int idx = tbl->idx;
120
121 assert(tbl->srtfile == 0);
122 assert(tbl->idxfile == 0);
123 assert(tbl->endfile == 0);
124
125 snprintf(fname, sizeof(fname), "%s/table%d.start", path, idx);
126 if ((tbl->srtfile = fopen(fname, "rb")) == 0) return 0;
127
128 snprintf(fname, sizeof(fname), "%s/table%d.index", path, idx);
129 if ((tbl->idxfile = fopen(fname, "rb")) == 0) return 0;
130
131 snprintf(fname, sizeof(fname), "%s/table%d.bin", path, idx);
132 if ((tbl->endfile = fopen(fname, "rb")) == 0) return 0;
133
134 table_set_size(tbl);
135
136 return 1;
137 }
138 /*-------------------------------------------------------------------------*/
139 void table_set_size(table_t *tbl) {
140 int fd;
141 struct stat buf;
142
143 fd = fileno(tbl->srtfile);
144 fstat(fd, &buf);
145 tbl->srtsize = (uint64_t)buf.st_size;
146
147 fd = fileno(tbl->idxfile);
148 fstat(fd, &buf);
149 tbl->idxsize = (uint64_t)buf.st_size;
150
151 fd = fileno(tbl->endfile);
152 fstat(fd, &buf);
153 tbl->endsize = (uint64_t)buf.st_size;
154 }
155 /*-------------------------------------------------------------------------*/
156 int table_verify(table_t *tbl) {
157 int idx = tbl->idx;
158 const uint64_t *sizes = tbl->sizes;
159
160 if (tbl->sizes == 0) return 0;
161
162 if (idx < sizes[0]) {
163 if (tbl->idxsize != sizes[1]) return -1;
164 if (tbl->endsize != sizes[2*idx+2]) return -1;
165 if (tbl->srtsize != sizes[2*idx+3]) return -1;
166 } else return -1;
167
168 return 1;
169 }
170 /*-------------------------------------------------------------------------*/
171 char *table_mmap(table_t *tbl, table_preload_t preload) {
172 FILE *file = NULL;
173 uint64_t size = 0;
174
175 switch (preload) {
176 case preload_idx:
177 file = tbl->idxfile;
178 size = tbl->idxsize;
179 break;
180
181 case preload_end:
182 file = tbl->endfile;
183 size = tbl->endsize;
184 break;
185
186 case preload_srt:
187 file = tbl->srtfile;
188 size = tbl->srtsize;
189 break;
190
191 default:
192 assert(0);
193 }
194
195 char *mem = NULL;
196
197 #ifndef WIN32
198 int prot = PROT_READ | PROT_WRITE;
199 int flags = MAP_PRIVATE | MAP_ANON;
200
201 /* We use mmap instead of malloc because malloc might not be able to
202 allocate the large amount of memory we request. */
203
204 mem = (char*)mmap(0, size, prot, flags, -1, 0);
205
206 if (mem == MAP_FAILED) {
207 fprintf(stderr,
208 "Problem while mapping memory for table %s,%d (preload=%d).\n",
209 tbl->name, tbl->idx, preload);
210 return 0;
211 }
212 #else
213 mem = (char*)malloc(size);
214
215 if (mem == 0) {
216 fprintf(stderr,
217 "Malloc failed for table %s,%d (preload=%d).\n",
218 tbl->name, tbl->idx, preload);
219 return 0;
220 }
221 #endif
222
223 rewind(file);
224
225 /* Copy the content of the file at the mapped memory location. */
226
227 if (fread(mem, 1, size, file) != size) {
228 fprintf(stderr,
229 "Problem while preloading table %s,%d (preload=%d).\n",
230 tbl->name, tbl->idx, preload);
231 perror(0);
232 #ifndef WIN32
233 munmap(mem, size);
234 #else
235 free(mem);
236 #endif
237 return 0;
238 }
239
240 return mem;
241 }
242 /*-------------------------------------------------------------------------*/
243 uint64_t table_preload(table_t *tbl, table_preload_t preload) {
244 uint64_t size = 0;
245
246 /* tbl->param is used to determine whether the table has been
247 initialised. Since a table for which tbl->init == NULL is
248 implicitly considered initialised, we store a non NULL pointer
249 into tbl->param to indicate that fact. */
250
251 if ((preload & preload_init) && !tbl->param) {
252 tbl->param = tbl->init ? tbl->init(tbl) : tbl;
253 size += (preload & preload_init) ? tbl->inisize : 0;
254 }
255
256 if ((preload & preload_idx) && !tbl->idxmem) {
257 tbl->idxmem = table_mmap(tbl, preload_idx);
258 size += tbl->idxsize;
259 }
260
261 if ((preload & preload_end) && !tbl->endmem) {
262 tbl->endmem = table_mmap(tbl, preload_end);
263 size += tbl->endsize;
264 }
265
266 if ((preload & preload_srt) && !tbl->srtmem) {
267 tbl->srtmem = table_mmap(tbl, preload_srt);
268 size += tbl->srtsize;
269 }
270
271 return size;
272 }
273 /*-------------------------------------------------------------------------*/
274 char *table_unmap(table_t *tbl, table_preload_t preload) {
275 char *mem = NULL;
276 uint64_t size = 0;
277
278 switch (preload) {
279 case preload_idx:
280 mem = tbl->idxmem;
281 size = tbl->idxsize;
282 break;
283
284 case preload_end:
285 mem = tbl->endmem;
286 size = tbl->endsize;
287 break;
288
289 case preload_srt:
290 mem = tbl->srtmem;
291 size = tbl->srtsize;
292 break;
293
294 default:
295 assert(0);
296 }
297
298 #ifndef WIN32
299 munmap(mem, size);
300 #else
301 free(mem);
302 #endif
303
304 return NULL;
305 }
306 /*-------------------------------------------------------------------------*/
307 uint64_t table_unload(table_t *tbl, table_preload_t preload) {
308 uint64_t size = 0;
309
310 if ((preload & preload_init) && tbl->param) {
311 if (tbl->cleanup) tbl->cleanup(tbl);
312 tbl->param = NULL;
313 size += tbl->inisize;
314 }
315
316 if ((preload & preload_idx) && tbl->idxmem) {
317 tbl->idxmem = table_unmap(tbl, preload_idx);
318 size += tbl->idxsize;
319 }
320
321 if ((preload & preload_end) && tbl->endmem) {
322 tbl->endmem = table_unmap(tbl, preload_end);
323 size += tbl->endsize;
324 }
325
326 if ((preload & preload_srt) && tbl->srtmem) {
327 tbl->srtmem = table_unmap(tbl, preload_srt);
328 size += tbl->srtsize;
329 }
330
331 return size;
332 }
333 /*-------------------------------------------------------------------------*/
334 uint64_t table_preload_size(table_t *tbl, table_preload_t preload) {
335 uint64_t size = 0;
336
337 if (preload & preload_init) size += tbl->inisize;
338 if (preload & preload_idx) size += tbl->idxsize;
339 if (preload & preload_end) size += tbl->endsize;
340 if (preload & preload_srt) size += tbl->srtsize;
341
342 return size;
343 }
344 /*-------------------------------------------------------------------------*/
345 uint64_t table_size(table_t *tbl) {
346 return table_preload_size(tbl, preload_full);
347 }
348 /*-------------------------------------------------------------------------*/
349 int table_open(list_t *tables, const char *dir, const char *tblstr) {
350 /* Get the name of the table. */
351
352 char name[STR_BUFF_SIZE];
353 char path[STR_BUFF_SIZE];
354 char *args;
355
356 strncpy(name, tblstr, sizeof(name));
357 args = strchr(name, ',');
358
359 if (args) {
360 args[0] = 0;
361 ++args;
362 }
363
364 if (strlen(dir) > 0)
365 snprintf(path, sizeof(path), "%s/%s", dir, name);
366 else
367 snprintf(path, sizeof(path), "%s", name);
368
369 /* We need to open the 0th .bin file to find out the table type. */
370
371 char buff[STR_BUFF_SIZE];
372 FILE *file;
373
374 snprintf(buff, sizeof(buff), "%s/table0.bin", path);
375 file = fopen(buff, "rb");
376
377 if (file == 0) return -1;
378
379 /* Read the first 4 bytes of the .bin table. Important: make sure
380 that the value read is in the correct byte order (little endian). */
381
382 uint32_t code;
383
384 fread(&code, sizeof(code), 1, file);
385 code = ftohl(code);
386
387 fclose(file);
388
389 /* Load all the table we find. */
390
391 int ntables = 0;
392 int i;
393
394 for (i=0;; ++i) {
395 table_t *tbl = table_alloc(code, path, i);
396
397 if (table_load(tbl)) {
398 list_add_tail(tables, tbl);
399 ++ntables;
400 } else
401 break;
402 }
403
404
405 /* If no specific tables have been requested, then we enable all of
406 them. */
407
408 list_nd_t *nd = 0;
409 int nenabled = 0;
410
411 if (args == 0)
412 for (i=0, nd = tables->tail; i<ntables; ++i, nd = nd->prev) {
413 table_t *tbl = nd->data;
414 tbl->enabled = 1;
415 ++nenabled;
416 }
417
418 /* If the first argument is a '-', then we do not enable any
419 table. Otherwise, we enable only the ones which have been
420 requested. */
421
422 else if (args[0] != '-'){
423 table_t **ptr = (table_t**)calloc(ntables, sizeof(table_t*));
424 char *p;
425
426 for (i=0, nd = tables->tail; i<ntables; ++i, nd = nd->prev) {
427 table_t *tbl = nd->data;
428
429 assert(tbl->idx >= 0 && tbl->idx < ntables);
430 ptr[tbl->idx] = tbl;
431 }
432
433 while ((p = strsep(&args, ",")) != 0 && strcmp(p, "") != 0) {
434 int idx = atoi(p);
435
436 if (idx >= 0 && idx < ntables && ptr[idx] != 0) {
437 ptr[idx]->enabled = 1;
438 ++nenabled;
439 } else
440 return -(idx+2);
441 }
442
443 free(ptr);
444 }
445
446 return nenabled;
447 }
448 /*-------------------------------------------------------------------------*/
449 table_preload_t table_preload_state(table_t *tbl) {
450 table_preload_t state = preload_none;
451
452 if (tbl->param) state += preload_init;
453 if (tbl->idxmem) state += preload_idx;
454 if (tbl->endmem) state += preload_end;
455 if (tbl->srtmem) state += preload_srt;
456
457 return state;
458 }
459 /*-------------------------------------------------------------------------*/
460 table_kind_t table_kind(uint32_t code) {
461 switch (code) {
462 case 0x3cc21790: return lmalphanum10k;
463 case 0x0fa2031c: return lmalphanum5k;
464 case 0x0e3402df: return lmextended;
465 case 0x103e02a6: return lmgermanv1;
466 case 0x10cb073e: return lmgermanv2;
467 case 0xc7ed7df5: return ntextended;
468 case 0x16501507: return ntdict;
469 case 0x2ee9fa88: return ntnine;
470 case 0x1990057f: return nteight;
471 case 0xdf28cff0: return ntnum;
472 case 0x9542377a: return ntseven;
473 case 0x05080366: return lmflash;
474 case 0x229e1899: return nteightxl;
475 case 0x82506b6e: return ntspecialxl;
476 case 0xbb00950e: return ntprobafree;
477 case 0x47000fdf: return ntproba10g;
478 case 0x2c002335: return ntproba60g;
479 default: return unknown;
480 }
481 }
482 /*-------------------------------------------------------------------------*/
483 const char *table_string(table_kind_t kind) {
484 switch (kind) {
485 case lmalphanum10k: return "XP free small";
486 case lmalphanum5k: return "XP free fast";
487 case lmextended: return "XP special";
488 case lmgermanv1: return "XP german v1";
489 case lmgermanv2: return "XP german v2";
490 case ntextended: return "Vista special";
491 case ntdict: return "Vista free";
492 case ntnine: return "Vista nine";
493 case nteight: return "Vista eight";
494 case ntnum: return "Vista num";
495 case ntseven: return "Vista seven";
496 case lmflash: return "XP flash";
497 case nteightxl: return "Vista eight XL";
498 case ntspecialxl: return "Vista special XL";
499 case ntprobafree: return "Vista probabilistic free";
500 case ntproba10g: return "Vista probabilistic 10G";
501 case ntproba60g: return "Vista probabilistic 60G";
502 case unknown: return "Unknown";
503 }
504
505 return "Unknown";
506 }