Debian Codegraph @

src/core/fsm.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 <assert.h>
34
35 #include "fsm.h"
36 /*-------------------------------------------------------------------------*/
37 fsm_t *fsm_alloc(ophcrack_t *crack) {
38 fsm_t *fsm = (fsm_t*)malloc(sizeof(fsm_t));
39
40 fsm->oldstate = st_wait;
41 fsm->state = st_wait;
42
43 fsm->crack = crack;
44 fsm->arg = crack->arg;
45
46 fsm->preload = 1;
47 fsm->bforce = fsm->arg->bforce;
48 fsm->pwait = 0;
49 fsm->ssave = 0;
50
51 fsm->pending_msg = list_alloc();
52 fsm->htoremove = list_alloc();
53
54 fsm_reset_preload(fsm);
55 fsm_reset_bforce(fsm);
56
57 return fsm;
58 }
59 /*-------------------------------------------------------------------------*/
60 message_t *fsm_next(fsm_t *fsm, message_t *msg) {
61 ophcrack_t *crack = fsm->crack;
62 scheduler_t *sched = crack->sched;
63
64 /* A table is being or has been preloaded. */
65
66 if (msg->kind == msg_preload) {
67 msg_load_t *load = (msg_load_t*)msg->data;
68 table_t *tbl = load->tbl;
69
70 /* Beginning of preloading. */
71
72 if (tbl == 0 && !load->done) {
73 fsm->psize_curr = 0;
74 fsm->psize_total = load->size;
75 }
76
77 /* The preloading of a single file is done. */
78
79 else if (tbl && load->done)
80 fsm->psize_curr += load->size;
81 }
82
83 /* The brute force has started or a brute force task has been
84 completed. */
85
86 else if (msg->kind == msg_bforce) {
87 msg_bforce_t *force = (msg_bforce_t*)msg->data;
88
89 if (!force->done) {
90 fsm->bforce_curr = 0;
91 fsm->bforce_total = force->count;
92 }
93
94 else {
95 fsm->bforce_curr += force->count;
96 assert(fsm->bforce_curr <= fsm->bforce_total);
97 }
98 }
99
100 /* All the columns of a table have been considered. */
101
102 else if (msg->kind == msg_done && ((msg_done_t*)msg->data)->kind == check) {
103
104 if (fsm->state != st_pause1 && fsm->state != st_pause2) {
105 scheduler_pause(sched);
106 fsm->pwait = sched->nthreads;
107 fsm->state = st_pause2;
108 }
109
110 fsm_reset_preload(fsm);
111 fsm->oldstate = st_start;
112 }
113
114 // Treat the other messages according to the current state.
115
116 else {
117 switch (fsm->state) {
118 case st_wait:
119 break;
120
121 case st_start:
122 break;
123
124 case st_pause1:
125 msg = fsm_handle_pause1(fsm, msg);
126 break;
127
128 case st_pause2:
129 msg = fsm_handle_pause2(fsm, msg);
130 break;
131
132 case st_preload:
133 msg = fsm_handle_preload(fsm, msg);
134 break;
135
136 case st_work1:
137 msg = fsm_handle_work1(fsm, msg);
138 break;
139
140 case st_work2:
141 msg = fsm_handle_work2(fsm, msg);
142 break;
143 }
144 }
145
146 return msg;
147 }
148 /*-------------------------------------------------------------------------*/
149 void fsm_handle_start(fsm_t *fsm) {
150 /* At this point, no work task is running or scheduled. If the brute
151 force is restarted or disabled, then it is safe to delete some
152 hashes. */
153
154 if (fsm->bforce || fsm->arg->bforce == 0) {
155 list_t *htoremove = fsm->htoremove;
156
157 while (htoremove->size > 0) {
158 hash_t *hsh = (hash_t*)list_rem_head(htoremove);
159 hash_free(hsh);
160 }
161 }
162
163 /* Create and launch a preload task. */
164
165 fsm_launch_preload(fsm);
166
167 /* Create and launch a brute force task. */
168
169 if (fsm->bforce) fsm_launch_bforce(fsm);
170
171 fsm->state = st_preload;
172 }
173 /*-------------------------------------------------------------------------*/
174 message_t *fsm_handle_preload(fsm_t *fsm, message_t *msg) {
175 ophcrack_t *crack = fsm->crack;
176 scheduler_t *sched = crack->sched;
177
178 // If the preloading of the tables is finished, then we start
179 // searching into the tables.
180
181 list_t *tasks = list_alloc();
182
183 if (msg->kind == msg_done) {
184 msg_done_t *done = (msg_done_t*)msg->data;
185
186 if (done->kind == preload_all) {
187 assert(fsm->psize_curr <= fsm->psize_total);
188
189 // Check that the tables have been correctly preloaded.
190
191 // if (fsm->psize_curr < fsm->psize_total)
192 // ophcrack_preload_check(crack);
193
194 // This is necessary to prevent the status bar from indicating
195 // that a preload task is 'waiting' if no preloading occurred.
196
197 fsm->psize_curr = 1;
198 fsm->psize_total = 1;
199
200 // Start the work tasks.
201
202 pthread_mutex_lock(sched->mutex);
203
204 int nthreads = sched->nthreads;
205 int ntasks = MY_MAX(1, nthreads-1);
206
207 for (int n=0; n<ntasks; ++n) {
208 ophtask_t *task = ophcrack_next(crack);
209
210 if (task)
211 list_add_tail(tasks, task);
212 else
213 break;
214 }
215
216 pthread_mutex_unlock(sched->mutex);
217
218 /* ... */
219
220 ntasks = tasks->size;
221
222 for (int i=0; i<ntasks; ++i) {
223 ophtask_t *task = (ophtask_t*)list_rem_head(tasks);
224 scheduler_add(sched, task, low);
225 }
226
227 assert(tasks->size == 0);
228
229 // If no tasks have been scheduled, then we schedule a fake task
230 // which will possibly trigger the 'done(all)' message if it is
231 // the last one to be executed. This trick is required since the
232 // brute force might still be running, therefore we should not
233 // stop here, but wait until the 'done(all)' message is
234 // received.
235
236 if (ntasks == 0) {
237 ophtask_t *task = ophtask_alloc(find);
238 scheduler_add(sched, task, low);
239 message_done(find);
240 }
241
242 fsm->state = st_work1;
243 }
244 }
245
246 list_free(tasks);
247
248 return msg;
249 }
250 /*-------------------------------------------------------------------------*/
251 message_t *fsm_handle_pause1(fsm_t *fsm, message_t *msg) {
252 list_t *pending_msg = fsm->pending_msg;
253
254 // A thread has entered into pause mode.
255
256 if (msg->kind == msg_pause) {
257 --fsm->pwait;
258
259 #ifndef NDEBUG
260 int pwait = fsm->pwait;
261 int nthreads = fsm->crack->sched->nthreads;
262
263 assert(pwait >= 0 && pwait <= nthreads);
264 #endif
265 }
266
267 // The other messages are accumulated.
268
269 else
270 list_add_tail(pending_msg, msg);
271
272 return 0;
273 }
274 /*-------------------------------------------------------------------------*/
275 message_t *fsm_handle_pause2(fsm_t *fsm, message_t *msg) {
276 ophcrack_t *crack = fsm->crack;
277 scheduler_t *sched = crack->sched;
278 list_t *pending_msg = fsm->pending_msg;
279
280 if (msg) {
281 // A thread has entered into pause mode.
282
283 if (msg->kind == msg_pause) {
284 --fsm->pwait;
285
286 #ifndef NDEBUG
287 int pwait = fsm->pwait;
288 int nthreads = crack->sched->nthreads;
289
290 assert(pwait >= 0 && pwait <= nthreads);
291 #endif
292 }
293
294 // The other messages are accumulated.
295
296 else {
297 list_add_tail(pending_msg, msg);
298 msg = 0;
299 }
300 }
301
302 // If all threads are in pause mode, then we can continue.
303
304 if (fsm->pwait == 0) {
305 fsm_reset(fsm);
306
307 // If the brute force has been enabled, then we schedule a brute
308 // force task. But we do not schedule a brute force task if a
309 // preload will occur because it is better if the brute force is
310 // started after the preload.
311
312 if (fsm->bforce && fsm->preload == 0) fsm_launch_bforce(fsm);
313
314 // Ask the scheduler to resume its activity. We also put back the
315 // pending messages into the message queue so that they can be
316 // processed normally.
317
318 scheduler_continue(sched);
319 message_insert_first(fsm->pending_msg);
320
321 // We go back to the state we were in before the pause. If the
322 // state has been reset to 'start', then we directly jump there.
323
324 fsm->state = fsm->preload ? st_start : fsm->oldstate;
325
326 if (fsm->state == st_start) {
327 assert(fsm->preload != 0);
328 fsm_handle_start(fsm);
329 }
330 }
331
332 return msg;
333 }
334 /*-------------------------------------------------------------------------*/
335 message_t *fsm_handle_work1(fsm_t *fsm, message_t *msg) {
336 if (msg->kind != msg_done) return msg;
337
338 msg_done_t *done = (msg_done_t*)msg->data;
339
340 if (done->kind == find)
341 fsm->state = st_work2;
342
343 return msg;
344 }
345 /*-------------------------------------------------------------------------*/
346 message_t *fsm_handle_work2(fsm_t *fsm, message_t *msg) {
347 ophcrack_t *crack = fsm->crack;
348 scheduler_t *sched = crack->sched;
349 arg_t *arg = fsm->arg;
350
351 if (msg->kind != msg_done) return msg;
352
353 msg_done_t *done = (msg_done_t*)msg->data;
354
355 if (done->kind == all) {
356 // If there are still some hashes to crack and some tables remain,
357 // then we continue.
358
359 int npwds_total = crack->npwds_total;
360 int npwds_found = crack->npwds_found;
361
362 if (crack->remaining->size > 0 && npwds_found < npwds_total) {
363 ophtask_t *task = ophtask_alloc(preload_all);
364 scheduler_add(sched, task, disk);
365
366 fsm->state = st_preload;
367 }
368
369 // Otherwise, we stop.
370
371 else {
372 if (arg->bforce) {
373 fsm->bforce_curr = 1;
374 fsm->bforce_total = 1;
375 }
376
377 fsm->state = st_wait;
378 }
379 }
380
381 return msg;
382 }
383 /*-------------------------------------------------------------------------*/
384 void fsm_ssave(fsm_t *fsm) {
385 fsm_pause(fsm);
386
387 fsm->ssave = 1;
388 fsm->state = st_pause2;
389 }
390 /*-------------------------------------------------------------------------*/
391 void fsm_pause(fsm_t *fsm) {
392 ophcrack_t *crack = fsm->crack;
393 scheduler_t *sched = crack->sched;
394
395 scheduler_pause(sched);
396
397 fsm->pwait = sched->nthreads;
398 fsm->oldstate = fsm->state;
399 fsm->state = st_pause1;
400 }
401 /*-------------------------------------------------------------------------*/
402 void fsm_reset_preload(fsm_t *fsm) {
403 fsm->preload = 1;
404 fsm->psize_curr = 1;
405 fsm->psize_total = 0;
406 }
407 /*-------------------------------------------------------------------------*/
408 void fsm_reset_bforce(fsm_t *fsm) {
409 fsm->bforce = fsm->arg->bforce;
410 fsm->bforce_curr = 1;
411 fsm->bforce_total = 0;
412 }
413 /*-------------------------------------------------------------------------*/
414 void fsm_launch_preload(fsm_t *fsm) {
415 ophcrack_t *crack = fsm->crack;
416 scheduler_t *sched = crack->sched;
417
418 ophtask_t *task = ophtask_alloc(preload_all);
419 ophload_t *load = (ophload_t*)task->data;
420
421 list_t *enabled = crack->enabled;
422 list_nd_t *tnd = 0;
423
424 for (tnd = enabled->head; tnd != 0; tnd = tnd->next)
425 list_add_tail(load->tables, tnd->data);
426
427 scheduler_add(sched, task, disk);
428 fsm->preload = 0;
429 }
430 /*-------------------------------------------------------------------------*/
431 void fsm_launch_bforce(fsm_t *fsm) {
432 ophcrack_t *crack = fsm->crack;
433 scheduler_t *sched = crack->sched;
434
435 ophtask_t *task = ophtask_alloc(bforce_all);
436 ophbforce_t *force = (ophbforce_t*)task->data;
437
438 list_t *hashes = crack->hashes;
439 list_nd_t *nd;
440
441 int nhashes = hashes->size;
442 int n;
443
444 force->nhashes = nhashes;
445 force->hashes = (hash_t**)malloc(nhashes*sizeof(hash_t*));
446
447 for (nd=hashes->head, n=0; nd!=0; nd=nd->next, ++n)
448 force->hashes[n] = (hash_t*)nd->data;
449
450 scheduler_add(sched, task, low);
451 fsm->bforce = 0;
452 }
453 /*-------------------------------------------------------------------------*/
454 void fsm_reset(fsm_t *fsm) {
455 assert(fsm->state == st_wait || fsm->state == st_pause2);
456
457 static const int bforce_mask = bforce_all + bforce_nt + bforce_lm;
458
459 ophcrack_t *crack = fsm->crack;
460 arg_t *arg = crack->arg;
461
462 // If the saving of the session has been requested, we do it.
463
464 if (arg->ssave && fsm->ssave) {
465 FILE *file = fopen(arg->sfname, "w");
466
467 if (file) {
468 ophcrack_reset(crack, all - bforce_mask, 0);
469 ophcrack_save(crack, file, 0, 1);
470 fclose(file);
471 }
472
473 fsm->ssave = 0;
474 }
475
476 // If a preload will be performed, then we remove all non-brute
477 // force tasks from the scheduler.
478
479 if (fsm->preload)
480 ophcrack_reset(crack, all - bforce_mask, 1);
481
482 // If a brute force is scheduled or if it has been disabled, then
483 // we remove all the brute force tasks from the scheduler.
484
485 if (fsm->bforce || arg->bforce == 0)
486 ophcrack_reset(crack, bforce_mask, 1);
487 }