common.h 14 KB

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