1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-11-22 16:18:09 +00:00

Helper: Added functions to check existence, type, read- and write-permissions and rewrite sources to use them instead of stat()

This commit is contained in:
jsteube 2017-01-27 11:46:45 +01:00
parent b7b2f1a79c
commit 17548d3ee8
12 changed files with 152 additions and 174 deletions

View File

@ -38,6 +38,7 @@
- Building: Add SHARED variable to Makefile to choose if hashcat is build as static or shared binary (using libhashcat.so/hashcat.dll)
- Building: Removed the use of RPATH on linker level
- Events: Improved the maximum event message handling. event_log () will now also internally make sure that the message is properly terminated
- Helper: Added functions to check existence, type, read- and write-permissions and rewrite sources to use them instead of stat()
* changes v3.20 -> v3.30:

View File

@ -40,4 +40,11 @@ int hc_fstat (int fd, hc_stat_t *buf);
void hc_qsort_r (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg);
void *hc_bsearch_r (const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg);
bool hc_path_is_file (const char *path);
bool hc_path_is_directory (const char *path);
bool hc_path_is_empty (const char *path);
bool hc_path_exist (const char *path);
bool hc_path_read (const char *path);
bool hc_path_write (const char *path);
#endif // _SHARED_H

View File

@ -44,13 +44,26 @@ int combinator_ctx_init (hashcat_ctx_t *hashcat_ctx)
char *dictfile1 = user_options_extra->hc_workv[0];
char *dictfile2 = user_options_extra->hc_workv[1];
// at this point we know the file actually exist
// find the bigger dictionary and use as base
if (hc_path_is_file (dictfile1) == false)
{
event_log_error (hashcat_ctx, "%s: Not a regular file", dictfile1);
return -1;
}
if (hc_path_is_file (dictfile2) == false)
{
event_log_error (hashcat_ctx, "%s: Not a regular file", dictfile2);
return -1;
}
FILE *fp1 = NULL;
FILE *fp2 = NULL;
hc_stat_t tmp_stat;
if ((fp1 = fopen (dictfile1, "rb")) == NULL)
{
event_log_error (hashcat_ctx, "%s: %m", dictfile1);
@ -58,50 +71,10 @@ int combinator_ctx_init (hashcat_ctx_t *hashcat_ctx)
return -1;
}
if (hc_stat (dictfile1, &tmp_stat) == -1)
{
event_log_error (hashcat_ctx, "%s: %m", dictfile1);
fclose (fp1);
return -1;
}
if (S_ISDIR (tmp_stat.st_mode))
{
event_log_error (hashcat_ctx, "%s must be a regular file", dictfile1);
fclose (fp1);
return -1;
}
if ((fp2 = fopen (dictfile2, "rb")) == NULL)
{
event_log_error (hashcat_ctx, "%s: %m", dictfile2);
fclose (fp1);
return -1;
}
if (hc_stat (dictfile2, &tmp_stat) == -1)
{
event_log_error (hashcat_ctx, "%s: %m", dictfile2);
fclose (fp1);
fclose (fp2);
return -1;
}
if (S_ISDIR (tmp_stat.st_mode))
{
event_log_error (hashcat_ctx, "%s must be a regular file", dictfile2);
fclose (fp1);
fclose (fp2);
return -1;
}

View File

