Debian Codegraph @

src/core/ophcrack.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 <stdint.h>
35 #include <unistd.h>
36 #include <math.h>
37 #include <assert.h>
38 #include <string.h>
39
40 #include "ophcrack.h"
41 #include "misc.h"
42 #include "table.h"
43 #include "hash.h"
44 #include "list.h"
45 #include "original.h"
46 #include "lmtable.h"
47 #include "lmgerman.h"
48 #include "ntextended.h"
49 #include "ntdict.h"
50 #include "ntnine.h"
51 #include "nteight.h"
52 #include "ntnum.h"
53 #include "ntseven.h"
54 #include "lmflash.h"
55 #include "nteightxl.h"
56 #include "ntspecialxl.h"
57 #include "ntproba.h"
58 #include "message.h"
59 /*-------------------------------------------------------------------------*/
60 ophcrack_t *ophcrack_alloc(int nthreads, arg_t *arg) {
61 ophcrack_t *crack = (ophcrack_t*)malloc(sizeof(ophcrack_t));
62
63 crack->sched = scheduler_alloc(nthreads);
64 crack->tables = list_alloc();
65
66 crack->hashes = list_alloc();
67 crack->active = list_alloc();
68 crack->enabled = list_alloc();
69 crack->remaining = list_alloc();
70 crack->hnd = 0;
71 crack->stat = ophstat_alloc();
72 crack->maxtid = 0;
73 crack->maxhid = 0;
74 crack->maxtkind = unknown;
75 crack->npwds_total = 0;
76 crack->npwds_found = 0;
77 crack->find_freeram = find_freeram;
78 crack->freeram = 0;
79 crack->arg = arg;
80
81 return crack;
82 }
83 /*-------------------------------------------------------------------------*/
84 void ophcrack_free(ophcrack_t *crack) {
85 scheduler_free(crack->sched);
86
87 list_free(crack->tables);
88 list_free(crack->hashes);
89 list_free(crack->active);
90 list_free(crack->enabled);
91 list_free(crack->remaining);
92
93 ophstat_free(crack->stat);
94
95 free(crack);
96 }
97 /*-------------------------------------------------------------------------*/
98 void ophcrack_start(ophcrack_t *crack) {
99 crack->freeram = crack->find_freeram();
100 scheduler_start(crack->sched, ophcrack_run, crack);
101 }
102 /*-------------------------------------------------------------------------*/
103 void ophcrack_stop(ophcrack_t *crack) {
104 scheduler_stop(crack->sched);
105 scheduler_wait(crack->sched);
106 }
107 /*-------------------------------------------------------------------------*/
108 int ophcrack_setup_table(table_t *tbl) {
109
110 /* Setup the table. We set the kind variable ourselves and
111 then call the appropriate setup function. */
112
113 tbl->kind = table_kind(tbl->code);
114 int ret = 0;
115
116 switch (tbl->kind) {
117 case lmalphanum10k:
118 ret = lmtable_setup(tbl);
119 break;
120
121 case lmalphanum5k:
122 ret = lmtable_setup(tbl);
123 break;
124
125 case lmextended:
126 ret = lmtable_setup(tbl);
127 break;
128
129 case lmgermanv1:
130 ret = lmgerman_setup(tbl);
131 break;
132
133 case lmgermanv2:
134 ret = lmgerman_setup(tbl);
135 break;
136
137 case ntextended:
138 ret = ntextended_setup(tbl);
139 break;
140
141 case ntdict:
142 ret = ntdict_setup(tbl);
143 break;
144
145 case ntnine:
146 ret = ntnine_setup(tbl);
147 break;
148
149 case nteight:
150 ret = nteight_setup(tbl);
151 break;
152
153 case ntnum:
154 ret = ntnum_setup(tbl);
155 break;
156
157 case ntseven:
158 ret = ntseven_setup(tbl);
159 break;
160
161 case lmflash:
162 ret = lmflash_setup(tbl);
163 break;
164
165 case nteightxl:
166 ret = nteightxl_setup(tbl);
167 break;
168
169 case ntspecialxl:
170 ret = ntspecialxl_setup(tbl);
171 break;
172
173 case ntprobafree:
174 ret = ntproba_setup(tbl);
175 break;
176
177 case ntproba10g:
178 ret = ntproba_setup(tbl);
179 break;
180
181 case ntproba60g:
182 ret = ntproba_setup(tbl);
183 break;
184
185 case unknown:
186 ret = -1;
187 break;
188 }
189
190 return ret;
191 }
192 /*-------------------------------------------------------------------------*/
193 void ophcrack_add_table(ophcrack_t *crack, table_t *tbl) {
194 scheduler_t *sched = crack->sched;
195 pthread_mutex_t *mutex = sched->mutex;
196
197 crack->maxtkind = MY_MAX(crack->maxtkind, tbl->kind+1);
198
199 tbl->id = crack->maxtid++;
200 tbl->cmin = tbl->ncols;
201 tbl->cmax = tbl->ncols;
202
203 pthread_mutex_lock(mutex);
204 list_add_tail(crack->tables, tbl);
205 pthread_mutex_unlock(mutex);
206 }
207 /*-------------------------------------------------------------------------*/
208 void ophcrack_add_hash(ophcrack_t *crack, hash_t *hsh) {
209 scheduler_t *sched = crack->sched;
210 pthread_mutex_t *mutex = sched->mutex;
211
212 pthread_mutex_lock(mutex);
213
214 /* Add the hash to the list of hash we want to crack and update the
215 hash id. */
216
217 list_add_tail(crack->hashes, hsh);
218 crack->maxhid = MY_MAX(crack->maxhid, hsh->id+1);
219
220 /* Since we consider a group of LM and NT hashes as a single
221 password, we only increase the password counter if the hash is the
222 1st LM hash or if the hash is not connected to any other hash. */
223
224 if (hsh->kind == lm1)
225 ++crack->npwds_total;
226
227 else if (hsh->lmhsh1 == 0 && hsh->lmhsh2 == 0)
228 ++crack->npwds_total;
229
230 pthread_mutex_unlock(mutex);
231
232 /* If the hash has already been cracked, then we mark it as not done
233 so that we go through the usual procedure which is carried out
234 when a password is found. */
235
236 if (hsh->done > 0) {
237 hsh->done = 0;
238
239 /* To avoid counting many times the same password as found, we
240 temporary set the done variable of the connected LM and/or NT
241 hash to zero. But we do this only if we are not dealing with a
242 1st LM hash because we want to count the password at least
243 once. */
244
245 if (hsh->kind != lm1 && hsh->lmhsh1) {
246 int tmp = hsh->lmhsh1->done;
247
248 hsh->lmhsh1->done = 0;
249 ophcrack_found(crack, hsh, 0, 0, hsh->pwd);
250 hsh->lmhsh1->done = tmp;
251 }
252
253 /* If we have a 1st LM hash or a hash which is not connected to
254 another hash, then we can safely call the _found function. */
255
256 else
257 ophcrack_found(crack, hsh, 0, 0, hsh->pwd);
258 }
259
260 else if (hsh->done < 0) {
261 hsh->done = 0;
262 ophcrack_notfound(crack, hsh);
263 }
264 }
265 /*-------------------------------------------------------------------------*/
266 void ophcrack_associate(ophcrack_t *crack, hash_t *hsh, table_t *tbl) {
267 scheduler_t *sched = crack->sched;
268 pthread_mutex_t *mutex = sched->mutex;
269
270 pthread_mutex_lock(mutex);
271 if (tbl->isvalid(hsh, tbl)) hash_add_table(hsh, tbl);
272 pthread_mutex_unlock(mutex);
273 }
274 /*-------------------------------------------------------------------------*/
275 void ophcrack_reset(ophcrack_t *crack, int kind_mask, int remove) {
276 static int work_mask = find | lookup_idx | lookup_end | lookup_srt | check;
277
278 scheduler_t *sched = crack->sched;
279 pthread_mutex_t *mutex = sched->mutex;
280
281 ophtask_t *task = 0;
282 int i;
283
284 pthread_mutex_lock(mutex);
285
286 for (i=0; i<NQUEUES; ++i) {
287 list_t *queue = sched->queue[i];
288 int size = queue->size;
289 int j;
290
291 for (j=0; j<size; ++j) {
292 task = list_rem_head(queue);
293
294 if (task->kind & work_mask) {
295 ophwork_t *work = task->data;
296 htbl_t *htbl = work->htbl;
297
298 if (htbl)
299 htbl->covered = MY_MAX(htbl->covered, work->cmax+1);
300 }
301
302 if (remove && task->kind & kind_mask) {
303 ophtask_free(task);
304 --sched->ntasks;
305 } else
306 list_add_tail(queue, task);
307 }
308 }
309
310 pthread_mutex_unlock(mutex);
311 }
312 /*-------------------------------------------------------------------------*/
313 void *ophcrack_run(void *arg) {
314 static int gid = 0;
315 static int wait = 0;
316
317 int myid = gid++;
318
319 ophcrack_t *crack = (ophcrack_t*)arg;
320 scheduler_t *sched = crack->sched;
321 pthread_mutex_t *mutex = sched->mutex;
322
323 /* Make sure we accept cancel requests in a deferred fashion. */
324
325 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
326 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 0);
327
328 /* Wait and process. */
329
330 list_t *tasks = list_alloc();
331
332 while (1) {
333 ophtask_t *task = scheduler_get(sched, myid);
334 ophkind_t kind = task->kind;
335
336 /* Tables preloading. */
337
338 if (kind == preload_all)
339 task = ophcrack_preload_all(crack, task, myid);
340
341 else if (kind == preload_one)
342 task = ophcrack_preload_one(crack, task, myid);
343
344 /* Tables unloading. */
345
346 else if (kind == unload)
347 task = ophcrack_unload(crack, task, myid);
348
349 /* Brute force. */
350
351 else if (kind == bforce_all ||
352 kind == bforce_nt ||
353 kind == bforce_lm ||
354 kind == resolve_nt) {
355
356 ophbforce_t *force = task->data;
357
358 /* Check whether all the hashes have been cracked. */
359
360 int nhashes = force->nhashes;
361 int n = 0;
362
363 if (force->hashes)
364 for (n=0; n<nhashes; ++n)
365 if (force->hashes[n]->done == 0) break;
366
367 /* If not all hashes have been cracked, then it is worth continuing the
368 brute force. */
369
370 if (n < nhashes)
371 switch (kind) {
372 case bforce_all:
373 task = ophcrack_bforce_all(crack, task, myid);
374 break;
375
376 case bforce_nt:
377 task = ophcrack_bforce_nt(crack, task, myid);
378 break;
379
380 case bforce_lm:
381 task = ophcrack_bforce_lm(crack, task, myid);
382 break;
383
384 case resolve_nt:
385 task = ophcrack_resolve_nt(crack, task, myid);
386 break;
387
388 default:
389 break;
390 }
391 }
392
393 /* Find, lookup, check. */
394
395 else {
396 ophwork_t *work = task->data;
397
398 if (work->hsh && work->hsh->done == 0) {
399
400 /* If the password has not been found yet, then we proceed with the
401 task. */
402
403 switch (kind) {
404 case find:
405 task = ophcrack_find(crack, task, myid);
406 break;
407
408 case lookup_idx:
409 task = ophcrack_lookup_idx(crack, task, myid);
410 break;
411
412 case lookup_end:
413 task = ophcrack_lookup_end(crack, task, myid);
414 break;
415
416 case lookup_srt:
417 task = ophcrack_lookup_srt(crack, task, myid);
418 break;
419
420 case check:
421 task = ophcrack_check(crack, task, myid);
422 break;
423
424 default:
425 break;
426 }
427
428 /* Store the mimimum column index which we have considered so far for
429 the given hash and the given table. */
430
431 if (task) {
432 htbl_t *htbl = work->htbl;
433 int cmin = work->cmin;
434
435 pthread_mutex_lock(mutex);
436
437 if (work->hsh->done != 0)
438 htbl->covered = 0;
439 else
440 htbl->covered = MY_MIN(htbl->covered, cmin);
441
442 pthread_mutex_unlock(mutex);
443 }
444 }
445 }
446
447 /* Beginning of mutexed region. */
448
449 pthread_mutex_lock(mutex);
450
451 /* Collect the statistics and delete the task. When the task we delete is a
452 brute force task then we also update the status of the brute force so
453 that things are displayed correctly. */
454
455 if (task) {
456 ophstat_add(crack->stat, task->stat);
457
458 if (task->kind & (bforce_lm + bforce_nt)) {
459 ophbforce_t *force = task->data;
460 uint64_t count = force->count;
461
462 message_bforce(1, count);
463 }
464
465 ophtask_free(task);
466 }
467
468 /* Check whether we can schedule other find tasks. */
469
470 int disk_size = sched->queue[disk]->size;
471 int mdqueue = crack->arg->mdqueue;
472 int ntasks = kind == find ? 1 : 0;
473
474 /* If we have been waiting for the disk queue to become smaller and it is
475 the case, then we schedule the find tasks that we have not been able to
476 schedule before. */
477
478 if (wait > 0 && disk_size <= mdqueue / 2) {
479 ntasks += wait;
480 wait = 0;
481 }
482
483 /* Allocate the requested number of find tasks. If a task cannot be
484 allocated, we will have to do it later, presumably when the size of the
485 disk queue becomes acceptable again. */
486
487 for (int i=0; i<ntasks; ++i) {
488 ophtask_t *task = ophcrack_next(crack);
489
490 if (task)
491 list_add_tail(tasks, task);
492 else
493 ++wait;
494 }
495
496 /* End of mutexed region. */
497
498 pthread_mutex_unlock(mutex);
499
500 /* ... */
501
502 ntasks = tasks->size;
503
504 for (int i=0; i<ntasks; ++i) {
505 ophtask_t *task = (ophtask_t*)list_rem_head(tasks);
506 scheduler_add(sched, task, low);
507 }
508
509 assert(tasks->size == 0);
510
511 /* ... */
512
513 if (kind == find && ntasks == 0 && wait == 0)
514 message_done(find);
515
516 /* Tell the scheduler we are done with this task and send a 'done' message
517 if I am the last one to finish my job. */
518
519 ntasks = scheduler_done(sched, myid);
520 if (ntasks == 0) message_done(all);
521 }
522
523 list_free(tasks);
524
525 return 0;
526 }
527 /*-------------------------------------------------------------------------*/
528 ophtask_t *ophcrack_find(ophcrack_t *crack, ophtask_t *task, int id) {
529 scheduler_t *sched = crack->sched;
530 pthread_mutex_t *mutex = sched->mutex;
531
532 ophwork_t *work = task->data;
533
534 hash_t *hsh = work->hsh;
535 table_t *tbl = work->htbl->tbl;
536
537 int cmin = work->cmin;
538 int cmax = work->cmax;
539 int size = cmax-cmin+1;
540
541 arg_t *arg = crack->arg;
542
543 if (arg->verbose)
544 message_work(hsh, tbl, find, cmin, cmax);
545
546 /* Convert the task to a lookup_idx task. */
547
548 ophtask_convert(task, find, lookup_idx);
549
550 /* Try to find the pwd by starting from each column. */
551
552 int i;
553 ophel_t **tosearch = work->tosearch;
554
555 for (i=0; i<size; ++i) {
556 if (arg->debug) {
557 pthread_mutex_lock(mutex);
558
559 int hid = hsh->id;
560 int idx = tbl->idx;
561 const char *name = tbl->name;
562
563 fprintf(arg->lfile, "find: ");
564
565 switch (hsh->kind) {
566 case lm1: fprintf(arg->lfile, "1st LM hash "); break;
567 case lm2: fprintf(arg->lfile, "2nd LM hash "); break;
568 case nt: fprintf(arg->lfile, "NT hash "); break;
569 }
570
571 fprintf(arg->lfile, "#%d in table %s,%d ", hid, name, idx);
572 fprintf(arg->lfile, "at column %d ... ", cmax-i);
573 }
574
575 assert(tbl->param != NULL);
576
577 tosearch[i]->col = cmax-i;
578 tbl->find(hsh, tbl, tosearch[i]);
579
580 if (arg->debug) {
581 uint32_t prefix = tosearch[i]->prefix;
582 uint16_t postfix = tosearch[i]->postfix;
583
584 fprintf(arg->lfile,
585 "prefix = %u, postfix = %u.\n",
586 (unsigned int)prefix,
587 (unsigned int)postfix);
588
589 pthread_mutex_unlock(mutex);
590 }
591 }
592
593 /* If the .index file has been preloaded, then we proceed with the
594 corresponding lookup task. */
595
596 if (tbl->idxmem)
597 task = ophcrack_lookup_idx(crack, task, id);
598
599 /* Otherwise, we add the lookup task to the scheduler. */
600
601 else {
602 scheduler_add(sched, task, disk);
603 task = 0;
604 }
605
606 return task;
607 }
608 /*-------------------------------------------------------------------------*/
609 ophtask_t *ophcrack_lookup_idx(ophcrack_t *crack, ophtask_t *task, int id) {
610 scheduler_t *sched = crack->sched;
611 pthread_mutex_t *mutex = sched->mutex;
612
613 ophwork_t *work = task->data;
614
615 hash_t *hsh = work->hsh;
616 table_t *tbl = work->htbl->tbl;
617
618 int cmin = work->cmin;
619 int cmax = work->cmax;
620 int size = cmax-cmin+1;
621
622 arg_t *arg = crack->arg;
623
624 if (arg->verbose)
625 message_work(hsh, tbl, lookup_idx, cmin, cmax);
626
627 /* Convert the task to a lookup_end task. */
628
629 task->kind = lookup_end;
630
631 /* If we access the disk, then it is better to query the .index file
632 by increasing order of the index. */
633
634 ophel_t **tosearch = work->tosearch;
635
636 if (!tbl->idxmem)
637 qsort(tosearch, size, sizeof(ophel_t*), ophcrack_pwd_cmp);
638
639 /* Find the lookup range of each prefix. */
640
641 int i;
642
643 for (i=0; i<size; ++i) {
644 if (arg->debug) {
645 pthread_mutex_lock(mutex);
646
647 int hid = hsh->id;
648 int idx = tbl->idx;
649 const char *name = tbl->name;
650
651 fprintf(arg->lfile, "lookup_idx: ");
652
653 switch (hsh->kind) {
654 case lm1: fprintf(arg->lfile, "1st LM hash "); break;
655 case lm2: fprintf(arg->lfile, "2nd LM hash "); break;
656 case nt: fprintf(arg->lfile, "NT hash "); break;
657 }
658
659 uint32_t prefix = tosearch[i]->prefix;
660 uint16_t postfix = tosearch[i]->postfix;
661
662 fprintf(arg->lfile, "#%d in table %s,%d ", hid, name, idx);
663 fprintf(arg->lfile, "at column %d, ", cmax-i);
664 fprintf(arg->lfile, "with prefix = %u ", prefix);
665 fprintf(arg->lfile, "and postfix = %u ... ", (unsigned int)postfix);
666 }
667
668 assert(tbl->param != NULL);
669 tbl->lookup_idx(hsh, tbl, tosearch[i]);
670
671 if (arg->debug) {
672 uint64_t low = tosearch[i]->low;
673 uint64_t high = tosearch[i]->high;
674
675 fprintf(arg->lfile, "low = %llu, high = %llu.\n", (long long unsigned)low, (long long unsigned)high);
676 pthread_mutex_unlock(mutex);
677 }
678 }
679
680 /* If we have been searching in memory (on disk) and we can continue
681 do so, we go ahead. */
682
683 int both_in_mem = tbl->idxmem && tbl->endmem;
684 int both_on_disk = !tbl->idxmem && !tbl->endmem;
685
686 if (both_in_mem || both_on_disk)
687 task = ophcrack_lookup_end(crack, task, id);
688
689 /* Otherwise, we add a disk (memory) task to the scheduler. */
690
691 else {
692 prty_t priority = tbl->endmem ? low : disk;
693 scheduler_add(sched, task, priority);
694 task = 0;
695 }
696
697 return task;
698 }
699 /*-------------------------------------------------------------------------*/
700 ophtask_t *ophcrack_lookup_end(ophcrack_t *crack, ophtask_t *task, int id) {
701 scheduler_t *sched = crack->sched;
702 pthread_mutex_t *mutex = sched->mutex;
703
704 ophwork_t *work = task->data;
705
706 hash_t *hsh = work->hsh;
707 table_t *tbl = work->htbl->tbl;
708
709 int cmin = work->cmin;
710 int cmax = work->cmax;
711 int size = cmax-cmin+1;
712
713 arg_t *arg = crack->arg;
714
715 if (arg->verbose)
716 message_work(hsh, tbl, lookup_end, cmin, cmax);
717
718 /* Convert the task to a lookup_start task. */
719
720 task->kind = lookup_srt;
721
722 /* Find the chain ends. */
723
724 int i;
725 int count = size;
726 ophel_t **tosearch = work->tosearch;
727
728 for (i=0; i<size; ++i) {
729 if (arg->debug) {
730 pthread_mutex_lock(mutex);
731
732 int hid = hsh->id;
733 int idx = tbl->idx;
734 const char *name = tbl->name;
735
736 fprintf(arg->lfile, "lookup_end: ");
737
738 switch (hsh->kind) {
739 case lm1: fprintf(arg->lfile, "1st LM hash "); break;
740 case lm2: fprintf(arg->lfile, "2nd LM hash "); break;
741 case nt: fprintf(arg->lfile, "NT hash "); break;
742 }
743
744 uint32_t prefix = tosearch[i]->prefix;
745 uint16_t postfix = tosearch[i]->postfix;
746 uint64_t low = tosearch[i]->low;
747 uint64_t high = tosearch[i]->high;
748
749 fprintf(arg->lfile, "#%d in table %s,%d ", hid, name, idx);
750 fprintf(arg->lfile, "at column %d, ", cmax-i);
751 fprintf(arg->lfile, "with prefix = %u, ", prefix);
752 fprintf(arg->lfile, "postfix = %u, ", (unsigned int)postfix);
753 fprintf(arg->lfile, "low = %llu and high = %llu ... ", (long long unsigned)low, (long long unsigned)high);
754 }
755
756 assert(tbl->param != NULL);
757 int ret = tbl->lookup_end(hsh, tbl, tosearch[i]);
758
759 /* Accumulate the statistics before freeing the element. */
760
761 if (ret == 0) {
762 ophstat_add(task->stat, tosearch[i]->stat);
763 ophel_free(tosearch[i]);
764
765 tosearch[i] = 0;
766 --count;
767 }
768
769 if (arg->debug) {
770 if (ret == 0)
771 fprintf(arg->lfile, "no offset found.\n");
772 else
773 fprintf(arg->lfile, "offset = %llu.\n", (long long unsigned int)tosearch[i]->offset);
774
775 pthread_mutex_unlock(mutex);
776 }
777 }
778
779 /* If tosearch is not empty, then we sort the chain starts by
780 increasing order of the column index. Otherwise we delete the
781 task. */
782
783 if (count > 0)
784 qsort(tosearch, size, sizeof(ophel_t*), ophcrack_col_cmp);
785 else
786 return task;
787
788 /* If we have been searching in memory (on disk) and we can continue
789 do so, we go ahead. */
790
791 int both_in_mem = tbl->endmem && tbl->srtmem;
792 int both_on_disk = !tbl->endmem && !tbl->srtmem;
793
794 if (both_in_mem || both_on_disk)
795 task = ophcrack_lookup_srt(crack, task, id);
796
797 /* Otherwise, we add a disk (memory) task to the scheduler. */
798
799 else {
800 prty_t priority = tbl->srtmem ? low : disk;
801 scheduler_add(sched, task, priority);
802 task = 0;
803 }
804
805 return task;
806 }
807 /*-------------------------------------------------------------------------*/
808 ophtask_t *ophcrack_lookup_srt(ophcrack_t *crack, ophtask_t *task, int id) {
809 scheduler_t *sched = crack->sched;
810 pthread_mutex_t *mutex = sched->mutex;
811
812 ophwork_t *work = task->data;
813
814 hash_t *hsh = work->hsh;
815 table_t *tbl = work->htbl->tbl;
816
817 int cmin = work->cmin;
818 int cmax = work->cmax;
819 int size = cmax-cmin+1;
820
821 arg_t *arg = crack->arg;
822
823 if (arg->verbose)
824 message_work(hsh, tbl, lookup_srt, cmin, cmax);
825
826 /* Convert the task to a check task. */
827
828 task->kind = check;
829
830 /* Find the chain starts. */
831
832 int i;
833 ophel_t **tosearch = work->tosearch;
834
835 for (i=0; i<size; ++i) {
836 if (tosearch[i] == 0) break;
837
838 if (arg->debug) {
839 pthread_mutex_lock(mutex);
840
841 int hid = hsh->id;
842 int idx = tbl->idx;
843 const char *name = tbl->name;
844
845 fprintf(arg->lfile, "lookup_srt: ");
846
847 switch (hsh->kind) {
848 case lm1: fprintf(arg->lfile, "1st LM hash "); break;
849 case lm2: fprintf(arg->lfile, "2nd LM hash "); break;
850 case nt: fprintf(arg->lfile, "NT hash "); break;
851 }
852
853 uint32_t prefix = tosearch[i]->prefix;
854 uint16_t postfix = tosearch[i]->postfix;
855 uint64_t low = tosearch[i]->low;
856 uint64_t high = tosearch[i]->high;
857 uint64_t offset = tosearch[i]->offset;
858
859 fprintf(arg->lfile, "#%d in table %s,%d ", hid, name, idx);
860 fprintf(arg->lfile, "at column %d, ", cmax-i);
861 fprintf(arg->lfile, "with prefix = %u, ", prefix);
862 fprintf(arg->lfile, "postfix = %u, ", (unsigned int)postfix);
863 fprintf(arg->lfile, "low = %llu, high = %llu ", (long long unsigned)low, (long long unsigned)high);
864 fprintf(arg->lfile, "and offset = %llu ... ", (long long unsigned int)offset);
865 }
866
867 assert(tbl->param != NULL);
868 tbl->lookup_srt(hsh, tbl, tosearch[i]);
869
870 if (arg->debug) {
871 fprintf(arg->lfile, "start = ");
872 fprintf_hex(arg->lfile, tosearch[i]->pwd, 16);
873 fprintf(arg->lfile, "\n.");
874
875 pthread_mutex_unlock(mutex);
876 }
877 }
878
879 /* Add the task to the scheduler with a high priority. */
880
881 scheduler_add(sched, task, high);
882
883 return 0;
884 }
885 /*-------------------------------------------------------------------------*/
886 ophtask_t *ophcrack_check(ophcrack_t *crack, ophtask_t *task, int id) {
887 scheduler_t *sched = crack->sched;
888 pthread_mutex_t *mutex = sched->mutex;
889
890 ophwork_t *work = task->data;
891
892 hash_t *hsh = work->hsh;
893 htbl_t *htbl = work->htbl;
894 table_t *tbl = htbl->tbl;
895
896 int i;
897 int cmin = work->cmin;
898 int cmax = work->cmax;
899 int size = cmax-cmin+1;
900
901 arg_t *arg = crack->arg;
902
903 if (arg->verbose > 0)
904 message_work(hsh, tbl, check, cmin, cmax);
905
906 ophel_t **tosearch = work->tosearch;
907
908 for (i=0; i<size; ++i) {
909 if (tosearch[i] == 0) continue;
910
911 if (arg->debug) {
912 pthread_mutex_lock(mutex);
913
914 int hid = hsh->id;
915 int idx = tbl->idx;
916 const char *name = tbl->name;
917
918 fprintf(arg->lfile, "check: ");
919
920 switch (hsh->kind) {
921 case lm1: fprintf(arg->lfile, "1st LM hash "); break;
922 case lm2: fprintf(arg->lfile, "2nd LM hash "); break;
923 case nt: fprintf(arg->lfile, "NT hash "); break;
924 }
925
926 uint32_t prefix = tosearch[i]->prefix;
927 uint16_t postfix = tosearch[i]->postfix;
928 uint64_t low = tosearch[i]->low;
929 uint64_t high = tosearch[i]->high;
930 uint64_t offset = tosearch[i]->offset;
931
932 fprintf(arg->lfile, "#%d in table %s,%d ", hid, name, idx);
933 fprintf(arg->lfile, "at column %d, ", cmax-i);
934 fprintf(arg->lfile, "with prefix = %u, ", prefix);
935 fprintf(arg->lfile, "postfix = %u, ", (unsigned int)postfix);
936 fprintf(arg->lfile, "low = %llu, high = %llu, ", (long long unsigned)low, (long long unsigned)high);
937 fprintf(arg->lfile, "offset = %llu ", (long long unsigned int)offset);
938 fprintf(arg->lfile, "and start = ");
939 fprintf_hex(arg->lfile, tosearch[i]->pwd, 16);
940 fprintf(arg->lfile, " ... ");
941 }
942
943 /* Check if the correct password has been found. */
944
945 assert(tbl->param != NULL);
946
947 int col = tosearch[i]->col;
948 char *pwd = tosearch[i]->pwd;
949 int found = tbl->check(hsh, tbl, tosearch[i]);
950
951 if (found)
952 ophcrack_found(crack, hsh, tbl, col, pwd);
953
954 ophstat_add(task->stat, tosearch[i]->stat);
955
956 if (arg->debug) {
957 fprintf(arg->lfile, "found.\n");
958 pthread_mutex_unlock(mutex);
959 }
960
961 /* If the password has been found, then we do not look any further. */
962
963 if (found) break;
964 }
965
966 return task;
967 }
968 /*-------------------------------------------------------------------------*/
969 ophtask_t *ophcrack_bforce_all(ophcrack_t *crack, ophtask_t *task, int id) {
970 scheduler_t *sched = crack->sched;
971
972 ophbforce_t *force = task->data;
973 hash_t **hashes = force->hashes;
974
975 /* Create a task for each type of hash. */
976
977 list_t *hashes_lm = list_alloc();
978 list_t *hashes_nt = list_alloc();
979
980 int nhashes = force->nhashes;
981 int n;
982
983 for (n=0; n<nhashes; ++n) {
984 hash_t *hsh = hashes[n];
985 hash_kind_t kind = hsh->kind;
986
987 if (hsh->done) continue;
988
989 switch (kind) {
990 case lm1:
991 /* Brute force a 1st LM hash only if the corresponding 2nd LM
992 hash is empty. */
993
994 if (hsh->lmhsh2->done && hsh->lmhsh2->pwd[0] == 0)
995 list_add_tail(hashes_lm, hsh);
996 break;
997
998 case lm2:
999 list_add_tail(hashes_lm, hsh);
1000 break;
1001
1002 case nt:
1003 /* Brute force a NT hash only if it is not connected to a LM
1004 hash. */
1005
1006 if (!hsh->lmhsh1 || !hsh->lmhsh2)
1007 list_add_tail(hashes_nt, hsh);
1008 break;
1009
1010 default:
1011 break;
1012 }
1013 }
1014
1015 /* Schedule the task only if there is something to do. */
1016
1017 int maxlen = crack->arg->bforce_len;
1018 list_nd_t *nd;
1019 int i;
1020
1021 /* LM hashes. */
1022
1023 int size_lm = sizeof(extended_chars);
1024 uint64_t count_lm = 0;
1025
1026 if (hashes_lm->size > 0) {
1027 ophtask_t *task = ophtask_alloc(bforce_lm);
1028 ophbforce_t *force = task->data;
1029
1030 int nhashes = hashes_lm->size;
1031
1032 force->nhashes = nhashes;
1033 force->hashes = (hash_t**)malloc(nhashes*sizeof(hash_t*));
1034
1035 for (nd=hashes_lm->head, n=0; nd!=0; nd=nd->next, ++n)
1036 force->hashes[n] = nd->data;
1037
1038 for (i=1; i<=maxlen; ++i)
1039 count_lm += pow(size_lm, i);
1040
1041 force->count = count_lm;
1042 scheduler_add(sched, task, low);
1043 }
1044
1045 /* NT hashes. */
1046
1047 int size_nt = sizeof(ntextended_ext95);
1048 uint64_t count_nt = 0;
1049
1050 if (hashes_nt->size > 0) {
1051 ophtask_t *task = ophtask_alloc(bforce_nt);
1052 ophbforce_t *force = task->data;
1053
1054 int nhashes = hashes_nt->size;
1055
1056 force->nhashes = nhashes;
1057 force->hashes = (hash_t**)malloc(nhashes*sizeof(hash_t*));
1058
1059 for (nd=hashes_nt->head, n=0; nd!=0; nd=nd->next, ++n)
1060 force->hashes[n] = nd->data;
1061
1062 for (i=1; i<=maxlen; ++i)
1063 count_nt += pow(size_nt, i);
1064
1065 force->count = count_nt;
1066 scheduler_add(sched, task, low);
1067 }
1068
1069 /* If there is nothing to do, then we direclty send a done message
1070 to indicate that the brute force is done. */
1071
1072 int count = count_lm + count_nt;
1073
1074 if (count == 0) {
1075 message_bforce(0, 1);
1076 message_bforce(1, 1);
1077 }
1078
1079 /* Otherwise, we send the number of passwords we will enumerate. */
1080
1081 else
1082 message_bforce(0, count);
1083
1084 list_free(hashes_lm);
1085 list_free(hashes_nt);
1086
1087 return task;
1088 }
1089 /*-------------------------------------------------------------------------*/
1090 ophtask_t *ophcrack_resolve_nt(ophcrack_t *crack, ophtask_t *task, int id) {
1091 ophbforce_t *force = task->data;
1092 int mode = RESOLVE_DO_NOT_INSIST;
1093
1094 hash_t **hashes = force->hashes;
1095 int nhashes = force->nhashes;
1096 int n;
1097
1098 for (n=0; n<nhashes; ++n) {
1099 hash_t *nthsh = hashes[n];
1100 hash_t *lmhsh1 = nthsh->lmhsh1;
1101 hash_t *lmhsh2 = nthsh->lmhsh2;
1102
1103 assert(lmhsh1 && lmhsh2);
1104 assert(lmhsh1->done && lmhsh2->done);
1105
1106 char *lmpwd1 = lmhsh1->pwd;
1107 char *lmpwd2 = lmhsh2->pwd;
1108 char *nthash = (char*)nthsh->hash;
1109 char ntpwd[16] = {0};
1110
1111 if (resolve_nt_hash(lmpwd1, lmpwd2, nthash, (uchar_t*)ntpwd, mode))
1112 ophcrack_found(crack, nthsh, 0, 0, ntpwd);
1113 }
1114
1115 return task;
1116 }
1117 /*-------------------------------------------------------------------------*/
1118 ophtask_t *ophcrack_bforce_nt(ophcrack_t *crack, ophtask_t *task, int id) {
1119 scheduler_t *sched = crack->sched;
1120 pthread_mutex_t *mutex = sched->mutex;
1121
1122 ophbforce_t *force = task->data;
1123
1124 /* If they do not already exist, create two arrays which will hold
1125 the hash value and whether the password has been found. */
1126
1127 hash_t **hashes = force->hashes;
1128 int nhashes = force->nhashes;
1129 int *bloom = force->bloom;
1130 int n;
1131 int bloom_byte, bloom_bit;
1132
1133 if (force->hash == 0) {
1134 assert(force->found == 0);
1135
1136 force->hash = (uchar_t**)calloc(nhashes, sizeof(uchar_t*));
1137 force->found = (int*)calloc(nhashes, sizeof(int));
1138
1139 for (n=0; n<nhashes; ++n) {
1140 hash_t *hsh = hashes[n];
1141
1142 force->hash[n] = (uchar_t*)malloc(16*sizeof(uchar_t));
1143 memcpy(force->hash[n], hsh->hash, 16);
1144
1145 bloom_byte = (* (int*)(force->hash[n])) & 0xfffff;
1146 bloom_bit = 1 << (bloom_byte >> 15);
1147 bloom[bloom_byte & 0x7fff] |= bloom_bit;
1148
1149 }
1150 }
1151
1152 /* Brute force. */
1153
1154 const uchar_t *chars = ntextended_ext95;
1155 int size = sizeof(ntextended_ext95);
1156
1157 uchar_t pwd[8] = {0};
1158 uchar_t hash[16] = {0};
1159 int *idx = force->idx;
1160 int len = force->len;
1161 int nhredux = crack->arg->nhredux;
1162 int maxlen = crack->arg->bforce_len;
1163
1164 assert(len > 0 && len <= maxlen);
1165
1166 /* Test at most 'count' passwords. */
1167
1168 int count = nhredux;
1169 int i = 0;
1170
1171 while (count > 0 && len <= maxlen) {
1172 while (idx[0] < size && count > 0) {
1173 --count;
1174 --force->count;
1175
1176 /* Move to the end of the password. */
1177
1178 while (i < len) {
1179 pwd[i] = chars[idx[i]];
1180 ++i;
1181 }
1182
1183 /* Hash the password and compare the result to each hash of the
1184 given list of hashes. */
1185
1186 ntextended_mkhash(pwd, hash);
1187
1188 int *bbp = (int*)hash;
1189
1190 bloom_byte = *bbp & 0xfffff;
1191 bloom_bit = 1 << (bloom_byte >> 15);
1192
1193 if (bloom[(bloom_byte & 0x7fff)] & bloom_bit) {
1194
1195 for (n=0; n<nhashes; ++n) {
1196 int found = force->found[n];
1197
1198 if (!found && memcmp(force->hash[n], hash, 16) == 0) {
1199 force->found[n] = 1;
1200 /* Do not report deleted hashes as found. */
1201
1202 if (hashes[n]->id >= 0) {
1203 ophcrack_found(crack, hashes[n], 0, 0, (char*)pwd);
1204 ++task->stat->match_bforce;
1205 }
1206 }
1207 }
1208 }
1209
1210 /* Advance to the next password. */
1211
1212 ++idx[--i];
1213
1214 while (idx[i] == size && i > 0) {
1215 idx[i] = 0;
1216 ++idx[--i];
1217 }
1218 }
1219
1220 if (idx[0] == size) {
1221 ++len;
1222 idx[0] = 0;
1223 }
1224 }
1225
1226 message_bforce(1, nhredux-count);
1227
1228 /* If we did not enumerate all the possible passwords, then we
1229 schedule another brute force task. We also update the
1230 statistics to avoid having to wait until the brute force is
1231 done before seeing the updated values. */
1232
1233 if (len <= maxlen) {
1234 force->len = len;
1235
1236 pthread_mutex_lock(mutex);
1237 ophstat_add(crack->stat, task->stat);
1238 pthread_mutex_unlock(mutex);
1239
1240 ophstat_reset(task->stat);
1241 scheduler_add(sched, task, low);
1242
1243 task = 0;
1244 }
1245
1246 #ifndef NDEBUG
1247 else {
1248 assert(force->count == 0);
1249 }
1250 #endif
1251
1252 /* It is important to return 0 if we recycled the task above. */
1253
1254 return task;
1255 }
1256 /*-------------------------------------------------------------------------*/
1257 ophtask_t *ophcrack_bforce_lm(ophcrack_t *crack, ophtask_t *task, int id) {
1258 scheduler_t *sched = crack->sched;
1259 pthread_mutex_t *mutex = sched->mutex;
1260
1261 ophbforce_t *force = task->data;
1262
1263 /* If they do not already exist, create two arrays which will hold
1264 the hash value and the password of each hash. */
1265
1266 hash_t **hashes = force->hashes;
1267 int nhashes = force->nhashes;
1268 int *bloom = force->bloom;
1269 int n;
1270 int bloom_byte, bloom_bit;
1271
1272 if (force->hash == 0) {
1273 assert(force->found == 0);
1274
1275 force->hash = (uchar_t**)calloc(nhashes, sizeof(uchar_t*));
1276 force->found = (int*)calloc(nhashes, sizeof(int));
1277
1278 for (n=0; n<nhashes; ++n) {
1279 hash_t *hsh = hashes[n];
1280
1281 force->hash[n] = (uchar_t*)malloc(8*sizeof(uchar_t));
1282 memcpy(force->hash[n], hsh->hash, 8);
1283
1284 bloom_byte = (* (int*)(force->hash[n])) & 0xfffff;
1285 bloom_bit = 1 << (bloom_byte >> 15);
1286 bloom[bloom_byte & 0x7fff] |= bloom_bit;
1287
1288 }
1289 }
1290
1291 /* Brute force. */
1292
1293 const uchar_t *chars = extended_chars;
1294 int size = sizeof(extended_chars);
1295
1296 uchar_t pwd[8] = {0};
1297 uchar_t hash[8] = {0};
1298 int *idx = force->idx;
1299 int len = force->len;
1300 int nhredux = crack->arg->nhredux;
1301 int maxlen = crack->arg->bforce_len;
1302
1303 assert(len > 0 && len <= maxlen);
1304
1305 /* Test at most 'count' passwords. */
1306
1307 int count = nhredux;
1308 int i = 0;
1309
1310 while (count > 0 && len <= maxlen) {
1311 while (idx[0] < size && count > 0) {
1312 --count;
1313 --force->count;
1314
1315 /* Move to the end of the password. */
1316
1317 while (i < len) {
1318 pwd[i] = chars[idx[i]];
1319 ++i;
1320 }
1321
1322 /* Hash the password and compare the result to each hash of the
1323 given list of hashes. */
1324
1325 lmtable_mkhash(pwd, hash);
1326
1327 int *bbp = (int*)hash;
1328
1329 bloom_byte = *bbp & 0xfffff;
1330 bloom_bit = 1 << (bloom_byte >> 15);
1331
1332 if (bloom[(bloom_byte & 0x7fff)] & bloom_bit) {
1333
1334 for (n=0; n<nhashes; ++n) {
1335 int found = force->found[n];
1336
1337 if (!found && memcmp(force->hash[n], hash, 8) == 0) {
1338 force->found[n] = 1;
1339 /* Do not report deleted hashes as found. */
1340
1341 if (hashes[n]->id >= 0) {
1342 ophcrack_found(crack, hashes[n], 0, 0, (char*)pwd);
1343 ++task->stat->match_bforce;
1344 }
1345 }
1346 }
1347 }
1348
1349 /* Advance to the next password. */
1350
1351 ++idx[--i];
1352
1353 while (idx[i] == size && i > 0) {
1354 idx[i] = 0;
1355 ++idx[--i];
1356 }
1357 }
1358
1359 if (idx[0] == size) {
1360 ++len;
1361 idx[0] = 0;
1362 }
1363 }
1364
1365 message_bforce(1, nhredux-count);
1366
1367 /* If we did not enumerate all the possible passwords, then we
1368 schedule another brute force task. We also update the statistics
1369 to avoid having to wait until the brute force is done before
1370 seeing the updated values. */
1371
1372 if (len <= maxlen) {
1373 force->len = len;
1374
1375 pthread_mutex_lock(mutex);
1376 ophstat_add(crack->stat, task->stat);
1377 pthread_mutex_unlock(mutex);
1378
1379 ophstat_reset(task->stat);
1380 scheduler_add(sched, task, low);
1381
1382 task = 0;
1383 }
1384
1385 #ifndef NDEBUG
1386 else {
1387 assert(force->count == 0);
1388 }
1389 #endif
1390
1391 /* It is important to return 0 if we recycled the task above. */
1392
1393 return task;
1394 }
1395 /*-------------------------------------------------------------------------*/
1396 int ophcrack_find_cmin(int ncols, int cmax, int total) {
1397 double y = ncols-cmax;
1398 double d = 1 - 4 * (-2*total - y*y + y);
1399 double x = floor(0.5 * (-1 + sqrt(d)));
1400
1401 assert((x-y+1)*(x+y) <= 2*total);
1402
1403 int cmin = ncols-(int)x;
1404
1405 /* Make sure that we progress, even if arg->nhredux < chain length */
1406 if (cmin >= cmax)
1407 cmin = cmax-1;
1408
1409 return cmin < 0 ? 0 : cmin;
1410 }
1411 /*-------------------------------------------------------------------------*/
1412 int ophcrack_has_next(ophcrack_t *crack) {
1413 list_t *hashes = crack->hashes;
1414 list_nd_t *hnd;
1415
1416 for (hnd = hashes->head; hnd != 0; hnd = hnd->next) {
1417 hash_t *hsh = hnd->data;
1418
1419 list_t *tables = hsh->tables;
1420 list_nd_t *tnd;
1421
1422 if (hsh->done != 0) continue;
1423
1424 for (tnd = tables->head; tnd != 0; tnd = tnd->next) {
1425 htbl_t *htbl = tnd->data;
1426 table_t *tbl = htbl->tbl;
1427
1428 if (tbl->active && htbl->col > 0) return 1;
1429 }
1430 }
1431
1432 return 0;
1433 }
1434 /*-------------------------------------------------------------------------*/
1435 ophtask_t *ophcrack_next(ophcrack_t *crack) {
1436 scheduler_t *sched = crack->sched;
1437
1438 list_t *hashes = crack->hashes;
1439 int nhashes = hashes->size;
1440
1441 int disk_size = sched->queue[disk]->size;
1442 int mdqueue = crack->arg->mdqueue;
1443
1444 ophtask_t *task = 0;
1445 ophwork_t *work = 0;
1446
1447 for (int h=0; h<nhashes && !task; ++h) {
1448
1449 list_nd_t *hnd = crack->hnd == 0 ? hashes->head : crack->hnd;
1450 hash_t *hsh = hnd->data;
1451 list_t *tables = hsh->tables;
1452 int ntables = tables->size;
1453
1454 if (hsh->done == 0 && ntables > 0) {
1455 list_nd_t *tnd = hsh->tnd == 0 ? tables->head : hsh->tnd;
1456 htbl_t *htbl = tnd->data;
1457 table_t *tbl = htbl->tbl;
1458 table_preload_t preload = table_preload_state(tbl);
1459
1460 /* Search for a table which is active and which has not been completely
1461 covered yet. We also check that the disk queue is not too big. If it is
1462 and searching into the table will cause a disk access, we look for
1463 another table. */
1464
1465 int found = 0;
1466
1467 for (int t=0; t<ntables && !found; ++t) {
1468
1469 int disk_queue_full = disk_size > mdqueue && preload != preload_full;
1470 int table_valid = tbl->active && htbl->col >= 0;
1471
1472 if (table_valid && !disk_queue_full) {
1473 found = 1;
1474 break;
1475 }
1476
1477 tnd = tnd->next == 0 ? tables->head : tnd->next;
1478 htbl = tnd->data;
1479 tbl = htbl->tbl;
1480 preload = table_preload_state(tbl);
1481 }
1482
1483 /* If a valid table has been found, we use it. */
1484
1485 if (found) {
1486 assert(tbl->enabled);
1487
1488 /* Calculate the mimimum column index such that at most 'nhredux'
1489 operations are performed. */
1490
1491 int nhredux = crack->arg->nhredux;
1492 int cmax = htbl->col;
1493 int cmin = ophcrack_find_cmin(tbl->ncols, cmax, nhredux);
1494
1495 /* Create a work task. */
1496
1497 task = ophtask_alloc(find);
1498 work = task->data;
1499
1500 work->hsh = hsh;
1501 work->htbl = htbl;
1502 work->cmin = cmin;
1503 work->cmax = cmax;
1504
1505 /* Store the column index from which we will start searching next time
1506 and move to the next table. */
1507
1508 htbl->col = cmin-1;
1509 hsh->tnd = tnd->next;
1510 }
1511 }
1512
1513 crack->hnd = hnd->next;
1514 }
1515
1516 return task;
1517 }
1518 /*-------------------------------------------------------------------------*/
1519 int ophcrack_pwd_cmp(const void *a, const void *b) {
1520 ophel_t *ela = *(ophel_t**)a;
1521 ophel_t *elb = *(ophel_t**)b;
1522
1523 assert(ela != 0);
1524 assert(elb != 0);
1525
1526 if (ela->prefix > elb->prefix)
1527 return 1;
1528 else if (ela->prefix < elb->prefix)
1529 return -1;
1530 else
1531 if (ela->postfix > elb->postfix)
1532 return 1;
1533 else if (ela->postfix < elb->postfix)
1534 return -1;
1535 else
1536 return 0;
1537 }
1538 /*-------------------------------------------------------------------------*/
1539 int ophcrack_col_cmp(const void *a, const void *b) {
1540 ophel_t *ela = *(ophel_t**)a;
1541 ophel_t *elb = *(ophel_t**)b;
1542
1543 if (ela == 0 && elb == 0)
1544 return 0;
1545 else if (ela == 0)
1546 return 1;
1547 else if (elb == 0)
1548 return -1;
1549
1550 if (ela->col < elb->col)
1551 return -1;
1552 else if (ela->col > elb->col)
1553 return 1;
1554 else
1555 return 0;
1556 }
1557 /*-------------------------------------------------------------------------*/
1558 void ophcrack_found(ophcrack_t *crack, hash_t *hsh, table_t *tbl, int col,
1559 char *pwd) {
1560
1561 scheduler_t *sched = crack->sched;
1562 pthread_mutex_t *mutex = sched->mutex;
1563 ophtask_t *task = 0;
1564 ophbforce_t *force = 0;
1565
1566 char *lmpwd1 = 0;
1567 char *lmpwd2 = 0;
1568
1569 extern struct timeval tm_main_total;
1570 extern struct timeval tm_main_start;
1571
1572 /* We must check the 'done' flag because somebody may have already
1573 cracked the hash before we reach this point. Furthermore, it
1574 might happen that we call this function with an hash whose
1575 password has already been set, in which case the pwd pointer is
1576 the same has the hsh->pwd pointer. We must also check that the
1577 hash id is valid because the hash could have been deleted. */
1578
1579 pthread_mutex_lock(mutex);
1580
1581 if (hsh->done == 0 && hsh->id >= 0) {
1582 if (hsh->pwd != pwd) strcpy(hsh->pwd, pwd);
1583 hsh->done = 1;
1584
1585 if (hsh->length == 0) hsh->length = strlen(pwd);
1586 hsh->table = (char*) malloc(32*sizeof(char));
1587 if (tbl == 0)
1588 snprintf(hsh->table, 32, "Bruteforce");
1589 else
1590 snprintf(hsh->table, 32, "%s", table_string(tbl->kind));
1591 hsh->category = categorize_password(hsh->pwd);
1592
1593 if (hsh->time == -1) {
1594 if (tm_main_total.tv_sec == 0) {
1595 if (tm_main_start.tv_sec == 0)
1596 hsh->time = 0;
1597 else {
1598 struct timeval now;
1599 gettimeofday(&now, 0);
1600 long tv_sec = now.tv_sec - tm_main_start.tv_sec;
1601 hsh->time = (int) tv_sec;
1602 }
1603 } else
1604 hsh->time = (int) tm_main_total.tv_sec;
1605 }
1606
1607 message_found(hsh, tbl, col);
1608
1609 /* A NT hash has been cracked. */
1610
1611 if (hsh->kind == nt) {
1612 ++crack->npwds_found;
1613 ++crack->stat->length[(hsh->length > 15) ? 15 : hsh->length];
1614 ++crack->stat->category[hsh->category];
1615 list_add_tail(crack->stat->time, &(hsh->time));
1616 }
1617
1618 /* A 1st LM hash which is not associated to a NT hash and whose
1619 corresponding 2nd LM hash has been cracked. */
1620
1621 else if (hsh->kind == lm1 && hsh->lmhsh1 == 0 && hsh->lmhsh2->done > 0) {
1622 ++crack->npwds_found;
1623 ++crack->stat->length[(hsh->length > 15) ? 15 : hsh->length];
1624 ++crack->stat->category[hsh->category];
1625 list_add_tail(crack->stat->time, &(hsh->time));
1626 }
1627
1628 /* A 2nd LM hash which is not associated to a NT hash and whose
1629 corresponding 1st LM hash has been cracked. */
1630
1631 else if (hsh->kind == lm2 && hsh->lmhsh2 == 0 && hsh->lmhsh1->done > 0) {
1632 ++crack->npwds_found;
1633 ++crack->stat->length[(hsh->length > 15) ? 15 : hsh->length];
1634 ++crack->stat->category[hsh->category];
1635 list_add_tail(crack->stat->time, &(hsh->time));
1636 }
1637
1638 /* If both LM hashes have been cracked, then we schedule a brute
1639 force task to try to crack the corresponding NT hash. */
1640
1641 if (hsh->kind == lm1 || hsh->kind == lm2) {
1642 hash_t *nthsh = hsh->kind == lm1 ? hsh->lmhsh1 : hsh->lmhsh2;
1643
1644 if (nthsh && nthsh->done == 0) {
1645 hash_t *lmhsh1 = nthsh->lmhsh1;
1646 hash_t *lmhsh2 = nthsh->lmhsh2;
1647
1648 if (lmhsh1->done > 0 && lmhsh2->done > 0) {
1649 task = ophtask_alloc(resolve_nt);
1650 force = task->data;
1651
1652 force->nhashes = 1;
1653 force->hashes = (hash_t**)malloc(sizeof(hash_t*));
1654 force->hashes[0] = nthsh;
1655 }
1656 }
1657 }
1658
1659 /* If we cracked a NT hash and the associated LM hashes have not
1660 been cracked yet, then we can mark the LM hashes as done. */
1661
1662 else {
1663 hash_t *lmhsh1 = hsh->lmhsh1;
1664 hash_t *lmhsh2 = hsh->lmhsh2;
1665
1666 char *pwd = hsh->pwd;
1667 int len = strlen(hsh->pwd);
1668
1669 if (lmhsh1 && lmhsh1->done == 0) {
1670 int n = MY_MIN(7, len);
1671 lmpwd1 = lm_from_nt(pwd, n, lmhsh1->hash);
1672 }
1673
1674 if (lmhsh2 && lmhsh2->done == 0 && len > 7) {
1675 int n = MY_MIN(7, len-7);
1676 lmpwd2 = lm_from_nt(pwd+7, n, lmhsh2->hash);
1677 }
1678 }
1679 }
1680
1681 pthread_mutex_unlock(mutex);
1682
1683 /* If some LM hashes have been cracked, then we mark them as
1684 done. This must be performed outside of the mutexed region
1685 because otherwise the call to ophcrack_found will block on the
1686 mutex. */
1687
1688 if (lmpwd1) {
1689 ophcrack_found(crack, hsh->lmhsh1, 0, 0, lmpwd1);
1690 free(lmpwd1);
1691 }
1692
1693 if (lmpwd2) {
1694 ophcrack_found(crack, hsh->lmhsh2, 0, 0, lmpwd2);
1695 free(lmpwd2);
1696 }
1697
1698 /* The scheduling of the task must also happen outside of the
1699 mutexed region because scheduler_add use the mutex too. */
1700
1701 if (task != 0) scheduler_add(sched, task, high);
1702 }
1703 /*-------------------------------------------------------------------------*/
1704 void ophcrack_notfound(ophcrack_t *crack, hash_t *hsh) {
1705 scheduler_t *sched = crack->sched;
1706 pthread_mutex_t *mutex = sched->mutex;
1707
1708 /* We must check the 'done' flag because somebody may have already
1709 cracked the hash before we reach this point. */
1710
1711 pthread_mutex_lock(mutex);
1712
1713 if (hsh->done == 0) {
1714 hsh->done = -1;
1715 message_found(hsh, 0, 0);
1716 }
1717
1718 pthread_mutex_unlock(mutex);
1719 }
1720 /*-------------------------------------------------------------------------*/
1721 void ophcrack_preload_check(ophcrack_t *crack) {
1722 list_t *active = crack->active;
1723
1724 assert(active->size > 0);
1725
1726 /* If there is only one active table, then we do not have anything
1727 to do. */
1728
1729 if (active->size == 1) return;
1730
1731 /* Otherwise, we check that the way the tables have been preloaded
1732 satisfies the preloading policy. */
1733
1734 list_t *tables = list_alloc();
1735
1736 int size = active->size;
1737 int flag = 1;
1738
1739 while (size--) {
1740 table_t *tbl = list_rem_head(active);
1741
1742 if (tbl->idxmem && tbl->endmem && tbl->srtmem)
1743 list_add_tail(active, tbl);
1744
1745 else if (tbl->idxmem && tbl->endmem && flag) {
1746 list_add_tail(active, tbl);
1747 flag = 0;
1748 }
1749
1750 else
1751 list_add_tail(tables, tbl);
1752 }
1753
1754 /* Put the tables which have been removed from the 'active' list
1755 into the 'remaining' list and mark them as inactive. */
1756
1757 list_t *remaining = crack->remaining;
1758
1759 while (tables->size > 0) {
1760 table_t *tbl = list_rem_tail(tables);
1761
1762 tbl->active = 0;
1763 list_add_head(remaining, tbl);
1764 }
1765
1766 list_free(tables);
1767 }
1768 /*-------------------------------------------------------------------------*/
1769 ophtask_t *ophcrack_preload_all(ophcrack_t *crack, ophtask_t *task, int id) {
1770 scheduler_t *sched = crack->sched;
1771 ophload_t *load = task->data;
1772
1773 list_t *tables = load->tables;
1774 list_t *active = crack->active;
1775 list_t *enabled = crack->enabled;
1776 list_t *remaining = crack->remaining;
1777
1778 uint64_t freeram0 = crack->find_freeram();
1779 uint64_t freeram1 = crack->freeram;
1780 uint64_t freeram = MY_MIN(freeram0, freeram1);
1781
1782 /* Compute the amount of memory available if all tables were
1783 unloaded. */
1784
1785 uint8_t *initialised = (uint8_t*)calloc(crack->maxtkind, sizeof(uint8_t));
1786
1787 for (list_nd_t *nd = crack->tables->head; nd != NULL; nd = nd->next) {
1788 table_t *tbl = nd->data;
1789 table_preload_t state = table_preload_state(tbl);
1790
1791 assert(tbl->kind >= 0 && tbl->kind < crack->maxtkind);
1792
1793 if (tbl->shared_init && initialised[tbl->kind])
1794 freeram += table_preload_size(tbl, state & ~preload_init);
1795 else
1796 freeram += table_preload_size(tbl, state);
1797
1798 initialised[tbl->kind] |= state & preload_init;
1799 }
1800
1801 /* Mark all tables as inactive and assume that nothing as been
1802 preloaded yet. */
1803
1804 table_preload_t *preload = (table_preload_t*)calloc(crack->maxtid, sizeof(table_preload_t));
1805
1806 for (list_nd_t *nd = crack->tables->head; nd != NULL; nd = nd->next) {
1807 table_t *tbl = nd->data;
1808
1809 assert(tbl->id >= 0 && tbl->id < crack->maxtid);
1810
1811 preload[tbl->id] = preload_none;
1812 initialised[tbl->kind] = 0;
1813
1814 tbl->active = 0;
1815 }
1816
1817 /* If tables are provided in the task, we process them. Otherwise we
1818 process the active and remaining tables which are enabled.*/
1819
1820 if (tables->size == 0) {
1821 for (list_nd_t *nd = active->head; nd != NULL; nd = nd->next) {
1822 table_t *tbl = nd->data;
1823
1824 if (tbl->enabled)
1825 list_add_tail(tables, tbl);
1826 }
1827
1828 for (list_nd_t *nd = remaining->head; nd != NULL; nd = nd->next) {
1829 table_t *tbl = nd->data;
1830
1831 if (tbl->enabled)
1832 list_add_tail(tables, tbl);
1833 }
1834 }
1835
1836 /* Try to preload as much as we can */
1837
1838 list_clear(active);
1839 list_clear(remaining);
1840
1841 static table_preload_t states[] = {
1842 preload_full,
1843 preload_full - preload_srt,
1844 preload_full - preload_srt - preload_end,
1845 preload_full - preload_srt - preload_end - preload_idx
1846 };
1847
1848 static int nstates = sizeof(states) / sizeof(table_preload_t);
1849
1850 for (int k=0; k<2; ++k) {
1851
1852 int lst_state_idx = nstates-1;
1853
1854 for (list_nd_t *nd = tables->head; nd != NULL; nd = nd->next) {
1855 table_t *tbl = nd->data;
1856
1857 /* Fully covered tables are ignored during the 1st pass. During the 2nd
1858 pass, it is the not fully covered tables which are ignored. This way
1859 fully covered tables are unloaded only when some memory space is
1860 required to preload tables which have not yet been fully covered. */
1861
1862 if ((k == 0 && tbl->cmax == 0) ||
1863 (k == 1 && tbl->cmax > 0))
1864 continue;
1865
1866 assert(tbl->enabled);
1867
1868 int fst_state_idx = nstates-1 - crack->arg->preload;
1869
1870 if (k == 1) {
1871 table_preload_t preload = table_preload_state(tbl);
1872
1873 for (int i=fst_state_idx; i<=lst_state_idx; ++i)
1874 if (states[i] == preload) {
1875 fst_state_idx = i;
1876 break;
1877 }
1878 }
1879
1880 for (int i=fst_state_idx; i<=lst_state_idx; ++i) {
1881 /* Compute the amount of memory required for the preload */
1882
1883 table_preload_t state = states[i];
1884 uint64_t size = 0;
1885
1886 if (tbl->shared_init && initialised[tbl->kind])
1887 size = table_preload_size(tbl, state & ~preload_init);
1888 else
1889 size = table_preload_size(tbl, state);
1890
1891 /* Check whether there is enough memory for the preload */
1892
1893 if (size <= freeram) {
1894 freeram -= size;
1895 preload[tbl->id] = state;
1896 initialised[tbl->kind] = 1;
1897
1898 /* If this table has not been completely preloaded, we want subsequent
1899 tables to have at least the same preload state. This is because we
1900 assume that a table with more files on disk will likely slow down
1901 the search. */
1902
1903 if (k == 0 && state != preload_full) lst_state_idx = i;
1904
1905 break;
1906 }
1907 }
1908
1909 if (k == 0 && preload[tbl->id] == preload_none)
1910 list_add_tail(remaining, tbl);
1911
1912 else if (tbl->cmax > 0)
1913 list_add_tail(active, tbl);
1914 }
1915 }
1916
1917 /* Mark tables in the active list as active and tables in the
1918 remaining list as inactive. */
1919
1920 for (list_nd_t *nd = active->head; nd != NULL; nd = nd->next) {
1921 table_t *tbl = nd->data;
1922 tbl->active = 1;
1923 }
1924
1925 for (list_nd_t *nd = remaining->head; nd != NULL; nd = nd->next) {
1926 table_t *tbl = nd->data;
1927 tbl->active = 0;
1928 }
1929
1930 /* Schedule an unload task for all tables which have been at least
1931 partially preloaded and have been disabled. */
1932
1933 for (list_nd_t *nd = crack->tables->head; nd != 0; nd = nd->next) {
1934 table_t *tbl = nd->data;
1935 table_preload_t state = table_preload_state(tbl);
1936
1937 if (!tbl->enabled && state != preload_none) {
1938 ophtask_t *task = ophtask_alloc(unload);
1939 ophload_t *load = task->data;
1940
1941 load->tbl = tbl;
1942 load->preload = state;
1943
1944 scheduler_add(sched, task, disk);
1945 }
1946 }
1947
1948 /* Schedule the un/preload tasks only if there is actually
1949 something to do with the tables currently activated. */
1950
1951 uint64_t total_size = 0;
1952
1953 if (ophcrack_has_next(crack)) {
1954 /* Schedule an unload task for all the tables for which the
1955 desired preload status is different from the current one. */
1956
1957 for (list_nd_t *nd = enabled->head; nd != 0; nd = nd->next) {
1958 table_t *tbl = nd->data;
1959 table_preload_t state = table_preload_state(tbl);
1960 table_preload_t unload_bits = state & ~preload[tbl->id];
1961
1962 if (unload_bits != preload_none) {
1963 ophtask_t *task = ophtask_alloc(unload);
1964 ophload_t *load = task->data;
1965
1966 load->tbl = tbl;
1967 load->preload = unload_bits;
1968
1969 scheduler_add(sched, task, disk);
1970 }
1971 }
1972
1973 /* Schedule an unload task with an empty table pointer. This will
1974 indicate, when the task will be executed, that the unloading is
1975 done. */
1976
1977 ophtask_t *unload_done = ophtask_alloc(unload);
1978 scheduler_add(sched, unload_done, disk);
1979
1980 /* Schedule a preload task for all the active tables for which the
1981 desired preload status is different from the current one. */
1982
1983 static table_preload_t steps[] = {
1984 preload_init,
1985 preload_idx,
1986 preload_end,
1987 preload_srt
1988 };
1989
1990 static int nsteps = sizeof(steps) / sizeof(table_preload_t);
1991
1992 for (list_nd_t *nd = enabled->head; nd != 0; nd = nd->next) {
1993 table_t *tbl = nd->data;
1994 table_preload_t state = table_preload_state(tbl);
1995 table_preload_t load_bits = ~state & preload[tbl->id];
1996
1997 if (load_bits == preload_none)
1998 continue;
1999
2000 total_size += table_preload_size(tbl, load_bits);
2001
2002 for (int i=0; i<nsteps; ++i) {
2003 if (load_bits & steps[i]) {
2004 ophtask_t *task = ophtask_alloc(preload_one);
2005 ophload_t *load = task->data;
2006
2007 load->tbl = tbl;
2008 load->preload = steps[i];
2009
2010 scheduler_add(sched, task, disk);
2011 }
2012 }
2013 }
2014 }
2015
2016 /* If no tables have been activated and some tables remain to be
2017 preloaded, it means the remaining tables have to be initialised,
2018 but there is not enough memory available. */
2019
2020 if (active->size == 0) {
2021 while (remaining->size > 0) {
2022 #ifdef NDEBUG
2023 list_rem_head(remaining);
2024 #else
2025 table_t *tbl = list_rem_head(remaining);
2026 assert(tbl->inisize > 0);
2027 #endif
2028 }
2029 }
2030
2031 /* Send a message indicating the amount of RAM occupied by the tables
2032 preloaded. */
2033
2034 message_preload(0, preload_none, 0, total_size);
2035
2036 /* Schedule a preload task with an empty table pointer. This will
2037 indicate, when the task will be executed, that the preloading is
2038 done. */
2039
2040 ophtask_t *preload_done = ophtask_alloc(preload_one);
2041 scheduler_add(sched, preload_done, disk);
2042
2043 free(preload);
2044 free(initialised);
2045
2046 return task;
2047 }
2048 /*-------------------------------------------------------------------------*/
2049 ophtask_t *ophcrack_preload_one(ophcrack_t *crack, ophtask_t *task, int id) {
2050 scheduler_t *sched = crack->sched;
2051 pthread_mutex_t *mutex = sched->mutex;
2052
2053 ophload_t *load = task->data;
2054 table_t *tbl = load->tbl;
2055
2056 if (tbl != 0) {
2057 assert(tbl->enabled);
2058
2059 uint64_t size = table_preload_size(tbl, load->preload);
2060
2061 message_preload(tbl, load->preload, 0, size);
2062 size = table_preload(tbl, load->preload);
2063
2064 pthread_mutex_lock(mutex);
2065 crack->freeram -= size;
2066 pthread_mutex_unlock(mutex);
2067
2068 message_preload(tbl, load->preload, 1, size);
2069 }
2070 else
2071 message_done(preload_all);
2072
2073 return task;
2074 }
2075 /*-------------------------------------------------------------------------*/
2076 ophtask_t *ophcrack_unload(ophcrack_t *crack, ophtask_t *task, int id) {
2077 scheduler_t *sched = crack->sched;
2078 pthread_mutex_t *mutex = sched->mutex;
2079
2080 ophload_t *load = task->data;
2081 table_t *tbl = load->tbl;
2082
2083 if (tbl != 0) {
2084 uint64_t size = table_unload(tbl, load->preload);
2085
2086 pthread_mutex_lock(mutex);
2087 crack->freeram += size;
2088 pthread_mutex_unlock(mutex);
2089
2090 message_unload(tbl, size);
2091 }
2092 else
2093 message_done(unload);
2094
2095 return task;
2096 }
2097 /*-------------------------------------------------------------------------*/
2098 void ophcrack_update(ophcrack_t *crack) {
2099 scheduler_t *sched = crack->sched;
2100 pthread_mutex_t *mutex = sched->mutex;
2101
2102 /* Create an array which will store whether we have already seen a
2103 table with a given id. */
2104
2105 int *seen = (int*)calloc(crack->maxtid, sizeof(int));
2106
2107 /* Go through all the hashes and update cmax according to the value
2108 found in each table encountered. */
2109
2110 list_t *hashes = crack->hashes;
2111 list_nd_t *hnd;
2112
2113 pthread_mutex_lock(mutex);
2114
2115 for (hnd = hashes->head; hnd != 0; hnd = hnd->next) {
2116 hash_t *hsh = hnd->data;
2117 list_t *tables = hsh->tables;
2118 list_nd_t *tnd;
2119
2120 for (tnd = tables->head; tnd != 0; tnd = tnd->next) {
2121 htbl_t *htbl = tnd->data;
2122 table_t *tbl = htbl->tbl;
2123
2124 /* cmin and cmax are set to zero if the hash has been cracked. */
2125
2126 int id = tbl->id;
2127 int cmin = hsh->done != 0 ? 0 : htbl->col+1;
2128 int cmax = hsh->done != 0 ? 0 : htbl->covered;
2129
2130 /* If we see the table for the first time, then we initialise
2131 it with the values we found. */
2132
2133 if (!seen[id]) {
2134 tbl->cmin = cmin;
2135 tbl->cmax = cmax;
2136
2137 seen[id] = 1;
2138 }
2139
2140 /* Otherwise, we update the stored values. */
2141
2142 else {
2143 tbl->cmin = MY_MAX(tbl->cmin, cmin);
2144 tbl->cmax = MY_MAX(tbl->cmax, cmax);
2145 }
2146 }
2147 }
2148
2149 // Check each active table to see whether all columns have been
2150 // considered. If this happens, then the table is marked as inactive
2151 // and a message is sent.
2152
2153 int done = 0;
2154
2155 list_t *active = crack->active;
2156 list_nd_t *nd;
2157
2158 for (nd = active->head; nd != 0; nd = nd->next) {
2159 table_t *tbl = nd->data;
2160
2161 if (tbl->active && tbl->cmax == 0) {
2162 tbl->active = 0;
2163 done = 1;
2164 }
2165 }
2166
2167 pthread_mutex_unlock(mutex);
2168
2169 if (done) message_done(check);
2170
2171 free(seen);
2172 }
2173 /*-------------------------------------------------------------------------*/
2174 void ophcrack_save(ophcrack_t *crack, FILE *file, int nice, int status) {
2175 /* We need to be careful to write each line only once. */
2176
2177 int maxhid = crack->maxhid;
2178 hash_t **id_to_hash = (hash_t**)calloc(maxhid, sizeof(hash_t*));
2179
2180 list_t *hashes = crack->hashes;
2181 list_nd_t *nd;
2182
2183 arg_t *arg = crack->arg;
2184
2185 for (nd = hashes->head; nd != 0; nd = nd->next) {
2186 hash_t *hsh = nd->data;
2187 id_to_hash[hsh->id] = hsh;
2188 }
2189
2190 /* Store the hashes in the order they have been read. */
2191
2192 int i;
2193
2194 for (i=0; i<maxhid; ++i) {
2195 assert(id_to_hash[i] != 0);
2196 hash_print(id_to_hash[i], file, nice, status, arg->hideuname);
2197 }
2198
2199 free(id_to_hash);
2200 }
2201 /*-------------------------------------------------------------------------*/
2202 void ophcrack_export_csv(ophcrack_t *crack, FILE *file, int *fields, char separator, char quote) {
2203
2204 /* We need to be careful to write each line only once. */
2205
2206 int maxhid = crack->maxhid;
2207 hash_t **id_to_hash = (hash_t**)calloc(maxhid, sizeof(hash_t*));
2208
2209 list_t *hashes = crack->hashes;
2210 list_nd_t *nd;
2211
2212 arg_t *arg = crack->arg;
2213 int hide = arg->hideuname;
2214
2215
2216 for (nd = hashes->head; nd != 0; nd = nd->next) {
2217 hash_t *hsh = nd->data;
2218 id_to_hash[hsh->id] = hsh;
2219 }
2220
2221 /* Store the hashes in the order they have been read. */
2222
2223 int i,first;
2224
2225 for (i=0; i<maxhid; ++i) {
2226 assert(id_to_hash[i] != 0);
2227
2228 first = 1;
2229 hash_t *hsh = id_to_hash[i];
2230 hash_kind_t kind = hsh->kind;
2231
2232 /* Find the associated LM and NT hashes. */
2233
2234 hash_t *lmhsh1 = 0;
2235 hash_t *lmhsh2 = 0;
2236 hash_t *nthsh = 0;
2237 int type = 0;
2238
2239 switch (kind) {
2240 case lm1:
2241 lmhsh1 = hsh;
2242 lmhsh2 = hsh->lmhsh2;
2243 nthsh = hsh->lmhsh1;
2244 if (lmhsh2 == 0 && nthsh == 0) type = 1;
2245 if (lmhsh2 != 0 && nthsh == 0) type = 2;
2246 break;
2247
2248 case lm2:
2249 lmhsh1 = hsh->lmhsh1;
2250 lmhsh2 = hsh;
2251 nthsh = hsh->lmhsh2;
2252 if (lmhsh1 != 0 && nthsh == 0) type = 2;
2253 break;
2254
2255 case nt:
2256 lmhsh1 = hsh->lmhsh1;
2257 lmhsh2 = hsh->lmhsh2;
2258 nthsh = hsh;
2259 if (lmhsh1 != 0 && lmhsh2 != 0) type = 3;
2260 if (lmhsh1 == 0 && lmhsh2 == 0) type = 4;
2261 break;
2262
2263 default:
2264 break;
2265 }
2266
2267 if (type) {
2268
2269 if (fields[0]) {
2270 if (!first) fprintf(file,"%c", separator);
2271 if (quote) fprintf(file, "%c%d%c", quote, hsh->id, quote);
2272 else fprintf(file, "%d", hsh->id);
2273 first = 0;
2274 }
2275
2276 if (fields[1]) {
2277 if (!first) fprintf(file,"%c", separator);
2278 if (quote) {if (hsh->uid) fprintf(file, "%c%d%c", quote, hsh->uid, quote); }
2279 else {if (hsh->uid) fprintf(file, "%d", hsh->uid); }
2280 first = 0;
2281 }
2282
2283 if (fields[2]) {
2284 if (!first) fprintf(file,"%c", separator);
2285 if (quote) {
2286 if (hide) fprintf(file, "%c*****%c", quote, quote);
2287 else {if (hsh->info[0]) fprintf(file, "%c%s%c", quote, hsh->info, quote); }
2288 }
2289 else {
2290 if (hide) fprintf(file, "*****");
2291 else {if (hsh->info[0]) fprintf(file, "%s", hsh->info);}
2292 }
2293 first = 0;
2294 }
2295
2296 if (fields[3]) {
2297 if (!first) fprintf(file,"%c", separator);
2298 if (quote) {if (lmhsh1) fprintf(file, "%c%s%c", quote, lmhsh1->str, quote); }
2299 else {if (lmhsh1) fprintf(file, "%s", lmhsh1->str); }
2300 first = 0;
2301 }
2302
2303 if (fields[4]) {
2304 if (!first) fprintf(file,"%c", separator);
2305 if (quote) {if (nthsh) fprintf(file, "%c%s%c", quote, nthsh->str, quote); }
2306 else {if (nthsh) fprintf(file, "%s", nthsh->str);}
2307 first = 0;
2308 }
2309
2310 if (fields[5]) {
2311 if (!first) fprintf(file,"%c", separator);
2312 if (quote) {if (lmhsh1) fprintf(file, "%c%s%c", quote, lmhsh1->pwd, quote);}
2313 else {if (lmhsh1) fprintf(file, "%s", lmhsh1->pwd);}
2314 first = 0;
2315 }
2316
2317 if (fields[6]) {
2318 if (!first) fprintf(file,"%c", separator);
2319 if (quote) {if (lmhsh2) fprintf(file, "%c%s%c", quote, lmhsh2->pwd, quote);}
2320 else {if (lmhsh2) fprintf(file, "%s", lmhsh2->pwd); }
2321 first = 0;
2322 }
2323
2324 if (fields[7]) {
2325 if (!first) fprintf(file,"%c", separator);
2326 if (quote) {if (nthsh) fprintf(file, "%c%s%c", quote, nthsh->pwd, quote); }
2327 else {if (nthsh) fprintf(file, "%s", nthsh->pwd);}
2328 first = 0;
2329 }
2330
2331 if (fields[8]) {
2332 if (!first) fprintf(file,"%c", separator);
2333 switch (type) {
2334 case 1: if (quote) fprintf(file, "%c%d%c", quote, lmhsh1->done, quote);
2335 else fprintf(file, "%d", lmhsh1->done);
2336 break;
2337 case 2: if (quote) fprintf(file, "%c%d%c", quote, lmhsh1->done && lmhsh2->done, quote);
2338 else fprintf(file, "%d", lmhsh1->done && lmhsh2->done);
2339 break;
2340 case 3:
2341 case 4:
2342 default: if (quote) fprintf(file, "%c%d%c", quote, nthsh->done, quote);
2343 else fprintf(file, "%d", nthsh->done);
2344 break;
2345 }
2346 first = 0;
2347 }
2348
2349 if (fields[9]) {
2350 if (!first) fprintf(file,"%c", separator);
2351 switch (type) {
2352 case 1: if (quote) {if (lmhsh1->done > 0) fprintf(file, "%c%d%c", quote, lmhsh1->time, quote);}
2353 else {if (lmhsh1->done > 0) fprintf(file, "%d", lmhsh1->time);}
2354 break;
2355 case 2: if (quote) {if (lmhsh1->done > 0 && lmhsh2->done > 0) fprintf(file, "%c%d%c", quote, (lmhsh1->time > lmhsh2->time) ? lmhsh1->time : lmhsh2->time, quote);}
2356 else {if (lmhsh1->done > 0 && lmhsh2->done > 0) fprintf(file, "%d", (lmhsh1->time > lmhsh2->time) ? lmhsh1->time : lmhsh2->time);}
2357 break;
2358 case 3:
2359 case 4:
2360 default: if (quote) {if (nthsh->done > 0) fprintf(file, "%c%d%c", quote, nthsh->time, quote);}
2361 else {if (nthsh->done > 0) fprintf(file, "%d", nthsh->time);}
2362 break;
2363 }
2364 first = 0;
2365 }
2366
2367 if (fields[10]) {
2368 if (!first) fprintf(file,"%c", separator);
2369 switch (type) {
2370 case 1: if (quote) {if (lmhsh1->done > 0) fprintf(file, "%c%d%c", quote, lmhsh1->length, quote);}
2371 else {if (lmhsh1->done > 0) fprintf(file, "%d", lmhsh1->length);}
2372 break;
2373 case 2: if (quote) {if (lmhsh1->done > 0 && lmhsh2->done > 0) fprintf(file, "%c%d%c", quote, lmhsh1->length + lmhsh2->length, quote);}
2374 else {if (lmhsh1->done > 0 && lmhsh2->done > 0) fprintf(file, "%d", lmhsh1->length + lmhsh2->length);}
2375 break;
2376 case 3:
2377 case 4:
2378 default: if (quote) {if (nthsh->done) fprintf(file, "%c%d%c", quote, nthsh->length, quote);}
2379 else {if (nthsh->done) fprintf(file, "%d", nthsh->length);}
2380 break;
2381 }
2382 first = 0;
2383 }
2384
2385 if (fields[11]) {
2386 if (!first) fprintf(file,"%c", separator);
2387 switch (type) {
2388 case 1: if (quote) {if (lmhsh1->done > 0) fprintf(file, "%c%s%c", quote, category_string(lmhsh1->category), quote);}
2389 else {if (lmhsh1->done > 0) fprintf(file, "%s", category_string(lmhsh1->category));}
2390 break;
2391 case 2: if (quote) {if (lmhsh1->done > 0 && lmhsh2->done > 0) fprintf(file, "%c%s%c", quote, category_string(lmhsh1->category|lmhsh2->category), quote);}
2392 else {if (lmhsh1->done > 0 && lmhsh2->done > 0) fprintf(file, "%s", category_string(lmhsh1->category|lmhsh2->category));}
2393 break;
2394 case 3:
2395 case 4:
2396 default: if (quote) {if (nthsh->done) fprintf(file, "%c%s%c", quote, category_string(nthsh->category), quote);}
2397 else {if (nthsh->done) fprintf(file, "%s", category_string(nthsh->category));}
2398 break;
2399 }
2400 first = 0;
2401 }
2402
2403 if (fields[12]) {
2404 if (!first) fprintf(file,"%c", separator);
2405 switch (type) {
2406 case 1: if (quote) {if (lmhsh1->done > 0) fprintf(file, "%c%s%c", quote, lmhsh1->table, quote);}
2407 else {if (lmhsh1->done > 0) fprintf(file, "%s", lmhsh1->table);}
2408 break;
2409 case 2: if (quote) {if (lmhsh1->done > 0 && lmhsh2->done > 0) fprintf(file, "%c%s/%s%c", quote, lmhsh1->table, lmhsh2->table, quote);}
2410 else {if (lmhsh1->done > 0 && lmhsh2->done > 0) fprintf(file, "%s/%s", lmhsh1->table, lmhsh2->table);}
2411 break;
2412 case 3: if (quote) {if (nthsh->done) fprintf(file, "%c%s/%s%c", quote, lmhsh1->table, lmhsh2->table, quote);}
2413 else {if (nthsh->done) fprintf(file, "%s/%s", lmhsh1->table, lmhsh2->table);}
2414 break;
2415 case 4:
2416 default: if (quote) {if (nthsh->done) fprintf(file, "%c%s%c", quote, nthsh->table, quote);}
2417 else {if (nthsh->done) fprintf(file, "%s", nthsh->table);}
2418 break;
2419 }
2420 first = 0;
2421 }
2422 fprintf(file, "\n");
2423 }
2424 }
2425
2426 free(id_to_hash);
2427 }
2428 /*-------------------------------------------------------------------------*/
2429 char *lm_from_nt(const char *pwd, int n, const uchar_t *lmhash) {
2430 uchar_t tmp[8] = {0};
2431 uchar_t hash[8] = {0};
2432 int i;
2433
2434 /* Map the password on the appropriate charset. */
2435
2436 assert(n < 8);
2437 memcpy(tmp, pwd, n);
2438
2439 for (i=0; i<n; ++i)
2440 if (tmp[i] >= 'a' && tmp[i] <= 'z')
2441 tmp[i] = tmp[i] + - 'a' + 'A';
2442 else
2443 switch (tmp[i]) {
2444 case 0xc3: case 0xe3: case 0xc2: case 0xc0:
2445 case 0xe2: case 0xe0:
2446 tmp[i] = 'A';
2447 break;
2448
2449 case 0xcb: case 0xeb: case 0xea: case 0xca:
2450 case 0xe8: case 0xc8:
2451 tmp[i] = 'E';
2452 break;
2453
2454 case 0xcd: case 0xed: case 0xcf: case 0xce:
2455 case 0xef: case 0xee:
2456 tmp[i] = 'I';
2457 break;
2458
2459 case 0xd5: case 0xd3: case 0xf5: case 0xf3:
2460 case 0xd4: case 0xf4:
2461 tmp[i] = 'O';
2462 break;
2463
2464 case 0xda: case 0xfa: case 0xdb: case 0xd9:
2465 case 0xfb: case 0xf9:
2466 tmp[i] = 'U';
2467 break;
2468
2469 case 0xe4:
2470 tmp[i] = 0xc4;
2471 break;
2472
2473 case 0xe7:
2474 tmp[i] = 0xe7;
2475 break;
2476
2477 case 0xe9:
2478 tmp[i] = 0xc9;
2479 break;
2480
2481 case 0xf1:
2482 tmp[i] = 0xd1;
2483 break;
2484
2485 case 0xf6:
2486 tmp[i] = 0xd6;
2487 break;
2488
2489 case 0xfc:
2490 tmp[i] = 0xdc;
2491 break;
2492
2493 default:
2494 break;
2495 }
2496
2497 /* Check that the pwd corresponds to the hash. */
2498
2499 lmtable_mkhash((uchar_t*)tmp, hash);
2500
2501 if (memcmp(lmhash, hash, sizeof(hash)) == 0)
2502 return strdup((char*)tmp);
2503 else
2504 return 0;
2505 }