Debian Codegraph @

src/test/test_all.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 #undef NDEBUG
33
34 #include <sys/time.h>
35 #include <assert.h>
36 #include <limits.h>
37
38 #include "ophcrack.h"
39 #include "message.h"
40 #include "lmtable.h"
41 #include "ntproba.h"
42 #include "ntseven.h"
43 #include "nteightxl.h"
44
45 struct timeval tm_main_total;
46 struct timeval tm_main_start;
47
48 void table_set_size_4test(table_t *tbl) {
49 const uint64_t *sizes = NULL;
50
51 switch (tbl->kind) {
52 case lmalphanum5k:
53 sizes = lmalphanum5k_sizes;
54 break;
55
56 case ntseven:
57 sizes = ntseven_sizes;
58 break;
59
60 case nteightxl:
61 sizes = nteightxl_sizes;
62 break;
63
64 case ntprobafree:
65 sizes = ntprobafree_sizes;
66 break;
67
68 case ntproba10g:
69 sizes = ntproba10g_sizes;
70 break;
71
72 case ntproba60g:
73 sizes = ntproba60g_sizes;
74 break;
75
76 default:
77 assert(0);
78 }
79
80 tbl->idxsize = sizes[1];
81 tbl->endsize = sizes[2*tbl->idx+2];
82 tbl->srtsize = sizes[2*tbl->idx+3];
83 }
84
85 uint64_t find_freeram_4preload(void) {
86 return ULLONG_MAX;
87 }
88
89 ophcrack_t *create_crack_4test(table_t **tables, int ntables) {
90 arg_t *arg = arg_alloc();
91 ophcrack_t *crack = ophcrack_alloc(1, arg);
92
93 crack->find_freeram = find_freeram_4preload;
94
95 // Add the tables
96
97 for (int i=0; i<ntables; ++i) {
98 table_t *tbl = tables[i];
99
100 table_set_size_4test(tbl);
101 ophcrack_setup_table(tbl);
102 ophcrack_add_table(crack, tbl);
103
104 tbl->enabled = 1;
105 list_add_tail(crack->enabled, tbl);
106 }
107
108 // Add some hashes
109
110 char pwdump[] =
111 "3e645334fd788c5f949a30c86b9f0fb1:535f34e7eb41001a5d5eea2714d20315";
112
113 list_t *hashes = list_alloc();
114 hash_extract_lmnt(pwdump, hashes, 0);
115
116 assert(hashes->size == 3);
117
118 for (list_nd_t *nd = hashes->head; nd != NULL; nd = nd->next) {
119 hash_t *hash = nd->data;
120
121 ophcrack_add_hash(crack, hash);
122
123 for (list_nd_t *nd = crack->enabled->head; nd != NULL; nd = nd->next) {
124 table_t *tbl = nd->data;
125 ophcrack_associate(crack, hash, tbl);
126 }
127 }
128
129 list_free(hashes);
130
131 return crack;
132 }
133
134 void preload_4test(ophcrack_t *crack, table_t **tables, int ntables) {
135 ophtask_t *task = ophtask_alloc(preload_all);
136 ophload_t *load = task->data;
137
138 for (int i=0; i<ntables; ++i)
139 list_add_tail(load->tables, tables[i]);
140
141 ophcrack_preload_all(crack, task, 0);
142 ophtask_free(task);
143 }
144
145 void free_crack_4test(ophcrack_t *crack) {
146 for (list_nd_t *nd = crack->hashes->head; nd != NULL; nd = nd->next) {
147 hash_t *hash = nd->data;
148 hash_free(hash);
149 }
150
151 for (list_nd_t *nd = crack->tables->head; nd != NULL; nd = nd->next) {
152 table_t *tbl = nd->data;
153 table_free(tbl);
154 }
155
156 arg_free(crack->arg);
157 ophcrack_free(crack);
158 }
159
160 void check_unload_task(scheduler_t *sched, table_t *tbl) {
161 assert(sched->ntasks > 0);
162
163 ophtask_t *task = scheduler_get(sched, 0);
164 assert(task->kind == unload);
165
166 ophload_t *load = task->data;
167 assert(load->tbl == tbl);
168
169 ophtask_free(task);
170 scheduler_done(sched, 0);
171 }
172
173 void check_preload_task0(scheduler_t *sched, table_t *tbl, table_preload_t preload) {
174 ophtask_t *task = scheduler_get(sched, 0);
175 assert(task->kind == preload_one);
176
177 ophload_t *load = task->data;
178 assert(load->tbl == tbl);
179 assert(load->preload == preload);
180
181 ophtask_free(task);
182 scheduler_done(sched, 0);
183 }
184
185 void check_preload_task(scheduler_t *sched, table_t *tbl, table_preload_t preload) {
186 static const table_preload_t preloads[] = { preload_init,
187 preload_idx,
188 preload_end,
189 preload_srt };
190
191 static const int npreloads = sizeof(preloads) / sizeof(table_preload_t);
192
193 if (preload == preload_none)
194 check_preload_task0(sched, tbl, preload_none);
195 else
196 for (int i=0; i<npreloads; ++i)
197 if (preload & preloads[i])
198 check_preload_task0(sched, tbl, preloads[i]);
199 }
200
201 void check_preload_message(table_t *tbl, table_preload_t preload, int done, uint64_t size) {
202 message_t *msg = message_tryget();
203
204 assert(msg != NULL);
205 assert(msg->kind == msg_preload);
206
207 msg_load_t *msg_load = msg->data;
208
209 assert(msg_load->tbl == tbl);
210 assert(msg_load->preload == preload);
211 assert(msg_load->done == done);
212 assert(msg_load->size == size);
213
214 message_free(msg);
215 }
216
217 void test_ophcrack_preload_all0() {
218 // Create and initialise a crack object
219
220 table_t *tables[] = {
221 // xp free fast (lmalphanum5k)
222 table_alloc(0x0fa2031c, ".", 0),
223 table_alloc(0x0fa2031c, ".", 1),
224 table_alloc(0x0fa2031c, ".", 2),
225 table_alloc(0x0fa2031c, ".", 3),
226 };
227
228 int ntables = sizeof(tables) / sizeof(table_t*);
229 ophcrack_t *crack = create_crack_4test(tables, ntables);
230
231 assert(crack->enabled->size == ntables);
232
233 // Assume we have enough RAM for all tables
234
235 uint64_t preload_size = 0;
236
237 for (int i=0; i<ntables; ++i)
238 preload_size += table_preload_size(tables[i], preload_full);
239
240 crack->freeram = preload_size;
241
242 // Perform preload
243
244 preload_4test(crack, tables, ntables);
245
246 assert(crack->active->size == 4);
247 assert(crack->remaining->size == 0);
248 assert(crack->remaining->size == crack->enabled->size - crack->active->size);
249
250 // Check the tasks
251
252 assert(crack->sched->ntasks == 18);
253
254 check_unload_task(crack->sched, NULL);
255
256 for (int i=0; i<ntables; ++i)
257 check_preload_task(crack->sched, tables[i], preload_full);
258
259 check_preload_task(crack->sched, NULL, preload_none);
260
261 assert(crack->sched->ntasks == 0);
262
263 // Check the messages
264
265 check_preload_message(NULL, preload_none, 0, preload_size);
266
267 message_t *msg = message_tryget();
268 assert(msg == NULL);
269
270 // Free memory
271
272 free_crack_4test(crack);
273 }
274
275 void test_ophcrack_preload_all1() {
276 table_t *tables[] = {
277 // vista probabilistic 10G (ntproba10g)
278 table_alloc(0x47000fdf, ".", 0),
279 table_alloc(0x47000fdf, ".", 1),
280 table_alloc(0x47000fdf, ".", 2),
281 table_alloc(0x47000fdf, ".", 3)
282 };
283
284 int ntables = sizeof(tables) / sizeof(table_t*);
285 ophcrack_t *crack = create_crack_4test(tables, ntables);
286
287 // Assume we have enough RAM to load everything but the .start files
288
289 crack->arg->preload = 2;
290
291 uint64_t preload_size =
292 table_preload_size(tables[0], preload_full - preload_srt) +
293 table_preload_size(tables[1], preload_full - preload_srt) +
294 table_preload_size(tables[2], preload_full - preload_srt) +
295 table_preload_size(tables[3], preload_full - preload_srt);
296
297 crack->freeram =
298 preload_size -
299 table_preload_size(tables[1], preload_init) -
300 table_preload_size(tables[2], preload_init) -
301 table_preload_size(tables[3], preload_init);
302
303 // Perform preload
304
305 preload_4test(crack, tables, ntables);
306
307 assert(crack->active->size == ntables);
308 assert(crack->remaining->size == 0);
309 assert(crack->remaining->size == crack->enabled->size - crack->active->size);
310
311 // Check the tasks
312
313 assert(crack->sched->ntasks == 1 + 4*3 + 1);
314 check_unload_task(crack->sched, NULL);
315
316 check_preload_task(crack->sched, tables[0], preload_full - preload_srt);
317 check_preload_task(crack->sched, tables[1], preload_full - preload_srt);
318 check_preload_task(crack->sched, tables[2], preload_full - preload_srt);
319 check_preload_task(crack->sched, tables[3], preload_full - preload_srt);
320
321 check_preload_task(crack->sched, NULL, preload_none);
322 assert(crack->sched->ntasks == 0);
323
324 // Check the messages
325
326 check_preload_message(NULL, preload_none, 0, preload_size);
327
328 message_t *msg = message_tryget();
329 assert(msg == NULL);
330
331 // Free memory
332
333 free_crack_4test(crack);
334 }
335
336 /* Check that if no RAM is available, all tables are active. */
337
338 void test_ophcrack_preload_all3() {
339 table_t *tables[] = {
340 // xp free fast (lmalphanum5k)
341 table_alloc(0x0fa2031c, ".", 0),
342 table_alloc(0x0fa2031c, ".", 1),
343 table_alloc(0x0fa2031c, ".", 2),
344 table_alloc(0x0fa2031c, ".", 3)
345 };
346
347 int ntables = sizeof(tables) / sizeof(table_t*);
348 ophcrack_t *crack = create_crack_4test(tables, ntables);
349
350 // Assume no RAM is available
351
352 crack->freeram = 0;
353
354 // Perform preload
355
356 preload_4test(crack, tables, ntables);
357
358 assert(crack->active->size == ntables);
359 assert(crack->remaining->size == 0);
360 assert(crack->remaining->size == crack->enabled->size - crack->active->size);
361
362 // Check the tasks
363
364 assert(crack->sched->ntasks == 1 + 4 + 1);
365
366 check_unload_task(crack->sched, NULL);
367 check_preload_task(crack->sched, tables[0], preload_init);
368 check_preload_task(crack->sched, tables[1], preload_init);
369 check_preload_task(crack->sched, tables[2], preload_init);
370 check_preload_task(crack->sched, tables[3], preload_init);
371 check_preload_task(crack->sched, NULL, preload_none);
372
373 assert(crack->sched->ntasks == 0);
374
375 // Check the messages
376
377 check_preload_message(NULL, preload_none, 0, 0);
378
379 message_t *msg = message_tryget();
380 assert(msg == NULL);
381
382 // Free memory
383
384 free_crack_4test(crack);
385 }
386
387 /* Test that only enabled tables are considered for preload. */
388
389 void test_ophcrack_preload_all4() {
390 table_t *tables[] = {
391 // xp free fast (lmalphanum5k)
392 table_alloc(0x0fa2031c, ".", 0),
393 table_alloc(0x0fa2031c, ".", 1),
394 table_alloc(0x0fa2031c, ".", 2),
395 table_alloc(0x0fa2031c, ".", 3)
396 };
397
398 int ntables = sizeof(tables) / sizeof(table_t*);
399 ophcrack_t *crack = create_crack_4test(tables, ntables);
400
401 // Assume a specific amount of RAM is availble
402
403 uint64_t preload_size =
404 table_preload_size(tables[0], preload_full) +
405 table_preload_size(tables[1], preload_full) +
406 table_preload_size(tables[2], preload_full) +
407 table_preload_size(tables[3], preload_full);
408
409 crack->freeram = preload_size;
410
411 for (int s=0; s<2; ++s) {
412 // Perform preload
413
414 if (s == 0) {
415 assert(crack->enabled->size == ntables);
416 preload_4test(crack, tables, ntables);
417
418 assert(crack->active->size == ntables);
419 assert(crack->remaining->size == 0);
420 }
421 else {
422 for (int i=0; i<ntables; ++i)
423 tables[i]->enabled = 0;
424
425 list_clear(crack->enabled);
426
427 assert(crack->enabled->size == 0);
428 preload_4test(crack, tables, 0);
429
430 assert(crack->active->size == 0);
431 assert(crack->remaining->size == 0);
432 }
433
434 assert(crack->remaining->size == crack->enabled->size - crack->active->size);
435
436 // Check the tasks
437
438 if (s == 0) {
439 assert(crack->sched->ntasks == 1 + 4*4 + 1);
440
441 while (crack->sched->ntasks > 0) {
442 ophtask_t *task = scheduler_get(crack->sched, 0);
443 ophtask_free(task);
444 scheduler_done(crack->sched, 0);
445 }
446 }
447 else {
448 assert(crack->sched->ntasks == 1);
449 check_preload_task(crack->sched, NULL, preload_none);
450 }
451
452 assert(crack->sched->ntasks == 0);
453
454 // Check the messages
455
456 if (s == 0)
457 check_preload_message(NULL, preload_none, 0, preload_size);
458 else
459 check_preload_message(NULL, preload_none, 0, 0);
460
461 message_t *msg = message_tryget();
462 assert(msg == NULL);
463 }
464
465 // Free memory
466
467 free_crack_4test(crack);
468 }
469
470 /* Test what happens when probabilistic tables cannot be initialised. */
471
472 void test_ophcrack_preload_all7() {
473 table_t *tables[] = {
474 // vista probabilistic free (ntprobafree)
475 table_alloc(0xbb00950e, ".", 0),
476 table_alloc(0xbb00950e, ".", 1),
477 table_alloc(0xbb00950e, ".", 2),
478 };
479
480 int ntables = sizeof(tables) / sizeof(table_t*);
481 ophcrack_t *crack = create_crack_4test(tables, ntables);
482
483 crack->freeram = table_preload_size(tables[0], preload_init) / 2;
484
485 preload_4test(crack, tables, ntables);
486
487 assert(crack->active->size == 0);
488 assert(crack->remaining->size == 0);
489
490 assert(crack->sched->ntasks == 1);
491
492 check_preload_task(crack->sched, NULL, preload_none);
493
494 check_preload_message(NULL, preload_none, 0, 0);
495 assert(message_tryget() == NULL);
496
497 free_crack_4test(crack);
498 }
499
500 void test_ophcrack_preload_all_idx_only() {
501 table_t *tables[] = {
502 // vista eight xl (nteightxl)
503 table_alloc(0x229e1899, ".", 0),
504 table_alloc(0x229e1899, ".", 1),
505 table_alloc(0x229e1899, ".", 2),
506 table_alloc(0x229e1899, ".", 3)
507 };
508
509 int ntables = sizeof(tables) / sizeof(table_t*);
510 ophcrack_t *crack = create_crack_4test(tables, ntables);
511
512 // Assume we have enough RAM to load only the .idx files
513
514 uint64_t preload_size =
515 table_preload_size(tables[0], preload_full - preload_end - preload_srt) +
516 table_preload_size(tables[1], preload_full - preload_end - preload_srt) +
517 table_preload_size(tables[2], preload_full - preload_end - preload_srt) +
518 table_preload_size(tables[3], preload_full - preload_end - preload_srt);
519
520 crack->freeram = preload_size;
521
522 preload_4test(crack, tables, ntables);
523
524 assert(crack->active->size == 4);
525
526 free_crack_4test(crack);
527 }
528
529 int main(int argc, char **argv) {
530 message_init();
531
532 gettimeofday(&tm_main_start, 0);
533 tm_main_total.tv_sec = 0;
534
535 test_ophcrack_preload_all0();
536 test_ophcrack_preload_all1();
537 test_ophcrack_preload_all3();
538 test_ophcrack_preload_all4();
539 test_ophcrack_preload_all7();
540 test_ophcrack_preload_all_idx_only();
541
542 return 0;
543 }