From cbc34706429a0d12fb494df987e635c6ea9623ae Mon Sep 17 00:00:00 2001 From: PenguinKeeper7 Date: Mon, 4 Mar 2024 22:22:31 +0000 Subject: [PATCH] Parse maskfiles and escaped ?s properly Replace the old reversal function with an improved one, that both respects custom charsets in mask files and escaped question marks --- src/mpsp.c | 60 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/src/mpsp.c b/src/mpsp.c index e9109590e..dd1e67ea5 100644 --- a/src/mpsp.c +++ b/src/mpsp.c @@ -1073,23 +1073,40 @@ static int mask_append_final (hashcat_ctx_t *hashcat_ctx, const char *mask) } // ?l?u?d -> ?d?u?l -static char* reverseMask (const char *mask) { - int length = strlen (mask); - char *tmp_buf = (char *) hcmalloc (256); - - for (int i = length - 1, j = 0; i >= 0; i--) { - if (mask[i] == '\0') - continue; - if (i != 0 && mask[i - 1] == '?') { - tmp_buf[j++] = '?'; - tmp_buf[j++] = mask[i]; - i--; - } else { - tmp_buf[j++] = mask[i]; - } - } +static char* reverseMask (const char *mask, const char *prepend) +{ + u32 maskLength = strlen (mask); + u32 prependLength = strlen (prepend); + + char *tmp_buf = (char *) hcmalloc (256); - return tmp_buf; + u32 i = 0; + + // Add prepend section to tmp_buf, avoiding reversal + if (prependLength != 0) + { + for (i = 0; i < prependLength ; i++) + { + tmp_buf[i] = prepend[i]; + } + tmp_buf[i++] = ','; + } + + for (u32 j = maskLength - 1; i <= maskLength - 1 ; i++) + { + if (mask[i] == '?' && mask[i + 1] != '\0') + { + tmp_buf[j--] = mask[i + 1]; + tmp_buf[j--] = mask[i]; + i++; + } + else + { + tmp_buf[j--] = mask[i]; + } + } + + return tmp_buf; } static int mask_append (hashcat_ctx_t *hashcat_ctx, const char *mask, const char *prepend) @@ -1130,14 +1147,21 @@ static int mask_append (hashcat_ctx_t *hashcat_ctx, const char *mask, const char if (user_options->increment_inverse == true) { - if (mp_get_truncated_mask (hashcat_ctx, reverseMask (mask), strlen (mask), increment_len, mask_truncated_next) == -1) + if (mp_get_truncated_mask (hashcat_ctx, reverseMask (mask, ""), strlen (mask), increment_len, mask_truncated_next) == -1) { hcfree (mask_truncated); break; } - mask_truncated = reverseMask (mask_truncated); + if (prepend) + { + mask_truncated = reverseMask (mask_truncated, prepend); + } + else + { + mask_truncated = reverseMask (mask_truncated, ""); + } } else {