1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-12-22 22:58:30 +00:00

Initial proposal

This commit is contained in:
jsteube 2016-10-07 16:56:44 +02:00
parent bea228dabe
commit 81650dcc46
5 changed files with 350 additions and 150 deletions

View File

@ -8,7 +8,7 @@
int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_folder, int argc, char **argv, const int comptime);
void hashcat_ctx_init (hashcat_ctx_t *hashcat_ctx);
void hashcat_ctx_init (hashcat_ctx_t *hashcat_ctx, int (*event) (hashcat_ctx_t *, const u32));
void hashcat_ctx_destroy (hashcat_ctx_t *hashcat_ctx);
#endif // _HASHCAT_H

View File

@ -12,6 +12,8 @@
#include <time.h>
#include <fcntl.h>
#define EVENT_SEND(id) { const int rc_event = hashcat_ctx->event (hashcat_ctx, (id)); if (rc_event == -1) return -1; }
bool is_power_of_2 (const u32 v);
u32 get_random_num (const u32 min, const u32 max);

View File

@ -81,6 +81,31 @@ typedef struct stat64 hc_stat;
// enums
typedef enum event_identifier
{
EVENT_WELCOME_SCREEN = 0x00000011,
EVENT_GOODBYE_SCREEN = 0x00000012,
EVENT_LOGFILE_TOP_INITIALIZE = 0x00000021,
EVENT_LOGFILE_TOP_FINALIZE = 0x00000022,
EVENT_LOGFILE_SUB_INITIALIZE = 0x00000023,
EVENT_LOGFILE_SUB_FINALIZE = 0x00000024,
EVENT_OUTERLOOP_STARTING = 0x00000031,
EVENT_OUTERLOOP_FINISHED = 0x00000032,
EVENT_INNERLOOP1_STARTING = 0x00000041,
EVENT_INNERLOOP1_FINISHED = 0x00000042,
EVENT_INNERLOOP2_STARTING = 0x00000051,
EVENT_INNERLOOP2_FINISHED = 0x00000052,
EVENT_AUTOTUNE_STARTING = 0x00000053,
EVENT_AUTOTUNE_FINISHED = 0x00000054,
EVENT_CRACKER_STARTING = 0x00000055,
EVENT_CRACKER_FINISHED = 0x00000056,
EVENT_CRACKER_FINAL_STATS = 0x00000057,
EVENT_CRACKER_HASH_CRACKED = 0x00000058,
// there will be much more event types soon
} event_identifier_t;
typedef enum amplifier_count
{
KERNEL_BFS = 1024,
@ -1350,6 +1375,15 @@ typedef struct status_ctx
} status_ctx_t;
typedef struct hashcat_user
{
// use this for context specific data
int outer_threads_cnt;
hc_thread_t *outer_threads;
} hashcat_user_t;
typedef struct hashcat_ctx
{
bitmap_ctx_t *bitmap_ctx;
@ -1358,6 +1392,7 @@ typedef struct hashcat_ctx
debugfile_ctx_t *debugfile_ctx;
dictstat_ctx_t *dictstat_ctx;
folder_config_t *folder_config;
hashcat_user_t *hashcat_user;
hashconfig_t *hashconfig;
hashes_t *hashes;
hwmon_ctx_t *hwmon_ctx;
@ -1377,6 +1412,8 @@ typedef struct hashcat_ctx
user_options_t *user_options;
wl_data_t *wl_data;
int (*event) (struct hashcat_ctx *, const u32);
} hashcat_ctx_t;
typedef struct thread_param

View File

