1 /**
2  * Author......: See docs/credits.txt
3  * License.....: MIT
4  */
5 
6 #include "common.h"
7 #include "types.h"
8 #include "memory.h"
9 #include "event.h"
10 #include "convert.h"
11 #include "debugfile.h"
12 #include "filehandling.h"
13 #include "hlfmt.h"
14 #include "terminal.h"
15 #include "logfile.h"
16 #include "loopback.h"
17 #include "backend.h"
18 #include "outfile.h"
19 #include "potfile.h"
20 #include "rp.h"
21 #include "shared.h"
22 #include "thread.h"
23 #include "locking.h"
24 #include "hashes.h"
25 
26 #ifdef WITH_BRAIN
27 #include "brain.h"
28 #endif
29 
sort_by_digest_p0p1(const void * v1,const void * v2,void * v3)30 int sort_by_digest_p0p1 (const void *v1, const void *v2, void *v3)
31 {
32   const u32 *d1 = (const u32 *) v1;
33   const u32 *d2 = (const u32 *) v2;
34 
35   hashconfig_t *hashconfig = (hashconfig_t *) v3;
36 
37   const u32 dgst_pos0 = hashconfig->dgst_pos0;
38   const u32 dgst_pos1 = hashconfig->dgst_pos1;
39   const u32 dgst_pos2 = hashconfig->dgst_pos2;
40   const u32 dgst_pos3 = hashconfig->dgst_pos3;
41 
42   if (d1[dgst_pos3] > d2[dgst_pos3]) return  1;
43   if (d1[dgst_pos3] < d2[dgst_pos3]) return -1;
44   if (d1[dgst_pos2] > d2[dgst_pos2]) return  1;
45   if (d1[dgst_pos2] < d2[dgst_pos2]) return -1;
46   if (d1[dgst_pos1] > d2[dgst_pos1]) return  1;
47   if (d1[dgst_pos1] < d2[dgst_pos1]) return -1;
48   if (d1[dgst_pos0] > d2[dgst_pos0]) return  1;
49   if (d1[dgst_pos0] < d2[dgst_pos0]) return -1;
50 
51   return 0;
52 }
53 
sort_by_salt(const void * v1,const void * v2)54 int sort_by_salt (const void *v1, const void *v2)
55 {
56   const salt_t *s1 = (const salt_t *) v1;
57   const salt_t *s2 = (const salt_t *) v2;
58 
59   const int res_pos = (int) s1->orig_pos - (int) s2->orig_pos;
60 
61   if (res_pos != 0) return (res_pos);
62 
63   const int res1 = (int) s1->salt_len - (int) s2->salt_len;
64 
65   if (res1 != 0) return (res1);
66 
67   const int res2 = (int) s1->salt_iter - (int) s2->salt_iter;
68 
69   if (res2 != 0) return (res2);
70 
71   for (int n = 0; n < 64; n++)
72   {
73     if (s1->salt_buf[n] > s2->salt_buf[n]) return  1;
74     if (s1->salt_buf[n] < s2->salt_buf[n]) return -1;
75   }
76 
77   for (int n = 0; n < 64; n++)
78   {
79     if (s1->salt_buf_pc[n] > s2->salt_buf_pc[n]) return  1;
80     if (s1->salt_buf_pc[n] < s2->salt_buf_pc[n]) return -1;
81   }
82 
83   return 0;
84 }
85 
sort_by_hash(const void * v1,const void * v2,void * v3)86 int sort_by_hash (const void *v1, const void *v2, void *v3)
87 {
88   const hash_t *h1 = (const hash_t *) v1;
89   const hash_t *h2 = (const hash_t *) v2;
90 
91   hashconfig_t *hashconfig = (hashconfig_t *) v3;
92 
93   if (hashconfig->is_salted == true)
94   {
95     const salt_t *s1 = h1->salt;
96     const salt_t *s2 = h2->salt;
97 
98     int res = sort_by_salt (s1, s2);
99 
100     if (res != 0) return (res);
101   }
102 
103   const void *d1 = h1->digest;
104   const void *d2 = h2->digest;
105 
106   return sort_by_digest_p0p1 (d1, d2, v3);
107 }
108 
sort_by_hash_no_salt(const void * v1,const void * v2,void * v3)109 int sort_by_hash_no_salt (const void *v1, const void *v2, void *v3)
110 {
111   const hash_t *h1 = (const hash_t *) v1;
112   const hash_t *h2 = (const hash_t *) v2;
113 
114   const void *d1 = h1->digest;
115   const void *d2 = h2->digest;
116 
117   return sort_by_digest_p0p1 (d1, d2, v3);
118 }
119 
hash_encode(const hashconfig_t * hashconfig,const hashes_t * hashes,const module_ctx_t * module_ctx,char * out_buf,const int out_size,const u32 salt_pos,const u32 digest_pos)120 int hash_encode (const hashconfig_t *hashconfig, const hashes_t *hashes, const module_ctx_t *module_ctx, char *out_buf, const int out_size, const u32 salt_pos, const u32 digest_pos)
121 {
122   if (module_ctx->module_hash_encode == MODULE_DEFAULT)
123   {
124     return snprintf (out_buf, out_size, "%s", hashes->hashfile);
125   }
126 
127   salt_t *salts_buf = hashes->salts_buf;
128 
129   salts_buf += salt_pos;
130 
131   const u32 digest_cur = salts_buf->digests_offset + digest_pos;
132 
133   void        *digests_buf    = hashes->digests_buf;
134   void        *esalts_buf     = hashes->esalts_buf;
135   void        *hook_salts_buf = hashes->hook_salts_buf;
136   hashinfo_t **hash_info      = hashes->hash_info;
137 
138   char       *digests_buf_ptr    = (char *) digests_buf;
139   char       *esalts_buf_ptr     = (char *) esalts_buf;
140   char       *hook_salts_buf_ptr = (char *) hook_salts_buf;
141   hashinfo_t *hash_info_ptr      = NULL;
142 
143   digests_buf_ptr    += digest_cur * hashconfig->dgst_size;
144   esalts_buf_ptr     += digest_cur * hashconfig->esalt_size;
145   hook_salts_buf_ptr += digest_cur * hashconfig->hook_salt_size;
146 
147   if (hash_info) hash_info_ptr = hash_info[digest_cur];
148 
149   const int out_len = module_ctx->module_hash_encode
150   (
151     hashconfig,
152     digests_buf_ptr,
153     salts_buf,
154     esalts_buf_ptr,
155     hook_salts_buf_ptr,
156     hash_info_ptr,
157     out_buf,
158     out_size
159   );
160 
161   return out_len;
162 }
163 
save_hash(hashcat_ctx_t * hashcat_ctx)164 int save_hash (hashcat_ctx_t *hashcat_ctx)
165 {
166   hashes_t        *hashes       = hashcat_ctx->hashes;
167   hashconfig_t    *hashconfig   = hashcat_ctx->hashconfig;
168   module_ctx_t    *module_ctx   = hashcat_ctx->module_ctx;
169   user_options_t  *user_options = hashcat_ctx->user_options;
170 
171   const char *hashfile = hashes->hashfile;
172 
173   char *new_hashfile;
174   char *old_hashfile;
175 
176   hc_asprintf (&new_hashfile, "%s.new", hashfile);
177   hc_asprintf (&old_hashfile, "%s.old", hashfile);
178 
179   unlink (new_hashfile);
180 
181   char separator = hashconfig->separator;
182 
183   HCFILE fp;
184 
185   if (hc_fopen (&fp, new_hashfile, "wb") == false)
186   {
187     event_log_error (hashcat_ctx, "%s: %s", new_hashfile, strerror (errno));
188 
189     hcfree (new_hashfile);
190     hcfree (old_hashfile);
191 
192     return -1;
193   }
194 
195   if (hc_lockfile (&fp) == -1)
196   {
197     hc_fclose (&fp);
198 
199     event_log_error (hashcat_ctx, "%s: %s", new_hashfile, strerror (errno));
200 
201     hcfree (new_hashfile);
202     hcfree (old_hashfile);
203 
204     return -1;
205   }
206 
207   u8 *out_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
208 
209   for (u32 salt_pos = 0; salt_pos < hashes->salts_cnt; salt_pos++)
210   {
211     if (hashes->salts_shown[salt_pos] == 1) continue;
212 
213     salt_t *salt_buf = &hashes->salts_buf[salt_pos];
214 
215     for (u32 digest_pos = 0; digest_pos < salt_buf->digests_cnt; digest_pos++)
216     {
217       const u32 idx = salt_buf->digests_offset + digest_pos;
218 
219       if (hashes->digests_shown[idx] == 1) continue;
220 
221       if (module_ctx->module_hash_binary_save != MODULE_DEFAULT)
222       {
223         char *binary_buf = NULL;
224 
225         const int binary_len = module_ctx->module_hash_binary_save (hashes, salt_pos, digest_pos, &binary_buf);
226 
227         hc_fwrite (binary_buf, binary_len, 1, &fp);
228 
229         hcfree (binary_buf);
230       }
231       else
232       {
233         if (user_options->username == true)
234         {
235           user_t *user = hashes->hash_info[idx]->user;
236 
237           u32 i;
238 
239           for (i = 0; i < user->user_len; i++) hc_fputc (user->user_name[i], &fp);
240 
241           hc_fputc (separator, &fp);
242         }
243 
244         const int out_len = hash_encode (hashcat_ctx->hashconfig, hashcat_ctx->hashes, hashcat_ctx->module_ctx, (char *) out_buf, HCBUFSIZ_LARGE, salt_pos, digest_pos);
245 
246         out_buf[out_len] = 0;
247 
248         hc_fprintf (&fp, "%s" EOL, out_buf);
249       }
250     }
251   }
252 
253   hcfree (out_buf);
254 
255   hc_fflush (&fp);
256 
257   if (hc_unlockfile (&fp) == -1)
258   {
259     hc_fclose (&fp);
260 
261     event_log_error (hashcat_ctx, "%s: %s", new_hashfile, strerror (errno));
262 
263     hcfree (new_hashfile);
264     hcfree (old_hashfile);
265 
266     return -1;
267   }
268 
269   hc_fclose (&fp);
270 
271   unlink (old_hashfile);
272 
273   if (rename (hashfile, old_hashfile) != 0)
274   {
275     event_log_error (hashcat_ctx, "Rename file '%s' to '%s': %s", hashfile, old_hashfile, strerror (errno));
276 
277     hcfree (new_hashfile);
278     hcfree (old_hashfile);
279 
280     return -1;
281   }
282 
283   unlink (hashfile);
284 
285   if (rename (new_hashfile, hashfile) != 0)
286   {
287     event_log_error (hashcat_ctx, "Rename file '%s' to '%s': %s", new_hashfile, hashfile, strerror (errno));
288 
289     hcfree (new_hashfile);
290     hcfree (old_hashfile);
291 
292     return -1;
293   }
294 
295   unlink (old_hashfile);
296 
297   hcfree (new_hashfile);
298   hcfree (old_hashfile);
299 
300   return 0;
301 }
302 
check_hash(hashcat_ctx_t * hashcat_ctx,hc_device_param_t * device_param,plain_t * plain)303 int check_hash (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain)
304 {
305   const debugfile_ctx_t *debugfile_ctx = hashcat_ctx->debugfile_ctx;
306   const hashes_t        *hashes        = hashcat_ctx->hashes;
307   const hashconfig_t    *hashconfig    = hashcat_ctx->hashconfig;
308   const loopback_ctx_t  *loopback_ctx  = hashcat_ctx->loopback_ctx;
309   const module_ctx_t    *module_ctx    = hashcat_ctx->module_ctx;
310 
311   const u32 salt_pos    = plain->salt_pos;
312   const u32 digest_pos  = plain->digest_pos;  // relative
313 
314   void *tmps = NULL;
315 
316   cl_event opencl_event;
317 
318   int rc = -1;
319 
320   if (hashconfig->opts_type & OPTS_TYPE_COPY_TMPS)
321   {
322     tmps = hcmalloc (hashconfig->tmp_size);
323 
324     if (device_param->is_cuda == true)
325     {
326       rc = hc_cuMemcpyDtoHAsync (hashcat_ctx, tmps, device_param->cuda_d_tmps + (plain->gidvid * hashconfig->tmp_size), hashconfig->tmp_size, device_param->cuda_stream);
327 
328       if (rc == 0)
329       {
330         rc = hc_cuEventRecord (hashcat_ctx, device_param->cuda_event3, device_param->cuda_stream);
331       }
332 
333       if (rc == -1)
334       {
335         hcfree (tmps);
336 
337         return -1;
338       }
339     }
340 
341     if (device_param->is_hip == true)
342     {
343       rc = hc_hipMemcpyDtoHAsync (hashcat_ctx, tmps, device_param->hip_d_tmps + (plain->gidvid * hashconfig->tmp_size), hashconfig->tmp_size, device_param->hip_stream);
344 
345       if (rc == 0)
346       {
347         rc = hc_hipEventRecord (hashcat_ctx, device_param->hip_event3, device_param->hip_stream);
348       }
349 
350       if (rc == -1)
351       {
352         hcfree (tmps);
353 
354         return -1;
355       }
356     }
357 
358     if (device_param->is_opencl == true)
359     {
360       rc = hc_clEnqueueReadBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_tmps, CL_FALSE, plain->gidvid * hashconfig->tmp_size, hashconfig->tmp_size, tmps, 0, NULL, &opencl_event);
361 
362       if (rc == 0)
363       {
364         rc = hc_clFlush (hashcat_ctx, device_param->opencl_command_queue);
365       }
366 
367       if (rc == -1)
368       {
369         hcfree (tmps);
370 
371         return -1;
372       }
373     }
374   }
375 
376   // hash
377 
378   u8 *out_buf = hashes->out_buf;
379 
380   int out_len = hash_encode (hashconfig, hashes, module_ctx, (char *) out_buf, HCBUFSIZ_LARGE, salt_pos, digest_pos);
381 
382   out_buf[out_len] = 0;
383 
384   // plain
385 
386   u8 plain_buf[HCBUFSIZ_TINY] = { 0 }; // while the password itself can have only length 256, the module could encode it with something like base64 which inflates the requires buffer size
387   u8 postprocess_buf[HCBUFSIZ_TINY] = { 0 };
388 
389   u8 *plain_ptr = plain_buf;
390 
391   int plain_len = 0;
392 
393   build_plain (hashcat_ctx, device_param, plain, (u32 *) plain_buf, &plain_len);
394 
395   if (module_ctx->module_build_plain_postprocess != MODULE_DEFAULT)
396   {
397     if (hashconfig->opts_type & OPTS_TYPE_COPY_TMPS)
398     {
399       if (device_param->is_cuda == true)
400       {
401         if (hc_cuEventSynchronize (hashcat_ctx, device_param->cuda_event3) == -1) return -1;
402       }
403 
404       if (device_param->is_hip == true)
405       {
406         if (hc_hipEventSynchronize (hashcat_ctx, device_param->hip_event3) == -1) return -1;
407       }
408 
409       if (device_param->is_opencl == true)
410       {
411         if (hc_clWaitForEvents (hashcat_ctx, 1, &opencl_event) == -1) return -1;
412       }
413     }
414 
415     plain_len = module_ctx->module_build_plain_postprocess (hashconfig, hashes, tmps, (u32 *) plain_buf, sizeof (plain_buf), plain_len, (u32 *) postprocess_buf, sizeof (postprocess_buf));
416 
417     plain_ptr = postprocess_buf;
418   }
419 
420   // crackpos
421 
422   u64 crackpos = 0;
423 
424   build_crackpos (hashcat_ctx, device_param, plain, &crackpos);
425 
426   // debug
427 
428   u8  debug_rule_buf[RP_PASSWORD_SIZE] = { 0 };
429   int debug_rule_len  = 0; // -1 error
430 
431   u8  debug_plain_ptr[RP_PASSWORD_SIZE] = { 0 };
432   int debug_plain_len = 0;
433 
434   build_debugdata (hashcat_ctx, device_param, plain, debug_rule_buf, &debug_rule_len, debug_plain_ptr, &debug_plain_len);
435 
436   // outfile, can be either to file or stdout
437   // if an error occurs opening the file, send to stdout as fallback
438   // the fp gets opened for each cracked hash so that the user can modify (move) the outfile while hashcat runs
439 
440   outfile_write_open (hashcat_ctx);
441 
442   u8 *tmp_buf = hashes->tmp_buf;
443 
444   tmp_buf[0] = 0;
445 
446   const int tmp_len = outfile_write (hashcat_ctx, (char *) out_buf, out_len, plain_ptr, plain_len, crackpos, NULL, 0, true, (char *) tmp_buf);
447 
448   EVENT_DATA (EVENT_CRACKER_HASH_CRACKED, tmp_buf, tmp_len);
449 
450   outfile_write_close (hashcat_ctx);
451 
452   // potfile
453   // we can have either used-defined hooks or reuse the same format as input format
454   // no need for locking, we're in a mutex protected function
455 
456   if (module_ctx->module_hash_encode_potfile != MODULE_DEFAULT)
457   {
458     if (hashconfig->opts_type & OPTS_TYPE_COPY_TMPS)
459     {
460       if (device_param->is_cuda == true)
461       {
462         if (hc_cuEventSynchronize (hashcat_ctx, device_param->cuda_event3) == -1) return -1;
463       }
464 
465       if (device_param->is_hip == true)
466       {
467         if (hc_hipEventSynchronize (hashcat_ctx, device_param->hip_event3) == -1) return -1;
468       }
469 
470       if (device_param->is_opencl == true)
471       {
472         if (hc_clWaitForEvents (hashcat_ctx, 1, &opencl_event) == -1) return -1;
473       }
474     }
475 
476     salt_t *salts_buf = hashes->salts_buf;
477 
478     salts_buf += salt_pos;
479 
480     const u32 digest_cur = salts_buf->digests_offset + digest_pos;
481 
482     void        *digests_buf    = hashes->digests_buf;
483     void        *esalts_buf     = hashes->esalts_buf;
484     void        *hook_salts_buf = hashes->hook_salts_buf;
485     hashinfo_t **hash_info      = hashes->hash_info;
486 
487     char       *digests_buf_ptr    = (char *) digests_buf;
488     char       *esalts_buf_ptr     = (char *) esalts_buf;
489     char       *hook_salts_buf_ptr = (char *) hook_salts_buf;
490     hashinfo_t *hash_info_ptr      = NULL;
491 
492     digests_buf_ptr    += digest_cur * hashconfig->dgst_size;
493     esalts_buf_ptr     += digest_cur * hashconfig->esalt_size;
494     hook_salts_buf_ptr += digest_cur * hashconfig->hook_salt_size;
495 
496     if (hash_info) hash_info_ptr = hash_info[digest_cur];
497 
498     out_len = module_ctx->module_hash_encode_potfile
499     (
500       hashconfig,
501       digests_buf_ptr,
502       salts_buf,
503       esalts_buf_ptr,
504       hook_salts_buf_ptr,
505       hash_info_ptr,
506       (char *) out_buf,
507       HCBUFSIZ_LARGE,
508       tmps
509     );
510 
511     out_buf[out_len] = 0;
512   }
513 
514   potfile_write_append (hashcat_ctx, (char *) out_buf, out_len, plain_ptr, plain_len);
515 
516   // if enabled, update also the loopback file
517 
518   if (loopback_ctx->fp.pfp != NULL)
519   {
520     loopback_write_append (hashcat_ctx, plain_ptr, plain_len);
521   }
522 
523   // if enabled, update also the (rule) debug file
524 
525   if (debugfile_ctx->fp.pfp != NULL)
526   {
527     // the next check implies that:
528     // - (user_options->attack_mode == ATTACK_MODE_STRAIGHT)
529     // - debug_mode > 0
530 
531     if ((debug_plain_len > 0) || (debug_rule_len > 0))
532     {
533       debugfile_write_append (hashcat_ctx, debug_rule_buf, debug_rule_len, plain_ptr, plain_len, debug_plain_ptr, debug_plain_len);
534     }
535   }
536 
537   if (hashconfig->opts_type & OPTS_TYPE_COPY_TMPS)
538   {
539     hcfree (tmps);
540 
541     if (device_param->is_opencl == true)
542     {
543       if (hc_clReleaseEvent (hashcat_ctx, opencl_event) == -1) return -1;
544     }
545   }
546 
547   return 0;
548 }
549 
550 //int check_cracked (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, const u32 salt_pos)
check_cracked(hashcat_ctx_t * hashcat_ctx,hc_device_param_t * device_param)551 int check_cracked (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param)
552 {
553   cpt_ctx_t      *cpt_ctx      = hashcat_ctx->cpt_ctx;
554   hashconfig_t   *hashconfig   = hashcat_ctx->hashconfig;
555   hashes_t       *hashes       = hashcat_ctx->hashes;
556   status_ctx_t   *status_ctx   = hashcat_ctx->status_ctx;
557   user_options_t *user_options = hashcat_ctx->user_options;
558 
559   u32 num_cracked = 0;
560 
561   int rc = -1;
562 
563   if (device_param->is_cuda == true)
564   {
565     if (hc_cuMemcpyDtoHAsync (hashcat_ctx, &num_cracked, device_param->cuda_d_result, sizeof (u32), device_param->cuda_stream) == -1) return -1;
566 
567     if (hc_cuStreamSynchronize (hashcat_ctx, device_param->cuda_stream) == -1) return -1;
568   }
569 
570   if (device_param->is_hip == true)
571   {
572     if (hc_hipMemcpyDtoHAsync (hashcat_ctx, &num_cracked, device_param->hip_d_result, sizeof (u32), device_param->hip_stream) == -1) return -1;
573 
574     if (hc_hipStreamSynchronize (hashcat_ctx, device_param->hip_stream) == -1) return -1;
575   }
576 
577   if (device_param->is_opencl == true)
578   {
579     /* blocking */
580     if (hc_clEnqueueReadBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_result, CL_TRUE, 0, sizeof (u32), &num_cracked, 0, NULL, NULL) == -1) return -1;
581   }
582 
583   if (num_cracked == 0 || user_options->speed_only == true)
584   {
585     // we want to get the num_cracked in benchmark mode because it has an influence in performance
586     // however if the benchmark cracks the artificial hash used for benchmarks we don't want to see that!
587 
588     return 0;
589   }
590 
591   plain_t *cracked = (plain_t *) hcmalloc (num_cracked * sizeof (plain_t));
592 
593   if (device_param->is_cuda == true)
594   {
595     rc = hc_cuMemcpyDtoHAsync (hashcat_ctx, cracked, device_param->cuda_d_plain_bufs, num_cracked * sizeof (plain_t), device_param->cuda_stream);
596 
597     if (rc == 0)
598     {
599       rc = hc_cuStreamSynchronize (hashcat_ctx, device_param->cuda_stream);
600     }
601 
602     if (rc == -1)
603     {
604       hcfree (cracked);
605 
606       return -1;
607     }
608   }
609 
610   if (device_param->is_hip == true)
611   {
612     rc = hc_hipMemcpyDtoHAsync (hashcat_ctx, cracked, device_param->hip_d_plain_bufs, num_cracked * sizeof (plain_t), device_param->hip_stream);
613 
614     if (rc == 0)
615     {
616       rc = hc_hipStreamSynchronize (hashcat_ctx, device_param->hip_stream);
617     }
618 
619     if (rc == -1)
620     {
621       hcfree (cracked);
622 
623       return -1;
624     }
625   }
626 
627   if (device_param->is_opencl == true)
628   {
629     /* blocking */
630     rc = hc_clEnqueueReadBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_plain_bufs, CL_TRUE, 0, num_cracked * sizeof (plain_t), cracked, 0, NULL, NULL);
631 
632     if (rc == -1)
633     {
634       hcfree (cracked);
635 
636       return -1;
637     }
638   }
639 
640   u32 cpt_cracked = 0;
641 
642   hc_thread_mutex_lock (status_ctx->mux_display);
643 
644   for (u32 i = 0; i < num_cracked; i++)
645   {
646     const u32 hash_pos = cracked[i].hash_pos;
647 
648     if (hashes->digests_shown[hash_pos] == 1) continue;
649 
650     const u32 salt_pos = cracked[i].salt_pos;
651     salt_t *salt_buf = &hashes->salts_buf[salt_pos];
652 
653     if ((hashconfig->opts_type & OPTS_TYPE_PT_NEVERCRACK) == 0)
654     {
655       hashes->digests_shown[hash_pos] = 1;
656 
657       hashes->digests_done++;
658 
659       cpt_cracked++;
660 
661       salt_buf->digests_done++;
662 
663       if (salt_buf->digests_done == salt_buf->digests_cnt)
664       {
665         hashes->salts_shown[salt_pos] = 1;
666 
667         hashes->salts_done++;
668       }
669     }
670 
671     if (hashes->salts_done == hashes->salts_cnt) mycracked (hashcat_ctx);
672 
673     rc = check_hash (hashcat_ctx, device_param, &cracked[i]);
674 
675     if (rc == -1)
676     {
677       break;
678     }
679 
680     if (hashconfig->opts_type & OPTS_TYPE_PT_NEVERCRACK)
681     {
682       // we need to reset cracked state on the device
683       // otherwise host thinks again and again the hash was cracked
684       // and returns invalid password each time
685 
686       if (device_param->is_cuda == true)
687       {
688         rc = run_cuda_kernel_bzero (hashcat_ctx, device_param, device_param->cuda_d_digests_shown + (salt_buf->digests_offset * sizeof (u32)), salt_buf->digests_cnt * sizeof (u32));
689 
690         if (rc == -1)
691         {
692           break;
693         }
694       }
695 
696       if (device_param->is_hip == true)
697       {
698         rc = run_hip_kernel_bzero (hashcat_ctx, device_param, device_param->hip_d_digests_shown + (salt_buf->digests_offset * sizeof (u32)), salt_buf->digests_cnt * sizeof (u32));
699 
700         if (rc == -1)
701         {
702           break;
703         }
704       }
705 
706       if (device_param->is_opencl == true)
707       {
708         /* NOTE: run_opencl_kernel_bzero() does not handle buffer offset */
709         rc = run_opencl_kernel_memset32 (hashcat_ctx, device_param, device_param->opencl_d_digests_shown, salt_buf->digests_offset * sizeof (u32), 0, salt_buf->digests_cnt * sizeof (u32));
710 
711         if (rc == -1)
712         {
713           break;
714         }
715       }
716     }
717   }
718 
719   hc_thread_mutex_unlock (status_ctx->mux_display);
720 
721   hcfree (cracked);
722 
723   if (rc == -1)
724   {
725     return -1;
726   }
727 
728   if (cpt_cracked > 0)
729   {
730     hc_thread_mutex_lock (status_ctx->mux_display);
731 
732     cpt_ctx->cpt_buf[cpt_ctx->cpt_pos].timestamp = time (NULL);
733     cpt_ctx->cpt_buf[cpt_ctx->cpt_pos].cracked   = cpt_cracked;
734 
735     cpt_ctx->cpt_pos++;
736 
737     cpt_ctx->cpt_total += cpt_cracked;
738 
739     if (cpt_ctx->cpt_pos == CPT_CACHE) cpt_ctx->cpt_pos = 0;
740 
741     hc_thread_mutex_unlock (status_ctx->mux_display);
742   }
743 
744   if (device_param->is_cuda == true)
745   {
746     if (run_cuda_kernel_bzero (hashcat_ctx, device_param, device_param->cuda_d_result, sizeof (u32)) == -1) return -1;
747   }
748 
749   if (device_param->is_hip == true)
750   {
751     if (run_hip_kernel_bzero (hashcat_ctx, device_param, device_param->hip_d_result, sizeof (u32)) == -1) return -1;
752   }
753 
754   if (device_param->is_opencl == true)
755   {
756     if (run_opencl_kernel_bzero (hashcat_ctx, device_param, device_param->opencl_d_result, sizeof (u32)) == -1) return -1;
757 
758     if (hc_clFlush (hashcat_ctx, device_param->opencl_command_queue) == -1) return -1;
759   }
760 
761   return 0;
762 }
763 
hashes_init_filename(hashcat_ctx_t * hashcat_ctx)764 int hashes_init_filename (hashcat_ctx_t *hashcat_ctx)
765 {
766   hashconfig_t         *hashconfig         = hashcat_ctx->hashconfig;
767   hashes_t             *hashes             = hashcat_ctx->hashes;
768   user_options_t       *user_options       = hashcat_ctx->user_options;
769   user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
770 
771   if (user_options->benchmark == true) return 0;
772 
773   /**
774    * load hashes, part I: find input mode, count hashes
775    */
776 
777   if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE)
778   {
779     if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE_OPTIONAL)
780     {
781       if ((user_options->benchmark == false) && (user_options->keyspace == false))
782       {
783         hashes->hashlist_mode = (hc_path_exist (user_options_extra->hc_hash) == true) ? HL_MODE_FILE_PLAIN : HL_MODE_ARG;
784 
785         if (hashes->hashlist_mode == HL_MODE_FILE_PLAIN)
786         {
787           hashes->hashfile = user_options_extra->hc_hash;
788         }
789       }
790     }
791     else
792     {
793       hashes->hashlist_mode = HL_MODE_FILE_BINARY;
794 
795       if ((user_options->benchmark == false) && (user_options->keyspace == false))
796       {
797         if (hc_path_read (user_options_extra->hc_hash) == false)
798         {
799           event_log_error (hashcat_ctx, "%s: %s", user_options_extra->hc_hash, strerror (errno));
800 
801           return -1;
802         }
803 
804         hashes->hashfile = user_options_extra->hc_hash;
805       }
806     }
807   }
808   else
809   {
810     hashes->hashlist_mode = (hc_path_exist (user_options_extra->hc_hash) == true) ? HL_MODE_FILE_PLAIN : HL_MODE_ARG;
811 
812     if (hashes->hashlist_mode == HL_MODE_FILE_PLAIN)
813     {
814       hashes->hashfile = user_options_extra->hc_hash;
815     }
816   }
817 
818   return 0;
819 }
820 
hashes_init_stage1(hashcat_ctx_t * hashcat_ctx)821 int hashes_init_stage1 (hashcat_ctx_t *hashcat_ctx)
822 {
823   hashconfig_t          *hashconfig         = hashcat_ctx->hashconfig;
824   hashes_t              *hashes             = hashcat_ctx->hashes;
825   module_ctx_t          *module_ctx         = hashcat_ctx->module_ctx;
826   user_options_t        *user_options       = hashcat_ctx->user_options;
827   user_options_extra_t  *user_options_extra = hashcat_ctx->user_options_extra;
828 
829   /**
830    * load hashes, part I: find input mode, count hashes
831    */
832 
833   const char *hashfile      = hashes->hashfile;
834   const u32   hashlist_mode = hashes->hashlist_mode;
835 
836   u32 hashlist_format = HLFMT_HASHCAT;
837 
838   u64 hashes_avail = 0;
839 
840   if ((user_options->benchmark == false) && (user_options->stdout_flag == false) && (user_options->keyspace == false))
841   {
842     if (hashlist_mode == HL_MODE_ARG)
843     {
844       hashes_avail = 1;
845     }
846     else if (hashlist_mode == HL_MODE_FILE_PLAIN)
847     {
848       HCFILE fp;
849 
850       if (hc_fopen (&fp, hashfile, "rb") == false)
851       {
852         event_log_error (hashcat_ctx, "%s: %s", hashfile, strerror (errno));
853 
854         return -1;
855       }
856 
857       EVENT_DATA (EVENT_HASHLIST_COUNT_LINES_PRE, hashfile, strlen (hashfile));
858 
859       hashes_avail = count_lines (&fp);
860 
861       EVENT_DATA (EVENT_HASHLIST_COUNT_LINES_POST, hashfile, strlen (hashfile));
862 
863       hc_rewind (&fp);
864 
865       if (hashes_avail == 0)
866       {
867         event_log_error (hashcat_ctx, "hashfile is empty or corrupt.");
868 
869         hc_fclose (&fp);
870 
871         return -1;
872       }
873 
874       hashlist_format = hlfmt_detect (hashcat_ctx, &fp, 100); // 100 = max numbers to "scan". could be hashes_avail, too
875 
876       hc_fclose (&fp);
877 
878       if ((user_options->remove == true) && (hashlist_format != HLFMT_HASHCAT))
879       {
880         event_log_error (hashcat_ctx, "Use of --remove is not supported in native hashfile-format mode.");
881 
882         return -1;
883       }
884     }
885     else if (hashlist_mode == HL_MODE_FILE_BINARY)
886     {
887       struct stat st;
888 
889       if (stat (hashes->hashfile, &st) == -1)
890       {
891         event_log_error (hashcat_ctx, "%s: %s", hashes->hashfile, strerror (errno));
892 
893         return -1;
894       }
895 
896       if (module_ctx->module_hash_binary_count != MODULE_DEFAULT)
897       {
898         const int binary_count = module_ctx->module_hash_binary_count (hashes);
899 
900         if (binary_count > 0)
901         {
902           hashes_avail = binary_count;
903         }
904         else if (binary_count == 0)
905         {
906           event_log_error (hashcat_ctx, "No hashes loaded.");
907 
908           return -1;
909         }
910         else if (binary_count == PARSER_HAVE_ERRNO)
911         {
912           event_log_error (hashcat_ctx, "%s: %s", hashes->hashfile, strerror (errno));
913 
914           return -1;
915         }
916         else
917         {
918           event_log_error (hashcat_ctx, "%s: %s", hashes->hashfile, strerror (binary_count));
919 
920           return -1;
921         }
922       }
923       else
924       {
925         hashes_avail = 1;
926       }
927     }
928   }
929   else
930   {
931     hashes_avail = 1;
932   }
933 
934   if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT) hashes_avail *= 2;
935 
936   hashes->hashlist_format = hashlist_format;
937 
938   /**
939    * load hashes, part II: allocate required memory, set pointers
940    */
941 
942   hash_t *hashes_buf     = (hash_t *) hccalloc (hashes_avail, sizeof (hash_t));
943   void   *digests_buf    =            hccalloc (hashes_avail, hashconfig->dgst_size);
944   salt_t *salts_buf      = NULL;
945   void   *esalts_buf     = NULL;
946   void   *hook_salts_buf = NULL;
947 
948   if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY) || (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT))
949   {
950     u64 hash_pos;
951 
952     for (hash_pos = 0; hash_pos < hashes_avail; hash_pos++)
953     {
954       hashinfo_t *hash_info = (hashinfo_t *) hcmalloc (sizeof (hashinfo_t));
955 
956       hashes_buf[hash_pos].hash_info = hash_info;
957 
958       if (user_options->username == true)
959       {
960         hash_info->user = (user_t *) hcmalloc (sizeof (user_t));
961       }
962 
963       if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
964       {
965         if (user_options->benchmark == false)
966         {
967           hash_info->orighash = (char *) hcmalloc (256);
968         }
969       }
970 
971       if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
972       {
973         hash_info->split = (split_t *) hcmalloc (sizeof (split_t));
974       }
975     }
976   }
977 
978   if (hashconfig->is_salted == true)
979   {
980     salts_buf = (salt_t *) hccalloc (hashes_avail, sizeof (salt_t));
981 
982     if (user_options->attack_mode == ATTACK_MODE_ASSOCIATION)
983     {
984       // this disables:
985       // - sorting by salt value
986       // - grouping by salt value
987       // - keep the salt in position relative to hashfile (not equal because of some hashes maybe failed to load)
988 
989       u64 hash_pos;
990 
991       for (hash_pos = 0; hash_pos < hashes_avail; hash_pos++)
992       {
993         salt_t *salt = &salts_buf[hash_pos];
994 
995         salt->orig_pos = hash_pos;
996       }
997     }
998 
999     if (hashconfig->esalt_size > 0)
1000     {
1001       esalts_buf = hccalloc (hashes_avail, hashconfig->esalt_size);
1002     }
1003 
1004     if (hashconfig->hook_salt_size > 0)
1005     {
1006       hook_salts_buf = hccalloc (hashes_avail, hashconfig->hook_salt_size);
1007     }
1008   }
1009   else
1010   {
1011     salts_buf = (salt_t *) hccalloc (1, sizeof (salt_t));
1012   }
1013 
1014   for (u64 hash_pos = 0; hash_pos < hashes_avail; hash_pos++)
1015   {
1016     /**
1017      * Initialize some values for later use
1018      */
1019 
1020     hashes_buf[hash_pos].orig_line_pos = hash_pos;
1021 
1022     hashes_buf[hash_pos].digest = ((char *) digests_buf) + (hash_pos * hashconfig->dgst_size);
1023 
1024     if (hashconfig->is_salted == true)
1025     {
1026       hashes_buf[hash_pos].salt = &salts_buf[hash_pos];
1027 
1028       if (hashconfig->esalt_size > 0)
1029       {
1030         hashes_buf[hash_pos].esalt = ((char *) esalts_buf) + (hash_pos * hashconfig->esalt_size);
1031       }
1032 
1033       if (hashconfig->hook_salt_size > 0)
1034       {
1035         hashes_buf[hash_pos].hook_salt = ((char *) hook_salts_buf) + (hash_pos * hashconfig->hook_salt_size);
1036       }
1037     }
1038     else
1039     {
1040       hashes_buf[hash_pos].salt = &salts_buf[0];
1041     }
1042   }
1043 
1044   hashes->hashes_buf     = hashes_buf;
1045   hashes->digests_buf    = digests_buf;
1046   hashes->salts_buf      = salts_buf;
1047   hashes->esalts_buf     = esalts_buf;
1048   hashes->hook_salts_buf = hook_salts_buf;
1049 
1050   /**
1051    * load hashes, part III: parse hashes
1052    */
1053 
1054   u32 hashes_cnt = 0;
1055 
1056   if (user_options->benchmark == true)
1057   {
1058     hashes->hashfile = "-";
1059 
1060     hashes_cnt = 1;
1061   }
1062   else if (user_options->hash_info == true)
1063   {
1064   }
1065   else if (user_options->keyspace == true)
1066   {
1067   }
1068   else if (user_options->stdout_flag == true)
1069   {
1070   }
1071   else if (user_options->backend_info == true)
1072   {
1073   }
1074   else
1075   {
1076     if (hashlist_mode == HL_MODE_ARG)
1077     {
1078       char *input_buf = user_options_extra->hc_hash;
1079 
1080       size_t input_len = strlen (input_buf);
1081 
1082       char  *hash_buf = NULL;
1083       int    hash_len = 0;
1084 
1085       hlfmt_hash (hashcat_ctx, hashlist_format, input_buf, input_len, &hash_buf, &hash_len);
1086 
1087       bool hash_fmt_error = false;
1088 
1089       if (hash_len < 1)     hash_fmt_error = true;
1090       if (hash_buf == NULL) hash_fmt_error = true;
1091 
1092       if (hash_fmt_error)
1093       {
1094         event_log_warning (hashcat_ctx, "Failed to parse hashes using the '%s' format.", strhlfmt (hashlist_format));
1095       }
1096       else
1097       {
1098         if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
1099         {
1100           hashinfo_t *hash_info_tmp = hashes_buf[hashes_cnt].hash_info;
1101 
1102           hash_info_tmp->orighash = hcstrdup (hash_buf);
1103         }
1104 
1105         if (hashconfig->is_salted == true)
1106         {
1107           memset (hashes_buf[0].salt, 0, sizeof (salt_t));
1108         }
1109 
1110         if (hashconfig->esalt_size > 0)
1111         {
1112           memset (hashes_buf[0].esalt, 0, hashconfig->esalt_size);
1113         }
1114 
1115         if (hashconfig->hook_salt_size > 0)
1116         {
1117           memset (hashes_buf[0].hook_salt, 0, hashconfig->hook_salt_size);
1118         }
1119 
1120         int parser_status = PARSER_OK;
1121 
1122         if (user_options->username == true)
1123         {
1124           char *user_buf = NULL;
1125           int   user_len = 0;
1126 
1127           hlfmt_user (hashcat_ctx, hashlist_format, input_buf, input_len, &user_buf, &user_len);
1128 
1129           // special case:
1130           // both hash_t need to have the username info if the pwdump format is used (i.e. we have 2 hashes for 3000, both with same user)
1131 
1132           u32 hashes_per_user = 1;
1133 
1134           if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
1135           {
1136             // the following conditions should be true if (hashlist_format == HLFMT_PWDUMP)
1137 
1138             if (hash_len == 32)
1139             {
1140               hashes_per_user = 2;
1141             }
1142           }
1143 
1144           for (u32 i = 0; i < hashes_per_user; i++)
1145           {
1146             user_t **user = &hashes_buf[hashes_cnt + i].hash_info->user;
1147 
1148             *user = (user_t *) hcmalloc (sizeof (user_t));
1149 
1150             user_t *user_ptr = *user;
1151 
1152             if (user_buf != NULL)
1153             {
1154               user_ptr->user_name = hcstrdup (user_buf);
1155             }
1156             else
1157             {
1158               user_ptr->user_name = hcstrdup ("");
1159             }
1160 
1161             user_ptr->user_len = (u32) user_len;
1162           }
1163         }
1164 
1165         if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
1166         {
1167           if (hash_len == 32)
1168           {
1169             hash_t *hash;
1170 
1171             hash = &hashes_buf[hashes_cnt];
1172 
1173             parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf +  0, 16);
1174 
1175             if (parser_status == PARSER_OK)
1176             {
1177               hashes_buf[hashes_cnt].hash_info->split->split_group  = 0;
1178               hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_LEFT;
1179 
1180               hashes_cnt++;
1181             }
1182             else
1183             {
1184               event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
1185             }
1186 
1187             hash = &hashes_buf[hashes_cnt];
1188 
1189             parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf + 16, 16);
1190 
1191             if (parser_status == PARSER_OK)
1192             {
1193               hashes_buf[hashes_cnt].hash_info->split->split_group  = 0;
1194               hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_RIGHT;
1195 
1196               hashes_cnt++;
1197             }
1198             else
1199             {
1200               event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
1201             }
1202           }
1203           else
1204           {
1205             hash_t *hash = &hashes_buf[hashes_cnt];
1206 
1207             parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf, hash_len);
1208 
1209             if (parser_status == PARSER_OK)
1210             {
1211               hashes_buf[hashes_cnt].hash_info->split->split_group  = 0;
1212               hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_NONE;
1213 
1214               hashes_cnt++;
1215             }
1216             else
1217             {
1218               event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
1219             }
1220           }
1221         }
1222         else
1223         {
1224           hash_t *hash = &hashes_buf[hashes_cnt];
1225 
1226           parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf, hash_len);
1227 
1228           if (parser_status == PARSER_OK)
1229           {
1230             hashes_cnt++;
1231           }
1232           else
1233           {
1234             event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
1235           }
1236         }
1237       }
1238     }
1239     else if (hashlist_mode == HL_MODE_FILE_PLAIN)
1240     {
1241       HCFILE fp;
1242 
1243       if (hc_fopen (&fp, hashfile, "rb") == false)
1244       {
1245         event_log_error (hashcat_ctx, "%s: %s", hashfile, strerror (errno));
1246 
1247         return -1;
1248       }
1249 
1250       u32 line_num = 0;
1251 
1252       char *line_buf = (char *) hcmalloc (HCBUFSIZ_LARGE);
1253 
1254       time_t prev = 0;
1255       time_t now  = 0;
1256 
1257       while (!hc_feof (&fp))
1258       {
1259         line_num++;
1260 
1261         const size_t line_len = fgetl (&fp, line_buf, HCBUFSIZ_LARGE);
1262 
1263         if (line_len == 0) continue;
1264 
1265         if (hashes_avail == hashes_cnt)
1266         {
1267           event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u: File changed during runtime. Skipping new data.", hashes->hashfile, line_num);
1268 
1269           break;
1270         }
1271 
1272         char *hash_buf = NULL;
1273         int   hash_len = 0;
1274 
1275         hlfmt_hash (hashcat_ctx, hashlist_format, line_buf, line_len, &hash_buf, &hash_len);
1276 
1277         bool hash_fmt_error = false;
1278 
1279         if (hash_len < 1)     hash_fmt_error = true;
1280         if (hash_buf == NULL) hash_fmt_error = true;
1281 
1282         if (hash_fmt_error)
1283         {
1284           event_log_warning (hashcat_ctx, "Failed to parse hashes using the '%s' format.", strhlfmt (hashlist_format));
1285 
1286           continue;
1287         }
1288 
1289         if (user_options->username == true)
1290         {
1291           char *user_buf = NULL;
1292           int   user_len = 0;
1293 
1294           hlfmt_user (hashcat_ctx, hashlist_format, line_buf, line_len, &user_buf, &user_len);
1295 
1296           // special case:
1297           // both hash_t need to have the username info if the pwdump format is used (i.e. we have 2 hashes for 3000, both with same user)
1298 
1299           u32 hashes_per_user = 1;
1300 
1301           if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
1302           {
1303             // the following conditions should be true if (hashlist_format == HLFMT_PWDUMP)
1304 
1305             if (hash_len == 32)
1306             {
1307               hashes_per_user = 2;
1308             }
1309           }
1310 
1311           for (u32 i = 0; i < hashes_per_user; i++)
1312           {
1313             user_t **user = &hashes_buf[hashes_cnt + i].hash_info->user;
1314 
1315             *user = (user_t *) hcmalloc (sizeof (user_t));
1316 
1317             user_t *user_ptr = *user;
1318 
1319             if (user_buf != NULL)
1320             {
1321               user_ptr->user_name = hcstrdup (user_buf);
1322             }
1323             else
1324             {
1325               user_ptr->user_name = hcstrdup ("");
1326             }
1327 
1328             user_ptr->user_len = (u32) user_len;
1329           }
1330         }
1331 
1332         if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
1333         {
1334           hashinfo_t *hash_info_tmp = hashes_buf[hashes_cnt].hash_info;
1335 
1336           hash_info_tmp->orighash = hcstrdup (hash_buf);
1337         }
1338 
1339         if (hashconfig->is_salted == true)
1340         {
1341           const u32 orig_pos = hashes_buf[hashes_cnt].salt->orig_pos;
1342 
1343           memset (hashes_buf[hashes_cnt].salt, 0, sizeof (salt_t));
1344 
1345           hashes_buf[hashes_cnt].salt->orig_pos = orig_pos;
1346         }
1347 
1348         if (hashconfig->esalt_size > 0)
1349         {
1350           memset (hashes_buf[hashes_cnt].esalt, 0, hashconfig->esalt_size);
1351         }
1352 
1353         if (hashconfig->hook_salt_size > 0)
1354         {
1355           memset (hashes_buf[hashes_cnt].hook_salt, 0, hashconfig->hook_salt_size);
1356         }
1357 
1358         if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
1359         {
1360           if (hash_len == 32)
1361           {
1362             hash_t *hash;
1363 
1364             hash = &hashes_buf[hashes_cnt];
1365 
1366             int parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf +  0, 16);
1367 
1368             if (parser_status < PARSER_GLOBAL_ZERO)
1369             {
1370               char *tmp_line_buf;
1371 
1372               hc_asprintf (&tmp_line_buf, "%s", line_buf);
1373 
1374               compress_terminal_line_length (tmp_line_buf, 38, 32);
1375 
1376               if (user_options->machine_readable == true)
1377               {
1378                 event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
1379               }
1380               else
1381               {
1382                 event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
1383               }
1384 
1385               hcfree (tmp_line_buf);
1386 
1387               continue;
1388             }
1389 
1390             hashes_buf[hashes_cnt].hash_info->split->split_group  = line_num;
1391             hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_LEFT;
1392 
1393             hashes_cnt++;
1394 
1395             hash = &hashes_buf[hashes_cnt];
1396 
1397             parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf + 16, 16);
1398 
1399             if (parser_status < PARSER_GLOBAL_ZERO)
1400             {
1401               char *tmp_line_buf;
1402 
1403               hc_asprintf (&tmp_line_buf, "%s", line_buf);
1404 
1405               compress_terminal_line_length (tmp_line_buf, 38, 32);
1406 
1407               if (user_options->machine_readable == true)
1408               {
1409                 event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
1410               }
1411               else
1412               {
1413                 event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
1414               }
1415 
1416               hcfree (tmp_line_buf);
1417 
1418               continue;
1419             }
1420 
1421             hashes_buf[hashes_cnt].hash_info->split->split_group  = line_num;
1422             hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_RIGHT;
1423 
1424             hashes_cnt++;
1425           }
1426           else
1427           {
1428             hash_t *hash = &hashes_buf[hashes_cnt];
1429 
1430             int parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf, hash_len);
1431 
1432             if (parser_status < PARSER_GLOBAL_ZERO)
1433             {
1434               char *tmp_line_buf;
1435 
1436               hc_asprintf (&tmp_line_buf, "%s", line_buf);
1437 
1438               compress_terminal_line_length (tmp_line_buf, 38, 32);
1439 
1440               if (user_options->machine_readable == true)
1441               {
1442                 event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
1443               }
1444               else
1445               {
1446                 event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
1447               }
1448 
1449               hcfree (tmp_line_buf);
1450 
1451               continue;
1452             }
1453 
1454             hashes_buf[hashes_cnt].hash_info->split->split_group  = line_num;
1455             hashes_buf[hashes_cnt].hash_info->split->split_origin = SPLIT_ORIGIN_NONE;
1456 
1457             hashes_cnt++;
1458           }
1459         }
1460         else
1461         {
1462           hash_t *hash = &hashes_buf[hashes_cnt];
1463 
1464           int parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, hash_buf, hash_len);
1465 
1466           if (parser_status < PARSER_GLOBAL_ZERO)
1467           {
1468             char *tmp_line_buf;
1469 
1470             hc_asprintf (&tmp_line_buf, "%s", line_buf);
1471 
1472             compress_terminal_line_length (tmp_line_buf, 38, 32);
1473 
1474             if (user_options->machine_readable == true)
1475             {
1476               event_log_warning (hashcat_ctx, "%s:%u:%s:%s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
1477             }
1478             else
1479             {
1480               event_log_warning (hashcat_ctx, "Hashfile '%s' on line %u (%s): %s", hashes->hashfile, line_num, tmp_line_buf, strparser (parser_status));
1481             }
1482 
1483             hcfree (tmp_line_buf);
1484 
1485             continue;
1486           }
1487 
1488           hashes_cnt++;
1489         }
1490 
1491         time (&now);
1492 
1493         if ((now - prev) == 0) continue;
1494 
1495         time (&prev);
1496 
1497         hashlist_parse_t hashlist_parse;
1498 
1499         hashlist_parse.hashes_cnt   = hashes_cnt;
1500         hashlist_parse.hashes_avail = hashes_avail;
1501 
1502         EVENT_DATA (EVENT_HASHLIST_PARSE_HASH, &hashlist_parse, sizeof (hashlist_parse_t));
1503       }
1504 
1505       hashlist_parse_t hashlist_parse;
1506 
1507       hashlist_parse.hashes_cnt   = hashes_cnt;
1508       hashlist_parse.hashes_avail = hashes_avail;
1509 
1510       EVENT_DATA (EVENT_HASHLIST_PARSE_HASH, &hashlist_parse, sizeof (hashlist_parse_t));
1511 
1512       hcfree (line_buf);
1513 
1514       hc_fclose (&fp);
1515     }
1516     else if (hashlist_mode == HL_MODE_FILE_BINARY)
1517     {
1518       char *input_buf = user_options_extra->hc_hash;
1519 
1520       size_t input_len = strlen (input_buf);
1521 
1522       if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
1523       {
1524         hashinfo_t *hash_info_tmp = hashes_buf[hashes_cnt].hash_info;
1525 
1526         hash_info_tmp->orighash = hcstrdup (input_buf);
1527       }
1528 
1529       if (hashconfig->is_salted == true)
1530       {
1531         memset (hashes_buf[0].salt, 0, sizeof (salt_t));
1532       }
1533 
1534       if (hashconfig->esalt_size > 0)
1535       {
1536         memset (hashes_buf[0].esalt, 0, hashconfig->esalt_size);
1537       }
1538 
1539       if (hashconfig->hook_salt_size > 0)
1540       {
1541         memset (hashes_buf[0].hook_salt, 0, hashconfig->hook_salt_size);
1542       }
1543 
1544       if (module_ctx->module_hash_binary_parse != MODULE_DEFAULT)
1545       {
1546         const int hashes_parsed = module_ctx->module_hash_binary_parse (hashconfig, user_options, user_options_extra, hashes);
1547 
1548         if (hashes_parsed > 0)
1549         {
1550           hashes_cnt = hashes_parsed;
1551         }
1552         else if (hashes_parsed == 0)
1553         {
1554           event_log_warning (hashcat_ctx, "No hashes loaded.");
1555         }
1556         else if (hashes_parsed == PARSER_HAVE_ERRNO)
1557         {
1558           event_log_warning (hashcat_ctx, "Hashfile '%s': %s", hashes->hashfile, strerror (errno));
1559         }
1560         else
1561         {
1562           event_log_warning (hashcat_ctx, "Hashfile '%s': %s", hashes->hashfile, strparser (hashes_parsed));
1563         }
1564       }
1565       else
1566       {
1567         hash_t *hash = &hashes_buf[hashes_cnt];
1568 
1569         int parser_status = module_ctx->module_hash_decode (hashconfig, hash->digest, hash->salt, hash->esalt, hash->hook_salt, hash->hash_info, input_buf, input_len);
1570 
1571         if (parser_status == PARSER_OK)
1572         {
1573           hashes_cnt++;
1574         }
1575         else
1576         {
1577           event_log_warning (hashcat_ctx, "Hash '%s': %s", input_buf, strparser (parser_status));
1578         }
1579       }
1580     }
1581   }
1582 
1583   hashes->hashes_cnt = hashes_cnt;
1584 
1585   if (hashes_cnt)
1586   {
1587     EVENT (EVENT_HASHLIST_SORT_HASH_PRE);
1588 
1589     if (hashconfig->is_salted == true)
1590     {
1591       hc_qsort_r (hashes_buf, hashes_cnt, sizeof (hash_t), sort_by_hash, (void *) hashconfig);
1592     }
1593     else
1594     {
1595       hc_qsort_r (hashes_buf, hashes_cnt, sizeof (hash_t), sort_by_hash_no_salt, (void *) hashconfig);
1596     }
1597 
1598     EVENT (EVENT_HASHLIST_SORT_HASH_POST);
1599   }
1600 
1601   if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
1602   {
1603     // update split split_neighbor after sorting
1604     // see https://github.com/hashcat/hashcat/issues/1034 for good examples for testing
1605 
1606     for (u32 i = 0; i < hashes_cnt; i++)
1607     {
1608       split_t *split1 = hashes_buf[i].hash_info->split;
1609 
1610       if (split1->split_origin != SPLIT_ORIGIN_LEFT) continue;
1611 
1612       for (u32 j = 0; j < hashes_cnt; j++)
1613       {
1614         split_t *split2 = hashes_buf[j].hash_info->split;
1615 
1616         if (split2->split_origin != SPLIT_ORIGIN_RIGHT) continue;
1617 
1618         if (split1->split_group != split2->split_group) continue;
1619 
1620         split1->split_neighbor = j;
1621         split2->split_neighbor = i;
1622 
1623         break;
1624       }
1625     }
1626   }
1627 
1628   return 0;
1629 }
1630 
hashes_init_stage2(hashcat_ctx_t * hashcat_ctx)1631 int hashes_init_stage2 (hashcat_ctx_t *hashcat_ctx)
1632 {
1633   const hashconfig_t   *hashconfig   = hashcat_ctx->hashconfig;
1634         hashes_t       *hashes       = hashcat_ctx->hashes;
1635   const user_options_t *user_options = hashcat_ctx->user_options;
1636 
1637   hash_t *hashes_buf = hashes->hashes_buf;
1638   u32     hashes_cnt = hashes->hashes_cnt;
1639 
1640   /**
1641    * Remove duplicates
1642    */
1643 
1644   EVENT (EVENT_HASHLIST_UNIQUE_HASH_PRE);
1645 
1646   u32 hashes_cnt_new = 1;
1647 
1648   for (u32 hashes_pos = 1; hashes_pos < hashes_cnt; hashes_pos++)
1649   {
1650     if (hashconfig->potfile_keep_all_hashes == true)
1651     {
1652       // do not sort, because we need to keep all hashes in this particular case
1653     }
1654     else if (hashconfig->is_salted == true)
1655     {
1656       if (sort_by_salt (hashes_buf[hashes_pos].salt, hashes_buf[hashes_pos - 1].salt) == 0)
1657       {
1658         if (sort_by_digest_p0p1 (hashes_buf[hashes_pos].digest, hashes_buf[hashes_pos - 1].digest, (void *) hashconfig) == 0) continue;
1659       }
1660     }
1661     else
1662     {
1663       if (sort_by_digest_p0p1 (hashes_buf[hashes_pos].digest, hashes_buf[hashes_pos - 1].digest, (void *) hashconfig) == 0) continue;
1664     }
1665 
1666     hash_t tmp;
1667 
1668     memcpy (&tmp, &hashes_buf[hashes_pos], sizeof (hash_t));
1669 
1670     memcpy (&hashes_buf[hashes_cnt_new], &tmp, sizeof (hash_t));
1671 
1672     hashes_cnt_new++;
1673   }
1674 
1675   for (u32 i = hashes_cnt_new; i < hashes->hashes_cnt; i++)
1676   {
1677     memset (&hashes_buf[i], 0, sizeof (hash_t));
1678   }
1679 
1680   hashes_cnt = hashes_cnt_new;
1681 
1682   hashes->hashes_cnt = hashes_cnt;
1683 
1684   EVENT (EVENT_HASHLIST_UNIQUE_HASH_POST);
1685 
1686   /**
1687    * Now generate all the buffers required for later
1688    */
1689 
1690   void   *digests_buf_new    = hccalloc (hashes_cnt, hashconfig->dgst_size);
1691   salt_t *salts_buf_new      = NULL;
1692   void   *esalts_buf_new     = NULL;
1693   void   *hook_salts_buf_new = NULL;
1694 
1695   if (hashconfig->is_salted == true)
1696   {
1697     salts_buf_new = (salt_t *) hccalloc (hashes_cnt, sizeof (salt_t));
1698   }
1699   else
1700   {
1701     salts_buf_new = (salt_t *) hccalloc (1, sizeof (salt_t));
1702   }
1703 
1704   if (hashconfig->esalt_size > 0)
1705   {
1706     esalts_buf_new = hccalloc (hashes_cnt, hashconfig->esalt_size);
1707   }
1708 
1709   if (hashconfig->hook_salt_size > 0)
1710   {
1711     hook_salts_buf_new = hccalloc (hashes_cnt, hashconfig->hook_salt_size);
1712   }
1713 
1714   EVENT (EVENT_HASHLIST_SORT_SALT_PRE);
1715 
1716   u32 digests_cnt  = hashes_cnt;
1717   u32 digests_done = 0;
1718 
1719   u32 *digests_shown     = (u32 *) hccalloc (digests_cnt, sizeof (u32));
1720 
1721   u32 salts_cnt   = 0;
1722   u32 salts_done  = 0;
1723 
1724   hashinfo_t **hash_info = NULL;
1725 
1726   if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY) || (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT))
1727   {
1728     hash_info = (hashinfo_t **) hccalloc (hashes_cnt, sizeof (hashinfo_t *));
1729   }
1730 
1731   u32 *salts_shown = (u32 *) hccalloc (digests_cnt, sizeof (u32));
1732 
1733   salt_t *salt_buf;
1734 
1735   {
1736     // copied from inner loop
1737 
1738     salt_buf = &salts_buf_new[salts_cnt];
1739 
1740     memcpy (salt_buf, hashes_buf[0].salt, sizeof (salt_t));
1741 
1742     hashes_buf[0].salt = salt_buf;
1743 
1744     if (hashconfig->hook_salt_size > 0)
1745     {
1746       char *hook_salts_buf_new_ptr = ((char *) hook_salts_buf_new) + (salts_cnt * hashconfig->hook_salt_size);
1747 
1748       memcpy (hook_salts_buf_new_ptr, hashes_buf[0].hook_salt, hashconfig->hook_salt_size);
1749 
1750       hashes_buf[0].hook_salt = hook_salts_buf_new_ptr;
1751     }
1752 
1753     salt_buf->digests_cnt    = 0;
1754     salt_buf->digests_done   = 0;
1755     salt_buf->digests_offset = 0;
1756 
1757     salts_cnt++;
1758   }
1759 
1760   salt_buf->digests_cnt++;
1761 
1762   char *digests_buf_new_ptr = ((char *) digests_buf_new) + (0 * hashconfig->dgst_size);
1763 
1764   memcpy (digests_buf_new_ptr, hashes_buf[0].digest, hashconfig->dgst_size);
1765 
1766   hashes_buf[0].digest = digests_buf_new_ptr;
1767 
1768   if (hashconfig->esalt_size > 0)
1769   {
1770     char *esalts_buf_new_ptr = ((char *) esalts_buf_new) + (0 * hashconfig->esalt_size);
1771 
1772     memcpy (esalts_buf_new_ptr, hashes_buf[0].esalt, hashconfig->esalt_size);
1773 
1774     hashes_buf[0].esalt = esalts_buf_new_ptr;
1775   }
1776 
1777   if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY) || (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT))
1778   {
1779     hash_info[0] = hashes_buf[0].hash_info;
1780   }
1781 
1782   // copy from inner loop
1783 
1784   for (u32 hashes_pos = 1; hashes_pos < hashes_cnt; hashes_pos++)
1785   {
1786     if (hashconfig->is_salted == true)
1787     {
1788       if (sort_by_salt (hashes_buf[hashes_pos].salt, hashes_buf[hashes_pos - 1].salt) != 0)
1789       {
1790         salt_buf = &salts_buf_new[salts_cnt];
1791 
1792         memcpy (salt_buf, hashes_buf[hashes_pos].salt, sizeof (salt_t));
1793 
1794         hashes_buf[hashes_pos].salt = salt_buf;
1795 
1796         if (hashconfig->hook_salt_size > 0)
1797         {
1798           char *hook_salts_buf_new_ptr = ((char *) hook_salts_buf_new) + (salts_cnt * hashconfig->hook_salt_size);
1799 
1800           memcpy (hook_salts_buf_new_ptr, hashes_buf[hashes_pos].hook_salt, hashconfig->hook_salt_size);
1801 
1802           hashes_buf[hashes_pos].hook_salt = hook_salts_buf_new_ptr;
1803         }
1804 
1805         salt_buf->digests_cnt    = 0;
1806         salt_buf->digests_done   = 0;
1807         salt_buf->digests_offset = hashes_pos;
1808 
1809         salts_cnt++;
1810       }
1811 
1812       hashes_buf[hashes_pos].salt = salt_buf;
1813 
1814       if (hashconfig->hook_salt_size > 0)
1815       {
1816         char *hook_salts_buf_new_ptr = ((char *) hook_salts_buf_new) + (salts_cnt * hashconfig->hook_salt_size);
1817 
1818         hashes_buf[hashes_pos].hook_salt = hook_salts_buf_new_ptr;
1819       }
1820     }
1821 
1822     salt_buf->digests_cnt++;
1823 
1824     digests_buf_new_ptr = ((char *) digests_buf_new) + (hashes_pos * hashconfig->dgst_size);
1825 
1826     memcpy (digests_buf_new_ptr, hashes_buf[hashes_pos].digest, hashconfig->dgst_size);
1827 
1828     hashes_buf[hashes_pos].digest = digests_buf_new_ptr;
1829 
1830     if (hashconfig->esalt_size > 0)
1831     {
1832       char *esalts_buf_new_ptr = ((char *) esalts_buf_new) + (hashes_pos * hashconfig->esalt_size);
1833 
1834       memcpy (esalts_buf_new_ptr, hashes_buf[hashes_pos].esalt, hashconfig->esalt_size);
1835 
1836       hashes_buf[hashes_pos].esalt = esalts_buf_new_ptr;
1837     }
1838 
1839     if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY) || (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT))
1840     {
1841       hash_info[hashes_pos] = hashes_buf[hashes_pos].hash_info;
1842     }
1843   }
1844 
1845   EVENT (EVENT_HASHLIST_SORT_SALT_POST);
1846 
1847   hcfree (hashes->digests_buf);
1848   hcfree (hashes->salts_buf);
1849   hcfree (hashes->esalts_buf);
1850   hcfree (hashes->hook_salts_buf);
1851 
1852   hashes->digests_cnt       = digests_cnt;
1853   hashes->digests_done      = digests_done;
1854   hashes->digests_buf       = digests_buf_new;
1855   hashes->digests_shown     = digests_shown;
1856 
1857   hashes->salts_cnt         = salts_cnt;
1858   hashes->salts_done        = salts_done;
1859   hashes->salts_buf         = salts_buf_new;
1860   hashes->salts_shown       = salts_shown;
1861 
1862   hashes->esalts_buf        = esalts_buf_new;
1863   hashes->hook_salts_buf    = hook_salts_buf_new;
1864 
1865   hashes->hash_info         = hash_info;
1866 
1867   return 0;
1868 }
1869 
hashes_init_stage3(hashcat_ctx_t * hashcat_ctx)1870 int hashes_init_stage3 (hashcat_ctx_t *hashcat_ctx)
1871 {
1872   hashes_t *hashes = hashcat_ctx->hashes;
1873 
1874   u32  digests_done  = hashes->digests_done;
1875   u32 *digests_shown = hashes->digests_shown;
1876 
1877   u32  salts_cnt     = hashes->salts_cnt;
1878   u32  salts_done    = hashes->salts_done;
1879   u32 *salts_shown   = hashes->salts_shown;
1880 
1881   hash_t *hashes_buf = hashes->hashes_buf;
1882 
1883   salt_t *salts_buf  = hashes->salts_buf;
1884 
1885   for (u32 salt_idx = 0; salt_idx < salts_cnt; salt_idx++)
1886   {
1887     salt_t *salt_buf = salts_buf + salt_idx;
1888 
1889     u32 digests_cnt = salt_buf->digests_cnt;
1890 
1891     for (u32 digest_idx = 0; digest_idx < digests_cnt; digest_idx++)
1892     {
1893       const u32 hashes_idx = salt_buf->digests_offset + digest_idx;
1894 
1895       if (hashes_buf[hashes_idx].cracked == 1)
1896       {
1897         digests_shown[hashes_idx] = 1;
1898 
1899         digests_done++;
1900 
1901         salt_buf->digests_done++;
1902       }
1903     }
1904 
1905     if (salt_buf->digests_done == salt_buf->digests_cnt)
1906     {
1907       salts_shown[salt_idx] = 1;
1908 
1909       salts_done++;
1910     }
1911 
1912     if (salts_done == salts_cnt) mycracked (hashcat_ctx);
1913   }
1914 
1915   hashes->digests_done = digests_done;
1916 
1917   hashes->salts_cnt   = salts_cnt;
1918   hashes->salts_done  = salts_done;
1919 
1920   return 0;
1921 }
1922 
hashes_init_stage4(hashcat_ctx_t * hashcat_ctx)1923 int hashes_init_stage4 (hashcat_ctx_t *hashcat_ctx)
1924 {
1925   hashconfig_t         *hashconfig         = hashcat_ctx->hashconfig;
1926   hashes_t             *hashes             = hashcat_ctx->hashes;
1927   module_ctx_t         *module_ctx         = hashcat_ctx->module_ctx;
1928   user_options_t       *user_options       = hashcat_ctx->user_options;
1929   user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
1930 
1931   if (hashes->salts_cnt == 1)
1932     hashconfig->opti_type |= OPTI_TYPE_SINGLE_SALT;
1933 
1934   if (hashes->digests_cnt == 1)
1935     hashconfig->opti_type |= OPTI_TYPE_SINGLE_HASH;
1936 
1937   if (hashconfig->attack_exec == ATTACK_EXEC_INSIDE_KERNEL)
1938     hashconfig->opti_type |= OPTI_TYPE_NOT_ITERATED;
1939 
1940   if (user_options->attack_mode == ATTACK_MODE_BF)
1941     hashconfig->opti_type |= OPTI_TYPE_BRUTE_FORCE;
1942 
1943   if (hashconfig->opti_type & OPTI_TYPE_BRUTE_FORCE)
1944   {
1945     if (hashconfig->opti_type & OPTI_TYPE_SINGLE_HASH)
1946     {
1947       if (hashconfig->opti_type & OPTI_TYPE_APPENDED_SALT)
1948       {
1949         if (hashconfig->opts_type & OPTS_TYPE_ST_ADD80)
1950         {
1951           hashconfig->opts_type &= ~OPTS_TYPE_ST_ADD80;
1952           hashconfig->opts_type |=  OPTS_TYPE_PT_ADD80;
1953         }
1954 
1955         if (hashconfig->opts_type & OPTS_TYPE_ST_ADDBITS14)
1956         {
1957           hashconfig->opts_type &= ~OPTS_TYPE_ST_ADDBITS14;
1958           hashconfig->opts_type |=  OPTS_TYPE_PT_ADDBITS14;
1959         }
1960 
1961         if (hashconfig->opts_type & OPTS_TYPE_ST_ADDBITS15)
1962         {
1963           hashconfig->opts_type &= ~OPTS_TYPE_ST_ADDBITS15;
1964           hashconfig->opts_type |=  OPTS_TYPE_PT_ADDBITS15;
1965         }
1966       }
1967     }
1968   }
1969 
1970   // test iteration count in association attack
1971 
1972   if (user_options->attack_mode == ATTACK_MODE_ASSOCIATION)
1973   {
1974     salt_t *salts_buf = hashes->salts_buf;
1975 
1976     for (u32 salt_idx = 1; salt_idx < hashes->salts_cnt; salt_idx++)
1977     {
1978       if (salts_buf[salt_idx - 1].salt_iter != salts_buf[salt_idx].salt_iter)
1979       {
1980         event_log_error (hashcat_ctx, "Mixed iteration counts are not supported in association attack-mode.");
1981 
1982         return -1;
1983       }
1984     }
1985   }
1986 
1987   // time to update extra_tmp_size which is tmp_size value based on hash configuration
1988 
1989   if (module_ctx->module_extra_tmp_size != MODULE_DEFAULT)
1990   {
1991     const u64 extra_tmp_size = module_ctx->module_extra_tmp_size (hashconfig, user_options, user_options_extra, hashes);
1992 
1993     if (extra_tmp_size == (u64) -1)
1994     {
1995       event_log_error (hashcat_ctx, "Mixed hash settings are not supported.");
1996 
1997       return -1;
1998     }
1999 
2000     hashconfig->tmp_size = extra_tmp_size;
2001   }
2002 
2003   // at this point we no longer need hash_t* structure
2004 
2005   hash_t *hashes_buf = hashes->hashes_buf;
2006 
2007   hcfree (hashes_buf);
2008 
2009   hashes->hashes_cnt = 0;
2010   hashes->hashes_buf = NULL;
2011 
2012   // starting from here, we should allocate some scratch buffer for later use
2013 
2014   u8 *out_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
2015 
2016   hashes->out_buf = out_buf;
2017 
2018   // we need two buffers in parallel
2019 
2020   u8 *tmp_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
2021 
2022   hashes->tmp_buf = tmp_buf;
2023 
2024   // brain session
2025 
2026   #ifdef WITH_BRAIN
2027   if (user_options->brain_client == true)
2028   {
2029     const u32 brain_session = brain_compute_session (hashcat_ctx);
2030 
2031     user_options->brain_session = brain_session;
2032   }
2033   #endif
2034 
2035   return 0;
2036 }
2037 
hashes_init_selftest(hashcat_ctx_t * hashcat_ctx)2038 int hashes_init_selftest (hashcat_ctx_t *hashcat_ctx)
2039 {
2040   folder_config_t *folder_config = hashcat_ctx->folder_config;
2041   hashconfig_t    *hashconfig    = hashcat_ctx->hashconfig;
2042   hashes_t        *hashes        = hashcat_ctx->hashes;
2043   module_ctx_t    *module_ctx    = hashcat_ctx->module_ctx;
2044   user_options_t  *user_options  = hashcat_ctx->user_options;
2045 
2046   if (hashconfig->st_hash == NULL) return 0;
2047 
2048   void   *st_digests_buf    = NULL;
2049   salt_t *st_salts_buf      = NULL;
2050   void   *st_esalts_buf     = NULL;
2051   void   *st_hook_salts_buf = NULL;
2052 
2053   st_digests_buf =          hccalloc (1, hashconfig->dgst_size);
2054 
2055   st_salts_buf = (salt_t *) hccalloc (1, sizeof (salt_t));
2056 
2057   if (hashconfig->esalt_size > 0)
2058   {
2059     st_esalts_buf = hccalloc (1, hashconfig->esalt_size);
2060   }
2061 
2062   if (hashconfig->hook_salt_size > 0)
2063   {
2064     st_hook_salts_buf = hccalloc (1, hashconfig->hook_salt_size);
2065   }
2066 
2067   hash_t hash;
2068 
2069   hash.digest    = st_digests_buf;
2070   hash.salt      = st_salts_buf;
2071   hash.esalt     = st_esalts_buf;
2072   hash.hook_salt = st_hook_salts_buf;
2073   hash.cracked   = 0;
2074   hash.hash_info = NULL;
2075   hash.pw_buf    = NULL;
2076   hash.pw_len    = 0;
2077 
2078   int parser_status;
2079 
2080   if (module_ctx->module_hash_init_selftest != MODULE_DEFAULT)
2081   {
2082     parser_status = module_ctx->module_hash_init_selftest (hashconfig, &hash);
2083   }
2084   else
2085   {
2086     if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE)
2087     {
2088       if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE_OPTIONAL)
2089       {
2090         parser_status = module_ctx->module_hash_decode (hashconfig, hash.digest, hash.salt, hash.esalt, hash.hook_salt, hash.hash_info, hashconfig->st_hash, strlen (hashconfig->st_hash));
2091       }
2092       else
2093       {
2094         char *tmpfile_bin;
2095 
2096         hc_asprintf (&tmpfile_bin, "%s/selftest.hash", folder_config->session_dir);
2097 
2098         HCFILE fp;
2099 
2100         hc_fopen (&fp, tmpfile_bin, "wb");
2101 
2102         const size_t st_hash_len = strlen (hashconfig->st_hash);
2103 
2104         for (size_t i = 0; i < st_hash_len; i += 2)
2105         {
2106           const u8 c = hex_to_u8 ((const u8 *) hashconfig->st_hash + i);
2107 
2108           hc_fputc (c, &fp);
2109         }
2110 
2111         hc_fclose (&fp);
2112 
2113         parser_status = module_ctx->module_hash_decode (hashconfig, hash.digest, hash.salt, hash.esalt, hash.hook_salt, hash.hash_info, tmpfile_bin, strlen (tmpfile_bin));
2114 
2115         unlink (tmpfile_bin);
2116 
2117         hcfree (tmpfile_bin);
2118       }
2119     }
2120     else
2121     {
2122       hashconfig_t *hashconfig_st = (hashconfig_t *) hcmalloc (sizeof (hashconfig_t));
2123 
2124       memcpy (hashconfig_st, hashconfig, sizeof (hashconfig_t));
2125 
2126       hashconfig_st->separator = ':';
2127 
2128       if (user_options->hex_salt)
2129       {
2130         if (hashconfig->salt_type == SALT_TYPE_GENERIC)
2131         {
2132           // this is save as there's no hash mode that has both SALT_TYPE_GENERIC and OPTS_TYPE_ST_HEX by default
2133 
2134           hashconfig_st->opts_type &= ~OPTS_TYPE_ST_HEX;
2135         }
2136       }
2137 
2138       parser_status = module_ctx->module_hash_decode (hashconfig_st, hash.digest, hash.salt, hash.esalt, hash.hook_salt, hash.hash_info, hashconfig->st_hash, strlen (hashconfig->st_hash));
2139 
2140       hcfree (hashconfig_st);
2141     }
2142   }
2143 
2144   if (parser_status == PARSER_OK)
2145   {
2146     // nothing to do
2147   }
2148   else
2149   {
2150     event_log_error (hashcat_ctx, "Self-test hash parsing error: %s", strparser (parser_status));
2151 
2152     return -1;
2153   }
2154 
2155   hashes->st_digests_buf    = st_digests_buf;
2156   hashes->st_salts_buf      = st_salts_buf;
2157   hashes->st_esalts_buf     = st_esalts_buf;
2158   hashes->st_hook_salts_buf = st_hook_salts_buf;
2159 
2160   return 0;
2161 }
2162 
hashes_init_benchmark(hashcat_ctx_t * hashcat_ctx)2163 int hashes_init_benchmark (hashcat_ctx_t *hashcat_ctx)
2164 {
2165   const hashconfig_t          *hashconfig         = hashcat_ctx->hashconfig;
2166         hashes_t              *hashes             = hashcat_ctx->hashes;
2167   const module_ctx_t          *module_ctx         = hashcat_ctx->module_ctx;
2168   const user_options_t        *user_options       = hashcat_ctx->user_options;
2169   const user_options_extra_t  *user_options_extra = hashcat_ctx->user_options_extra;
2170 
2171   if (user_options->benchmark == false) return 0;
2172 
2173   if (hashconfig->is_salted == false) return 0;
2174 
2175   if (module_ctx->module_benchmark_salt != MODULE_DEFAULT)
2176   {
2177     salt_t *ptr = module_ctx->module_benchmark_salt (hashconfig, user_options, user_options_extra);
2178 
2179     memcpy (hashes->salts_buf, ptr, sizeof (salt_t));
2180 
2181     hcfree (ptr);
2182   }
2183   else
2184   {
2185     memcpy (hashes->salts_buf, hashes->st_salts_buf, sizeof (salt_t));
2186   }
2187 
2188   if (hashconfig->esalt_size > 0)
2189   {
2190     if (module_ctx->module_benchmark_esalt != MODULE_DEFAULT)
2191     {
2192       void *ptr = module_ctx->module_benchmark_esalt (hashconfig, user_options, user_options_extra);
2193 
2194       memcpy (hashes->esalts_buf, ptr, hashconfig->esalt_size);
2195 
2196       hcfree (ptr);
2197     }
2198     else
2199     {
2200       memcpy (hashes->esalts_buf, hashes->st_esalts_buf, hashconfig->esalt_size);
2201     }
2202   }
2203 
2204   if (hashconfig->hook_salt_size > 0)
2205   {
2206     if (module_ctx->module_benchmark_hook_salt != MODULE_DEFAULT)
2207     {
2208       void *ptr = module_ctx->module_benchmark_hook_salt (hashconfig, user_options, user_options_extra);
2209 
2210       memcpy (hashes->hook_salts_buf, ptr, hashconfig->hook_salt_size);
2211 
2212       hcfree (ptr);
2213     }
2214     else
2215     {
2216       memcpy (hashes->hook_salts_buf, hashes->st_hook_salts_buf, hashconfig->hook_salt_size);
2217     }
2218   }
2219 
2220   return 0;
2221 }
2222 
hashes_init_zerohash(hashcat_ctx_t * hashcat_ctx)2223 int hashes_init_zerohash (hashcat_ctx_t *hashcat_ctx)
2224 {
2225   const hashconfig_t   *hashconfig   = hashcat_ctx->hashconfig;
2226   const hashes_t       *hashes       = hashcat_ctx->hashes;
2227   const module_ctx_t   *module_ctx   = hashcat_ctx->module_ctx;
2228 
2229   // do not use this unless really needed, for example as in LM
2230 
2231   if (module_ctx->module_hash_decode_zero_hash == MODULE_DEFAULT) return 0;
2232 
2233   hash_t *hashes_buf = hashes->hashes_buf;
2234   u32     hashes_cnt = hashes->hashes_cnt;
2235 
2236   // no solution for these special hash types (for instane because they use hashfile in output etc)
2237 
2238   hash_t hash_buf;
2239 
2240   hash_buf.digest    = hcmalloc (hashconfig->dgst_size);
2241   hash_buf.salt      = NULL;
2242   hash_buf.esalt     = NULL;
2243   hash_buf.hook_salt = NULL;
2244   hash_buf.cracked   = 0;
2245   hash_buf.hash_info = NULL;
2246   hash_buf.pw_buf    = NULL;
2247   hash_buf.pw_len    = 0;
2248 
2249   if (hashconfig->is_salted == true)
2250   {
2251     hash_buf.salt = (salt_t *) hcmalloc (sizeof (salt_t));
2252   }
2253 
2254   if (hashconfig->esalt_size > 0)
2255   {
2256     hash_buf.esalt = hcmalloc (hashconfig->esalt_size);
2257   }
2258 
2259   if (hashconfig->hook_salt_size > 0)
2260   {
2261     hash_buf.hook_salt = hcmalloc (hashconfig->hook_salt_size);
2262   }
2263 
2264   module_ctx->module_hash_decode_zero_hash (hashconfig, hash_buf.digest, hash_buf.salt, hash_buf.esalt, hash_buf.hook_salt, hash_buf.hash_info);
2265 
2266   for (u32 i = 0; i < hashes_cnt; i++)
2267   {
2268     hash_t *next = &hashes_buf[i];
2269 
2270     int rc = sort_by_hash_no_salt (&hash_buf, next, (void *) hashconfig);
2271 
2272     if (rc == 0)
2273     {
2274       next->pw_buf = (char *) hcmalloc (1);
2275       next->pw_len = 0;
2276 
2277       next->cracked = 1;
2278 
2279       // should we show the cracked zero hash to the user?
2280 
2281       if (false)
2282       {
2283         // digest pos
2284 
2285         const u32 digest_pos = next - hashes_buf;
2286 
2287         // show the crack
2288 
2289         u8 *out_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
2290 
2291         int out_len = hash_encode (hashcat_ctx->hashconfig, hashcat_ctx->hashes, hashcat_ctx->module_ctx, (char *) out_buf, HCBUFSIZ_LARGE, 0, digest_pos);
2292 
2293         out_buf[out_len] = 0;
2294 
2295         // outfile, can be either to file or stdout
2296         // if an error occurs opening the file, send to stdout as fallback
2297         // the fp gets opened for each cracked hash so that the user can modify (move) the outfile while hashcat runs
2298 
2299         outfile_write_open (hashcat_ctx);
2300 
2301         const u8 *plain = (const u8 *) "";
2302 
2303         u8 *tmp_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE);
2304 
2305         tmp_buf[0] = 0;
2306 
2307         const int tmp_len = outfile_write (hashcat_ctx, (char *) out_buf, out_len, plain, 0, 0, NULL, 0, true, (char *) tmp_buf);
2308 
2309         EVENT_DATA (EVENT_CRACKER_HASH_CRACKED, tmp_buf, tmp_len);
2310 
2311         outfile_write_close (hashcat_ctx);
2312 
2313         hcfree (tmp_buf);
2314         hcfree (out_buf);
2315       }
2316     }
2317   }
2318 
2319   if (hashconfig->esalt_size > 0)
2320   {
2321     hcfree (hash_buf.esalt);
2322   }
2323 
2324   if (hashconfig->hook_salt_size > 0)
2325   {
2326     hcfree (hash_buf.hook_salt);
2327   }
2328 
2329   if (hashconfig->is_salted == true)
2330   {
2331     hcfree (hash_buf.salt);
2332   }
2333 
2334   hcfree (hash_buf.digest);
2335 
2336   return 0;
2337 }
2338 
hashes_destroy(hashcat_ctx_t * hashcat_ctx)2339 void hashes_destroy (hashcat_ctx_t *hashcat_ctx)
2340 {
2341   hashconfig_t   *hashconfig   = hashcat_ctx->hashconfig;
2342   hashes_t       *hashes       = hashcat_ctx->hashes;
2343   user_options_t *user_options = hashcat_ctx->user_options;
2344 
2345   hcfree (hashes->digests_buf);
2346   hcfree (hashes->digests_shown);
2347 
2348   hcfree (hashes->salts_buf);
2349   hcfree (hashes->salts_shown);
2350 
2351   if ((user_options->username == true) || (hashconfig->opts_type & OPTS_TYPE_HASH_COPY))
2352   {
2353     for (u32 hash_pos = 0; hash_pos < hashes->hashes_cnt; hash_pos++)
2354     {
2355       if (user_options->username == true)
2356       {
2357         hcfree (hashes->hash_info[hash_pos]->user);
2358       }
2359 
2360       if (hashconfig->opts_type & OPTS_TYPE_HASH_COPY)
2361       {
2362         hcfree (hashes->hash_info[hash_pos]->orighash);
2363       }
2364 
2365       if (hashconfig->opts_type & OPTS_TYPE_HASH_SPLIT)
2366       {
2367         hcfree (hashes->hash_info[hash_pos]->split);
2368       }
2369     }
2370   }
2371 
2372   hcfree (hashes->hash_info);
2373 
2374   hcfree (hashes->esalts_buf);
2375   hcfree (hashes->hook_salts_buf);
2376 
2377   hcfree (hashes->out_buf);
2378   hcfree (hashes->tmp_buf);
2379 
2380   hcfree (hashes->st_digests_buf);
2381   hcfree (hashes->st_salts_buf);
2382   hcfree (hashes->st_esalts_buf);
2383   hcfree (hashes->st_hook_salts_buf);
2384 
2385   memset (hashes, 0, sizeof (hashes_t));
2386 }
2387 
hashes_logger(hashcat_ctx_t * hashcat_ctx)2388 void hashes_logger (hashcat_ctx_t *hashcat_ctx)
2389 {
2390   hashes_t      *hashes      = hashcat_ctx->hashes;
2391   logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
2392 
2393   logfile_top_string (hashes->hashfile);
2394   logfile_top_uint   (hashes->hashlist_mode);
2395   logfile_top_uint   (hashes->hashlist_format);
2396   logfile_top_uint   (hashes->hashes_cnt);
2397   logfile_top_uint   (hashes->digests_cnt);
2398   logfile_top_uint   (hashes->digests_done);
2399   logfile_top_uint   (hashes->salts_cnt);
2400   logfile_top_uint   (hashes->salts_done);
2401 }
2402