From c43094267632d19c8caec365e74853a9bcf1769f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Bolvansk=C3=BD?= Date: Thu, 27 Apr 2023 11:35:59 +0200 Subject: [PATCH] JSON-escape example_hash in hash info --- src/terminal.c | 72 +++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/src/terminal.c b/src/terminal.c index 2de912e0a..d141cdad2 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -634,6 +634,37 @@ void compress_terminal_line_length (char *out_buf, const size_t keep_from_beginn *ptr1 = 0; } +void json_encode (char *text, char *escaped) +{ + /* + * Based on https://www.freeformatter.com/json-escape.html, below these 7 different chars + * are getting escaped before being printed. + */ + + size_t len = strlen (text); + unsigned long i, j; + + for (i = 0, j = 0; i < len; i++, j++) + { + char c = text[i]; + + switch (c) + { + case '\b': c = 'b'; escaped[j] = '\\'; j++; break; + case '\t': c = 't'; escaped[j] = '\\'; j++; break; + case '\n': c = 'n'; escaped[j] = '\\'; j++; break; + case '\f': c = 'f'; escaped[j] = '\\'; j++; break; + case '\r': c = 'r'; escaped[j] = '\\'; j++; break; + case '\\': c = '\\'; escaped[j] = '\\'; j++; break; + case '"': c = '"'; escaped[j] = '\\'; j++; break; + } + + escaped[j] = c; + } + + escaped[j] = 0; +} + void hash_info_single_json (hashcat_ctx_t *hashcat_ctx, user_options_extra_t *user_options_extra) { if (hashconfig_init (hashcat_ctx) == 0) @@ -692,14 +723,20 @@ void hash_info_single_json (hashcat_ctx_t *hashcat_ctx, user_options_extra_t *us { printf ("\"example_hash_format\": \"%s\", ", "hex-encoded (binary file only)"); } - printf ("\"example_hash\": \"%s\", ", hashconfig->st_hash); } else { printf ("\"example_hash_format\": \"%s\", ", "plain"); - printf ("\"example_hash\": \"%s\", ", hashconfig->st_hash); } + char *example_hash_json_encoded = (char *) hcmalloc (strlen (hashconfig->st_hash) * 2); + + json_encode ((char *)hashconfig->st_hash, example_hash_json_encoded); + + printf ("\"example_hash\": \"%s\", ", example_hash_json_encoded); + + hcfree (example_hash_json_encoded); + if (need_hexify ((const u8 *) hashconfig->st_pass, strlen (hashconfig->st_pass), user_options_extra->separator, false)) { char *tmp_buf = (char *) hcmalloc (HCBUFSIZ_LARGE); @@ -1760,37 +1797,6 @@ void status_display_machine_readable (hashcat_ctx_t *hashcat_ctx) hcfree (hashcat_status); } -void json_encode (char *text, char *escaped) -{ - /* - * Based on https://www.freeformatter.com/json-escape.html, below these 7 different chars - * are getting escaped before being printed. - */ - - size_t len = strlen (text); - unsigned long i, j; - - for (i = 0, j = 0; i < len; i++, j++) - { - char c = text[i]; - - switch (c) - { - case '\b': c = 'b'; escaped[j] = '\\'; j++; break; - case '\t': c = 't'; escaped[j] = '\\'; j++; break; - case '\n': c = 'n'; escaped[j] = '\\'; j++; break; - case '\f': c = 'f'; escaped[j] = '\\'; j++; break; - case '\r': c = 'r'; escaped[j] = '\\'; j++; break; - case '\\': c = '\\'; escaped[j] = '\\'; j++; break; - case '"': c = '"'; escaped[j] = '\\'; j++; break; - } - - escaped[j] = c; - } - - escaped[j] = 0; -} - void status_display_status_json (hashcat_ctx_t *hashcat_ctx) { const status_ctx_t *status_ctx = hashcat_ctx->status_ctx;