@ -46,7 +46,6 @@
#include "rp.h"
#include "status.h"
#include "straight.h"
#include "terminal.h"
#include "tuningdb.h"
#include "usage.h"
#include "user_options.h"
@ -56,14 +55,24 @@
extern const u32 DEFAULT_BENCHMARK_ALGORITHMS_CNT;
extern const u32 DEFAULT_BENCHMARK_ALGORITHMS_BUF[];
void hashcat_ctx_init (hashcat_ctx_t *hashcat_ctx)
void hashcat_ctx_init (hashcat_ctx_t *hashcat_ctx, int (*event) (hashcat_ctx_t *, const u32))
{
if (event == NULL)
{
fprintf (stderr, "Event callback function is mandatory\n");
exit (-1);
}
hashcat_ctx->event = event;
hashcat_ctx->bitmap_ctx = (bitmap_ctx_t *) mymalloc (sizeof (bitmap_ctx_t));
hashcat_ctx->combinator_ctx = (combinator_ctx_t *) mymalloc (sizeof (combinator_ctx_t));
hashcat_ctx->cpt_ctx = (cpt_ctx_t *) mymalloc (sizeof (cpt_ctx_t));
hashcat_ctx->debugfile_ctx = (debugfile_ctx_t *) mymalloc (sizeof (debugfile_ctx_t));
hashcat_ctx->dictstat_ctx = (dictstat_ctx_t *) mymalloc (sizeof (dictstat_ctx_t));
hashcat_ctx->folder_config = (folder_config_t *) mymalloc (sizeof (folder_config_t));
hashcat_ctx->hashcat_user = (hashcat_user_t *) mymalloc (sizeof (hashcat_user_t));
hashcat_ctx->hashconfig = (hashconfig_t *) mymalloc (sizeof (hashconfig_t));
hashcat_ctx->hashes = (hashes_t *) mymalloc (sizeof (hashes_t));
hashcat_ctx->hwmon_ctx = (hwmon_ctx_t *) mymalloc (sizeof (hwmon_ctx_t));
@ -135,9 +144,7 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
status_ctx->devices_status = STATUS_INIT;
logfile_generate_subid (hashcat_ctx);
logfile_sub_msg ("START");
EVENT_SEND (EVENT_LOGFILE_SUB_INITIALIZE);
status_progress_reset (hashcat_ctx);
@ -188,6 +195,8 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
return 0;
}
// restore stuff
if (status_ctx->words_cur > status_ctx->words_base)
{
log_error ("ERROR: Restore value greater keyspace");
@ -195,8 +204,6 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
return -1;
}
// restore progress
if (status_ctx->words_cur)
{
const u64 progress_restored = status_ctx->words_cur * user_options_extra_amplifier (hashcat_ctx);
@ -219,6 +226,8 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
* create autotune threads
*/
EVENT_SEND (EVENT_AUTOTUNE_STARTING);
thread_param_t *threads_param = (thread_param_t *) mycalloc (opencl_ctx->devices_cnt, sizeof (thread_param_t));
hc_thread_t *c_threads = (hc_thread_t *) mycalloc (opencl_ctx->devices_cnt, sizeof (hc_thread_t));
@ -237,6 +246,8 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
hc_thread_wait (opencl_ctx->devices_cnt, c_threads);
EVENT_SEND (EVENT_AUTOTUNE_FINISHED);
/**
* autotune modified kernel_accel, which modifies opencl_ctx->kernel_power_all
*/
@ -252,23 +263,6 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
loopback_write_open (hashcat_ctx);
}
/**
* Tell user we're about to start
*/
if ((user_options_extra->wordlist_mode == WL_MODE_FILE) || (user_options_extra->wordlist_mode == WL_MODE_MASK))
{
if ((user_options->quiet == false) && (user_options->status == false) && (user_options->benchmark == false))
{
if (user_options->quiet == false) send_prompt ();
}
}
else if (user_options_extra->wordlist_mode == WL_MODE_STDIN)
{
if (user_options->quiet == false) log_info ("Starting attack in stdin mode...");
if (user_options->quiet == false) log_info ("");
}
/**
* Prepare cracking stats
*/
@ -287,6 +281,8 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
* create cracker threads
*/
EVENT_SEND (EVENT_CRACKER_STARTING);
status_ctx->devices_status = STATUS_RUNNING;
for (u32 device_id = 0; device_id < opencl_ctx->devices_cnt; device_id++)
@ -312,8 +308,6 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
myfree (threads_param);
// calculate final status
if ((status_ctx->devices_status != STATUS_CRACKED)
&& (status_ctx->devices_status != STATUS_ABORTED)
&& (status_ctx->devices_status != STATUS_QUIT)
@ -322,7 +316,7 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
status_ctx->devices_status = STATUS_EXHAUSTED;
}
logfile_sub_var_uint ("status-after-work", status_ctx->devices_status);
EVENT_SEND (EVENT_CRACKER_FINISHED);
// update some timer
@ -337,7 +331,7 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
time (&status_ctx->prepare_start);
logfile_sub_msg ("STOP");
EVENT_SEND (EVENT_CRACKER_FINAL_STATS);
// no more skip and restore from here
@ -353,39 +347,7 @@ static int inner2_loop (hashcat_ctx_t *hashcat_ctx)
loopback_write_close (hashcat_ctx);
}
// print final status
if (user_options->benchmark == true)
{
status_benchmark (hashcat_ctx);
if (user_options->machine_readable == false)
{
log_info ("");
}
}
else
{
if (user_options->quiet == false)
{
clear_prompt ();
if (hashes->digests_saved != hashes->digests_done) log_info ("");
status_display (hashcat_ctx);
log_info ("");
}
else
{
if (user_options->status == true)
{
status_display (hashcat_ctx);
log_info ("");
}
}
}
EVENT_SEND (EVENT_LOGFILE_SUB_FINALIZE);
// New induction folder check
@ -908,7 +870,7 @@ static int outer_loop (hashcat_ctx_t *hashcat_ctx)
potfile_write_close (hashcat_ctx);
// finalize session
// finalize opencl session
opencl_session_destroy (hashcat_ctx);
@ -937,22 +899,9 @@ static int outer_loop (hashcat_ctx_t *hashcat_ctx)
int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_folder, int argc, char **argv, const int comptime)
{
/**
* To help users a bit
*/
setup_environment_variables ();
setup_umask ();
/**
* main init
*/
logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
user_options_t *user_options = hashcat_ctx->user_options;
logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
user_options_t *user_options = hashcat_ctx->user_options;
/**
* status init
@ -962,6 +911,8 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
if (rc_status_init == -1) return -1;
EVENT_SEND (EVENT_WELCOME_SCREEN);
/**
* folder
*/
@ -986,26 +937,24 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
user_options_extra_init (hashcat_ctx);
// from here all user configuration is pre-processed so we can start logging
EVENT_SEND (EVENT_LOGFILE_TOP_INITIALIZE);
/**
* To help users a bit
*/
setup_environment_variables ();
setup_umask ();
/**
* prepare seeding for random number generator, required by logfile and rules generator
*/
setup_seeding (user_options->rp_gen_seed_chgd, user_options->rp_gen_seed);
/**
* logfile init
*/
const int rc_logfile_init = logfile_init (hashcat_ctx);
if (rc_logfile_init == -1) return -1;
logfile_generate_topid (hashcat_ctx);
logfile_top_msg ("START");
user_options_logger (hashcat_ctx);
/**
* tuning db
*/
@ -1105,6 +1054,7 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
const int rc_devices_init = opencl_ctx_devices_init (hashcat_ctx, comptime);
if (rc_devices_init == -1) return -1;
/**
* HM devices: init
*/
@ -1113,30 +1063,12 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
if (rc_hwmon_init == -1) return -1;
/**
* keypress thread
*/
int outer_threads_cnt = 0;
hc_thread_t *outer_threads = (hc_thread_t *) mycalloc (10, sizeof (hc_thread_t));
status_ctx->shutdown_outer = false;
if (user_options->keyspace == false && user_options->benchmark == false && user_options->stdout_flag == false)
{
if ((user_options_extra->wordlist_mode == WL_MODE_FILE) || (user_options_extra->wordlist_mode == WL_MODE_MASK))
{
hc_thread_create (outer_threads[outer_threads_cnt], thread_keypress, hashcat_ctx);
outer_threads_cnt++;
}
}
/**
* outer loop
*/
EVENT_SEND (EVENT_OUTERLOOP_STARTING);
if (user_options->benchmark == true)
{
user_options->quiet = true;
@ -1168,16 +1100,7 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
if (rc == -1) return -1;
}
// wait for outer threads
status_ctx->shutdown_outer = true;
for (int thread_idx = 0; thread_idx < outer_threads_cnt; thread_idx++)
{
hc_thread_wait (1, &outer_threads[thread_idx]);
}
myfree (outer_threads);
EVENT_SEND (EVENT_OUTERLOOP_FINISHED);
if (user_options->benchmark == true)
{
@ -1225,9 +1148,7 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
logfile_top_uint (status_ctx->proc_start);
logfile_top_uint (status_ctx->proc_stop);
logfile_top_msg ("STOP");
logfile_destroy (hashcat_ctx);
EVENT_SEND (EVENT_LOGFILE_TOP_FINALIZE);
user_options_extra_destroy (hashcat_ctx);
@ -1240,7 +1161,11 @@ int hashcat (hashcat_ctx_t *hashcat_ctx, char *install_folder, char *shared_fold
if (status_ctx->devices_status == STATUS_EXHAUSTED) rc_final = 1;
if (status_ctx->devices_status == STATUS_CRACKED) rc_final = 0;
EVENT_SEND (EVENT_GOODBYE_SCREEN);
status_ctx_destroy (hashcat_ctx);
// done
return rc_final;
}

View File

@ -11,18 +11,276 @@
#include "types.h"
#include "user_options.h"
#include "hashcat.h"
#include "memory.h" // commandline only
#include "terminal.h" // commandline only
#include "usage.h" // commandline only
#include "logging.h" // commandline only
#include "logfile.h" // commandline only
#include "thread.h" // commandline only
#include "status.h" // commandline only
#define RUN_AS_COMMANDLINE true
#if (RUN_AS_COMMANDLINE == true)
static int event_welcome_screen (hashcat_ctx_t *hashcat_ctx)
{
// sets dos window size (windows only)
setup_console ();
// Inform user things getting started
const status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
welcome_screen (hashcat_ctx, status_ctx->proc_start, VERSION_TAG);
return 0;
}
static int event_goodbye_screen (hashcat_ctx_t *hashcat_ctx)
{
// Inform user we're done
const status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
goodbye_screen (hashcat_ctx, status_ctx->proc_start, status_ctx->proc_stop);
return 0;
}
static int event_logfile_top_initialize (hashcat_ctx_t *hashcat_ctx)
{
const logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
// logfile init
const int rc_logfile_init = logfile_init (hashcat_ctx);
if (rc_logfile_init == -1) return -1;
logfile_generate_topid (hashcat_ctx);
logfile_top_msg ("START");
// add all user options to logfile in case we want to debug some user session
user_options_logger (hashcat_ctx);
return 0;
}
static int event_logfile_top_finalize (hashcat_ctx_t *hashcat_ctx)
{
const logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
logfile_top_msg ("STOP");
logfile_destroy (hashcat_ctx);
return 0;
}
static int event_logfile_sub_initialize (hashcat_ctx_t *hashcat_ctx)
{
const logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
logfile_generate_subid (hashcat_ctx);
logfile_sub_msg ("START");
return 0;
}
static int event_logfile_sub_finalize (hashcat_ctx_t *hashcat_ctx)
{
const logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
logfile_sub_msg ("STOP");
return 0;
}
static int event_outerloop_starting (hashcat_ctx_t *hashcat_ctx)
{
hashcat_user_t *hashcat_user = hashcat_ctx->hashcat_user;
status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
user_options_t *user_options = hashcat_ctx->user_options;
user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
/**
* keypress thread
*/
hashcat_user->outer_threads_cnt = 0;
hashcat_user->outer_threads = (hc_thread_t *) mycalloc (2, sizeof (hc_thread_t));
status_ctx->shutdown_outer = false;
if (user_options->keyspace == false && user_options->benchmark == false && user_options->stdout_flag == false)
{
if ((user_options_extra->wordlist_mode == WL_MODE_FILE) || (user_options_extra->wordlist_mode == WL_MODE_MASK))
{
// see thread_keypress() how to access status information
hc_thread_create (hashcat_user->outer_threads[hashcat_user->outer_threads_cnt], thread_keypress, hashcat_ctx);
hashcat_user->outer_threads_cnt++;
}
}
return 0;
}
static int event_outerloop_finished (hashcat_ctx_t *hashcat_ctx)
{
hashcat_user_t *hashcat_user = hashcat_ctx->hashcat_user;
status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
// wait for outer threads
status_ctx->shutdown_outer = true;
for (int thread_idx = 0; thread_idx < hashcat_user->outer_threads_cnt; thread_idx++)
{
hc_thread_wait (1, &hashcat_user->outer_threads[thread_idx]);
}
myfree (hashcat_user->outer_threads);
hashcat_user->outer_threads_cnt = 0;
return 0;
}
static int event_cracker_starting (hashcat_ctx_t *hashcat_ctx)
{
user_options_t *user_options = hashcat_ctx->user_options;
user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
// Tell the user we're about to start
if ((user_options_extra->wordlist_mode == WL_MODE_FILE) || (user_options_extra->wordlist_mode == WL_MODE_MASK))
{
if ((user_options->quiet == false) && (user_options->status == false) && (user_options->benchmark == false))
{
if (user_options->quiet == false) send_prompt ();
}
}
else if (user_options_extra->wordlist_mode == WL_MODE_STDIN)
{
if (user_options->quiet == false) log_info ("Starting attack in stdin mode...");
if (user_options->quiet == false) log_info ("");
}
return 0;
}
static int event_cracker_finished (hashcat_ctx_t *hashcat_ctx)
{
logfile_ctx_t *logfile_ctx = hashcat_ctx->logfile_ctx;
status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
logfile_sub_var_uint ("status-after-work", status_ctx->devices_status);
return 0;
}
static int event_cracker_final_stats (hashcat_ctx_t *hashcat_ctx)
{
hashes_t *hashes = hashcat_ctx->hashes;
user_options_t *user_options = hashcat_ctx->user_options;
// print final status
if (user_options->benchmark == true)
{
status_benchmark (hashcat_ctx);
if (user_options->machine_readable == false)
{
log_info ("");
}
}
else
{
if (user_options->quiet == false)
{
clear_prompt ();
if (hashes->digests_saved != hashes->digests_done) log_info ("");
status_display (hashcat_ctx);
log_info ("");
}
else
{
if (user_options->status == true)
{
status_display (hashcat_ctx);
log_info ("");
}
}
}
return 0;
}
static int event_cracker_hash_cracked (hashcat_ctx_t *hashcat_ctx)
{
hashes_t *hashes = hashcat_ctx->hashes;
user_options_t *user_options = hashcat_ctx->user_options;
if (hashes == NULL) hashes = NULL;
if (user_options == NULL) user_options = NULL;
return 0;
}
int event (hashcat_ctx_t *hashcat_ctx, const u32 event)
{
int rc = 0;
switch (event)
{
case EVENT_WELCOME_SCREEN: rc = event_welcome_screen (hashcat_ctx); break;
case EVENT_GOODBYE_SCREEN: rc = event_goodbye_screen (hashcat_ctx); break;
case EVENT_LOGFILE_TOP_INITIALIZE: rc = event_logfile_top_initialize (hashcat_ctx); break;
case EVENT_LOGFILE_TOP_FINALIZE: rc = event_logfile_top_finalize (hashcat_ctx); break;
case EVENT_LOGFILE_SUB_INITIALIZE: rc = event_logfile_sub_initialize (hashcat_ctx); break;
case EVENT_LOGFILE_SUB_FINALIZE: rc = event_logfile_sub_finalize (hashcat_ctx); break;
case EVENT_OUTERLOOP_STARTING: rc = event_outerloop_starting (hashcat_ctx); break;
case EVENT_OUTERLOOP_FINISHED: rc = event_outerloop_finished (hashcat_ctx); break;
case EVENT_CRACKER_STARTING: rc = event_cracker_starting (hashcat_ctx); break;
case EVENT_CRACKER_FINISHED: rc = event_cracker_finished (hashcat_ctx); break;
case EVENT_CRACKER_FINAL_STATS: rc = event_cracker_final_stats (hashcat_ctx); break;
case EVENT_CRACKER_HASH_CRACKED: rc = event_cracker_hash_cracked (hashcat_ctx); break;
}
return rc;
}
#else
int event (hashcat_ctx_t *hashcat_ctx, const u32 event)
{
switch (event)
{
}
}
#endif
int main (int argc, char **argv)
{
// hashcat main context
hashcat_ctx_t *hashcat_ctx = (hashcat_ctx_t *) malloc (sizeof (hashcat_ctx_t));
hashcat_ctx_init (hashcat_ctx);
hashcat_ctx_init (hashcat_ctx, event);
// initialize the session via getops for commandline use or
// alternatively you can set the user_options directly
@ -46,12 +304,6 @@ int main (int argc, char **argv)
shared_folder = SHARED_FOLDER;
#endif
// sets dos window size (windows only)
const int rc_console = setup_console ();
if (rc_console == -1) return -1;
// initialize the user options with some defaults (you can override them later)
user_options_init (hashcat_ctx);
@ -84,25 +336,9 @@ int main (int argc, char **argv)
return 0;
}
// Inform user things getting started
time_t proc_start;
time (&proc_start);
welcome_screen (hashcat_ctx, proc_start, VERSION_TAG);
// now run hashcat
rc_hashcat = hashcat (hashcat_ctx, install_folder, shared_folder, argc, argv, COMPTIME);
// Inform user we're done
time_t proc_stop;
time (&proc_stop);
goodbye_screen (hashcat_ctx, proc_start, proc_stop);
}
else
{
@ -141,4 +377,4 @@ int main (int argc, char **argv)
free (hashcat_ctx);
return rc_hashcat;
}
}