common.h 14 KB

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