From bd5bc294e6d1abfd79fb9ace77a88a0fcdeda7da Mon Sep 17 00:00:00 2001 From: jsteube Date: Mon, 12 Sep 2016 14:58:25 +0200 Subject: [PATCH] Move loopback related stuff to loopback.c --- include/data.h | 4 +- include/loopback.h | 23 ++++++++ src/data.c | 1 + src/hashcat.c | 130 +++++++++++++-------------------------------- src/hlfmt.c | 1 + src/hwmon.c | 1 + src/logfile.c | 1 + src/loopback.c | 101 +++++++++++++++++++++++++++++++++++ src/mpsp.c | 1 + src/opencl.c | 1 + src/restore.c | 1 + src/rp_cpu.c | 1 + src/status.c | 1 + src/stdout.c | 1 + src/thread.c | 1 + src/wordlist.c | 1 + 16 files changed, 174 insertions(+), 96 deletions(-) diff --git a/include/data.h b/include/data.h index 067965f21..bd41e21af 100644 --- a/include/data.h +++ b/include/data.h @@ -142,7 +142,7 @@ typedef struct char *induction_directory; char *outfile_check_directory; uint loopback; - char *loopback_file; + uint restore; uint restore_timer; uint restore_disable; @@ -177,8 +177,8 @@ typedef struct hashconfig_t *hashconfig; outfile_ctx_t *outfile_ctx; - potfile_ctx_t *potfile_ctx; + loopback_ctx_t *loopback_ctx; #if defined (HAVE_HWMON) uint gpu_temp_disable; diff --git a/include/loopback.h b/include/loopback.h index 127543b59..edd066009 100644 --- a/include/loopback.h +++ b/include/loopback.h @@ -6,4 +6,27 @@ #ifndef _LOOPBACK_H #define _LOOPBACK_H +#include +#include +#include +#include + +#define LOOPBACK 0 +#define LOOPBACK_FILE "hashcat.loopback" + +typedef struct +{ + FILE *fp; + char *filename; + +} loopback_ctx_t; + +void loopback_init (loopback_ctx_t *loopback_ctx); +void loopback_destroy (loopback_ctx_t *loopback_ctx); +int loopback_write_open (loopback_ctx_t *loopback_ctx, const char *induction_directory); +void loopback_write_close (loopback_ctx_t *loopback_ctx); +void loopback_format_plain (loopback_ctx_t *loopback_ctx, const u8 *plain_ptr, const unsigned int plain_len); +void loopback_write_append (loopback_ctx_t *loopback_ctx, const u8 *plain_ptr, const unsigned int plain_len); +void loopback_write_unlink (loopback_ctx_t *loopback_ctx); + #endif // _LOOPBACK_H diff --git a/src/data.c b/src/data.c index 04a1129c6..ae1430fef 100644 --- a/src/data.c +++ b/src/data.c @@ -20,6 +20,7 @@ #include "opencl.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" hc_global_data_t data; diff --git a/src/hashcat.c b/src/hashcat.c index 9b2f05c13..b9274eddb 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -59,6 +59,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" #include "affinity.h" #include "bitmap.h" @@ -72,7 +73,6 @@ #include "version.h" #include "benchmark.h" #include "outfile_check.h" -#include "loopback.h" #include "weak_hash.h" #include "hash_management.h" #include "remove.h" @@ -110,51 +110,6 @@ const int comptime = COMPTIME; // outfile_check #define OUTFILE_CHECK_TIMER 5 -// loopback -#define LOOPBACK 0 -#define LOOPBACK_FILE "hashcat.loopback" -typedef struct -{ - FILE *fp; - -} loopback_ctx_t; -void loopback_format_plain (loopback_ctx_t *loopback_ctx, const unsigned char *plain_ptr, const uint plain_len) -{ - int needs_hexify = 0; - - for (uint i = 0; i < plain_len; i++) - { - if (plain_ptr[i] < 0x20) - { - needs_hexify = 1; - - break; - } - - if (plain_ptr[i] > 0x7f) - { - needs_hexify = 1; - - break; - } - } - - if (needs_hexify == 1) - { - fprintf (loopback_ctx->fp, "$HEX["); - - for (uint i = 0; i < plain_len; i++) - { - fprintf (loopback_ctx->fp, "%02x", plain_ptr[i]); - } - - fprintf (loopback_ctx->fp, "]"); - } - else - { - fwrite (plain_ptr, plain_len, 1, loopback_ctx->fp); - } -} // weak_hash #define WEAK_HASH_THRESHOLD 100 @@ -479,7 +434,6 @@ static void check_checkpoint () static void check_hash (hc_device_param_t *device_param, plain_t *plain) { uint quiet = data.quiet; - uint loopback = data.loopback; uint debug_mode = data.debug_mode; char *debug_file = data.debug_file; @@ -742,24 +696,13 @@ static void check_hash (hc_device_param_t *device_param, plain_t *plain) } } - // loopback + // if enabled, update also the loopback file - if (loopback) + loopback_ctx_t *loopback_ctx = data.loopback_ctx; + + if (loopback_ctx->fp) { - char *loopback_file = data.loopback_file; - - loopback_ctx_t loopback_ctx; - - loopback_ctx.fp = fopen (loopback_file, "ab"); - - if (loopback_ctx.fp != NULL) - { - loopback_format_plain (&loopback_ctx, plain_ptr, plain_len); - - fputc ('\n', loopback_ctx.fp); - - fclose (loopback_ctx.fp); - } + loopback_write_append (loopback_ctx, plain_ptr, plain_len); } // (rule) debug mode @@ -5111,14 +5054,6 @@ int main (int argc, char **argv) data.induction_directory = induction_directory; - /** - * loopback - */ - - size_t loopback_size = strlen (session_dir) + 1 + session_size + strlen (LOOPBACK_FILE) + 12; - - char *loopback_file = (char *) mymalloc (loopback_size); - /** * tuning db */ @@ -5600,6 +5535,16 @@ int main (int argc, char **argv) SUPPRESS_OUTPUT = 0; } + /** + * loopback + */ + + loopback_ctx_t *loopback_ctx = mymalloc (sizeof (loopback_ctx_t)); + + data.loopback_ctx = loopback_ctx; + + loopback_init (loopback_ctx); + /** * word len */ @@ -12226,23 +12171,6 @@ int main (int argc, char **argv) } } - /* - * Update loopback file - */ - - if (loopback == 1) - { - time_t now; - - time (&now); - - uint random_num = get_random_num (0, 9999); - - snprintf (loopback_file, loopback_size - 1, "%s/%s.%d_%i", induction_directory, LOOPBACK_FILE, (int) now, random_num); - - data.loopback_file = loopback_file; - } - /* * Update dictionary statistic */ @@ -12252,6 +12180,15 @@ int main (int argc, char **argv) dictstat_write (dictstat_ctx); } + /** + * Update loopback file + */ + + if (loopback == 1) + { + loopback_write_open (loopback_ctx, induction_directory); + } + /** * create autotune threads */ @@ -12430,6 +12367,15 @@ int main (int argc, char **argv) dictpos--; } + /** + * Update loopback file + */ + + if (loopback == 1) + { + loopback_write_close (loopback_ctx); + } + time_t runtime_stop; time (&runtime_stop); @@ -12835,6 +12781,8 @@ int main (int argc, char **argv) dictstat_destroy (dictstat_ctx); + loopback_destroy (loopback_ctx); + local_free (all_kernel_rules_cnt); local_free (all_kernel_rules_buf); @@ -12910,12 +12858,6 @@ int main (int argc, char **argv) tuning_db_destroy (tuning_db); - // loopback - - local_free (loopback_file); - - if (loopback == 1) unlink (loopback_file); - // induction directory if (induction_dir == NULL) diff --git a/src/hlfmt.c b/src/hlfmt.c index f65f70774..60d4ff6ad 100644 --- a/src/hlfmt.c +++ b/src/hlfmt.c @@ -30,6 +30,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" #include "hlfmt.h" diff --git a/src/hwmon.c b/src/hwmon.c index 262901358..8e6cf2548 100644 --- a/src/hwmon.c +++ b/src/hwmon.c @@ -23,6 +23,7 @@ #include "thread.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" hc_thread_mutex_t mux_hwmon; diff --git a/src/logfile.c b/src/logfile.c index af1c03862..8ade8229e 100644 --- a/src/logfile.c +++ b/src/logfile.c @@ -26,6 +26,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" #include "logfile.h" diff --git a/src/loopback.c b/src/loopback.c index f33111804..0ddbd6234 100644 --- a/src/loopback.c +++ b/src/loopback.c @@ -4,4 +4,105 @@ */ #include "common.h" +#include "types_int.h" +#include "memory.h" +#include "logging.h" +#include "shared.h" #include "loopback.h" + +void loopback_init (loopback_ctx_t *loopback_ctx) +{ + loopback_ctx->fp = NULL; + + loopback_ctx->filename = (char *) mymalloc (HCBUFSIZ_TINY); +} + +void loopback_destroy (loopback_ctx_t *loopback_ctx) +{ + myfree (loopback_ctx->filename); +} + +int loopback_write_open (loopback_ctx_t *loopback_ctx, const char *induction_directory) +{ + time_t now; + + time (&now); + + const uint random_num = get_random_num (0, 9999); + + snprintf (loopback_ctx->filename, HCBUFSIZ_TINY - 1, "%s/%s.%d_%u", induction_directory, LOOPBACK_FILE, (int) now, random_num); + + loopback_ctx->fp = fopen (loopback_ctx->filename, "ab"); + + if (loopback_ctx->fp == NULL) + { + log_error ("ERROR: %s: %s", loopback_ctx->filename, strerror (errno)); + + return -1; + } + + return 0; +} + +void loopback_write_unlink (loopback_ctx_t *loopback_ctx) +{ + if (loopback_ctx->filename == NULL) return; + + unlink (loopback_ctx->filename); +} + +void loopback_write_close (loopback_ctx_t *loopback_ctx) +{ + if (loopback_ctx->fp == NULL) return; + + fclose (loopback_ctx->fp); +} + +void loopback_format_plain (loopback_ctx_t *loopback_ctx, const u8 *plain_ptr, const unsigned int plain_len) +{ + int needs_hexify = 0; + + for (uint i = 0; i < plain_len; i++) + { + if (plain_ptr[i] < 0x20) + { + needs_hexify = 1; + + break; + } + + if (plain_ptr[i] > 0x7f) + { + needs_hexify = 1; + + break; + } + } + + if (needs_hexify == 1) + { + fprintf (loopback_ctx->fp, "$HEX["); + + for (uint i = 0; i < plain_len; i++) + { + fprintf (loopback_ctx->fp, "%02x", plain_ptr[i]); + } + + fprintf (loopback_ctx->fp, "]"); + } + else + { + fwrite (plain_ptr, plain_len, 1, loopback_ctx->fp); + } +} + +void loopback_write_append (loopback_ctx_t *loopback_ctx, const u8 *plain_ptr, const unsigned int plain_len) +{ + FILE *fp = loopback_ctx->fp; + + loopback_format_plain (loopback_ctx, plain_ptr, plain_len); + + fputc ('\n', fp); + + fflush (fp); +} diff --git a/src/mpsp.c b/src/mpsp.c index b62327ba5..6b9043c09 100644 --- a/src/mpsp.c +++ b/src/mpsp.c @@ -29,6 +29,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" //#include "shared.h" diff --git a/src/opencl.c b/src/opencl.c index ed3da5988..59586f18e 100644 --- a/src/opencl.c +++ b/src/opencl.c @@ -24,6 +24,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" extern hc_global_data_t data; diff --git a/src/restore.c b/src/restore.c index 072bd48dc..3bcb8c781 100644 --- a/src/restore.c +++ b/src/restore.c @@ -23,6 +23,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" extern hc_global_data_t data; diff --git a/src/rp_cpu.c b/src/rp_cpu.c index 1f13c2b9a..ddb1298b9 100644 --- a/src/rp_cpu.c +++ b/src/rp_cpu.c @@ -30,6 +30,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" #include "shared.h" #include "rp_cpu.h" diff --git a/src/status.c b/src/status.c index 2ef9e64fb..c62f45b47 100644 --- a/src/status.c +++ b/src/status.c @@ -25,6 +25,7 @@ #include "interface.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" //#include "shared.h" #include "status.h" diff --git a/src/stdout.c b/src/stdout.c index 64ce84ec7..4bc794134 100644 --- a/src/stdout.c +++ b/src/stdout.c @@ -29,6 +29,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" #include "stdout.h" diff --git a/src/thread.c b/src/thread.c index e0db0ab17..776f8a237 100644 --- a/src/thread.c +++ b/src/thread.c @@ -22,6 +22,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "status.h" #include "data.h" diff --git a/src/wordlist.c b/src/wordlist.c index 43ec968d7..bb70983bd 100644 --- a/src/wordlist.c +++ b/src/wordlist.c @@ -30,6 +30,7 @@ #include "restore.h" #include "outfile.h" #include "potfile.h" +#include "loopback.h" #include "data.h" #include "wordlist.h"