From 97c9e86d155df741fc50bc61453d63888427ef86 Mon Sep 17 00:00:00 2001 From: Jens Steube Date: Tue, 6 Aug 2019 12:22:24 +0200 Subject: [PATCH] Filehandling: Print a truncation warning in case an oversized line was detected --- docs/changes.txt | 1 + include/filehandling.h | 3 ++- src/backend.c | 4 ++-- src/filehandling.c | 18 +++++++++++++++--- src/hashes.c | 2 +- src/hlfmt.c | 2 +- src/keyboard_layout.c | 2 +- src/mpsp.c | 6 +++--- src/outfile_check.c | 2 +- src/potfile.c | 2 +- src/rp.c | 2 +- src/slow_candidates.c | 4 ++-- 12 files changed, 31 insertions(+), 17 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index bb37c2c85..0d5998a54 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -82,6 +82,7 @@ - Building: Updated BUILD.md - Cracking bcrypt and Password Safe v2: Use a feedback from the compute API backend to dynamically find out optimal thread count - Documents: Added README on how to build hashcat on MSYS2 +- Filehandling: Print a truncation warning in case an oversized line was detected - My Wallet: Added additional plaintext pattern used in newer versions - OpenCL Runtime: Disable OpenCL kernel cache on Apple for Intel CPU (throws CL_BUILD_PROGRAM_FAILURE for no reason) - OpenCL Runtime: Do not run a shared- and constant-memory size check if their memory type is of type global memory (typically CPU) diff --git a/include/filehandling.h b/include/filehandling.h index 32f3044b0..8b73699d4 100644 --- a/include/filehandling.h +++ b/include/filehandling.h @@ -9,6 +9,7 @@ #include #include #include +#include #if defined (__CYGWIN__) int _wopen (const char *path, int oflag, ...); @@ -31,7 +32,7 @@ char *hc_fgets (char *buf, int len, HCFILE *fp); size_t hc_fwrite (void *ptr, size_t size, size_t nmemb, HCFILE *fp); size_t hc_fread (void *ptr, size_t size, size_t nmemb, HCFILE *fp); -size_t fgetl (HCFILE *fp, char *line_buf); +size_t fgetl (HCFILE *fp, char *line_buf, const size_t line_sz); u64 count_lines (HCFILE *fp); size_t in_superchop (char *buf); size_t superchop_with_length (char *buf, const size_t len); diff --git a/src/backend.c b/src/backend.c index edef5c229..4c0ff1179 100644 --- a/src/backend.c +++ b/src/backend.c @@ -4183,7 +4183,7 @@ int run_cracker (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, co { if (hc_feof (combs_fp)) break; - size_t line_len = fgetl (combs_fp, line_buf); + size_t line_len = fgetl (combs_fp, line_buf, HCBUFSIZ_LARGE); line_len = convert_from_hex (hashcat_ctx, line_buf, line_len); @@ -4328,7 +4328,7 @@ int run_cracker (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, co { if (hc_feof (combs_fp)) break; - size_t line_len = fgetl (combs_fp, line_buf); + size_t line_len = fgetl (combs_fp, line_buf, HCBUFSIZ_LARGE); line_len = convert_from_hex (hashcat_ctx, line_buf, line_len); diff --git a/src/filehandling.c b/src/filehandling.c index 921e7e0ed..9ea564f2a 100644 --- a/src/filehandling.c +++ b/src/filehandling.c @@ -454,8 +454,10 @@ void hc_fclose (HCFILE *fp) fp->mode = NULL; } -size_t fgetl (HCFILE *fp, char *line_buf) +size_t fgetl (HCFILE *fp, char *line_buf, const size_t line_sz) { + size_t line_truncated = 0; + size_t line_len = 0; while (!hc_feof (fp)) @@ -464,15 +466,25 @@ size_t fgetl (HCFILE *fp, char *line_buf) if (c == EOF) break; + if (line_len == line_sz) + { + line_truncated++; + + continue; + } + line_buf[line_len] = (char) c; line_len++; - if (line_len == HCBUFSIZ_LARGE) line_len--; - if (c == '\n') break; } + if (line_truncated > 0) + { + fprintf (stderr, "\nOversized line detected! Truncated %" PRIu64 " bytes\n", line_truncated); + } + if (line_len == 0) return 0; while (line_len) diff --git a/src/hashes.c b/src/hashes.c index 3e93b6579..a92af200e 100644 --- a/src/hashes.c +++ b/src/hashes.c @@ -1018,7 +1018,7 @@ int hashes_init_stage1 (hashcat_ctx_t *hashcat_ctx) { line_num++; - const size_t line_len = fgetl (&fp, line_buf); + const size_t line_len = fgetl (&fp, line_buf, HCBUFSIZ_LARGE); if (line_len == 0) continue; diff --git a/src/hlfmt.c b/src/hlfmt.c index c049f24fc..68ea27c82 100644 --- a/src/hlfmt.c +++ b/src/hlfmt.c @@ -350,7 +350,7 @@ u32 hlfmt_detect (hashcat_ctx_t *hashcat_ctx, HCFILE *fp, u32 max_check) while (!hc_feof (fp)) { - const size_t line_len = fgetl (fp, line_buf); + const size_t line_len = fgetl (fp, line_buf, HCBUFSIZ_LARGE); if (line_len == 0) continue; diff --git a/src/keyboard_layout.c b/src/keyboard_layout.c index b20ec6d35..fd1680388 100644 --- a/src/keyboard_layout.c +++ b/src/keyboard_layout.c @@ -30,7 +30,7 @@ bool initialize_keyboard_layout_mapping (const char *filename, keyboard_layout_m while (!hc_feof (&fp)) { - const size_t line_len = fgetl (&fp, line_buf); + const size_t line_len = fgetl (&fp, line_buf, HCBUFSIZ_LARGE); if (line_len == 0) continue; diff --git a/src/mpsp.c b/src/mpsp.c index 03f1836ac..3d7d64dfe 100644 --- a/src/mpsp.c +++ b/src/mpsp.c @@ -1434,7 +1434,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) while (!hc_feof (&mask_fp)) { - const size_t line_len = fgetl (&mask_fp, line_buf); + const size_t line_len = fgetl (&mask_fp, line_buf, HCBUFSIZ_LARGE); if (line_len == 0) continue; @@ -1519,7 +1519,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) while (!hc_feof (&mask_fp)) { - const size_t line_len = fgetl (&mask_fp, line_buf); + const size_t line_len = fgetl (&mask_fp, line_buf, HCBUFSIZ_LARGE); if (line_len == 0) continue; @@ -1589,7 +1589,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) while (!hc_feof (&mask_fp)) { - const size_t line_len = fgetl (&mask_fp, line_buf); + const size_t line_len = fgetl (&mask_fp, line_buf, HCBUFSIZ_LARGE); if (line_len == 0) continue; diff --git a/src/outfile_check.c b/src/outfile_check.c index c67b9429c..84d47b1e9 100644 --- a/src/outfile_check.c +++ b/src/outfile_check.c @@ -185,7 +185,7 @@ static int outfile_remove (hashcat_ctx_t *hashcat_ctx) while (!hc_feof (&fp)) { - size_t line_len = fgetl (&fp, line_buf); + size_t line_len = fgetl (&fp, line_buf, HCBUFSIZ_LARGE); if (line_len == 0) continue; diff --git a/src/potfile.c b/src/potfile.c index 866548bff..58a247406 100644 --- a/src/potfile.c +++ b/src/potfile.c @@ -521,7 +521,7 @@ int potfile_remove_parse (hashcat_ctx_t *hashcat_ctx) while (!hc_feof (&potfile_ctx->fp)) { - size_t line_len = fgetl (&potfile_ctx->fp, line_buf); + size_t line_len = fgetl (&potfile_ctx->fp, line_buf, HCBUFSIZ_LARGE); if (line_len == 0) continue; diff --git a/src/rp.c b/src/rp.c index 716e451bf..2fdce44ca 100644 --- a/src/rp.c +++ b/src/rp.c @@ -751,7 +751,7 @@ int kernel_rules_load (hashcat_ctx_t *hashcat_ctx, kernel_rule_t **out_buf, u32 while (!hc_feof (&fp)) { - rule_len = (u32) fgetl (&fp, rule_buf); + rule_len = (u32) fgetl (&fp, rule_buf, HCBUFSIZ_LARGE); rule_line++; diff --git a/src/slow_candidates.c b/src/slow_candidates.c index 76b8386f7..222cc4a92 100644 --- a/src/slow_candidates.c +++ b/src/slow_candidates.c @@ -123,7 +123,7 @@ void slow_candidates_seek (hashcat_ctx_t *hashcat_ctx, void *extra_info, const u while (1) { - line_len = (u32) fgetl (combs_fp, line_buf); + line_len = (u32) fgetl (combs_fp, line_buf, HCBUFSIZ_LARGE); // post-process rule engine @@ -286,7 +286,7 @@ void slow_candidates_next (hashcat_ctx_t *hashcat_ctx, void *extra_info) while (1) { - line_len = (u32) fgetl (combs_fp, line_buf); + line_len = (u32) fgetl (combs_fp, line_buf, HCBUFSIZ_LARGE); // post-process rule engine