From 100cd5fea4fd02e3f22a1ad9fd30e5d07bf176c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Bolvansk=C3=BD?= Date: Fri, 18 Mar 2022 09:42:01 +0100 Subject: [PATCH 1/3] status-json: also print device name --- include/status.h | 1 + include/types.h | 1 + src/hashcat.c | 1 + src/status.c | 9 +++++++++ src/terminal.c | 1 + 5 files changed, 13 insertions(+) diff --git a/include/status.h b/include/status.h index eb6dafe9d..985b530f1 100644 --- a/include/status.h +++ b/include/status.h @@ -87,6 +87,7 @@ int status_get_innerloop_pos_dev (const hashcat_ctx_t *hash int status_get_innerloop_left_dev (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx); int status_get_iteration_pos_dev (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx); int status_get_iteration_left_dev (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx); +char *status_get_device_name (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx); #ifdef WITH_BRAIN int status_get_brain_session (const hashcat_ctx_t *hashcat_ctx); int status_get_brain_attack (const hashcat_ctx_t *hashcat_ctx); diff --git a/include/types.h b/include/types.h index 1f0204172..f7ff4e897 100644 --- a/include/types.h +++ b/include/types.h @@ -2586,6 +2586,7 @@ typedef struct device_info int innerloop_left_dev; int iteration_pos_dev; int iteration_left_dev; + char *device_name; #ifdef WITH_BRAIN int brain_link_client_id_dev; int brain_link_status_dev; diff --git a/src/hashcat.c b/src/hashcat.c index 2fff7f4b5..734d65428 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -2017,6 +2017,7 @@ int hashcat_get_status (hashcat_ctx_t *hashcat_ctx, hashcat_status_t *hashcat_st device_info->innerloop_left_dev = status_get_innerloop_left_dev (hashcat_ctx, device_id); device_info->iteration_pos_dev = status_get_iteration_pos_dev (hashcat_ctx, device_id); device_info->iteration_left_dev = status_get_iteration_left_dev (hashcat_ctx, device_id); + device_info->device_name = status_get_device_name (hashcat_ctx, device_id); #ifdef WITH_BRAIN device_info->brain_link_client_id_dev = status_get_brain_link_client_id_dev (hashcat_ctx, device_id); device_info->brain_link_status_dev = status_get_brain_link_status_dev (hashcat_ctx, device_id); diff --git a/src/status.c b/src/status.c index b22c9846d..87a8a9ec5 100644 --- a/src/status.c +++ b/src/status.c @@ -1829,6 +1829,15 @@ int status_get_iteration_left_dev (const hashcat_ctx_t *hashcat_ctx, const int b return iteration_left; } +char *status_get_device_name (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx) +{ + const backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx; + + hc_device_param_t *device_param = &backend_ctx->devices_param[backend_devices_idx]; + + return device_param->device_name; +} + #ifdef WITH_BRAIN int status_get_brain_link_client_id_dev (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx) { diff --git a/src/terminal.c b/src/terminal.c index 6d21e0b5a..e0fe2fd17 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -1660,6 +1660,7 @@ void status_display_status_json (hashcat_ctx_t *hashcat_ctx) } printf (" { \"device_id\": %d,", device_id + 1); + printf (" { \"device_name\": \"%s\",", device_info->device_name); printf (" \"speed\": %" PRIu64 ",", (u64) (device_info->hashes_msec_dev * 1000)); if (hwmon_ctx->enabled == true) From 50aea559c573a966731412d5780a09240b007d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Bolvansk=C3=BD?= Date: Fri, 18 Mar 2022 11:37:35 +0100 Subject: [PATCH 2/3] added device type --- include/status.h | 1 + include/types.h | 1 + src/hashcat.c | 1 + src/status.c | 9 +++++++++ src/terminal.c | 7 ++++++- 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/status.h b/include/status.h index 985b530f1..fda89f9ae 100644 --- a/include/status.h +++ b/include/status.h @@ -88,6 +88,7 @@ int status_get_innerloop_left_dev (const hashcat_ctx_t *hash int status_get_iteration_pos_dev (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx); int status_get_iteration_left_dev (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx); char *status_get_device_name (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx); +cl_device_type status_get_device_type (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx); #ifdef WITH_BRAIN int status_get_brain_session (const hashcat_ctx_t *hashcat_ctx); int status_get_brain_attack (const hashcat_ctx_t *hashcat_ctx); diff --git a/include/types.h b/include/types.h index f7ff4e897..b502789af 100644 --- a/include/types.h +++ b/include/types.h @@ -2587,6 +2587,7 @@ typedef struct device_info int iteration_pos_dev; int iteration_left_dev; char *device_name; + cl_device_type device_type; #ifdef WITH_BRAIN int brain_link_client_id_dev; int brain_link_status_dev; diff --git a/src/hashcat.c b/src/hashcat.c index 734d65428..087ee79db 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -2018,6 +2018,7 @@ int hashcat_get_status (hashcat_ctx_t *hashcat_ctx, hashcat_status_t *hashcat_st device_info->iteration_pos_dev = status_get_iteration_pos_dev (hashcat_ctx, device_id); device_info->iteration_left_dev = status_get_iteration_left_dev (hashcat_ctx, device_id); device_info->device_name = status_get_device_name (hashcat_ctx, device_id); + device_info->device_type = status_get_device_type (hashcat_ctx, device_id); #ifdef WITH_BRAIN device_info->brain_link_client_id_dev = status_get_brain_link_client_id_dev (hashcat_ctx, device_id); device_info->brain_link_status_dev = status_get_brain_link_status_dev (hashcat_ctx, device_id); diff --git a/src/status.c b/src/status.c index 87a8a9ec5..596f8c94a 100644 --- a/src/status.c +++ b/src/status.c @@ -1838,6 +1838,15 @@ char *status_get_device_name (const hashcat_ctx_t *hashcat_ctx, const int backen return device_param->device_name; } +cl_device_type status_get_device_type (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx) +{ + const backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx; + + hc_device_param_t *device_param = &backend_ctx->devices_param[backend_devices_idx]; + + return device_param->opencl_device_type; +} + #ifdef WITH_BRAIN int status_get_brain_link_client_id_dev (const hashcat_ctx_t *hashcat_ctx, const int backend_devices_idx) { diff --git a/src/terminal.c b/src/terminal.c index e0fe2fd17..c5604f629 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -1660,7 +1660,12 @@ void status_display_status_json (hashcat_ctx_t *hashcat_ctx) } printf (" { \"device_id\": %d,", device_id + 1); - printf (" { \"device_name\": \"%s\",", device_info->device_name); + printf (" \"device_name\": \"%s\",", device_info->device_name); + + const char *device_type_desc = ((device_info->device_type & CL_DEVICE_TYPE_CPU) ? "CPU" : + ((device_info->device_type & CL_DEVICE_TYPE_GPU) ? "GPU" : "Accelerator")); + printf(" \"device_type\": \"%s\",", device_type_desc); + printf (" \"speed\": %" PRIu64 ",", (u64) (device_info->hashes_msec_dev * 1000)); if (hwmon_ctx->enabled == true) From f018dd92636c334c0afc513c622400abf15276aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Bolvansk=C3=BD?= Date: Mon, 21 Mar 2022 09:25:26 +0100 Subject: [PATCH 3/3] json-encode device name --- src/terminal.c | 79 ++++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/src/terminal.c b/src/terminal.c index c5604f629..200accf32 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -1552,6 +1552,37 @@ 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 hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx; @@ -1584,37 +1615,6 @@ void status_display_status_json (hashcat_ctx_t *hashcat_ctx) end = time_now + sec_etc; } - /* - * As the hash target can contain the hash (in case of a single attacked hash), especially - * some salts can contain chars which need to be escaped to not break the JSON encoding. - * Based on https://www.freeformatter.com/json-escape.html, below these 7 different chars - * are getting escaped before being printed. - */ - - char *target_json_encoded = (char *) hcmalloc (strlen (hashcat_status->hash_target) * 2); - - unsigned long i, j; - - for (i = 0, j = 0; i < strlen (hashcat_status->hash_target); i++, j++) - { - char c = hashcat_status->hash_target[i]; - - switch (c) - { - case '\b': c = 'b'; target_json_encoded[j] = '\\'; j++; break; - case '\t': c = 't'; target_json_encoded[j] = '\\'; j++; break; - case '\n': c = 'n'; target_json_encoded[j] = '\\'; j++; break; - case '\f': c = 'f'; target_json_encoded[j] = '\\'; j++; break; - case '\r': c = 'r'; target_json_encoded[j] = '\\'; j++; break; - case '\\': c = '\\'; target_json_encoded[j] = '\\'; j++; break; - case '"': c = '"'; target_json_encoded[j] = '\\'; j++; break; - } - - target_json_encoded[j] = c; - } - - target_json_encoded[j] = 0; - printf ("{ \"session\": \"%s\",", hashcat_status->session); printf (" \"guess\": {"); if (hashcat_status->guess_base) @@ -1635,7 +1635,16 @@ void status_display_status_json (hashcat_ctx_t *hashcat_ctx) printf (" \"guess_mode\": %d", hashcat_status->guess_mode); printf (" },"); printf (" \"status\": %d,", hashcat_status->status_number); + + /* + * As the hash target can contain the hash (in case of a single attacked hash), especially + * some salts can contain chars which need to be escaped to not break the JSON encoding. + */ + char *target_json_encoded = (char *) hcmalloc (strlen (hashcat_status->hash_target) * 2); + json_encode(hashcat_status->hash_target, target_json_encoded); printf (" \"target\": \"%s\",", target_json_encoded); + hcfree (target_json_encoded); + printf (" \"progress\": [%" PRIu64 ", %" PRIu64 "],", hashcat_status->progress_cur_relative_skip, hashcat_status->progress_end_relative_skip); printf (" \"restore_point\": %" PRIu64 ",", hashcat_status->restore_point); printf (" \"recovered_hashes\": [%d, %d],", hashcat_status->digests_done, hashcat_status->digests_cnt); @@ -1643,8 +1652,6 @@ void status_display_status_json (hashcat_ctx_t *hashcat_ctx) printf (" \"rejected\": %" PRIu64 ",", hashcat_status->progress_rejected); printf (" \"devices\": ["); - hcfree (target_json_encoded); - int device_num = 0; for (int device_id = 0; device_id < hashcat_status->device_info_cnt; device_id++) @@ -1660,7 +1667,11 @@ void status_display_status_json (hashcat_ctx_t *hashcat_ctx) } printf (" { \"device_id\": %d,", device_id + 1); - printf (" \"device_name\": \"%s\",", device_info->device_name); + + char *device_name_json_encoded = (char *)hcmalloc(strlen(device_info->device_name) * 2); + json_encode(device_info->device_name, device_name_json_encoded); + printf(" \"device_name\": \"%s\",", device_name_json_encoded); + hcfree(device_name_json_encoded); const char *device_type_desc = ((device_info->device_type & CL_DEVICE_TYPE_CPU) ? "CPU" : ((device_info->device_type & CL_DEVICE_TYPE_GPU) ? "GPU" : "Accelerator"));