From 57cb082fdac137f81e86e87a2a33884908c3478b Mon Sep 17 00:00:00 2001 From: jsteube Date: Mon, 3 Oct 2016 01:27:55 +0200 Subject: [PATCH] Add some useful stuff to main.c --- include/dictstat.h | 2 +- include/hashcat.h | 5 +- include/terminal.h | 4 +- src/Makefile | 25 ++----- src/dictstat.c | 24 +------ src/folder.c | 2 + src/hashcat.c | 70 ++------------------ src/main.c | 158 ++++++++++++++++++++++++++++++++++++++------- src/restore.c | 3 + src/terminal.c | 10 +-- 10 files changed, 164 insertions(+), 139 deletions(-) diff --git a/include/dictstat.h b/include/dictstat.h index 3d588df8a..054d1e553 100644 --- a/include/dictstat.h +++ b/include/dictstat.h @@ -21,7 +21,7 @@ int sort_by_dictstat (const void *s1, const void *s2); void dictstat_init (dictstat_ctx_t *dictstat_ctx, const user_options_t *user_options, const folder_config_t *folder_config); void dictstat_destroy (dictstat_ctx_t *dictstat_ctx); -void dictstat_read (dictstat_ctx_t *dictstat_ctx, const int comptime); +void dictstat_read (dictstat_ctx_t *dictstat_ctx); int dictstat_write (dictstat_ctx_t *dictstat_ctx); u64 dictstat_find (dictstat_ctx_t *dictstat_ctx, dictstat_t *d); void dictstat_append (dictstat_ctx_t *dictstat_ctx, dictstat_t *d); diff --git a/include/hashcat.h b/include/hashcat.h index 1d840c173..8b3b3f9fb 100644 --- a/include/hashcat.h +++ b/include/hashcat.h @@ -6,6 +6,9 @@ #ifndef _HASHCAT_H #define _HASHCAT_H -int hashcat (hashcat_ctx_t *hashcat_ctx, int argc, char **argv); +int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_folder, int argc, char **argv, const int comptime); + +void hashcat_ctx_init (hashcat_ctx_t *hashcat_ctx); +void hashcat_ctx_destroy (hashcat_ctx_t *hashcat_ctx); #endif // _HASHCAT_H diff --git a/include/terminal.h b/include/terminal.h index c532167a3..a17a8c4ed 100644 --- a/include/terminal.h +++ b/include/terminal.h @@ -23,8 +23,8 @@ #include #endif // _WIN -void welcome_screen (const user_options_t *user_options, const status_ctx_t *status_ctx, char *version_tag); -void goodbye_screen (const user_options_t *user_options, const status_ctx_t *status_ctx); +void welcome_screen (const user_options_t *user_options, const time_t proc_start, const char *version_tag); +void goodbye_screen (const user_options_t *user_options, const time_t proc_start, const time_t proc_stop); int setup_console (); diff --git a/src/Makefile b/src/Makefile index 864eba044..f1dcfc06c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -257,11 +257,8 @@ uninstall: obj/%.NATIVE.o: src/%.c $(CC) $(CFLAGS_NATIVE) -c -o $@ $< -obj/hashcat.NATIVE.o: src/hashcat.c - $(CC) $(CFLAGS_NATIVE) -c -o $@ $< -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\" - $(BINARY_NATIVE): src/main.c $(NATIVE_OBJS) - $(CC) $(CFLAGS_NATIVE) -o $(BINARY_NATIVE) $^ $(LFLAGS_NATIVE) + $(CC) $(CFLAGS_NATIVE) -o $(BINARY_NATIVE) $^ $(LFLAGS_NATIVE) -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\" ## ## cross compiled hashcat for binary release version @@ -279,26 +276,14 @@ obj/%.WIN.32.o: src/%.c obj/%.WIN.64.o: src/%.c $(CC_WIN_64) $(CFLAGS_CROSS_WIN) $(CFLAGS_CROSS_64) -c -o $@ $< -obj/hashcat.LINUX.32.o: src/hashcat.c - $(CC_LINUX_32) $(CFLAGS_CROSS_LINUX) $(CFLAGS_CROSS_32) -c -o $@ $< -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\" - -obj/hashcat.LINUX.64.o: src/hashcat.c - $(CC_LINUX_64) $(CFLAGS_CROSS_LINUX) $(CFLAGS_CROSS_64) -c -o $@ $< -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\" - -obj/hashcat.WIN.32.o: src/hashcat.c - $(CC_WIN_32) $(CFLAGS_CROSS_WIN) $(CFLAGS_CROSS_32) -c -o $@ $< -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" - -obj/hashcat.WIN.64.o: src/hashcat.c - $(CC_WIN_64) $(CFLAGS_CROSS_WIN) $(CFLAGS_CROSS_64) -c -o $@ $< -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" - hashcat32.bin: src/main.c $(LINUX_32_OBJS) - $(CC_LINUX_32) $(CFLAGS_CROSS_LINUX) $(CFLAGS_CROSS_32) -o $@ $^ $(LFLAGS_CROSS_LINUX) + $(CC_LINUX_32) $(CFLAGS_CROSS_LINUX) $(CFLAGS_CROSS_32) -o $@ $^ $(LFLAGS_CROSS_LINUX) -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\" hashcat64.bin: src/main.c $(LINUX_64_OBJS) - $(CC_LINUX_64) $(CFLAGS_CROSS_LINUX) $(CFLAGS_CROSS_64) -o $@ $^ $(LFLAGS_CROSS_LINUX) + $(CC_LINUX_64) $(CFLAGS_CROSS_LINUX) $(CFLAGS_CROSS_64) -o $@ $^ $(LFLAGS_CROSS_LINUX) -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\" hashcat32.exe: src/main.c $(WIN_32_OBJS) - $(CC_WIN_32) $(CFLAGS_CROSS_WIN) $(CFLAGS_CROSS_32) -o $@ $^ $(LFLAGS_CROSS_WIN) + $(CC_WIN_32) $(CFLAGS_CROSS_WIN) $(CFLAGS_CROSS_32) -o $@ $^ $(LFLAGS_CROSS_WIN) -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" hashcat64.exe: src/main.c $(WIN_64_OBJS) - $(CC_WIN_64) $(CFLAGS_CROSS_WIN) $(CFLAGS_CROSS_64) -o $@ $^ $(LFLAGS_CROSS_WIN) + $(CC_WIN_64) $(CFLAGS_CROSS_WIN) $(CFLAGS_CROSS_64) -o $@ $^ $(LFLAGS_CROSS_WIN) -DCOMPTIME=$(COMPTIME) -DVERSION_TAG=\"$(VERSION_TAG)\" diff --git a/src/dictstat.c b/src/dictstat.c index 75a9f3379..806141258 100644 --- a/src/dictstat.c +++ b/src/dictstat.c @@ -56,7 +56,7 @@ void dictstat_destroy (dictstat_ctx_t *dictstat_ctx) memset (dictstat_ctx, 0, sizeof (dictstat_ctx_t)); } -void dictstat_read (dictstat_ctx_t *dictstat_ctx, const int comptime) +void dictstat_read (dictstat_ctx_t *dictstat_ctx) { if (dictstat_ctx->enabled == false) return; @@ -69,28 +69,6 @@ void dictstat_read (dictstat_ctx_t *dictstat_ctx, const int comptime) return; } - hc_stat tmpstat; - - #if defined (_POSIX) - fstat (fileno (fp), &tmpstat); - #endif - - #if defined (_WIN) - _fstat64 (fileno (fp), &tmpstat); - #endif - - if (tmpstat.st_mtime < comptime) - { - /* with v0.15 the format changed so we have to ensure user is using a good version - since there is no version-header in the dictstat file */ - - fclose (fp); - - unlink (dictstat_ctx->filename); - - return; - } - while (!feof (fp)) { dictstat_t d; diff --git a/src/folder.c b/src/folder.c index fbd6f013c..2467a8843 100644 --- a/src/folder.c +++ b/src/folder.c @@ -262,6 +262,8 @@ int folder_config_init (folder_config_t *folder_config, const char *install_fold #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) + if (install_folder == NULL) install_folder = "/"; // makes library use easier + char *resolved_install_folder = realpath (install_folder, NULL); char *resolved_exec_path = realpath (exec_path, NULL); diff --git a/src/hashcat.c b/src/hashcat.c index 6ebe25c6c..ba4fbe377 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -56,7 +56,7 @@ extern const u32 DEFAULT_BENCHMARK_ALGORITHMS_CNT; extern const u32 DEFAULT_BENCHMARK_ALGORITHMS_BUF[]; -static void hashcat_ctx_init (hashcat_ctx_t *hashcat_ctx) +void hashcat_ctx_init (hashcat_ctx_t *hashcat_ctx) { hashcat_ctx->bitmap_ctx = (bitmap_ctx_t *) mymalloc (sizeof (bitmap_ctx_t)); hashcat_ctx->combinator_ctx = (combinator_ctx_t *) mymalloc (sizeof (combinator_ctx_t)); @@ -84,7 +84,7 @@ static void hashcat_ctx_init (hashcat_ctx_t *hashcat_ctx) hashcat_ctx->wl_data = (wl_data_t *) mymalloc (sizeof (wl_data_t)); } -static void hashcat_ctx_destroy (hashcat_ctx_t *hashcat_ctx) +void hashcat_ctx_destroy (hashcat_ctx_t *hashcat_ctx) { myfree (hashcat_ctx->bitmap_ctx); myfree (hashcat_ctx->combinator_ctx); @@ -847,7 +847,7 @@ static int inner1_loop (hashcat_ctx_t *hashcat_ctx) * dictstat read */ - dictstat_read (dictstat_ctx, COMPTIME); + dictstat_read (dictstat_ctx); /** * dictionary pad @@ -1713,16 +1713,12 @@ static int outer_loop (hashcat_ctx_t *hashcat_ctx) return 0; } -int hashcat (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) +int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_folder, int argc, char **argv, const int comptime) { /** * To help users a bit */ - const int rc_console = setup_console (); - - if (rc_console == -1) return -1; - setup_environment_variables (); setup_umask (); @@ -1731,8 +1727,6 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) * main init */ - hashcat_ctx_init (hashcat_ctx); - debugfile_ctx_t *debugfile_ctx = hashcat_ctx->debugfile_ctx; dictstat_ctx_t *dictstat_ctx = hashcat_ctx->dictstat_ctx; folder_config_t *folder_config = hashcat_ctx->folder_config; @@ -1762,51 +1756,8 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) * folder */ - char *install_folder = NULL; - char *shared_folder = NULL; - - #if defined (INSTALL_FOLDER) - install_folder = INSTALL_FOLDER; - #endif - - #if defined (SHARED_FOLDER) - shared_folder = SHARED_FOLDER; - #endif - folder_config_init (folder_config, install_folder, shared_folder); - /** - * commandline parameters - */ - - user_options_init (user_options); - - const int rc_options_getopt = user_options_getopt (user_options, argc, argv); - - if (rc_options_getopt == -1) return -1; - - const int rc_options_sanity = user_options_sanity (user_options); - - if (rc_options_sanity == -1) return -1; - - /** - * some early exits - */ - - if (user_options->version == true) - { - log_info ("%s", VERSION_TAG); - - return 0; - } - - if (user_options->usage == true) - { - usage_big_print (PROGNAME); - - return 0; - } - /** * restore */ @@ -1829,13 +1780,6 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) setup_seeding (user_options->rp_gen_seed_chgd, user_options->rp_gen_seed); - /** - * Inform user things getting started, - * - this is giving us a visual header before preparations start, so we do not need to clear them afterwards - */ - - welcome_screen (user_options, status_ctx, VERSION_TAG); - /** * logfile init */ @@ -1938,7 +1882,7 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) * Init OpenCL devices */ - const int rc_devices_init = opencl_ctx_devices_init (opencl_ctx, user_options, COMPTIME); + const int rc_devices_init = opencl_ctx_devices_init (opencl_ctx, user_options, comptime); if (rc_devices_init == -1) { @@ -2070,8 +2014,6 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) logfile_destroy (logfile_ctx); - goodbye_screen (user_options, status_ctx); - user_options_destroy (user_options); int rc_final = -1; @@ -2083,7 +2025,5 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) status_ctx_destroy (status_ctx); - hashcat_ctx_destroy (hashcat_ctx); - return rc_final; } diff --git a/src/main.c b/src/main.c index 39576bf00..701d4d797 100644 --- a/src/main.c +++ b/src/main.c @@ -1,22 +1,136 @@ -/** - * Author......: See docs/credits.txt - * License.....: MIT - */ - -#include -#include - -#include "common.h" -#include "types.h" -#include "hashcat.h" - -int main (int argc, char **argv) -{ - hashcat_ctx_t *hashcat_ctx = malloc (sizeof (hashcat_ctx_t)); - - const int rc = hashcat (hashcat_ctx, argc, argv); - - free (hashcat_ctx); - - return rc; -} +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#include +#include +#include + +#include "common.h" +#include "types.h" +#include "user_options.h" +#include "hashcat.h" +#include "terminal.h" // commandline only +#include "usage.h" // commandline only + +#define RUN_AS_COMMANDLINE true + +int main (int argc, char **argv) +{ + // hashcat main context + + hashcat_ctx_t *hashcat_ctx = (hashcat_ctx_t *) malloc (sizeof (hashcat_ctx_t)); + + hashcat_ctx_init (hashcat_ctx); + + // initialize the user options with some defaults (you can override them) + + user_options_t *user_options = hashcat_ctx->user_options; + + user_options_init (user_options); + + // initialize the session via getops for commandline use or + // alternatively you can set the user_options directly + + int rc_hashcat = 0; + + bool run_as_commandline = RUN_AS_COMMANDLINE; + + if (run_as_commandline == true) + { + // install and shared folder need to be set to recognize "make install" use + + char *install_folder = NULL; + char *shared_folder = NULL; + + #if defined (INSTALL_FOLDER) + install_folder = INSTALL_FOLDER; + #endif + + #if defined (SHARED_FOLDER) + shared_folder = SHARED_FOLDER; + #endif + + // sets dos window size (windows only) + + const int rc_console = setup_console (); + + if (rc_console == -1) return -1; + + // parse commandline parameters and check them + + const int rc_options_getopt = user_options_getopt (user_options, argc, argv); + + if (rc_options_getopt == -1) return -1; + + const int rc_options_sanity = user_options_sanity (user_options); + + if (rc_options_sanity == -1) return -1; + + // some early exits + + if (user_options->version == true) + { + printf ("%s", VERSION_TAG); + + return 0; + } + + if (user_options->usage == true) + { + usage_big_print (PROGNAME); + + return 0; + } + + // Inform user things getting started + + time_t proc_start; + + time (&proc_start); + + welcome_screen (user_options, proc_start, VERSION_TAG); + + // now run hashcat + + rc_hashcat = hashcat (hashcat_ctx, install_folder, shared_folder, argc, argv, COMPTIME); + + // Inform user we're done + + time_t proc_stop; + + time (&proc_stop); + + goodbye_screen (user_options, proc_start, proc_stop); + } + else + { + // this is a bit ugly, but it's the example you're looking for + + char *hash = "8743b52063cd84097a65d1633f5c74f5"; + char *mask = "?l?l?l?l?l?l?l"; + + char *hc_argv[] = { hash, mask, NULL }; + + user_options->hc_argv = hc_argv; + user_options->hc_argc = 2; + user_options->quiet = true; + user_options->potfile_disable = true; + user_options->attack_mode = ATTACK_MODE_BF; // this is -a 3 + user_options->hash_mode = 0; // MD5 + user_options->workload_profile = 3; + + // now run hashcat + + rc_hashcat = hashcat (hashcat_ctx, NULL, NULL, 0, NULL, 0); + } + + // finished with hashcat, clean up + + hashcat_ctx_destroy (hashcat_ctx); + + free (hashcat_ctx); + + return rc_hashcat; +} \ No newline at end of file diff --git a/src/restore.c b/src/restore.c index 40418dcb5..6c349bcfc 100644 --- a/src/restore.c +++ b/src/restore.c @@ -357,6 +357,9 @@ int restore_ctx_init (restore_ctx_t *restore_ctx, user_options_t *user_options, init_restore (restore_ctx); + if (argc == 0) return 0; + if (argv == NULL) return 0; + if (user_options->benchmark == true) return 0; if (user_options->keyspace == true) return 0; if (user_options->left == true) return 0; diff --git a/src/terminal.c b/src/terminal.c index 251f5c953..d3788e83a 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -31,7 +31,7 @@ const char *PROMPT = "[s]tatus [p]ause [r]esume [b]ypass [c]heckpoint [q]uit => "; -void welcome_screen (const user_options_t *user_options, const status_ctx_t *status_ctx, char *version_tag) +void welcome_screen (const user_options_t *user_options, const time_t proc_start, const char *version_tag) { if (user_options->quiet == true) return; if (user_options->keyspace == true) return; @@ -48,7 +48,7 @@ void welcome_screen (const user_options_t *user_options, const status_ctx_t *sta } else { - log_info ("# %s (%s) %s", PROGNAME, version_tag, ctime (&status_ctx->proc_start)); + log_info ("# %s (%s) %s", PROGNAME, version_tag, ctime (&proc_start)); } } else if (user_options->restore == true) @@ -63,7 +63,7 @@ void welcome_screen (const user_options_t *user_options, const status_ctx_t *sta } } -void goodbye_screen (const user_options_t *user_options, const status_ctx_t *status_ctx) +void goodbye_screen (const user_options_t *user_options, const time_t proc_start, const time_t proc_stop) { if (user_options->quiet == true) return; if (user_options->keyspace == true) return; @@ -71,8 +71,8 @@ void goodbye_screen (const user_options_t *user_options, const status_ctx_t *sta if (user_options->show == true) return; if (user_options->left == true) return; - log_info_nn ("Started: %s", ctime (&status_ctx->proc_start)); - log_info_nn ("Stopped: %s", ctime (&status_ctx->proc_stop)); + log_info_nn ("Started: %s", ctime (&proc_start)); + log_info_nn ("Stopped: %s", ctime (&proc_stop)); } int setup_console ()