From 378f852cecc4f50b4cf979ee92dd39ca304cc998 Mon Sep 17 00:00:00 2001 From: jsteube Date: Sun, 19 Mar 2017 15:11:41 +0100 Subject: [PATCH] Parameter: Detect and error when users try to use a non-digit where a digit is expected Fixes https://github.com/hashcat/hashcat/issues/1189 --- docs/changes.txt | 5 +++-- include/shared.h | 3 +++ src/shared.c | 18 ++++++++++++++++ src/user_options.c | 52 +++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index ac674ef62..ca8b5c25e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -29,8 +29,9 @@ ## - Building: Added missing prototypes for atlassian_parse_hash function -- Files: Detect and warn when users try to use -r with a parameter which is not a file -- Parameter: Detect and warn when users try to use an empty string (length 0) for parameters like --session= +- Files: Detect and error when users try to use -r with a parameter which is not a file +- Parameter: Detect and error when users try to use an empty string (length 0) for parameters like --session= +- Parameter: Detect and error when users try to use a non-digit where a digit is expected - Sessions: Improved string comparison in case user sets --session to "hashcat" * changes v3.30 -> v3.40: diff --git a/include/shared.h b/include/shared.h index 3e1968cb7..0228b4293 100644 --- a/include/shared.h +++ b/include/shared.h @@ -12,6 +12,7 @@ #include #include #include +#include bool overflow_check_u32_add (const u32 a, const u32 b); bool overflow_check_u32_mul (const u32 a, const u32 b); @@ -53,4 +54,6 @@ bool hc_path_read (const char *path); bool hc_path_write (const char *path); bool hc_path_create (const char *path); +bool hc_string_is_digit (const char *s); + #endif // _SHARED_H diff --git a/src/shared.c b/src/shared.c index b73a67b2c..84ff85a0e 100644 --- a/src/shared.c +++ b/src/shared.c @@ -298,6 +298,24 @@ bool hc_path_create (const char *path) return true; } +bool hc_string_is_digit (const char *s) +{ + if (s == NULL) return false; + + const size_t len = strlen (s); + + if (len == 0) return false; + + for (size_t i = 0; i < len; i++) + { + const int c = (const int) s[i]; + + if (isdigit (c) == 0) return false; + } + + return true; +} + void setup_environment_variables () { char *compute = getenv ("COMPUTE"); diff --git a/src/user_options.c b/src/user_options.c index 94e943549..4b4a7b9b6 100644 --- a/src/user_options.c +++ b/src/user_options.c @@ -228,10 +228,60 @@ int user_options_getopt (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) int c = -1; + int option_index; + optind = 1; optopt = 0; - int option_index = 0; + option_index = 0; + + while (((c = getopt_long (argc, argv, short_options, long_options, &option_index)) != -1) && optopt == 0) + { + switch (c) + { + case IDX_REMOVE_TIMER: + case IDX_DEBUG_MODE: + case IDX_SKIP: + case IDX_LIMIT: + case IDX_STATUS_TIMER: + case IDX_WEAK_HASH_THRESHOLD: + case IDX_HASH_MODE: + case IDX_RUNTIME: + case IDX_ATTACK_MODE: + case IDX_RP_GEN: + case IDX_RP_GEN_FUNC_MIN: + case IDX_RP_GEN_FUNC_MAX: + case IDX_RP_GEN_SEED: + case IDX_MARKOV_THRESHOLD: + case IDX_OUTFILE_FORMAT: + case IDX_OUTFILE_CHECK_TIMER: + case IDX_OPENCL_VECTOR_WIDTH: + case IDX_WORKLOAD_PROFILE: + case IDX_KERNEL_ACCEL: + case IDX_KERNEL_LOOPS: + case IDX_NVIDIA_SPIN_DAMP: + case IDX_GPU_TEMP_ABORT: + case IDX_GPU_TEMP_RETAIN: + case IDX_HCCAPX_MESSAGE_PAIR: + case IDX_NONCE_ERROR_CORRECTIONS: + case IDX_VERACRYPT_PIM: + case IDX_SEGMENT_SIZE: + case IDX_SCRYPT_TMTO: + case IDX_BITMAP_MIN: + case IDX_BITMAP_MAX: + case IDX_INCREMENT_MIN: + case IDX_INCREMENT_MAX: + + if (hc_string_is_digit (optarg) == false) + { + event_log_error (hashcat_ctx, "Not a number '%s'", optarg); + + return -1; + } + } + } + + option_index = 0; while (((c = getopt_long (argc, argv, short_options, long_options, &option_index)) != -1) && optopt == 0) {