1 /**
2  * Author......: Jens Steube <jens.steube@gmail.com>
3  * License.....: MIT
4  */
5 
6 #ifndef COMMON_H
7 #define COMMON_H
8 
9 #define _GNU_SOURCE
10 #define _FILE_OFFSET_BITS 64
11 #define __MSVCRT_VERSION__ 0x0700
12 
13 #include <assert.h>
14 #include <ctype.h>
15 #include <dirent.h>
16 #include <errno.h>
17 #include <math.h>
18 #include <getopt.h>
19 #include <search.h>
20 #include <signal.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <gmp.h>
31 
32 #ifdef OSX
33 #include <emmintrin.h>
34 #else
35 #include <x86intrin.h>
36 #endif
37 
38 #define SHARED_H
39 
40 #include "constants.h"
41 
42 #define PROGNAME              (const char *) "hashcat"
43 
44 #define POTFILE               "hashcat.pot"
45 
46 #define VERSION_TXT           "2.00"
47 #define VERSION_BIN           200
48 
49 #define BLOCK_SIZE            64
50 
51 #define MIN_THREADS           1
52 #define MAX_THREADS           512
53 
54 #define ETC_MAX               (60 * 60 * 24 * 365 * 10)
55 
56 #ifndef BUFSIZ
57 #define BUFSIZ 0x2000
58 #endif
59 
60 #define CHARSIZ               0x100
61 
62 #define BYTESWAP(x)   __asm__ __volatile__ ("bswap %0": "=r" (x): "0" (x))
63 
64 #ifdef __x86_64__
65 #define BYTESWAP64(x) __asm__ __volatile__ ("bswap %q0": "=r" (x): "0" (x))
66 #else
67 #define BYTESWAP64(x) x = \
68    ((((x) & 0xff00000000000000ull) >> 56)   \
69   | (((x) & 0x00ff000000000000ull) >> 40)   \
70   | (((x) & 0x0000ff0000000000ull) >> 24)   \
71   | (((x) & 0x000000ff00000000ull) >>  8)   \
72   | (((x) & 0x00000000ff000000ull) <<  8)   \
73   | (((x) & 0x0000000000ff0000ull) << 24)   \
74   | (((x) & 0x000000000000ff00ull) << 40)   \
75   | (((x) & 0x00000000000000ffull) << 56))
76 #endif
77 
78 #ifndef MIN
79 #define MIN(a,b) ((a) < (b)) ? (a) : (b)
80 #endif
81 
82 #ifndef MAX
83 #define MAX(a,b) ((a) > (b)) ? (a) : (b)
84 #endif
85 
86 #ifdef WINDOWS
87 #include <windows.h>
88 #include <process.h>
89 #include <intrin.h>
90 typedef HANDLE THREAD;
91 typedef HANDLE MUTEX;
92 typedef unsigned (__stdcall *PTHREAD_START) (void *);
93 #define ACCreateThreadEx(Dthread,Dstart,Darg,Did) Dthread = (HANDLE) _beginthreadex (NULL, 0, (PTHREAD_START) Dstart, Darg, 0, Did)
94 #define ACMutexLock(Dmutex)                       WaitForSingleObject (Dmutex, INFINITE)
95 #define ACMutexUnlock(Dmutex)                     ReleaseMutex (Dmutex)
96 #define ACMutexInit(Dmutex)                       Dmutex = CreateMutex (0, FALSE, 0)
97 #endif
98 
99 #ifdef POSIX
100 #include <pthread.h>
101 typedef pthread_t THREAD;
102 typedef pthread_mutex_t MUTEX;
103 #define ACCreateThreadEx(Dthread,Dstart,Darg,Did) pthread_create        (&Dthread, NULL, (void *) Dstart, Darg)
104 #define ACMutexLock(Dmutex)                       pthread_mutex_lock    (&Dmutex)
105 #define ACMutexUnlock(Dmutex)                     pthread_mutex_unlock  (&Dmutex)
106 #define ACMutexInit(Dmutex)                       pthread_mutex_init    (&Dmutex, NULL)
107 #endif
108 
109 #ifdef WINDOWS
110 #define SetPriorityLow()    { HANDLE hProc = GetCurrentProcess(); SetPriorityClass (hProc, IDLE_PRIORITY_CLASS);   }
111 #define SetPriorityNormal() { HANDLE hProc = GetCurrentProcess(); SetPriorityClass (hProc, NORMAL_PRIORITY_CLASS); }
112 #define SetPriorityHigh()   { HANDLE hProc = GetCurrentProcess(); SetPriorityClass (hProc, HIGH_PRIORITY_CLASS);   }
113 #endif
114 
115 #ifdef POSIX
116 #include <sys/resource.h>
117 #define SetPriorityLow()    setpriority (PRIO_PROCESS, 0, 1)
118 #define SetPriorityNormal() setpriority (PRIO_PROCESS, 0, 0)
119 #define SetPriorityHigh()   setpriority (PRIO_PROCESS, 0, -1)
120 #endif
121 
122 #ifdef WINDOWS
123 #define hc_sleep(x) Sleep ((x) * 1000);
124 #endif
125 
126 #ifdef POSIX
127 #define hc_sleep(x) sleep ((x));
128 #endif
129 
130 #ifdef WINDOWS
131 typedef UINT8  uint8_t;
132 typedef UINT16 uint16_t;
133 typedef UINT32 uint32_t;
134 typedef UINT64 uint64_t;
135 typedef INT8   int8_t;
136 typedef INT16  int16_t;
137 typedef INT32  int32_t;
138 typedef INT64  int64_t;
139 #endif
140 
141 typedef uint32_t uint;
142 typedef uint64_t uint64;
143 
144 
145 /*
146  * types
147  */
148 
149 typedef unsigned int bool;
150 
151 typedef struct
152 {
153   uint8_t     w_buf[16];
154   uint8_t     w_len;
155 
156 } hc_wchar_t;
157 
158 typedef struct
159 {
160   hc_wchar_t  tbl_buf[4096];
161   uint32_t    tbl_cnt;
162 
163 } tbl_t;
164 
165 typedef struct
166 {
167   char        cs_buf[CHARSIZ];
168   uint32_t    cs_len;
169   uint8_t     cs_pos;
170 
171   uint8_t     buf_pos;
172 
173 } cs_t;
174 
175 typedef struct
176 {
177   uint pke[25];
178   uint eapol[64];
179   int  eapol_size;
180   int  keyver;
181 
182 } wpa_t;
183 
184 typedef struct
185 {
186   char *URI_server;
187   char *URI_client;
188 
189   char *user;
190   char *realm;
191   char *method;
192 
193   char *URI_prefix;
194   char *URI_resource;
195   char *URI_suffix;
196 
197   char *nonce;
198   char *nonce_client;
199   char *nonce_count;
200 
201   char *qop;
202   char *directive; // only "MD5" supported, no support for MD5-sess yet
203 
204 } sip_t;
205 
206 typedef struct
207 {
208   char          essid[36];
209 
210   unsigned char mac1[6];
211   unsigned char mac2[6];
212   unsigned char nonce1[32];
213   unsigned char nonce2[32];
214 
215   unsigned char eapol[256];
216   int           eapol_size;
217 
218   int           keyver;
219   unsigned char keymic[16];
220 
221 } hccap_t;
222 
223 typedef struct
224 {
225   char     *cache_buf;
226   uint64_t  cache_cnt;
227   uint64_t  cache_avail;
228 
229   char    **words_buf;
230   uint32_t *words_len;
231   uint64_t  words_cnt;
232   uint64_t  words_avail;
233 
234 } words_t;
235 
236 typedef struct
237 {
238   char    **rules_buf;
239   uint32_t *rules_len;
240   uint64_t  rules_cnt;
241   uint64_t  rules_avail;
242 
243   void *root_rule;
244 
245 } rules_t;
246 
247 typedef struct
248 {
249   char *user_name;
250   uint  user_len;
251 
252 } user_t;
253 
254 typedef union
255 {
256   uint32_t  md4[8];
257   uint32_t  md5[4];
258   uint32_t  sha1[5];
259   uint32_t  sha256[8];
260   uint64_t  sha512[8];
261   uint32_t  mysql[2];
262   uint32_t  descrypt[2];
263   uint32_t  bcrypt[6];
264   uint64_t  keccak[25];
265   uint32_t  gost[8];
266   char      plain[64];
267 
268 } digest_types_u;
269 
270 typedef struct
271 {
272   digest_types_u buf;
273 
274   char *plain;
275 
276   uint32_t found;
277 
278   user_t *user;
279 
280 } digest_t;
281 
282 typedef struct
283 {
284   union
285   {
286     uint8_t  buf8[128];
287     uint32_t buf32[16];
288     uint64_t buf64[8];
289     __m128i  buf128[4];
290   };
291 
292 } digest_md5_sse2_t;
293 
294 typedef struct
295 {
296   union
297   {
298     uint8_t  buf8[128];
299     uint32_t buf32[16];
300     uint64_t buf64[8];
301     __m128i  buf128[4];
302   };
303 
304 } digest_md4_sse2_t;
305 
306 typedef struct
307 {
308   union
309   {
310     uint8_t  buf8[160];
311     uint32_t buf32[20];
312     uint64_t buf64[10];
313     __m128i  buf128[5];
314   };
315 
316 } digest_sha1_sse2_t;
317 
318 typedef struct
319 {
320   union
321   {
322     uint8_t  buf8[256];
323     uint32_t buf32[32];
324     uint64_t buf64[16];
325     __m128i  buf128[8];
326   };
327 
328 } digest_sha256_sse2_t;
329 
330 typedef struct
331 {
332   union
333   {
334     uint8_t  buf8[512];
335     uint32_t buf32[64];
336     uint64_t buf64[32];
337     __m128i  buf128[16];
338   };
339 
340 } digest_sha512_sse2_t;
341 
342 typedef struct
343 {
344   union
345   {
346     uint8_t  buf8[192];
347     uint32_t buf32[24];
348     uint64_t buf64[12];
349     __m128i  buf128[6];
350   };
351 
352 } digest_bcrypt_sse2_t;
353 
354 typedef struct
355 {
356   digest_t **digests_buf;
357   uint64_t  digests_cnt;
358   uint64_t  digests_avail;
359   uint64_t  digests_found;
360 
361 } index_t;
362 
363 typedef struct
364 {
365   uint32_t nr_buf[16];
366   uint32_t nr_len;
367 
368   uint32_t msg_buf[128];
369   uint32_t msg_len;
370 
371 } ikepsk_t;
372 
373 typedef struct
374 {
375   uint user_len;
376   uint domain_len;
377   uint srvchall_len;
378   uint clichall_len;
379 
380   uint userdomain_buf[16];
381   uint chall_buf[256];
382 
383 } netntlm_t;
384 
385 typedef struct
386 {
387   union
388   {
389     uint8_t  buf8[256];
390     uint32_t buf[64];
391     uint64_t buf64[32];
392     __m128i  buf128[16];
393   };
394 
395   uint32_t len;
396 
397   char  *debug_buf;
398   int    debug_len;
399 
400   uint64_t pos;
401 
402 } plain_t;
403 
404 typedef struct
405 {
406   char     *salt_plain_buf;
407   uint32_t  salt_plain_len;
408 
409   plain_t   salt_plain_struct[4];
410   plain_t   additional_plain_struct[4];
411 
412   char     *salt_prehashed_buf;
413   uint32_t  salt_prehashed_len;
414 
415   uint32_t *ipad_prehashed_buf;
416   uint32_t *opad_prehashed_buf;
417 
418   uint64_t *ipad_prehashed_buf64;
419   uint64_t *opad_prehashed_buf64;
420 
421   uint32_t  netntlmv1_pc;
422 
423   netntlm_t *netntlm;
424   ikepsk_t  *ikepsk;
425   wpa_t     *wpa;
426   sip_t     *sip;
427 
428   char      md5chap_idbyte;
429 
430   uint32_t  keccak_rsiz;
431   uint32_t  keccak_mdlen;
432 
433   uint32_t  iterations;
434 
435   char     *signature;
436 
437   index_t **indexes_buf;
438   uint64_t  indexes_cnt;
439   uint64_t  indexes_avail;
440   uint64_t  indexes_found;
441 
442   uint32_t scrypt_N;
443   uint32_t scrypt_r;
444   uint32_t scrypt_p;
445 
446 } salt_t;
447 
448 typedef struct
449 {
450   rules_t *rules;
451   words_t *words;
452 
453   salt_t **salts_buf;
454 
455   uint64_t salts_cnt;
456   uint64_t salts_avail;
457   uint64_t salts_found;
458 
459 } db_t;
460 
461 typedef struct
462 {
463   digest_t digest;
464   salt_t   *salt;
465   void     *esalt;
466 
467 } hash_t;
468 
469 typedef struct
470 {
471   char    plain_buf[256];
472   int     plain_len;
473 
474   hash_t  hash;
475 
476   uint    pot_cnt;
477 
478 } pot_t;
479 
480 typedef uint8_t  u8;
481 typedef uint16_t u16;
482 typedef uint32_t u32;
483 typedef uint64_t u64;
484 
485 #define IN_LEN_MIN    1
486 #define IN_LEN_MAX    32
487 #define OUT_LEN_MAX   32 /* Limited by (u32)(1 << pw_len - 1) */
488 #define ELEM_CNT_MIN  1
489 #define ELEM_CNT_MAX  8
490 
491 typedef struct
492 {
493   int len;
494   u64 cnt;
495 
496 } pw_order_t;
497 
498 typedef struct
499 {
500   u8   *buf;
501 
502 } elem_t;
503 
504 typedef struct
505 {
506   u8   *buf;
507   int   cnt;
508 
509   mpz_t ks_cnt;
510   mpz_t ks_pos;
511 
512 } chain_t;
513 
514 typedef struct
515 {
516   elem_t  *elems_buf;
517   u64      elems_cnt;
518   u64      elems_alloc;
519 
520   chain_t *chains_buf;
521   int      chains_cnt;
522   int      chains_pos;
523   int      chains_alloc;
524 
525   u64      cur_chain_ks_poses[OUT_LEN_MAX];
526 
527 } db_entry_t;
528 
529 typedef struct
530 {
531   uint32_t attack_mode;
532   uint32_t hash_mode;
533   uint32_t hash_type;
534   uint32_t debug_mode;
535   uint32_t salt_type;
536   uint32_t num_threads;
537   uint32_t cache_size;
538   uint64_t words_skip;
539   uint64_t words_limit;
540   uint32_t hex_salt;
541 
542   uint32_t hashcat_status;
543   uint32_t benchmark;
544 
545   char    *mask;
546   uint32_t maskcnt;
547   uint32_t maskpos;
548   cs_t    *css_buf;
549   uint32_t css_cnt;
550   uint32_t pw_len;
551 
552   uint32_t perm_min;
553   uint32_t perm_max;
554 
555   uint32_t table_min;
556   uint32_t table_max;
557   tbl_t    table_buf[256];
558 
559   char     separator;
560   uint32_t output_autohex;
561   uint32_t username;
562   uint32_t show;
563   uint32_t left;
564   uint32_t remove;
565   uint32_t quiet;
566 
567   struct timeval timer_paused;
568   float          ms_paused;
569 
570   uint32_t status_timer;
571   uint32_t runtime;
572   uint32_t status_automat;
573 
574   uint32_t hex_charset;
575 
576   char    *file_words;
577   char    *file_hashes;
578   char    *file_output;
579   char    *file_debug;
580   char    *file_pot;
581 
582   uint32_t output_format;
583 
584   uint32_t plain_size_max;
585 
586   pot_t   *pot;
587 
588 } engine_parameter_t;
589 
590 typedef struct __thread_parameter
591 {
592   uint32_t hash_type;
593 
594   uint32_t thread_id;
595 
596   uint32_t num_threads;
597 
598   uint64_t thread_words_skip;
599   uint64_t thread_words_limit;
600   uint64_t thread_words_done;
601 
602   uint64_t thread_plains_done;
603 
604   uint32_t plain_size_max;
605 
606   void (*indb) (struct __thread_parameter *, plain_t *, digest_t *, salt_t *);
607 
608   void (*hashing) (struct __thread_parameter *, plain_t *);
609 
610   int (*compare_digest) (const void *, const void *);
611 
612   void (*store_out) (plain_t *, digest_t *, salt_t *);
613 
614   void (*store_debug) (char *, int);
615 
616   void (*done) ();
617 
618   uint32_t *hashcat_status;
619 
620   uint32_t (*get_index) (digest_t *);
621 
622   db_t *db;
623 
624   digest_t *quick_digest;
625 
626   cs_t    *css_buf;
627   uint32_t css_cnt;
628   uint32_t pw_len;
629 
630   tbl_t   *table_buf;
631 
632   uint32_t debug_mode;
633   char    *debug_file;
634 
635   uint32_t fake;
636 
637   char    separator;
638 
639   uint32_t *scrypt_P[4];
640   __m128i *scrypt_V;
641   __m128i *scrypt_X;
642   __m128i *scrypt_Y;
643 
644   /**
645    * prince
646    */
647 
648   int order_cnt;
649 
650   mpz_t total_ks_cnt;
651   mpz_t total_ks_pos;
652   mpz_t total_ks_left;
653 
654   db_entry_t *db_entries;
655   pw_order_t *pw_orders;
656   u64        *wordlen_dist;
657 
658 } thread_parameter_t;
659 
660 typedef struct
661 {
662   engine_parameter_t *engine_parameter;
663 
664   db_t *db;
665 
666   struct timeval cache_start;
667   struct timeval cache_current;
668 
669   uint64_t segment_pos;
670   uint64_t segment_cnt;
671 
672   uint64_t proc_words;
673   uint64_t proc_hashes;
674   uint64_t proc_recovered;
675   uint64_t proc_saved;
676 
677 } status_info_t;
678 
679 typedef struct
680 {
681   uint64_t state[8];
682 
683   union
684   {
685     uint64_t w[16];
686     uint8_t  buf[128];
687   };
688 
689   int len;
690 
691 } hc_sha512_ctx;
692 
693 typedef struct
694 {
695   uint32_t state[8];
696 
697   union
698   {
699     uint32_t w[16];
700     uint8_t  buf[64];
701   };
702 
703   int len;
704 
705 } hc_sha256_ctx;
706 
707 /*
708  * functions
709  */
710 
711 void dump_hex (const char *s, size_t size);
712 
713 void log_info (const char *fmt, ...);
714 
715 void log_warning (const char *fmt, ...);
716 
717 void log_error (const char *fmt, ...);
718 
719 uint32_t get_random_num (uint32_t min, uint32_t max);
720 
721 void *mycalloc (size_t nmemb, size_t size);
722 
723 void *mymalloc (size_t size);
724 
725 void *malloc_tiny (const size_t size);
726 
727 void myfree (void *ptr);
728 
729 void *myrealloc (void *ptr, size_t size);
730 
731 char *mystrdup (const char *s);
732 
733 int in_superchop (char *buf);
734 
735 /*
736  * bits rotate/shift
737  */
738 
739 #define ROTL32(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
740 #define ROTR32(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
741 
742 #define ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n))))
743 #define ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
744 
745 #define SHR(x, n) ((x) >> (n))
746 #define SHR32_SSE(x, n) _mm_srli_epi32 ((x), (n))
747 
748 #define SHR64_SSE(x, n) _mm_srli_epi64 ((x), (n))
749 
750 #ifdef __XOP__
751 
752 #define ROTL32_SSE(x, n) _mm_roti_epi32 ((x), (n))
753 
754 #define ROTL64_SSE(x, n) _mm_roti_epi64 ((x), (n))
755 #define ROTR64_SSE(x, n) _mm_roti_epi64 ((x), (-n))
756 
757 #else
758 
759 #define ROTL32_SSE(x, n) _mm_or_si128 (_mm_slli_epi32 ((x), (n)), _mm_srli_epi32 ((x), (32 - (n))))
760 
761 #define ROTL64_SSE(x, n) _mm_or_si128 (_mm_slli_epi64 ((x), (n)), _mm_srli_epi64 ((x), (64 - (n))))
762 #define ROTR64_SSE(x, n) _mm_or_si128 (_mm_srli_epi64 ((x), (n)), _mm_slli_epi64 ((x), (64 - (n))))
763 
764 #endif /* __XOP___*/
765 
766 #ifdef __SSSE3__
767 #define SWAP64_SSE(v) _mm_shuffle_epi8 (v, _mm_set_epi32 (0x08090a0b, 0x0c0d0e0f, 0x00010203, 0x04050607))
768 #else
769 #define SWAP64_SSE(v) \
770     _mm_slli_epi64 (v, 56) \
771   | _mm_and_si128 (_mm_slli_epi64 (v, 40), _mm_set1_epi64 ((__m64 ) 0x00FF000000000000ULL)) \
772   | _mm_and_si128 (_mm_slli_epi64 (v, 24), _mm_set1_epi64 ((__m64 ) 0x0000FF0000000000ULL)) \
773   | _mm_and_si128 (_mm_slli_epi64 (v,  8), _mm_set1_epi64 ((__m64 ) 0x000000FF00000000ULL)) \
774   | _mm_and_si128 (_mm_srli_epi64 (v,  8), _mm_set1_epi64 ((__m64 ) 0x00000000FF000000ULL)) \
775   | _mm_and_si128 (_mm_srli_epi64 (v, 24), _mm_set1_epi64 ((__m64 ) 0x0000000000FF0000ULL)) \
776   | _mm_and_si128 (_mm_srli_epi64 (v, 40), _mm_set1_epi64 ((__m64 ) 0x000000000000FF00ULL)) \
777   | _mm_srli_epi64 (v, 56)
778 #endif
779 
780 #ifdef __SSSE3__
781 #define SWAP32_SSE(v) _mm_shuffle_epi8 (v, _mm_set_epi32 (0x0c0d0e0f, 0x08090a0b, 0x04050607, 0x00010203))
782 #else
783 #define SWAP32_SSE(v) \
784     _mm_slli_epi32 (v, 24) \
785   | _mm_and_si128 (_mm_slli_epi32 (v, 8), _mm_set1_epi32 (0x00FF0000)) \
786   | _mm_and_si128 (_mm_srli_epi32 (v, 8), _mm_set1_epi32 (0x0000FF00)) \
787   | _mm_srli_epi32 (v, 24)
788 #endif
789 
790 #endif /* COMMON_H */
791