From 533a87b685b1762635d56da64da1d0866e8891f1 Mon Sep 17 00:00:00 2001 From: jsteube Date: Mon, 12 Sep 2016 12:59:40 +0200 Subject: [PATCH] Move count_words() to wordlist.c --- include/thread.h | 19 ++++ include/wordlist.h | 2 + src/hashcat.c | 251 +-------------------------------------------- src/thread.c | 134 ++++++++++++++++++++++++ src/wordlist.c | 141 +++++++++++++++++++++++++ 5 files changed, 297 insertions(+), 250 deletions(-) diff --git a/include/thread.h b/include/thread.h index b3eb7ea86..59e944314 100644 --- a/include/thread.h +++ b/include/thread.h @@ -6,6 +6,8 @@ #ifndef _THREAD_H #define _THREAD_H +#include + #if defined (_POSIX) #include #include @@ -46,4 +48,21 @@ typedef pthread_mutex_t hc_thread_mutex_t; #endif +#if defined (_WIN) + +BOOL WINAPI sigHandler_default (DWORD sig); +BOOL WINAPI sigHandler_benchmark (DWORD sig); +void hc_signal (BOOL WINAPI (callback) (DWORD)); + +#else + +void sigHandler_default (int sig); +void sigHandler_benchmark (int sig); +void hc_signal (void (callback) (int)); + +#endif + +void myabort (); +void myquit (); + #endif // _THREAD_H diff --git a/include/wordlist.h b/include/wordlist.h index 0b60bd46f..692193f14 100644 --- a/include/wordlist.h +++ b/include/wordlist.h @@ -31,4 +31,6 @@ void get_next_word (wl_data_t *wl_data, FILE *fd, char **out_buf, uint *out_len) void pw_add (hc_device_param_t *device_param, const u8 *pw_buf, const int pw_len); +u64 count_words (wl_data_t *wl_data, FILE *fd, const char *dictfile, dictstat_ctx_t *dictstat_ctx); + #endif // _WORDLIST_H diff --git a/src/hashcat.c b/src/hashcat.c index 6958ddfbc..9b2f05c13 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -67,8 +67,8 @@ #include "hlfmt.h" #include "filenames.h" #include "stdout.h" -#include "wordlist.h" #include "dictstat.h" +#include "wordlist.h" #include "version.h" #include "benchmark.h" #include "outfile_check.h" @@ -418,117 +418,7 @@ int sort_by_mtime (const void *p1, const void *p2) // thread static hc_thread_mutex_t mux_dispatcher; static hc_thread_mutex_t mux_counter; -static void myabort () -{ - data.devices_status = STATUS_ABORTED; -} -static void myquit () -{ - data.devices_status = STATUS_QUIT; -} -#if defined (_WIN) - -static BOOL WINAPI sigHandler_default (DWORD sig) -{ - switch (sig) - { - case CTRL_CLOSE_EVENT: - - /* - * special case see: https://stackoverflow.com/questions/3640633/c-setconsolectrlhandler-routine-issue/5610042#5610042 - * if the user interacts w/ the user-interface (GUI/cmd), we need to do the finalization job within this signal handler - * function otherwise it is too late (e.g. after returning from this function) - */ - - myabort (); - - SetConsoleCtrlHandler (NULL, TRUE); - - hc_sleep (10); - - return TRUE; - - case CTRL_C_EVENT: - case CTRL_LOGOFF_EVENT: - case CTRL_SHUTDOWN_EVENT: - - myabort (); - - SetConsoleCtrlHandler (NULL, TRUE); - - return TRUE; - } - - return FALSE; -} - -static BOOL WINAPI sigHandler_benchmark (DWORD sig) -{ - switch (sig) - { - case CTRL_CLOSE_EVENT: - - myquit (); - - SetConsoleCtrlHandler (NULL, TRUE); - - hc_sleep (10); - - return TRUE; - - case CTRL_C_EVENT: - case CTRL_LOGOFF_EVENT: - case CTRL_SHUTDOWN_EVENT: - - myquit (); - - SetConsoleCtrlHandler (NULL, TRUE); - - return TRUE; - } - - return FALSE; -} - -static void hc_signal (BOOL WINAPI (callback) (DWORD)) -{ - if (callback == NULL) - { - SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, FALSE); - } - else - { - SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, TRUE); - } -} - -#else - -static void sigHandler_default (int sig) -{ - myabort (); - - signal (sig, NULL); -} - -static void sigHandler_benchmark (int sig) -{ - myquit (); - - signal (sig, NULL); -} - -static void hc_signal (void (callback) (int)) -{ - if (callback == NULL) callback = SIG_DFL; - - signal (SIGINT, callback); - signal (SIGTERM, callback); - signal (SIGABRT, callback); -} - -#endif // restore static void check_checkpoint () @@ -2474,145 +2364,6 @@ static int run_cracker (hc_device_param_t *device_param, hashconfig_t *hashconfi return 0; } -static u64 count_words (wl_data_t *wl_data, FILE *fd, const char *dictfile, dictstat_ctx_t *dictstat_ctx) -{ - hc_signal (NULL); - - dictstat_t d; - - d.cnt = 0; - - #if defined (_POSIX) - fstat (fileno (fd), &d.stat); - #endif - - #if defined (_WIN) - _fstat64 (fileno (fd), &d.stat); - #endif - - d.stat.st_mode = 0; - d.stat.st_nlink = 0; - d.stat.st_uid = 0; - d.stat.st_gid = 0; - d.stat.st_rdev = 0; - d.stat.st_atime = 0; - - #if defined (_POSIX) - d.stat.st_blksize = 0; - d.stat.st_blocks = 0; - #endif - - if (d.stat.st_size == 0) return 0; - - const u64 cached_cnt = dictstat_find (dictstat_ctx, &d); - - if (run_rule_engine (data.rule_len_l, data.rule_buf_l) == 0) - { - if (cached_cnt) - { - u64 keyspace = cached_cnt; - - if (data.attack_kern == ATTACK_KERN_STRAIGHT) - { - keyspace *= data.kernel_rules_cnt; - } - else if (data.attack_kern == ATTACK_KERN_COMBI) - { - keyspace *= data.combs_cnt; - } - - if (data.quiet == 0) log_info ("Cache-hit dictionary stats %s: %" PRIu64 " bytes, %" PRIu64 " words, %" PRIu64 " keyspace", dictfile, d.stat.st_size, cached_cnt, keyspace); - if (data.quiet == 0) log_info (""); - - hc_signal (sigHandler_default); - - return (keyspace); - } - } - - time_t now = 0; - time_t prev = 0; - - u64 comp = 0; - u64 cnt = 0; - u64 cnt2 = 0; - - while (!feof (fd)) - { - load_segment (wl_data, fd); - - comp += wl_data->cnt; - - u32 i = 0; - - while (i < wl_data->cnt) - { - u32 len; - u32 off; - - get_next_word_func (wl_data->buf + i, wl_data->cnt - i, &len, &off); - - if (run_rule_engine (data.rule_len_l, data.rule_buf_l)) - { - char rule_buf_out[BLOCK_SIZE] = { 0 }; - - int rule_len_out = -1; - - if (len < BLOCK_SIZE) - { - rule_len_out = _old_apply_rule (data.rule_buf_l, data.rule_len_l, wl_data->buf + i, len, rule_buf_out); - } - - if (rule_len_out < 0) - { - len = PW_MAX1; - } - else - { - len = rule_len_out; - } - } - - if (len < PW_MAX1) - { - if (data.attack_kern == ATTACK_KERN_STRAIGHT) - { - cnt += data.kernel_rules_cnt; - } - else if (data.attack_kern == ATTACK_KERN_COMBI) - { - cnt += data.combs_cnt; - } - - d.cnt++; - } - - i += off; - - cnt2++; - } - - time (&now); - - if ((now - prev) == 0) continue; - - double percent = (double) comp / (double) d.stat.st_size; - - if (data.quiet == 0) log_info_nn ("Generating dictionary stats for %s: %" PRIu64 " bytes (%.2f%%), %" PRIu64 " words, %" PRIu64 " keyspace", dictfile, comp, percent * 100, cnt2, cnt); - - time (&prev); - } - - if (data.quiet == 0) log_info ("Generated dictionary stats for %s: %" PRIu64 " bytes, %" PRIu64 " words, %" PRIu64 " keyspace", dictfile, comp, cnt2, cnt); - if (data.quiet == 0) log_info (""); - - dictstat_append (dictstat_ctx, &d); - - hc_signal (sigHandler_default); - - return (cnt); -} - static void *thread_monitor (void *p) { uint runtime_check = 0; diff --git a/src/thread.c b/src/thread.c index 1fba742f9..e0db0ab17 100644 --- a/src/thread.c +++ b/src/thread.c @@ -4,4 +4,138 @@ */ #include "common.h" +#include "types_int.h" +#include "types.h" +#include "timer.h" +#include "ext_OpenCL.h" +#include "ext_ADL.h" +#include "ext_nvapi.h" +#include "ext_nvml.h" +#include "ext_xnvctrl.h" +#include "opencl.h" #include "thread.h" +#include "rp_cpu.h" +#include "terminal.h" +#include "interface.h" +#include "hwmon.h" +#include "mpsp.h" +#include "restore.h" +#include "outfile.h" +#include "potfile.h" +#include "status.h" +#include "data.h" + +extern hc_global_data_t data; + +#if defined (_WIN) + +BOOL WINAPI sigHandler_default (DWORD sig) +{ + switch (sig) + { + case CTRL_CLOSE_EVENT: + + /* + * special case see: https://stackoverflow.com/questions/3640633/c-setconsolectrlhandler-routine-issue/5610042#5610042 + * if the user interacts w/ the user-interface (GUI/cmd), we need to do the finalization job within this signal handler + * function otherwise it is too late (e.g. after returning from this function) + */ + + myabort (); + + SetConsoleCtrlHandler (NULL, TRUE); + + hc_sleep (10); + + return TRUE; + + case CTRL_C_EVENT: + case CTRL_LOGOFF_EVENT: + case CTRL_SHUTDOWN_EVENT: + + myabort (); + + SetConsoleCtrlHandler (NULL, TRUE); + + return TRUE; + } + + return FALSE; +} + +BOOL WINAPI sigHandler_benchmark (DWORD sig) +{ + switch (sig) + { + case CTRL_CLOSE_EVENT: + + myquit (); + + SetConsoleCtrlHandler (NULL, TRUE); + + hc_sleep (10); + + return TRUE; + + case CTRL_C_EVENT: + case CTRL_LOGOFF_EVENT: + case CTRL_SHUTDOWN_EVENT: + + myquit (); + + SetConsoleCtrlHandler (NULL, TRUE); + + return TRUE; + } + + return FALSE; +} + +void hc_signal (BOOL WINAPI (callback) (DWORD)) +{ + if (callback == NULL) + { + SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, FALSE); + } + else + { + SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, TRUE); + } +} + +#else + +void sigHandler_default (int sig) +{ + myabort (); + + signal (sig, NULL); +} + +void sigHandler_benchmark (int sig) +{ + myquit (); + + signal (sig, NULL); +} + +void hc_signal (void (callback) (int)) +{ + if (callback == NULL) callback = SIG_DFL; + + signal (SIGINT, callback); + signal (SIGTERM, callback); + signal (SIGABRT, callback); +} + +#endif + +void myabort () +{ + data.devices_status = STATUS_ABORTED; +} + +void myquit () +{ + data.devices_status = STATUS_QUIT; +} diff --git a/src/wordlist.c b/src/wordlist.c index 9b2885e68..43ec968d7 100644 --- a/src/wordlist.c +++ b/src/wordlist.c @@ -24,6 +24,8 @@ #include "interface.h" #include "shared.h" #include "hwmon.h" +#include "thread.h" +#include "dictstat.h" #include "mpsp.h" #include "restore.h" #include "outfile.h" @@ -282,3 +284,142 @@ void pw_add (hc_device_param_t *device_param, const u8 *pw_buf, const int pw_len // return; //} } + +u64 count_words (wl_data_t *wl_data, FILE *fd, const char *dictfile, dictstat_ctx_t *dictstat_ctx) +{ + hc_signal (NULL); + + dictstat_t d; + + d.cnt = 0; + + #if defined (_POSIX) + fstat (fileno (fd), &d.stat); + #endif + + #if defined (_WIN) + _fstat64 (fileno (fd), &d.stat); + #endif + + d.stat.st_mode = 0; + d.stat.st_nlink = 0; + d.stat.st_uid = 0; + d.stat.st_gid = 0; + d.stat.st_rdev = 0; + d.stat.st_atime = 0; + + #if defined (_POSIX) + d.stat.st_blksize = 0; + d.stat.st_blocks = 0; + #endif + + if (d.stat.st_size == 0) return 0; + + const u64 cached_cnt = dictstat_find (dictstat_ctx, &d); + + if (run_rule_engine (data.rule_len_l, data.rule_buf_l) == 0) + { + if (cached_cnt) + { + u64 keyspace = cached_cnt; + + if (data.attack_kern == ATTACK_KERN_STRAIGHT) + { + keyspace *= data.kernel_rules_cnt; + } + else if (data.attack_kern == ATTACK_KERN_COMBI) + { + keyspace *= data.combs_cnt; + } + + if (data.quiet == 0) log_info ("Cache-hit dictionary stats %s: %" PRIu64 " bytes, %" PRIu64 " words, %" PRIu64 " keyspace", dictfile, d.stat.st_size, cached_cnt, keyspace); + if (data.quiet == 0) log_info (""); + + hc_signal (sigHandler_default); + + return (keyspace); + } + } + + time_t now = 0; + time_t prev = 0; + + u64 comp = 0; + u64 cnt = 0; + u64 cnt2 = 0; + + while (!feof (fd)) + { + load_segment (wl_data, fd); + + comp += wl_data->cnt; + + u32 i = 0; + + while (i < wl_data->cnt) + { + u32 len; + u32 off; + + get_next_word_func (wl_data->buf + i, wl_data->cnt - i, &len, &off); + + if (run_rule_engine (data.rule_len_l, data.rule_buf_l)) + { + char rule_buf_out[BLOCK_SIZE] = { 0 }; + + int rule_len_out = -1; + + if (len < BLOCK_SIZE) + { + rule_len_out = _old_apply_rule (data.rule_buf_l, data.rule_len_l, wl_data->buf + i, len, rule_buf_out); + } + + if (rule_len_out < 0) + { + len = PW_MAX1; + } + else + { + len = rule_len_out; + } + } + + if (len < PW_MAX1) + { + if (data.attack_kern == ATTACK_KERN_STRAIGHT) + { + cnt += data.kernel_rules_cnt; + } + else if (data.attack_kern == ATTACK_KERN_COMBI) + { + cnt += data.combs_cnt; + } + + d.cnt++; + } + + i += off; + + cnt2++; + } + + time (&now); + + if ((now - prev) == 0) continue; + + double percent = (double) comp / (double) d.stat.st_size; + + if (data.quiet == 0) log_info_nn ("Generating dictionary stats for %s: %" PRIu64 " bytes (%.2f%%), %" PRIu64 " words, %" PRIu64 " keyspace", dictfile, comp, percent * 100, cnt2, cnt); + + time (&prev); + } + + if (data.quiet == 0) log_info ("Generated dictionary stats for %s: %" PRIu64 " bytes, %" PRIu64 " words, %" PRIu64 " keyspace", dictfile, comp, cnt2, cnt); + if (data.quiet == 0) log_info (""); + + dictstat_append (dictstat_ctx, &d); + + hc_signal (sigHandler_default); + + return (cnt); +}