From 73ed017daf6fe8dde3bc40b45bea8636451f6e4d Mon Sep 17 00:00:00 2001 From: philsmd Date: Wed, 17 Oct 2018 10:55:47 +0200 Subject: [PATCH] stdin: add read timeout checks (abort if no input for a long time) --- docs/changes.txt | 1 + include/monitor.h | 3 +++ include/types.h | 8 ++++++++ src/dispatch.c | 8 ++++++++ src/main.c | 20 ++++++++++++++++++++ src/monitor.c | 21 +++++++++++++++++++++ 6 files changed, 61 insertions(+) diff --git a/docs/changes.txt b/docs/changes.txt index dc4be4838..7bd724e7c 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -36,6 +36,7 @@ - Increased the maximum size of edata2 in Kerberos 5 TGS-REP etype 23 - Allow hashfile for -m 16800 to be used with -m 16801 - Make the masks parser more restrictive by rejecting a single '?' at the end of the mask (use ?? instead) +- Add a periodic check for read timeouts in stdin/pipe mode and abort if no input was provided ## ## Bugs diff --git a/include/monitor.h b/include/monitor.h index 7aff8d077..f05b090c7 100644 --- a/include/monitor.h +++ b/include/monitor.h @@ -6,6 +6,9 @@ #ifndef _MONITOR_H #define _MONITOR_H +#define STDIN_TIMEOUT_MIN 20 // warn after no input from stdin for x seconds +#define STDIN_TIMEOUT_MAX 120 // abort after no input from stdin for x seconds + int get_runtime_left (const hashcat_ctx_t *hashcat_ctx); HC_API_CALL void *thread_monitor (void *p); diff --git a/include/types.h b/include/types.h index ade1d28a5..1467bdc63 100644 --- a/include/types.h +++ b/include/types.h @@ -120,6 +120,8 @@ typedef enum event_identifier EVENT_MONITOR_THROTTLE2 = 0x00000084, EVENT_MONITOR_THROTTLE3 = 0x00000085, EVENT_MONITOR_PERFORMANCE_HINT = 0x00000086, + EVENT_MONITOR_NOINPUT_HINT = 0x00000087, + EVENT_MONITOR_NOINPUT_ABORT = 0x00000088, EVENT_OPENCL_SESSION_POST = 0x00000090, EVENT_OPENCL_SESSION_PRE = 0x00000091, EVENT_OUTERLOOP_FINISHED = 0x000000a0, @@ -2022,6 +2024,12 @@ typedef struct status_ctx double msec_paused; // timer on current dict + /** + * read timeouts + */ + + u32 stdin_read_timeout_cnt; + } status_ctx_t; typedef struct hashcat_user diff --git a/src/dispatch.c b/src/dispatch.c index 0bd93bf62..e3d6776e2 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -179,9 +179,13 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par { if (status_ctx->run_thread_level1 == false) break; + status_ctx->stdin_read_timeout_cnt++; + continue; } + status_ctx->stdin_read_timeout_cnt = 0; + char *line_buf = fgets (buf, HCBUFSIZ_LARGE - 1, stdin); if (line_buf == NULL) break; @@ -343,9 +347,13 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par { if (status_ctx->run_thread_level1 == false) break; + status_ctx->stdin_read_timeout_cnt++; + continue; } + status_ctx->stdin_read_timeout_cnt = 0; + char *line_buf = fgets (buf, HCBUFSIZ_LARGE - 1, stdin); if (line_buf == NULL) break; diff --git a/src/main.c b/src/main.c index 5b3210196..3d0dfe2e2 100644 --- a/src/main.c +++ b/src/main.c @@ -700,6 +700,24 @@ static void main_monitor_performance_hint (MAYBE_UNUSED hashcat_ctx_t *hashcat_c } } +static void main_monitor_noinput_hint (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const void *buf, MAYBE_UNUSED const size_t len) +{ + const user_options_t *user_options = hashcat_ctx->user_options; + + if (user_options->quiet == true) return; + + event_log_advice (hashcat_ctx, "ATTENTION! Read timeout in stdin mode. The password candidates input is too slow:"); + event_log_advice (hashcat_ctx, "* Are you sure that you are using the correct attack mode (--attack-mode or -a)?"); + event_log_advice (hashcat_ctx, "* Are you sure that you want to use input from standard input (stdin)?"); + event_log_advice (hashcat_ctx, "* If so, are you sure that the input from stdin (the pipe) is working correctly and is fast enough?"); + event_log_advice (hashcat_ctx, NULL); +} + +static void main_monitor_noinput_abort (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const void *buf, MAYBE_UNUSED const size_t len) +{ + event_log_error (hashcat_ctx, "No password candidates received in stdin mode, aborting..."); +} + static void main_monitor_temp_abort (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const void *buf, MAYBE_UNUSED const size_t len) { const user_options_t *user_options = hashcat_ctx->user_options; @@ -952,6 +970,8 @@ static void event (const u32 id, hashcat_ctx_t *hashcat_ctx, const void *buf, co case EVENT_MONITOR_THROTTLE2: main_monitor_throttle2 (hashcat_ctx, buf, len); break; case EVENT_MONITOR_THROTTLE3: main_monitor_throttle3 (hashcat_ctx, buf, len); break; case EVENT_MONITOR_PERFORMANCE_HINT: main_monitor_performance_hint (hashcat_ctx, buf, len); break; + case EVENT_MONITOR_NOINPUT_HINT: main_monitor_noinput_hint (hashcat_ctx, buf, len); break; + case EVENT_MONITOR_NOINPUT_ABORT: main_monitor_noinput_abort (hashcat_ctx, buf, len); break; case EVENT_OPENCL_SESSION_POST: main_opencl_session_post (hashcat_ctx, buf, len); break; case EVENT_OPENCL_SESSION_PRE: main_opencl_session_pre (hashcat_ctx, buf, len); break; case EVENT_OUTERLOOP_FINISHED: main_outerloop_finished (hashcat_ctx, buf, len); break; diff --git a/src/monitor.c b/src/monitor.c index 9554ab718..1cb998237 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -282,6 +282,27 @@ static int monitor (hashcat_ctx_t *hashcat_ctx) if (performance_warnings == 10) EVENT_DATA (EVENT_MONITOR_PERFORMANCE_HINT, NULL, 0); } } + + // stdin read timeout check + + if (status_ctx->stdin_read_timeout_cnt >= STDIN_TIMEOUT_MIN) + { + if (status_ctx->stdin_read_timeout_cnt >= STDIN_TIMEOUT_MAX) + { + EVENT_DATA (EVENT_MONITOR_NOINPUT_ABORT, NULL, 0); + + myabort (hashcat_ctx); + + status_ctx->shutdown_inner = true; + + break; + } + + if ((status_ctx->stdin_read_timeout_cnt % STDIN_TIMEOUT_MIN) == 0) + { + EVENT_DATA (EVENT_MONITOR_NOINPUT_HINT, NULL, 0); + } + } } // final round of save_hash