1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-11-27 02:18:21 +00:00
hashcat/src/folder.c

426 lines
9.3 KiB
C
Raw Normal View History

2016-09-07 10:45:08 +00:00
/**
* Author......: See docs/credits.txt
2016-09-07 10:45:08 +00:00
* License.....: MIT
*/
#if defined (__APPLE__)
2016-09-07 10:45:08 +00:00
#include <stdio.h>
#endif
#include "common.h"
2016-09-16 15:01:18 +00:00
#include "types.h"
2016-09-07 10:45:08 +00:00
#include "memory.h"
#include "event.h"
2016-09-21 21:06:11 +00:00
#include "shared.h"
2016-09-07 10:45:08 +00:00
#include "folder.h"
#if defined (__APPLE__)
#include "event.h"
2016-09-07 10:45:08 +00:00
#endif
2016-09-11 08:53:29 +00:00
int sort_by_stringptr (const void *p1, const void *p2)
{
const char **s1 = (const char **) p1;
const char **s2 = (const char **) p2;
return strcmp (*s1, *s2);
}
static int get_exec_path (char *exec_path, const size_t exec_path_sz)
2016-09-07 10:45:08 +00:00
{
#if defined (__linux__)
2016-09-07 10:45:08 +00:00
char tmp[32] = { 0 };
snprintf (tmp, sizeof (tmp) - 1, "/proc/%d/exe", getpid ());
const int len = readlink (tmp, exec_path, exec_path_sz - 1);
2016-09-07 10:45:08 +00:00
#elif defined (_WIN)
2016-09-07 10:45:08 +00:00
const int len = GetModuleFileName (NULL, exec_path, exec_path_sz - 1);
2016-09-07 10:45:08 +00:00
#elif defined (__APPLE__)
2016-09-07 10:45:08 +00:00
u32 size = (u32) exec_path_sz;
2016-09-07 10:45:08 +00:00
if (_NSGetExecutablePath (exec_path, &size) != 0) return -1;
2016-09-07 10:45:08 +00:00
2016-09-30 16:09:29 +00:00
const size_t len = strlen (exec_path);
2016-09-07 10:45:08 +00:00
#elif defined (__FreeBSD__)
2016-09-07 10:45:08 +00:00
int mib[4];
2016-09-30 16:09:29 +00:00
2016-09-07 10:45:08 +00:00
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
char tmp[32] = { 0 };
size_t size = exec_path_sz;
2016-09-30 16:09:29 +00:00
sysctl (mib, 4, exec_path, &size, NULL, 0);
2016-09-07 10:45:08 +00:00
const int len = readlink (tmp, exec_path, exec_path_sz - 1);
2016-09-07 10:45:08 +00:00
#else
#error Your Operating System is not supported or detected
#endif
exec_path[len] = 0;
return 0;
2016-09-07 10:45:08 +00:00
}
void get_install_dir (char *install_dir, const char *exec_path)
2016-09-07 10:45:08 +00:00
{
strncpy (install_dir, exec_path, HCBUFSIZ_TINY - 1);
char *last_slash = NULL;
2016-09-07 10:45:08 +00:00
if ((last_slash = strrchr (install_dir, '/')) != NULL)
{
*last_slash = 0;
}
else if ((last_slash = strrchr (install_dir, '\\')) != NULL)
{
*last_slash = 0;
}
else
{
install_dir[0] = '.';
install_dir[1] = 0;
}
}
void get_profile_dir (char *profile_dir, const char *home_dir)
2016-09-07 10:45:08 +00:00
{
snprintf (profile_dir, HCBUFSIZ_TINY - 1, "%s/%s", home_dir, DOT_HASHCAT);
2016-09-07 10:45:08 +00:00
}
void get_session_dir (char *session_dir, const char *profile_dir)
2016-09-07 10:45:08 +00:00
{
2016-09-21 21:06:11 +00:00
snprintf (session_dir, HCBUFSIZ_TINY - 1, "%s/%s", profile_dir, SESSIONS_FOLDER);
2016-09-07 10:45:08 +00:00
}
int count_dictionaries (char **dictionary_files)
{
if (dictionary_files == NULL) return 0;
int cnt = 0;
for (int d = 0; dictionary_files[d] != NULL; d++)
{
cnt++;
}
return (cnt);
}
char **scan_directory (hashcat_ctx_t *hashcat_ctx, const char *path)
2016-09-07 10:45:08 +00:00
{
char *tmp_path = hcstrdup (hashcat_ctx, path);
2016-09-07 10:45:08 +00:00
size_t tmp_path_len = strlen (tmp_path);
while (tmp_path[tmp_path_len - 1] == '/' || tmp_path[tmp_path_len - 1] == '\\')
{
tmp_path[tmp_path_len - 1] = 0;
tmp_path_len = strlen (tmp_path);
}
char **files = NULL;
size_t num_files = 0;
2016-09-07 10:45:08 +00:00
DIR *d = NULL;
if ((d = opendir (tmp_path)) != NULL)
{
#if defined (__APPLE__)
2016-09-07 10:45:08 +00:00
struct dirent e;
for (;;)
{
memset (&e, 0, sizeof (e));
struct dirent *de = NULL;
if (readdir_r (d, &e, &de) != 0) break;
2016-09-07 10:45:08 +00:00
if (de == NULL) break;
#else
struct dirent *de;
while ((de = readdir (d)) != NULL)
{
#endif
if ((strcmp (de->d_name, ".") == 0) || (strcmp (de->d_name, "..") == 0)) continue;
char *path_file = (char *) hcmalloc (hashcat_ctx, HCBUFSIZ_TINY);
2016-09-07 10:45:08 +00:00
snprintf (path_file, HCBUFSIZ_TINY - 1, "%s/%s", tmp_path, de->d_name);
2016-09-07 10:45:08 +00:00
DIR *d_test;
if ((d_test = opendir (path_file)) != NULL)
{
closedir (d_test);
hcfree (path_file);
2016-09-07 10:45:08 +00:00
}
else
{
files = (char **) hcrealloc (hashcat_ctx, files, (num_files + 1) * sizeof (char *), sizeof (char *));
2016-09-07 10:45:08 +00:00
files[num_files] = path_file;
2016-09-07 10:45:08 +00:00
num_files++;
2016-09-07 10:45:08 +00:00
}
}
closedir (d);
}
else if (errno == ENOTDIR)
{
files = (char **) hcrealloc (hashcat_ctx, files, (num_files + 1) * sizeof (char *), sizeof (char *));
2016-09-07 10:45:08 +00:00
files[num_files] = hcstrdup (hashcat_ctx, path);
2016-09-07 10:45:08 +00:00
num_files++;
2016-09-07 10:45:08 +00:00
}
files = (char **) hcrealloc (hashcat_ctx, files, (num_files + 1) * sizeof (char *), sizeof (char *));
2016-09-07 10:45:08 +00:00
files[num_files] = NULL;
2016-09-07 10:45:08 +00:00
num_files++;
2016-09-07 10:45:08 +00:00
hcfree (tmp_path);
2016-09-07 10:45:08 +00:00
return (files);
}
2016-09-21 21:06:11 +00:00
int folder_config_init (hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const char *install_folder, MAYBE_UNUSED const char *shared_folder)
2016-09-21 21:06:11 +00:00
{
2016-10-06 14:17:29 +00:00
folder_config_t *folder_config = hashcat_ctx->folder_config;
2016-09-21 21:06:11 +00:00
/**
* There's some buggy OpenCL runtime that do not support -I.
* A workaround is to chdir() to the OpenCL folder,
* then compile the kernels,
* then chdir() back to where we came from so we need to save it first
*/
char *cwd = (char *) hcmalloc (hashcat_ctx, HCBUFSIZ_TINY); VERIFY_PTR (cwd);
2016-09-21 21:06:11 +00:00
if (getcwd (cwd, HCBUFSIZ_TINY - 1) == NULL)
{
2016-10-11 08:55:02 +00:00
event_log_error (hashcat_ctx, "getcwd(): %s", strerror (errno));
2016-09-21 21:06:11 +00:00
return -1;
}
/**
* folders, as discussed on https://github.com/hashcat/hashcat/issues/20
*/
const size_t exec_path_sz = 1024;
char *exec_path = (char *) hcmalloc (hashcat_ctx, exec_path_sz); VERIFY_PTR (exec_path);
const int rc = get_exec_path (exec_path, exec_path_sz);
2016-09-21 21:06:11 +00:00
if (rc == -1)
{
2016-10-11 08:55:02 +00:00
event_log_error (hashcat_ctx, "get_exec_path() failed");
return -1;
}
2016-09-21 21:06:11 +00:00
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
2016-10-02 23:27:55 +00:00
if (install_folder == NULL) install_folder = "/"; // makes library use easier
2016-09-21 21:06:11 +00:00
char *resolved_install_folder = realpath (install_folder, NULL);
char *resolved_exec_path = realpath (exec_path, NULL);
if (resolved_install_folder == NULL)
{
2016-10-11 08:55:02 +00:00
event_log_error (hashcat_ctx, "%s: %s", resolved_install_folder, strerror (errno));
2016-09-21 21:06:11 +00:00
return -1;
}
if (resolved_exec_path == NULL)
{
2016-10-11 08:55:02 +00:00
event_log_error (hashcat_ctx, "%s: %s", resolved_exec_path, strerror (errno));
2016-09-21 21:06:11 +00:00
return -1;
}
char *install_dir = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY); VERIFY_PTR (install_dir);
get_install_dir (install_dir, resolved_exec_path);
2016-09-21 21:06:11 +00:00
char *profile_dir = NULL;
char *session_dir = NULL;
char *shared_dir = NULL;
if (strcmp (install_dir, resolved_install_folder) == 0)
{
struct passwd *pw = getpwuid (getuid ());
const char *home_dir = pw->pw_dir;
profile_dir = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY); VERIFY_PTR (profile_dir);
session_dir = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY); VERIFY_PTR (session_dir);
2016-09-21 21:06:11 +00:00
get_profile_dir (profile_dir, home_dir);
get_session_dir (session_dir, profile_dir);
shared_dir = hcstrdup (hashcat_ctx, shared_folder);
2016-09-21 21:06:11 +00:00
2016-10-01 11:51:06 +00:00
hc_mkdir (profile_dir, 0700);
hc_mkdir (session_dir, 0700);
2016-09-21 21:06:11 +00:00
}
else
{
profile_dir = install_dir;
session_dir = install_dir;
shared_dir = install_dir;
}
hcfree (resolved_install_folder);
hcfree (resolved_exec_path);
2016-09-21 21:06:11 +00:00
#else
char *install_dir = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY); VERIFY_PTR (install_dir);
get_install_dir (install_dir, exec_path);
2016-09-21 21:06:11 +00:00
char *profile_dir = install_dir;
char *session_dir = install_dir;
char *shared_dir = install_dir;
#endif
hcfree (exec_path);
2016-09-21 21:06:11 +00:00
/**
* There's alot of problem related to bad support -I parameters when building the kernel.
* Each OpenCL runtime handles it slightly different.
* The most problematic is with new AMD drivers on Windows, which can not handle quote characters!
* The best workaround found so far is to modify the TMP variable (only inside hashcat process) before the runtime is load
*/
char *cpath = (char *) hcmalloc (hashcat_ctx, HCBUFSIZ_TINY); VERIFY_PTR (cpath);
2016-09-21 21:06:11 +00:00
#if defined (_WIN)
snprintf (cpath, HCBUFSIZ_TINY - 1, "%s\\OpenCL\\", shared_dir);
char *cpath_real = (char *) hcmalloc (hashcat_ctx, HCBUFSIZ_TINY); VERIFY_PTR (cpath_real);
2016-09-21 21:06:11 +00:00
if (GetFullPathName (cpath, HCBUFSIZ_TINY - 1, cpath_real, NULL) == 0)
{
2016-10-11 08:55:02 +00:00
event_log_error (hashcat_ctx, "%s: %s", cpath, "GetFullPathName()");
2016-09-21 21:06:11 +00:00
return -1;
}
#else
snprintf (cpath, HCBUFSIZ_TINY - 1, "%s/OpenCL/", shared_dir);
char *cpath_real = (char *) hcmalloc (hashcat_ctx, PATH_MAX); VERIFY_PTR (cpath_real);
2016-09-21 21:06:11 +00:00
if (realpath (cpath, cpath_real) == NULL)
{
2016-10-11 08:55:02 +00:00
event_log_error (hashcat_ctx, "%s: %s", cpath, strerror (errno));
2016-09-21 21:06:11 +00:00
return -1;
}
#endif
hcfree (cpath);
2016-09-21 21:06:11 +00:00
//if (getenv ("TMP") == NULL)
if (1)
{
char tmp[1000];
snprintf (tmp, sizeof (tmp) - 1, "TMP=%s", cpath_real);
putenv (tmp);
}
#if defined (_WIN)
naive_replace (cpath_real, '\\', '/');
// not escaping here, windows using quotes later
// naive_escape (cpath_real, PATH_MAX, ' ', '\\');
#else
naive_escape (cpath_real, PATH_MAX, ' ', '\\');
#endif
/**
* kernel cache, we need to make sure folder exist
*/
char *kernels_folder = (char *) hcmalloc (hashcat_ctx, HCBUFSIZ_TINY); VERIFY_PTR (kernels_folder);
2016-09-21 21:06:11 +00:00
snprintf (kernels_folder, HCBUFSIZ_TINY - 1, "%s/kernels", profile_dir);
2016-10-01 11:51:06 +00:00
hc_mkdir (kernels_folder, 0700);
2016-09-21 21:06:11 +00:00
hcfree (kernels_folder);
2016-09-21 21:06:11 +00:00
/**
* store for later use
*/
folder_config->cwd = cwd;
folder_config->install_dir = install_dir;
folder_config->profile_dir = profile_dir;
folder_config->session_dir = session_dir;
folder_config->shared_dir = shared_dir;
folder_config->cpath_real = cpath_real;
return 0;
}
2016-10-06 14:17:29 +00:00
void folder_config_destroy (hashcat_ctx_t *hashcat_ctx)
2016-09-21 21:06:11 +00:00
{
2016-10-06 14:17:29 +00:00
folder_config_t *folder_config = hashcat_ctx->folder_config;
hcfree (folder_config->cpath_real);
hcfree (folder_config->cwd);
hcfree (folder_config->install_dir);
2016-09-21 21:06:11 +00:00
memset (folder_config, 0, sizeof (folder_config_t));
2016-09-21 21:06:11 +00:00
}
2016-10-01 11:51:06 +00:00
int hc_mkdir (const char *name, MAYBE_UNUSED const int mode)
2016-10-01 11:51:06 +00:00
{
#if defined (_WIN)
return _mkdir (name);
#else
return mkdir (name, mode);
#endif
}