1
mirror of https://github.com/hashcat/hashcat synced 2024-12-09 02:13:10 +01:00
hashcat/src/dictstat.c

164 lines
4.0 KiB
C
Raw Normal View History

2016-09-08 18:56:33 +02:00
/**
* Author......: See docs/credits.txt
2016-09-08 18:56:33 +02:00
* License.....: MIT
*/
#include "common.h"
2016-09-16 17:01:18 +02:00
#include "types.h"
#include "memory.h"
2016-10-08 23:55:57 +02:00
#include "event.h"
2016-09-08 18:56:33 +02:00
#include "dictstat.h"
2016-11-05 11:33:29 +01:00
#include "locking.h"
2016-12-24 00:40:40 +01:00
#include "shared.h"
2016-09-08 18:56:33 +02:00
int sort_by_dictstat (const void *s1, const void *s2)
{
dictstat_t *d1 = (dictstat_t *) s1;
dictstat_t *d2 = (dictstat_t *) s2;
2016-11-29 22:39:22 +01:00
#if defined (__linux__) || defined (__CYGWIN__)
2016-09-08 18:56:33 +02:00
d2->stat.st_atim = d1->stat.st_atim;
#else
d2->stat.st_atime = d1->stat.st_atime;
#endif
return memcmp (&d1->stat, &d2->stat, sizeof (struct stat));
}
2016-10-06 17:26:15 +02:00
int dictstat_init (hashcat_ctx_t *hashcat_ctx)
{
2016-10-06 16:55:17 +02: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;
dictstat_ctx->enabled = false;
2016-10-06 17:26:15 +02:00
if (user_options->benchmark == true) return 0;
if (user_options->keyspace == true) return 0;
if (user_options->left == true) return 0;
if (user_options->opencl_info == true) return 0;
if (user_options->show == true) return 0;
if (user_options->usage == true) return 0;
if (user_options->version == true) return 0;
2016-09-30 11:50:13 +02:00
2016-10-06 17:26:15 +02:00
if (user_options->attack_mode == ATTACK_MODE_BF) return 0;
2016-09-30 11:50:13 +02:00
if (user_options->hash_mode == 3000) return 0; // this mode virtually creates words in the wordlists
2016-09-30 11:50:13 +02:00
dictstat_ctx->enabled = true;
dictstat_ctx->base = (dictstat_t *) hccalloc (MAX_DICTSTAT, sizeof (dictstat_t));
dictstat_ctx->cnt = 0;
2016-12-24 00:40:40 +01:00
hc_asprintf (&dictstat_ctx->filename, "%s/hashcat.dictstat", folder_config->profile_dir);
2016-10-06 17:26:15 +02:00
return 0;
}
2016-10-06 16:55:17 +02:00
void dictstat_destroy (hashcat_ctx_t *hashcat_ctx)
{
2016-10-06 16:55:17 +02:00
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
if (dictstat_ctx->enabled == false) return;
hcfree (dictstat_ctx->filename);
hcfree (dictstat_ctx->base);
2016-09-30 11:50:13 +02:00
memset (dictstat_ctx, 0, sizeof (dictstat_ctx_t));
}
2016-10-06 16:55:17 +02:00
void dictstat_read (hashcat_ctx_t *hashcat_ctx)
{
2016-10-06 16:55:17 +02:00
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
if (dictstat_ctx->enabled == false) return;
FILE *fp = fopen (dictstat_ctx->filename, "rb");
if (fp == NULL)
{
// first run, file does not exist, do not error out
return;
}
while (!feof (fp))
{
dictstat_t d;
2016-11-17 06:17:28 +01:00
const size_t nread = fread (&d, sizeof (dictstat_t), 1, fp);
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 10:55:02 +02:00
event_log_error (hashcat_ctx, "There are too many entries in the %s database. You have to remove/rename it.", dictstat_ctx->filename);
break;
}
}
fclose (fp);
}
2016-10-06 16:55:17 +02:00
int dictstat_write (hashcat_ctx_t *hashcat_ctx)
{
2016-10-06 16:55:17 +02:00
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
if (dictstat_ctx->enabled == false) return 0;
FILE *fp = fopen (dictstat_ctx->filename, "wb");
if (fp == NULL)
{
event_log_error (hashcat_ctx, "%s: %s", dictstat_ctx->filename, strerror (errno));
2016-11-05 11:33:29 +01:00
return -1;
}
if (lock_file (fp) == -1)
{
fclose (fp);
event_log_error (hashcat_ctx, "%s: %s", dictstat_ctx->filename, strerror (errno));
return -1;
}
fwrite (dictstat_ctx->base, sizeof (dictstat_t), dictstat_ctx->cnt, fp);
fclose (fp);
return 0;
}
2016-10-06 16:55:17 +02:00
u64 dictstat_find (hashcat_ctx_t *hashcat_ctx, dictstat_t *d)
{
2016-10-06 16:55:17 +02:00
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
if (dictstat_ctx->enabled == false) return 0;
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 16:55:17 +02:00
void dictstat_append (hashcat_ctx_t *hashcat_ctx, dictstat_t *d)
{
2016-10-06 16:55:17 +02:00
dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx;
if (dictstat_ctx->enabled == false) return;
if (dictstat_ctx->cnt == MAX_DICTSTAT)
{
2016-10-11 10:55:02 +02:00
event_log_error (hashcat_ctx, "There are too many entries in the %s database. You have to remove/rename it.", dictstat_ctx->filename);
return;
}
lsearch (d, dictstat_ctx->base, &dictstat_ctx->cnt, sizeof (dictstat_t), sort_by_dictstat);
}