1 /**
2  * Author......: See docs/credits.txt
3  * License.....: MIT
4  */
5 
6 #include "common.h"
7 #include "types.h"
8 #include "event.h"
9 #include "shared.h"
10 #include "wordlist.h"
11 #include "combinator.h"
12 
combinator_ctx_init(hashcat_ctx_t * hashcat_ctx)13 int combinator_ctx_init (hashcat_ctx_t *hashcat_ctx)
14 {
15   combinator_ctx_t     *combinator_ctx      = hashcat_ctx->combinator_ctx;
16   hashconfig_t         *hashconfig          = hashcat_ctx->hashconfig;
17   user_options_extra_t *user_options_extra  = hashcat_ctx->user_options_extra;
18   user_options_t       *user_options        = hashcat_ctx->user_options;
19 
20   combinator_ctx->enabled = false;
21 
22   if (user_options->hash_info      == true) return 0;
23   if (user_options->left           == true) return 0;
24   if (user_options->backend_info   == true) return 0;
25   if (user_options->show           == true) return 0;
26   if (user_options->usage          == true) return 0;
27   if (user_options->version        == true) return 0;
28 
29   if ((user_options->attack_mode != ATTACK_MODE_COMBI)
30    && (user_options->attack_mode != ATTACK_MODE_HYBRID1)
31    && (user_options->attack_mode != ATTACK_MODE_HYBRID2)) return 0;
32 
33   combinator_ctx->enabled = true;
34 
35   if (user_options->slow_candidates == true)
36   {
37     // this is always need to be COMBINATOR_MODE_BASE_LEFT
38 
39     if (user_options->attack_mode == ATTACK_MODE_COMBI)
40     {
41       // display
42 
43       char *dictfile1 = user_options_extra->hc_workv[0];
44       char *dictfile2 = user_options_extra->hc_workv[1];
45 
46       // at this point we know the file actually exist
47       // find the bigger dictionary and use as base
48 
49       if (hc_path_is_file (dictfile1) == false)
50       {
51         event_log_error (hashcat_ctx, "%s: Not a regular file.", dictfile1);
52 
53         return -1;
54       }
55 
56       if (hc_path_is_file (dictfile2) == false)
57       {
58         event_log_error (hashcat_ctx, "%s: Not a regular file.", dictfile2);
59 
60         return -1;
61       }
62 
63       HCFILE fp1;
64       HCFILE fp2;
65 
66       if (hc_fopen (&fp1, dictfile1, "rb") == false)
67       {
68         event_log_error (hashcat_ctx, "%s: %s", dictfile1, strerror (errno));
69 
70         return -1;
71       }
72 
73       if (hc_fopen (&fp2, dictfile2, "rb") == false)
74       {
75         event_log_error (hashcat_ctx, "%s: %s", dictfile2, strerror (errno));
76 
77         hc_fclose (&fp1);
78 
79         return -1;
80       }
81 
82       combinator_ctx->combs_cnt = 1;
83 
84       u64 words1_cnt = 0;
85 
86       const int rc1 = count_words (hashcat_ctx, &fp1, dictfile1, &words1_cnt);
87 
88       if (rc1 == -1)
89       {
90         event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", dictfile1);
91 
92         hc_fclose (&fp1);
93         hc_fclose (&fp2);
94 
95         return -1;
96       }
97 
98       if (words1_cnt == 0)
99       {
100         event_log_error (hashcat_ctx, "%s: empty file.", dictfile1);
101 
102         hc_fclose (&fp1);
103         hc_fclose (&fp2);
104 
105         return -1;
106       }
107 
108       combinator_ctx->combs_cnt = 1;
109 
110       u64 words2_cnt = 0;
111 
112       const int rc2 = count_words (hashcat_ctx, &fp2, dictfile2, &words2_cnt);
113 
114       hc_fclose (&fp1);
115       hc_fclose (&fp2);
116 
117       if (rc2 == -1)
118       {
119         event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", dictfile2);
120 
121         return -1;
122       }
123 
124       if (words2_cnt == 0)
125       {
126         event_log_error (hashcat_ctx, "%s: empty file.", dictfile2);
127 
128         return -1;
129       }
130 
131       combinator_ctx->dict1 = dictfile1;
132       combinator_ctx->dict2 = dictfile2;
133 
134       combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_LEFT;
135       combinator_ctx->combs_cnt  = words2_cnt;
136     }
137   }
138   else
139   {
140     if (hashconfig->opti_type & OPTI_TYPE_OPTIMIZED_KERNEL)
141     {
142       if (user_options->attack_mode == ATTACK_MODE_COMBI)
143       {
144         // display
145 
146         char *dictfile1 = user_options_extra->hc_workv[0];
147         char *dictfile2 = user_options_extra->hc_workv[1];
148 
149         // at this point we know the file actually exist
150         // find the bigger dictionary and use as base
151 
152         if (hc_path_is_file (dictfile1) == false)
153         {
154           event_log_error (hashcat_ctx, "%s: Not a regular file.", dictfile1);
155 
156           return -1;
157         }
158 
159         if (hc_path_is_file (dictfile2) == false)
160         {
161           event_log_error (hashcat_ctx, "%s: Not a regular file.", dictfile2);
162 
163           return -1;
164         }
165 
166         HCFILE fp1;
167         HCFILE fp2;
168 
169         if (hc_fopen (&fp1, dictfile1, "rb") == false)
170         {
171           event_log_error (hashcat_ctx, "%s: %s", dictfile1, strerror (errno));
172 
173           return -1;
174         }
175 
176         if (hc_fopen (&fp2, dictfile2, "rb") == false)
177         {
178           event_log_error (hashcat_ctx, "%s: %s", dictfile2, strerror (errno));
179 
180           hc_fclose (&fp1);
181 
182           return -1;
183         }
184 
185         combinator_ctx->combs_cnt = 1;
186 
187         u64 words1_cnt = 0;
188 
189         const int rc1 = count_words (hashcat_ctx, &fp1, dictfile1, &words1_cnt);
190 
191         if (rc1 == -1)
192         {
193           event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", dictfile1);
194 
195           hc_fclose (&fp1);
196           hc_fclose (&fp2);
197 
198           return -1;
199         }
200 
201         if (words1_cnt == 0)
202         {
203           event_log_error (hashcat_ctx, "%s: empty file.", dictfile1);
204 
205           hc_fclose (&fp1);
206           hc_fclose (&fp2);
207 
208           return -1;
209         }
210 
211         combinator_ctx->combs_cnt = 1;
212 
213         u64 words2_cnt = 0;
214 
215         const int rc2 = count_words (hashcat_ctx, &fp2, dictfile2, &words2_cnt);
216 
217         hc_fclose (&fp1);
218         hc_fclose (&fp2);
219 
220         if (rc2 == -1)
221         {
222           event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", dictfile2);
223 
224           return -1;
225         }
226 
227         if (words2_cnt == 0)
228         {
229           event_log_error (hashcat_ctx, "%s: empty file.", dictfile2);
230 
231           return -1;
232         }
233 
234         combinator_ctx->dict1 = dictfile1;
235         combinator_ctx->dict2 = dictfile2;
236 
237         if (words1_cnt >= words2_cnt)
238         {
239           combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_LEFT;
240           combinator_ctx->combs_cnt  = words2_cnt;
241         }
242         else
243         {
244           combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_RIGHT;
245           combinator_ctx->combs_cnt  = words1_cnt;
246 
247           // we also have to switch wordlist related rules!
248 
249           const char *tmpc = user_options->rule_buf_l;
250 
251           user_options->rule_buf_l = user_options->rule_buf_r;
252           user_options->rule_buf_r = tmpc;
253 
254           u32 tmpi = user_options_extra->rule_len_l;
255 
256           user_options_extra->rule_len_l = user_options_extra->rule_len_r;
257           user_options_extra->rule_len_r = tmpi;
258         }
259       }
260       else if (user_options->attack_mode == ATTACK_MODE_HYBRID1)
261       {
262         combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_LEFT;
263       }
264       else if (user_options->attack_mode == ATTACK_MODE_HYBRID2)
265       {
266         combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_RIGHT;
267       }
268     }
269     else
270     {
271       // this is always need to be COMBINATOR_MODE_BASE_LEFT
272 
273       if (user_options->attack_mode == ATTACK_MODE_COMBI)
274       {
275         // display
276 
277         char *dictfile1 = user_options_extra->hc_workv[0];
278         char *dictfile2 = user_options_extra->hc_workv[1];
279 
280         // at this point we know the file actually exist
281         // find the bigger dictionary and use as base
282 
283         if (hc_path_is_file (dictfile1) == false)
284         {
285           event_log_error (hashcat_ctx, "%s: Not a regular file.", dictfile1);
286 
287           return -1;
288         }
289 
290         if (hc_path_is_file (dictfile2) == false)
291         {
292           event_log_error (hashcat_ctx, "%s: Not a regular file.", dictfile2);
293 
294           return -1;
295         }
296 
297         HCFILE fp1;
298         HCFILE fp2;
299 
300         if (hc_fopen (&fp1, dictfile1, "rb") == false)
301         {
302           event_log_error (hashcat_ctx, "%s: %s", dictfile1, strerror (errno));
303 
304           return -1;
305         }
306 
307         if (hc_fopen (&fp2, dictfile2, "rb") == false)
308         {
309           event_log_error (hashcat_ctx, "%s: %s", dictfile2, strerror (errno));
310 
311           hc_fclose (&fp1);
312 
313           return -1;
314         }
315 
316         combinator_ctx->combs_cnt = 1;
317 
318         u64 words1_cnt = 0;
319 
320         const int rc1 = count_words (hashcat_ctx, &fp1, dictfile1, &words1_cnt);
321 
322         if (rc1 == -1)
323         {
324           event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", dictfile1);
325 
326           hc_fclose (&fp1);
327           hc_fclose (&fp2);
328 
329           return -1;
330         }
331 
332         if (words1_cnt == 0)
333         {
334           event_log_error (hashcat_ctx, "%s: empty file.", dictfile1);
335 
336           hc_fclose (&fp1);
337           hc_fclose (&fp2);
338 
339           return -1;
340         }
341 
342         combinator_ctx->combs_cnt = 1;
343 
344         u64 words2_cnt = 0;
345 
346         const int rc2 = count_words (hashcat_ctx, &fp2, dictfile2, &words2_cnt);
347 
348         hc_fclose (&fp1);
349         hc_fclose (&fp2);
350 
351         if (rc2 == -1)
352         {
353           event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", dictfile2);
354 
355           return -1;
356         }
357 
358         if (words2_cnt == 0)
359         {
360           event_log_error (hashcat_ctx, "%s: empty file.", dictfile2);
361 
362           return -1;
363         }
364 
365         combinator_ctx->dict1 = dictfile1;
366         combinator_ctx->dict2 = dictfile2;
367 
368         combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_LEFT;
369         combinator_ctx->combs_cnt  = words2_cnt;
370       }
371       else if (user_options->attack_mode == ATTACK_MODE_HYBRID1)
372       {
373         combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_LEFT;
374       }
375       else if (user_options->attack_mode == ATTACK_MODE_HYBRID2)
376       {
377         mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
378 
379         char *dictfile = user_options_extra->hc_workv[1];
380 
381         // at this point we know the file actually exist
382 
383         if (hc_path_is_file (dictfile) == false)
384         {
385           event_log_error (hashcat_ctx, "%s: Not a regular file.", dictfile);
386 
387           return -1;
388         }
389 
390         HCFILE fp;
391 
392         if (hc_fopen (&fp, dictfile, "rb") == false)
393         {
394           event_log_error (hashcat_ctx, "%s: %s", dictfile, strerror (errno));
395 
396           return -1;
397         }
398 
399         mask_ctx->bfs_cnt = 1;
400 
401         u64 words_cnt = 0;
402 
403         const int rc = count_words (hashcat_ctx, &fp, dictfile, &words_cnt);
404 
405         hc_fclose (&fp);
406 
407         if (rc == -1)
408         {
409           event_log_error (hashcat_ctx, "Integer overflow detected in keyspace of wordlist: %s", dictfile);
410 
411           return -1;
412         }
413 
414         combinator_ctx->combs_cnt  = words_cnt;
415         combinator_ctx->combs_mode = COMBINATOR_MODE_BASE_LEFT;
416       }
417     }
418   }
419 
420   return 0;
421 }
422 
combinator_ctx_destroy(hashcat_ctx_t * hashcat_ctx)423 void combinator_ctx_destroy (hashcat_ctx_t *hashcat_ctx)
424 {
425   combinator_ctx_t *combinator_ctx = hashcat_ctx->combinator_ctx;
426 
427   if (combinator_ctx->enabled == false) return;
428 
429   memset (combinator_ctx, 0, sizeof (combinator_ctx_t));
430 }
431