@ -453,9 +453,7 @@ int hashes_init_stage1 (hashcat_ctx_t *hashcat_ctx)
if ((user_options->benchmark == false) && (user_options->stdout_flag == false) && (user_options->keyspace == false))
{
hc_stat_t f;
hashlist_mode = (hc_stat (hash_or_file, &f) == 0) ? HL_MODE_FILE : HL_MODE_ARG;
hashlist_mode = (hc_path_exist (hash_or_file) == true) ? HL_MODE_FILE : HL_MODE_ARG;
if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE)
{

View File

@ -14,7 +14,7 @@
// sysfs functions
static int sysfs_init (hashcat_ctx_t *hashcat_ctx)
static bool sysfs_init (hashcat_ctx_t *hashcat_ctx)
{
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
@ -26,13 +26,11 @@ static int sysfs_init (hashcat_ctx_t *hashcat_ctx)
snprintf (path, HCBUFSIZ_TINY - 1, "%s", SYS_BUS_PCI_DEVICES);
hc_stat_t s;
int rc = hc_stat (path, &s);
const bool r = hc_path_read (path);
hcfree (path);
return rc;
return r;
}
static void sysfs_close (hashcat_ctx_t *hashcat_ctx)
@ -3281,7 +3279,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
hwmon_ctx->hm_sysfs = sysfs;
if (sysfs_init (hashcat_ctx) == -1)
if (sysfs_init (hashcat_ctx) == false)
{
hcfree (hwmon_ctx->hm_sysfs);

View File

@ -1242,9 +1242,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
char *arg = user_options_extra->hc_workv[0];
hc_stat_t file_stat;
if (hc_stat (arg, &file_stat) == -1)
if (hc_path_exist (arg) == false)
{
const int rc = mask_append (hashcat_ctx, arg, NULL);
@ -1258,14 +1256,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
arg = user_options_extra->hc_workv[i];
if (hc_stat (arg, &file_stat) == -1)
{
event_log_error (hashcat_ctx, "%s: %m", arg);
return -1;
}
if (S_ISREG (file_stat.st_mode))
if (hc_path_is_file (arg) == true)
{
FILE *mask_fp = fopen (arg, "r");
@ -1348,9 +1339,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx)
// mod
hc_stat_t file_stat;
if (hc_stat (arg, &file_stat) == -1)
if (hc_path_exist (arg) == false)
{
const int rc = mask_append (hashcat_ctx, arg, NULL);
@ -1358,7 +1347,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx)
}
else
{
if (S_ISREG (file_stat.st_mode))
if (hc_path_is_file (arg) == true)
{
mask_ctx->mask_from_file = true;
@ -1424,9 +1413,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx)
// mod
hc_stat_t file_stat;
if (hc_stat (arg, &file_stat) == -1)
if (hc_path_exist (arg) == false)
{
const int rc = mask_append (hashcat_ctx, arg, NULL);
@ -1434,7 +1421,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx)
}
else
{
if (S_ISREG (file_stat.st_mode))
if (hc_path_is_file (arg) == true)
{
mask_ctx->mask_from_file = true;

View File

@ -50,9 +50,7 @@ static int ocl_check_dri (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx)
// No GPU available! That's fine, so we don't need to check if we have access to it.
hc_stat_t stat;
if (hc_stat (dri_card0_path, &stat) == -1) return 0;
if (hc_path_exist (dri_card0_path) == false) return 0;
// Now we need to check if this an AMD vendor, because this is when the problems start
@ -3797,22 +3795,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
for (int i = 0; i < files_cnt; i++)
{
FILE *fd = fopen (files_names[i], "r");
if (fd == NULL)
{
event_log_error (hashcat_ctx, "%s: %m", files_names[i]);
return -1;
}
char buf[1] = { 0 };
size_t n = fread (buf, 1, 1, fd);
fclose (fd);
if (n != 1)
if (hc_path_read (files_names[i]) == false)
{
event_log_error (hashcat_ctx, "%s: %m", files_names[i]);
@ -3849,9 +3832,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
generate_source_kernel_filename (hashconfig->attack_exec, user_options_extra->attack_kern, hashconfig->kern_type, folder_config->shared_dir, source_file);
hc_stat_t sst;
if (hc_stat (source_file, &sst) == -1)
if (hc_path_read (source_file) == false)
{
event_log_error (hashcat_ctx, "%s: %m", source_file);
@ -3866,13 +3847,16 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
generate_cached_kernel_filename (hashconfig->attack_exec, user_options_extra->attack_kern, hashconfig->kern_type, folder_config->profile_dir, device_name_chksum, cached_file);
int cached = 1;
bool cached = true;
hc_stat_t cst;
if ((hc_stat (cached_file, &cst) == -1) || cst.st_size == 0)
if (hc_path_read (cached_file) == false)
{
cached = 0;
cached = false;
}
if (hc_path_is_empty (cached_file) == true)
{
cached = false;
}
/**
@ -3885,7 +3869,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
if (opencl_ctx->force_jit_compilation == -1)
{
if (cached == 0)
if (cached == false)
{
#if defined (DEBUG)
if (user_options->quiet == false) event_log_warning (hashcat_ctx, "* Device #%u: Kernel %s not found in cache! Building may take a while...", device_id + 1, filename_from_filepath (cached_file));
@ -4057,9 +4041,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
generate_source_kernel_mp_filename (hashconfig->opti_type, hashconfig->opts_type, folder_config->shared_dir, source_file);
hc_stat_t sst;
if (hc_stat (source_file, &sst) == -1)
if (hc_path_read (source_file) == false)
{
event_log_error (hashcat_ctx, "%s: %m", source_file);
@ -4074,13 +4056,16 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
generate_cached_kernel_mp_filename (hashconfig->opti_type, hashconfig->opts_type, folder_config->profile_dir, device_name_chksum, cached_file);
int cached = 1;
bool cached = true;
hc_stat_t cst;
if (hc_stat (cached_file, &cst) == -1)
if (hc_path_read (cached_file) == false)
{
cached = 0;
cached = false;
}
if (hc_path_is_empty (cached_file) == true)
{
cached = false;
}
/**
@ -4091,7 +4076,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
char **kernel_sources = (char **) hcmalloc (sizeof (char *));
if (cached == 0)
if (cached == false)
{
#if defined (DEBUG)
if (user_options->quiet == false) event_log_warning (hashcat_ctx, "* Device #%u: Kernel %s not found in cache! Building may take a while...", device_id + 1, filename_from_filepath (cached_file));
@ -4199,9 +4184,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
generate_source_kernel_amp_filename (user_options_extra->attack_kern, folder_config->shared_dir, source_file);
hc_stat_t sst;
if (hc_stat (source_file, &sst) == -1)
if (hc_path_read (source_file) == false)
{
event_log_error (hashcat_ctx, "%s: %m", source_file);
@ -4216,13 +4199,16 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
generate_cached_kernel_amp_filename (user_options_extra->attack_kern, folder_config->profile_dir, device_name_chksum, cached_file);
int cached = 1;
bool cached = true;
hc_stat_t cst;
if (hc_stat (cached_file, &cst) == -1)
if (hc_path_read (cached_file) == false)
{
cached = 0;
cached = false;
}
if (hc_path_is_empty (cached_file) == true)
{
cached = false;
}
/**
@ -4233,7 +4219,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx)
char **kernel_sources = (char **) hcmalloc (sizeof (char *));
if (cached == 0)
if (cached == false)
{
#if defined (DEBUG)
if (user_options->quiet == false) event_log_warning (hashcat_ctx, "* Device #%u: Kernel %s not found in cache! Building may take a while...", device_id + 1, filename_from_filepath (cached_file));

View File

@ -67,14 +67,21 @@ static int outfile_remove (hashcat_ctx_t *hashcat_ctx)
if (check_left == 0)
{
hc_stat_t outfile_check_stat;
if (hc_stat (root_directory, &outfile_check_stat) == 0)
if (hc_path_exist (root_directory) == true)
{
u32 is_dir = S_ISDIR (outfile_check_stat.st_mode);
const bool is_dir = hc_path_is_directory (root_directory);
if (is_dir == 1)
if (is_dir == true)
{
hc_stat_t outfile_check_stat;
if (hc_stat (root_directory, &outfile_check_stat) == -1)
{
event_log_error (hashcat_ctx, "%s: %m", root_directory);
return -1;
}
if (outfile_check_stat.st_mtime > folder_mtime)
{
char **out_files_new = scan_directory (root_directory);
@ -349,15 +356,13 @@ int outcheck_ctx_init (hashcat_ctx_t *hashcat_ctx)
outcheck_ctx->root_directory = user_options->outfile_check_dir;
}
hc_stat_t outfile_check_stat;
if (hc_stat (outcheck_ctx->root_directory, &outfile_check_stat) == 0)
if (hc_path_exist (outcheck_ctx->root_directory) == true)
{
const u32 is_dir = S_ISDIR (outfile_check_stat.st_mode);
const bool is_dir = hc_path_is_directory (outcheck_ctx->root_directory);
if (is_dir == 0)
if (is_dir == false)
{
event_log_error (hashcat_ctx, "Directory specified in outfile-check '%s' is not a valid directory", outcheck_ctx->root_directory);
event_log_error (hashcat_ctx, "Directory specified in outfile-check '%s' is not a directory", outcheck_ctx->root_directory);
return -1;
}

View File

@ -116,9 +116,7 @@ int potfile_init (hashcat_ctx_t *hashcat_ctx)
hc_asprintf (&potfile_old, "%s/hashcat.pot", folder_config->profile_dir);
hc_stat_t st;
if (hc_stat (potfile_old, &st) == 0)
if (hc_path_exist (potfile_old) == true)
{
event_log_warning (hashcat_ctx, "Old potfile detected: %s", potfile_old);
event_log_warning (hashcat_ctx, "New potfile is: %s ", potfile_ctx->filename);

View File

@ -293,17 +293,15 @@ int cycle_restore (hashcat_ctx_t *hashcat_ctx)
if (rc_write_restore == -1) return -1;
hc_stat_t st;
if (hc_stat (eff_restore_file, &st) == 0)
if (hc_path_exist (eff_restore_file) == true)
{
if (unlink (eff_restore_file))
if (unlink (eff_restore_file) == -1)
{
event_log_warning (hashcat_ctx, "Unlink file '%s': %m", eff_restore_file);
}
}
if (rename (new_restore_file, eff_restore_file))
if (rename (new_restore_file, eff_restore_file) == -1)
{
event_log_warning (hashcat_ctx, "Rename file '%s' to '%s': %m", new_restore_file, eff_restore_file);
}

View File

@ -188,6 +188,60 @@ void *hc_bsearch_r (const void *key, const void *base, size_t nmemb, size_t size
return (NULL);
}
bool hc_path_is_file (const char *path)
{
hc_stat_t s;
if (hc_stat (path, &s) == -1) return false;
if (S_ISREG (s.st_mode)) return true;
return false;
}
bool hc_path_is_directory (const char *path)
{
hc_stat_t s;
if (hc_stat (path, &s) == -1) return false;
if (S_ISDIR (s.st_mode)) return true;
return false;
}
bool hc_path_is_empty (const char *path)
{
hc_stat_t s;
if (hc_stat (path, &s) == -1) return false;
if (s.st_size == 0) return true;
return false;
}
bool hc_path_exist (const char *path)
{
if (access (path, F_OK) == -1) return false;
return true;
}
bool hc_path_read (const char *path)
{
if (access (path, R_OK) == -1) return false;
return true;
}
bool hc_path_write (const char *path)
{
if (access (path, W_OK) == -1) return false;
return true;
}
void setup_environment_variables ()
{
char *compute = getenv ("COMPUTE");

View File

@ -268,16 +268,9 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
char *l0_filename = user_options_extra->hc_workv[i];
hc_stat_t l0_stat;
// at this point we already verified the path actually exist and is readable
if (hc_stat (l0_filename, &l0_stat) == -1)
{
event_log_error (hashcat_ctx, "%s: %m", l0_filename);
return -1;
}
if (S_ISDIR (l0_stat.st_mode))
if (hc_path_is_directory (l0_filename) == true)
{
char **dictionary_files = NULL;
@ -291,16 +284,14 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
char *l1_filename = dictionary_files[d];
hc_stat_t l1_stat;
if (hc_stat (l1_filename, &l1_stat) == -1)
if (hc_path_read (l1_filename) == false)
{
event_log_error (hashcat_ctx, "%s: %m", l1_filename);
return -1;
}
if (S_ISREG (l1_stat.st_mode))
if (hc_path_is_file (l1_filename) == true)
{
const int rc = straight_ctx_add_wl (hashcat_ctx, l1_filename);
@ -341,16 +332,9 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
char *l0_filename = user_options_extra->hc_workv[i];
hc_stat_t l0_stat;
// at this point we already verified the path actually exist and is readable
if (hc_stat (l0_filename, &l0_stat) == -1)
{
event_log_error (hashcat_ctx, "%s: %m", l0_filename);
return -1;
}
if (S_ISDIR (l0_stat.st_mode))
if (hc_path_is_directory (l0_filename) == true)
{
char **dictionary_files = NULL;
@ -364,16 +348,14 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
char *l1_filename = dictionary_files[d];
hc_stat_t l1_stat;
if (hc_stat (l1_filename, &l1_stat) == -1)
if (hc_path_read (l1_filename) == false)
{
event_log_error (hashcat_ctx, "%s: %m", l1_filename);
return -1;
}
if (S_ISREG (l1_stat.st_mode))
if (hc_path_is_file (l1_filename) == true)
{
const int rc = straight_ctx_add_wl (hashcat_ctx, l1_filename);
@ -405,16 +387,9 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
char *l0_filename = user_options_extra->hc_workv[i];
hc_stat_t l0_stat;
// at this point we already verified the path actually exist and is readable
if (hc_stat (l0_filename, &l0_stat) == -1)
{
event_log_error (hashcat_ctx, "%s: %m", l0_filename);
return -1;
}
if (S_ISDIR (l0_stat.st_mode))
if (hc_path_is_directory (l0_filename) == true)
{
char **dictionary_files = NULL;
@ -428,16 +403,14 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
char *l1_filename = dictionary_files[d];
hc_stat_t l1_stat;
if (hc_stat (l1_filename, &l1_stat) == -1)
if (hc_path_read (l1_filename) == false)
{
event_log_error (hashcat_ctx, "%s: %m", l1_filename);
return -1;
}
if (S_ISREG (l1_stat.st_mode))
if (hc_path_is_file (l1_filename) == true)
{
const int rc = straight_ctx_add_wl (hashcat_ctx, l1_filename);