2016-09-08 16:56:33 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-08 16:56:33 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2016-09-16 15:01:18 +00:00
|
|
|
#include "types.h"
|
2016-09-09 08:22:21 +00:00
|
|
|
#include "memory.h"
|
2017-09-20 08:22:18 +00:00
|
|
|
#include "bitops.h"
|
2016-10-08 21:55:57 +00:00
|
|
|
#include "event.h"
|
2016-11-05 10:33:29 +00:00
|
|
|
#include "locking.h"
|
2016-12-23 23:40:40 +00:00
|
|
|
#include "shared.h"
|
2019-03-31 15:39:00 +00:00
|
|
|
#include "dictstat.h"
|
2016-09-08 16:56:33 +00:00
|
|
|
|
|
|
|
int sort_by_dictstat (const void *s1, const void *s2)
|
|
|
|
{
|
2018-02-08 18:13:29 +00:00
|
|
|
const dictstat_t *d1 = (const dictstat_t *) s1;
|
|
|
|
const dictstat_t *d2 = (const dictstat_t *) s2;
|
2017-09-29 10:13:29 +00:00
|
|
|
|
2020-03-10 14:42:55 +00:00
|
|
|
const int rc_hash = memcmp (d1->hash_filename, d2->hash_filename, 16);
|
|
|
|
|
|
|
|
if (rc_hash != 0) return rc_hash;
|
|
|
|
|
2017-04-14 14:36:28 +00:00
|
|
|
const int rc_from = strcmp (d1->encoding_from, d2->encoding_from);
|
|
|
|
|
|
|
|
if (rc_from != 0) return rc_from;
|
|
|
|
|
|
|
|
const int rc_to = strcmp (d1->encoding_to, d2->encoding_to);
|
|
|
|
|
|
|
|
if (rc_to != 0) return rc_to;
|
|
|
|
|
2018-02-08 18:13:29 +00:00
|
|
|
struct stat stat1;
|
|
|
|
struct stat stat2;
|
|
|
|
|
|
|
|
memcpy (&stat1, &d1->stat, sizeof (struct stat));
|
|
|
|
memcpy (&stat2, &d2->stat, sizeof (struct stat));
|
|
|
|
|
|
|
|
stat1.st_atime = 0;
|
|
|
|
stat2.st_atime = 0;
|
|
|
|
|
|
|
|
#if defined (STAT_NANOSECONDS_ACCESS_TIME)
|
|
|
|
stat1.STAT_NANOSECONDS_ACCESS_TIME = 0;
|
|
|
|
stat2.STAT_NANOSECONDS_ACCESS_TIME = 0;
|
|
|
|
#endif
|
|
|
|
|
2018-02-12 13:56:28 +00:00
|
|
|
const int rc_memcmp = memcmp (&stat1, &stat2, sizeof (struct stat));
|
|
|
|
|
|
|
|
return rc_memcmp;
|
2016-09-08 16:56:33 +00:00
|
|
|
}
|
2016-09-09 08:22:21 +00:00
|
|
|
|
2016-10-06 15:26:15 +00:00
|
|
|
int dictstat_init (hashcat_ctx_t *hashcat_ctx)
|
2016-09-09 08:22:21 +00:00
|
|
|
{
|
2016-10-06 14:55:17 +00:00
|
|
|
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
|
|
|
|
folder_config_t *folder_config = hashcat_ctx->folder_config;
|
|
|
|
user_options_t *user_options = hashcat_ctx->user_options;
|
|
|
|
|
2016-09-24 11:01:17 +00:00
|
|
|
dictstat_ctx->enabled = false;
|
|
|
|
|
2023-05-01 15:38:42 +00:00
|
|
|
if (user_options->usage > 0) return 0;
|
|
|
|
if (user_options->backend_info > 0) return 0;
|
|
|
|
|
2021-06-12 08:47:48 +00:00
|
|
|
if (user_options->benchmark == true) return 0;
|
|
|
|
if (user_options->hash_info == true) return 0;
|
|
|
|
if (user_options->keyspace == true) return 0;
|
|
|
|
if (user_options->left == true) return 0;
|
|
|
|
if (user_options->show == true) return 0;
|
|
|
|
if (user_options->version == true) return 0;
|
|
|
|
if (user_options->identify == true) return 0;
|
2016-09-30 09:50:13 +00:00
|
|
|
|
2016-10-06 15:26:15 +00:00
|
|
|
if (user_options->attack_mode == ATTACK_MODE_BF) return 0;
|
2016-09-30 09:50:13 +00:00
|
|
|
|
2023-05-01 15:38:42 +00:00
|
|
|
dictstat_ctx->enabled = true;
|
|
|
|
dictstat_ctx->base = (dictstat_t *) hccalloc (MAX_DICTSTAT, sizeof (dictstat_t));
|
|
|
|
dictstat_ctx->cnt = 0;
|
2016-09-09 08:22:21 +00:00
|
|
|
|
2017-09-20 08:22:18 +00:00
|
|
|
hc_asprintf (&dictstat_ctx->filename, "%s/%s", folder_config->profile_dir, DICTSTAT_FILENAME);
|
2016-10-06 15:26:15 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-09-09 08:22:21 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 14:55:17 +00:00
|
|
|
void dictstat_destroy (hashcat_ctx_t *hashcat_ctx)
|
2016-09-09 08:22:21 +00:00
|
|
|
{
|
2016-10-06 14:55:17 +00:00
|
|
|
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
|
|
|
|
|
2016-10-01 22:00:21 +00:00
|
|
|
if (dictstat_ctx->enabled == false) return;
|
2016-09-24 11:01:17 +00:00
|
|
|
|
2016-10-10 09:03:11 +00:00
|
|
|
hcfree (dictstat_ctx->filename);
|
|
|
|
hcfree (dictstat_ctx->base);
|
2016-09-30 09:50:13 +00:00
|
|
|
|
2016-10-01 22:00:21 +00:00
|
|
|
memset (dictstat_ctx, 0, sizeof (dictstat_ctx_t));
|
2016-09-09 08:22:21 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 14:55:17 +00:00
|
|
|
void dictstat_read (hashcat_ctx_t *hashcat_ctx)
|
2016-09-09 08:22:21 +00:00
|
|
|
{
|
2018-12-18 14:00:00 +00:00
|
|
|
hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
|
2016-10-06 14:55:17 +00:00
|
|
|
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
|
|
|
|
|
2016-09-24 11:01:17 +00:00
|
|
|
if (dictstat_ctx->enabled == false) return;
|
|
|
|
|
2018-12-18 14:00:00 +00:00
|
|
|
if (hashconfig->dictstat_disable == true) return;
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
HCFILE fp;
|
2016-09-09 08:22:21 +00:00
|
|
|
|
2019-07-01 15:27:08 +00:00
|
|
|
if (hc_fopen (&fp, dictstat_ctx->filename, "rb") == false)
|
2016-09-09 08:22:21 +00:00
|
|
|
{
|
|
|
|
// first run, file does not exist, do not error out
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-20 08:22:18 +00:00
|
|
|
// parse header
|
|
|
|
|
|
|
|
u64 v;
|
|
|
|
u64 z;
|
|
|
|
|
2019-06-27 18:18:47 +00:00
|
|
|
const size_t nread1 = hc_fread (&v, sizeof (u64), 1, &fp);
|
|
|
|
const size_t nread2 = hc_fread (&z, sizeof (u64), 1, &fp);
|
2017-09-20 08:22:18 +00:00
|
|
|
|
|
|
|
if ((nread1 != 1) || (nread2 != 1))
|
|
|
|
{
|
|
|
|
event_log_error (hashcat_ctx, "%s: Invalid header", dictstat_ctx->filename);
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
hc_fclose (&fp);
|
2017-10-02 13:44:03 +00:00
|
|
|
|
2017-09-20 08:22:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v = byte_swap_64 (v);
|
|
|
|
z = byte_swap_64 (z);
|
|
|
|
|
2017-10-02 13:44:03 +00:00
|
|
|
if ((v & 0xffffffffffffff00) != (DICTSTAT_VERSION & 0xffffffffffffff00))
|
2017-09-20 08:22:18 +00:00
|
|
|
{
|
2017-10-02 13:44:03 +00:00
|
|
|
event_log_error (hashcat_ctx, "%s: Invalid header, ignoring content", dictstat_ctx->filename);
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
hc_fclose (&fp);
|
2017-09-20 08:22:18 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (z != 0)
|
|
|
|
{
|
2017-10-02 13:44:03 +00:00
|
|
|
event_log_error (hashcat_ctx, "%s: Invalid header, ignoring content", dictstat_ctx->filename);
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
hc_fclose (&fp);
|
2017-10-02 13:44:03 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((v & 0xff) < (DICTSTAT_VERSION & 0xff))
|
|
|
|
{
|
|
|
|
event_log_warning (hashcat_ctx, "%s: Outdated header version, ignoring content", dictstat_ctx->filename);
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
hc_fclose (&fp);
|
2017-09-20 08:22:18 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse data
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
while (!hc_feof (&fp))
|
2016-09-09 08:22:21 +00:00
|
|
|
{
|
|
|
|
dictstat_t d;
|
|
|
|
|
2019-06-27 18:18:47 +00:00
|
|
|
const size_t nread = hc_fread (&d, sizeof (dictstat_t), 1, &fp);
|
2016-09-09 08:22:21 +00:00
|
|
|
|
|
|
|
if (nread == 0) continue;
|
|
|
|
|
|
|
|
lsearch (&d, dictstat_ctx->base, &dictstat_ctx->cnt, sizeof (dictstat_t), sort_by_dictstat);
|
|
|
|
|
|
|
|
if (dictstat_ctx->cnt == MAX_DICTSTAT)
|
|
|
|
{
|
2016-10-11 08:55:02 +00:00
|
|
|
event_log_error (hashcat_ctx, "There are too many entries in the %s database. You have to remove/rename it.", dictstat_ctx->filename);
|
2016-09-09 08:22:21 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
hc_fclose (&fp);
|
2016-09-09 08:22:21 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 14:55:17 +00:00
|
|
|
int dictstat_write (hashcat_ctx_t *hashcat_ctx)
|
2016-09-09 08:22:21 +00:00
|
|
|
{
|
2018-12-18 14:00:00 +00:00
|
|
|
hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
|
2016-10-06 14:55:17 +00:00
|
|
|
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
|
|
|
|
|
2016-09-24 11:01:17 +00:00
|
|
|
if (dictstat_ctx->enabled == false) return 0;
|
|
|
|
|
2018-12-18 14:00:00 +00:00
|
|
|
if (hashconfig->dictstat_disable == true) return 0;
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
HCFILE fp;
|
2016-09-09 08:22:21 +00:00
|
|
|
|
2019-07-01 15:27:08 +00:00
|
|
|
if (hc_fopen (&fp, dictstat_ctx->filename, "wb") == false)
|
2016-09-09 08:22:21 +00:00
|
|
|
{
|
2017-02-04 01:53:50 +00:00
|
|
|
event_log_error (hashcat_ctx, "%s: %s", dictstat_ctx->filename, strerror (errno));
|
2016-11-05 10:33:29 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 23:30:24 +00:00
|
|
|
if (hc_lockfile (&fp) == -1)
|
2016-11-05 10:33:29 +00:00
|
|
|
{
|
2019-06-26 17:06:46 +00:00
|
|
|
hc_fclose (&fp);
|
2016-11-20 22:15:54 +00:00
|
|
|
|
2017-02-04 01:53:50 +00:00
|
|
|
event_log_error (hashcat_ctx, "%s: %s", dictstat_ctx->filename, strerror (errno));
|
2016-09-09 08:22:21 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-09-20 08:22:18 +00:00
|
|
|
// header
|
|
|
|
|
|
|
|
u64 v = DICTSTAT_VERSION;
|
|
|
|
u64 z = 0;
|
|
|
|
|
|
|
|
v = byte_swap_64 (v);
|
|
|
|
z = byte_swap_64 (z);
|
|
|
|
|
2019-06-27 18:18:47 +00:00
|
|
|
hc_fwrite (&v, sizeof (u64), 1, &fp);
|
|
|
|
hc_fwrite (&z, sizeof (u64), 1, &fp);
|
2017-09-20 08:22:18 +00:00
|
|
|
|
|
|
|
// data
|
|
|
|
|
2019-06-27 18:18:47 +00:00
|
|
|
hc_fwrite (dictstat_ctx->base, sizeof (dictstat_t), dictstat_ctx->cnt, &fp);
|
2016-09-09 08:22:21 +00:00
|
|
|
|
2020-02-29 09:40:47 +00:00
|
|
|
if (hc_unlockfile (&fp) == -1)
|
|
|
|
{
|
|
|
|
hc_fclose (&fp);
|
|
|
|
|
|
|
|
event_log_error (hashcat_ctx, "%s: %s", dictstat_ctx->filename, strerror (errno));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
hc_fclose (&fp);
|
2016-09-09 08:22:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 14:55:17 +00:00
|
|
|
u64 dictstat_find (hashcat_ctx_t *hashcat_ctx, dictstat_t *d)
|
2016-09-09 08:22:21 +00:00
|
|
|
{
|
2018-12-18 14:00:00 +00:00
|
|
|
hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
|
2016-10-06 14:55:17 +00:00
|
|
|
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
|
|
|
|
|
2016-09-24 11:01:17 +00:00
|
|
|
if (dictstat_ctx->enabled == false) return 0;
|
|
|
|
|
2018-12-18 14:00:00 +00:00
|
|
|
if (hashconfig->dictstat_disable == true) return 0;
|
|
|
|
|
2016-09-09 08:22:21 +00:00
|
|
|
dictstat_t *d_cache = (dictstat_t *) lfind (d, dictstat_ctx->base, &dictstat_ctx->cnt, sizeof (dictstat_t), sort_by_dictstat);
|
|
|
|
|
|
|
|
if (d_cache == NULL) return 0;
|
|
|
|
|
|
|
|
return d_cache->cnt;
|
|
|
|
}
|
|
|
|
|
2016-10-06 14:55:17 +00:00
|
|
|
void dictstat_append (hashcat_ctx_t *hashcat_ctx, dictstat_t *d)
|
2016-09-09 08:22:21 +00:00
|
|
|
{
|
2018-12-18 14:00:00 +00:00
|
|
|
hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
|
2016-10-06 14:55:17 +00:00
|
|
|
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
|
|
|
|
|
2016-09-24 11:01:17 +00:00
|
|
|
if (dictstat_ctx->enabled == false) return;
|
|
|
|
|
2018-12-18 14:00:00 +00:00
|
|
|
if (hashconfig->dictstat_disable == true) return;
|
|
|
|
|
2016-09-09 08:22:21 +00:00
|
|
|
if (dictstat_ctx->cnt == MAX_DICTSTAT)
|
|
|
|
{
|
2016-10-11 08:55:02 +00:00
|
|
|
event_log_error (hashcat_ctx, "There are too many entries in the %s database. You have to remove/rename it.", dictstat_ctx->filename);
|
2016-09-09 08:22:21 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lsearch (d, dictstat_ctx->base, &dictstat_ctx->cnt, sizeof (dictstat_t), sort_by_dictstat);
|
|
|
|
}
|