1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-11-23 00:28:11 +00:00

Move all induction folder related host code into induct.c

This commit is contained in:
jsteube 2016-09-22 17:06:53 +02:00
parent b6f966970b
commit 919b5d5148
4 changed files with 162 additions and 132 deletions

View File

@ -10,8 +10,11 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#define INDUCT_DIR "induct" static const char INDUCT_DIR[] = "induct";
int sort_by_mtime (const void *p1, const void *p2); int induct_ctx_init (induct_ctx_t *induct_ctx, const user_options_t *user_options, const folder_config_t *folder_config, const time_t proc_start);
void induct_ctx_scan (induct_ctx_t *induct_ctx);
void induct_ctx_cleanup (induct_ctx_t *induct_ctx);
void induct_ctx_destroy (induct_ctx_t *induct_ctx);
#endif // _INDUCT_H #endif // _INDUCT_H

View File

@ -892,6 +892,17 @@ typedef struct
} folder_config_t; } folder_config_t;
typedef struct
{
bool enabled;
char *root_directory;
char **induction_dictionaries;
int induction_dictionaries_cnt;
} induct_ctx_t;
typedef struct typedef struct
{ {
/** /**
@ -965,7 +976,6 @@ typedef struct
u32 maskpos; u32 maskpos;
char *eff_restore_file; char *eff_restore_file;
char *new_restore_file; char *new_restore_file;
char *induction_directory;
char *outfile_check_directory; char *outfile_check_directory;
u32 pw_min; u32 pw_min;
u32 pw_max; u32 pw_max;
@ -981,6 +991,7 @@ typedef struct
debugfile_ctx_t *debugfile_ctx; debugfile_ctx_t *debugfile_ctx;
session_ctx_t *session_ctx; session_ctx_t *session_ctx;
bitmap_ctx_t *bitmap_ctx; bitmap_ctx_t *bitmap_ctx;
induct_ctx_t *induct_ctx;
/** /**
* used for restore * used for restore

View File

@ -331,65 +331,13 @@ int main (int argc, char **argv)
* induction directory * induction directory
*/ */
char *induction_directory = NULL; induct_ctx_t *induct_ctx = (induct_ctx_t *) mymalloc (sizeof (induct_ctx_t));
if (user_options->attack_mode != ATTACK_MODE_BF) data.induct_ctx = induct_ctx;
{
if ((user_options->keyspace == false) && (user_options->benchmark == false) && (user_options->opencl_info == false))
{
if (user_options->induction_dir == NULL)
{
induction_directory = (char *) mymalloc (HCBUFSIZ_TINY);
snprintf (induction_directory, HCBUFSIZ_TINY - 1, "%s/%s.%s", folder_config->session_dir, user_options->session, INDUCT_DIR); const int rc_induct_ctx_init = induct_ctx_init (induct_ctx, user_options, folder_config, proc_start);
// create induction folder if it does not already exist if (rc_induct_ctx_init == -1) return -1;
if (user_options->keyspace == false)
{
if (rmdir (induction_directory) == -1)
{
if (errno == ENOENT)
{
// good, we can ignore
}
else if (errno == ENOTEMPTY)
{
char *induction_directory_mv = (char *) mymalloc (HCBUFSIZ_TINY);
snprintf (induction_directory_mv, HCBUFSIZ_TINY - 1, "%s/%s.induct.%d", folder_config->session_dir, user_options->session, (int) proc_start);
if (rename (induction_directory, induction_directory_mv) != 0)
{
log_error ("ERROR: Rename directory %s to %s: %s", induction_directory, induction_directory_mv, strerror (errno));
return -1;
}
}
else
{
log_error ("ERROR: %s: %s", induction_directory, strerror (errno));
return -1;
}
}
if (mkdir (induction_directory, 0700) == -1)
{
log_error ("ERROR: %s: %s", induction_directory, strerror (errno));
return -1;
}
}
}
else
{
induction_directory = user_options->induction_dir;
}
}
}
data.induction_directory = induction_directory;
/** /**
* tuning db * tuning db
@ -2581,10 +2529,6 @@ int main (int argc, char **argv)
} }
} }
char **induction_dictionaries = NULL;
int induction_dictionaries_cnt = 0;
hcstat_table_t *root_table_buf = NULL; hcstat_table_t *root_table_buf = NULL;
hcstat_table_t *markov_table_buf = NULL; hcstat_table_t *markov_table_buf = NULL;
@ -2849,24 +2793,11 @@ int main (int argc, char **argv)
} }
} }
free (induction_dictionaries); /**
* update induction directory scan
*/
// induction_dictionaries_cnt = 0; // implied induct_ctx_scan (induct_ctx);
if (user_options->attack_mode != ATTACK_MODE_BF)
{
if ((user_options->keyspace == false) && (user_options->benchmark == false) && (user_options->opencl_info == false))
{
induction_dictionaries = scan_directory (induction_directory);
induction_dictionaries_cnt = count_dictionaries (induction_dictionaries);
}
}
if (induction_dictionaries_cnt)
{
qsort (induction_dictionaries, induction_dictionaries_cnt, sizeof (char *), sort_by_mtime);
}
/** /**
* prevent the user from using --skip/--limit together w/ maskfile and or dictfile * prevent the user from using --skip/--limit together w/ maskfile and or dictfile
@ -2977,9 +2908,9 @@ int main (int argc, char **argv)
{ {
char *dictfile = NULL; char *dictfile = NULL;
if (induction_dictionaries_cnt) if (induct_ctx->induction_dictionaries_cnt)
{ {
dictfile = induction_dictionaries[0]; dictfile = induct_ctx->induction_dictionaries[0];
} }
else else
{ {
@ -3066,9 +2997,9 @@ int main (int argc, char **argv)
{ {
char *dictfile = NULL; char *dictfile = NULL;
if (induction_dictionaries_cnt) if (induct_ctx->induction_dictionaries_cnt)
{ {
dictfile = induction_dictionaries[0]; dictfile = induct_ctx->induction_dictionaries[0];
} }
else else
{ {
@ -3426,7 +3357,7 @@ int main (int argc, char **argv)
if (user_options->loopback == true) if (user_options->loopback == true)
{ {
loopback_write_open (loopback_ctx, induction_directory); loopback_write_open (loopback_ctx, induct_ctx->root_directory);
} }
/** /**
@ -3584,22 +3515,14 @@ int main (int argc, char **argv)
user_options->restore = false; user_options->restore = false;
if (induction_dictionaries_cnt) if (induct_ctx->induction_dictionaries_cnt)
{ {
unlink (induction_dictionaries[0]); unlink (induct_ctx->induction_dictionaries[0]);
} }
free (induction_dictionaries); myfree (induct_ctx->induction_dictionaries);
if (user_options->attack_mode != ATTACK_MODE_BF) induct_ctx_scan (induct_ctx);
{
if ((user_options->keyspace == false) && (user_options->benchmark == false) && (user_options->opencl_info == false))
{
induction_dictionaries = scan_directory (induction_directory);
induction_dictionaries_cnt = count_dictionaries (induction_dictionaries);
}
}
if (user_options->benchmark == true) if (user_options->benchmark == true)
{ {
@ -3631,10 +3554,8 @@ int main (int argc, char **argv)
} }
} }
if (induction_dictionaries_cnt) if (induct_ctx->induction_dictionaries_cnt)
{ {
qsort (induction_dictionaries, induction_dictionaries_cnt, sizeof (char *), sort_by_mtime);
// yeah, this next statement is a little hack to make sure that --loopback runs correctly (because with it we guarantee that the loop iterates one more time) // yeah, this next statement is a little hack to make sure that --loopback runs correctly (because with it we guarantee that the loop iterates one more time)
dictpos--; dictpos--;
@ -3713,15 +3634,7 @@ int main (int argc, char **argv)
// if cracked / aborted remove last induction dictionary // if cracked / aborted remove last induction dictionary
for (int file_pos = 0; file_pos < induction_dictionaries_cnt; file_pos++) induct_ctx_cleanup (induct_ctx);
{
struct stat induct_stat;
if (stat (induction_dictionaries[file_pos], &induct_stat) == 0)
{
unlink (induction_dictionaries[file_pos]);
}
}
// wait for inner threads // wait for inner threads
@ -3990,28 +3903,7 @@ int main (int argc, char **argv)
// induction directory // induction directory
if (induction_directory != NULL) induct_ctx_destroy (induct_ctx);
{
if (rmdir (induction_directory) == -1)
{
if (errno == ENOENT)
{
// good, we can ignore
}
else if (errno == ENOTEMPTY)
{
// good, we can ignore
}
else
{
log_error ("ERROR: %s: %s", induction_directory, strerror (errno));
return -1;
}
}
local_free (induction_directory);
}
// outfile-check directory // outfile-check directory

View File

@ -4,9 +4,13 @@
*/ */
#include "common.h" #include "common.h"
#include "types.h"
#include "memory.h"
#include "logging.h"
#include "folder.h"
#include "induct.h" #include "induct.h"
int sort_by_mtime (const void *p1, const void *p2) static int sort_by_mtime (const void *p1, const void *p2)
{ {
const char **f1 = (const char **) p1; const char **f1 = (const char **) p1;
const char **f2 = (const char **) p2; const char **f2 = (const char **) p2;
@ -16,3 +20,123 @@ int sort_by_mtime (const void *p1, const void *p2)
return s2.st_mtime - s1.st_mtime; return s2.st_mtime - s1.st_mtime;
} }
int induct_ctx_init (induct_ctx_t *induct_ctx, const user_options_t *user_options, const folder_config_t *folder_config, const time_t proc_start)
{
induct_ctx->enabled = false;
if (user_options->attack_mode == ATTACK_MODE_BF) return 0;
if (user_options->keyspace == true) return 0;
if (user_options->benchmark == true) return 0;
if (user_options->opencl_info == true) return 0;
if (user_options->induction_dir == NULL)
{
char *root_directory = (char *) mymalloc (HCBUFSIZ_TINY);
snprintf (root_directory, HCBUFSIZ_TINY - 1, "%s/%s.%s", folder_config->session_dir, user_options->session, INDUCT_DIR);
if (rmdir (root_directory) == -1)
{
if (errno == ENOENT)
{
// good, we can ignore
}
else if (errno == ENOTEMPTY)
{
char *root_directory_mv = (char *) mymalloc (HCBUFSIZ_TINY);
snprintf (root_directory_mv, HCBUFSIZ_TINY - 1, "%s/%s.induct.%d", folder_config->session_dir, user_options->session, (int) proc_start);
if (rename (root_directory, root_directory_mv) != 0)
{
log_error ("ERROR: Rename directory %s to %s: %s", root_directory, root_directory_mv, strerror (errno));
return -1;
}
}
else
{
log_error ("ERROR: %s: %s", root_directory, strerror (errno));
return -1;
}
}
if (mkdir (root_directory, 0700) == -1)
{
log_error ("ERROR: %s: %s", root_directory, strerror (errno));
return -1;
}
induct_ctx->root_directory = root_directory;
}
else
{
induct_ctx->root_directory = mystrdup (user_options->induction_dir);
}
induct_ctx->enabled = true;
return 0;
}
void induct_ctx_scan (induct_ctx_t *induct_ctx)
{
if (induct_ctx->enabled == false) return;
induct_ctx->induction_dictionaries = scan_directory (induct_ctx->root_directory);
induct_ctx->induction_dictionaries_cnt = count_dictionaries (induct_ctx->induction_dictionaries);
qsort (induct_ctx->induction_dictionaries, induct_ctx->induction_dictionaries_cnt, sizeof (char *), sort_by_mtime);
}
void induct_ctx_cleanup (induct_ctx_t *induct_ctx)
{
if (induct_ctx->enabled == false) return;
for (int file_pos = 0; file_pos < induct_ctx->induction_dictionaries_cnt; file_pos++)
{
struct stat induct_stat;
if (stat (induct_ctx->induction_dictionaries[file_pos], &induct_stat) == 0)
{
unlink (induct_ctx->induction_dictionaries[file_pos]);
}
}
}
void induct_ctx_destroy (induct_ctx_t *induct_ctx)
{
if (induct_ctx->enabled == false)
{
myfree (induct_ctx);
return;
}
if (rmdir (induct_ctx->root_directory) == -1)
{
if (errno == ENOENT)
{
// good, we can ignore
}
else if (errno == ENOTEMPTY)
{
// good, we can ignore
}
else
{
log_error ("ERROR: %s: %s", induct_ctx->root_directory, strerror (errno));
//return -1;
}
}
myfree (induct_ctx->root_directory);
myfree (induct_ctx);
}