2016-09-27 11:13:07 +00:00
/**
* Author . . . . . . : See docs / credits . txt
* License . . . . . : MIT
*/
# include "common.h"
# include "types.h"
# include "memory.h"
2016-10-09 20:41:55 +00:00
# include "event.h"
2016-10-06 20:18:20 +00:00
# include "logfile.h"
2016-09-27 11:13:07 +00:00
# include "shared.h"
2016-10-03 14:27:34 +00:00
# include "folder.h"
2016-09-27 11:13:07 +00:00
# include "rp.h"
2016-10-06 20:18:20 +00:00
# include "wordlist.h"
2019-03-31 15:39:00 +00:00
# include "straight.h"
2016-09-27 11:13:07 +00:00
2016-10-13 08:07:04 +00:00
static int straight_ctx_add_wl ( hashcat_ctx_t * hashcat_ctx , const char * dict )
2016-09-27 11:13:07 +00:00
{
2018-07-30 11:23:48 +00:00
if ( hc_path_has_bom ( dict ) = = true )
{
event_log_error ( hashcat_ctx , " %s: Byte Order Mark (BOM) was detected " , dict ) ;
return - 1 ;
}
2016-10-10 09:03:11 +00:00
straight_ctx_t * straight_ctx = hashcat_ctx - > straight_ctx ;
2016-10-06 08:21:39 +00:00
if ( straight_ctx - > dicts_avail = = straight_ctx - > dicts_cnt )
{
2016-11-20 21:54:52 +00:00
straight_ctx - > dicts = ( char * * ) hcrealloc ( straight_ctx - > dicts , straight_ctx - > dicts_avail * sizeof ( char * ) , INCR_DICTS * sizeof ( char * ) ) ;
2016-10-06 08:21:39 +00:00
straight_ctx - > dicts_avail + = INCR_DICTS ;
}
2016-11-20 21:54:52 +00:00
straight_ctx - > dicts [ straight_ctx - > dicts_cnt ] = hcstrdup ( dict ) ;
2016-10-06 08:21:39 +00:00
straight_ctx - > dicts_cnt + + ;
2016-10-13 08:07:04 +00:00
return 0 ;
2016-10-06 08:21:39 +00:00
}
2016-10-06 20:18:20 +00:00
int straight_ctx_update_loop ( hashcat_ctx_t * hashcat_ctx )
{
combinator_ctx_t * combinator_ctx = hashcat_ctx - > combinator_ctx ;
induct_ctx_t * induct_ctx = hashcat_ctx - > induct_ctx ;
2020-09-29 13:56:32 +00:00
hashes_t * hashes = hashcat_ctx - > hashes ;
2016-10-06 20:18:20 +00:00
logfile_ctx_t * logfile_ctx = hashcat_ctx - > logfile_ctx ;
mask_ctx_t * mask_ctx = hashcat_ctx - > mask_ctx ;
status_ctx_t * status_ctx = hashcat_ctx - > status_ctx ;
straight_ctx_t * straight_ctx = hashcat_ctx - > straight_ctx ;
user_options_extra_t * user_options_extra = hashcat_ctx - > user_options_extra ;
user_options_t * user_options = hashcat_ctx - > user_options ;
if ( user_options - > attack_mode = = ATTACK_MODE_STRAIGHT )
{
if ( user_options_extra - > wordlist_mode = = WL_MODE_FILE )
{
if ( induct_ctx - > induction_dictionaries_cnt )
{
straight_ctx - > dict = induct_ctx - > induction_dictionaries [ induct_ctx - > induction_dictionaries_pos ] ;
}
else
{
straight_ctx - > dict = straight_ctx - > dicts [ straight_ctx - > dicts_pos ] ;
}
logfile_sub_string ( straight_ctx - > dict ) ;
for ( u32 i = 0 ; i < user_options - > rp_files_cnt ; i + + )
{
logfile_sub_var_string ( " rulefile " , user_options - > rp_files [ i ] ) ;
}
2019-06-26 17:06:46 +00:00
HCFILE fp ;
2016-10-06 20:18:20 +00:00
2019-07-01 15:27:08 +00:00
if ( hc_fopen ( & fp , straight_ctx - > dict , " rb " ) = = false )
2016-10-06 20:18:20 +00:00
{
2017-02-04 01:53:50 +00:00
event_log_error ( hashcat_ctx , " %s: %s " , straight_ctx - > dict , strerror ( errno ) ) ;
2016-10-06 20:18:20 +00:00
return - 1 ;
}
2019-06-26 17:06:46 +00:00
const int rc = count_words ( hashcat_ctx , & fp , straight_ctx - > dict , & status_ctx - > words_cnt ) ;
2017-02-22 15:33:23 +00:00
2019-06-26 17:06:46 +00:00
hc_fclose ( & fp ) ;
2017-11-05 06:20:36 +00:00
2017-02-22 15:33:23 +00:00
if ( rc = = - 1 )
{
event_log_error ( hashcat_ctx , " Integer overflow detected in keyspace of wordlist: %s " , straight_ctx - > dict ) ;
return - 1 ;
}
2016-10-06 20:18:20 +00:00
if ( status_ctx - > words_cnt = = 0 )
{
logfile_sub_msg ( " STOP " ) ;
return 0 ;
}
}
}
else if ( user_options - > attack_mode = = ATTACK_MODE_COMBI )
{
logfile_sub_string ( combinator_ctx - > dict1 ) ;
logfile_sub_string ( combinator_ctx - > dict2 ) ;
if ( combinator_ctx - > combs_mode = = COMBINATOR_MODE_BASE_LEFT )
{
2019-06-26 17:06:46 +00:00
HCFILE fp ;
2016-10-06 20:18:20 +00:00
2019-07-01 15:27:08 +00:00
if ( hc_fopen ( & fp , combinator_ctx - > dict1 , " rb " ) = = false )
2016-10-06 20:18:20 +00:00
{
2017-02-04 01:53:50 +00:00
event_log_error ( hashcat_ctx , " %s: %s " , combinator_ctx - > dict1 , strerror ( errno ) ) ;
2016-10-06 20:18:20 +00:00
return - 1 ;
}
2019-06-26 17:06:46 +00:00
const int rc = count_words ( hashcat_ctx , & fp , combinator_ctx - > dict1 , & status_ctx - > words_cnt ) ;
2017-02-22 15:33:23 +00:00
2019-06-26 17:06:46 +00:00
hc_fclose ( & fp ) ;
2017-11-05 06:20:36 +00:00
2017-02-22 15:33:23 +00:00
if ( rc = = - 1 )
{
event_log_error ( hashcat_ctx , " Integer overflow detected in keyspace of wordlist: %s " , combinator_ctx - > dict1 ) ;
return - 1 ;
}
2016-10-06 20:18:20 +00:00
}
else if ( combinator_ctx - > combs_mode = = COMBINATOR_MODE_BASE_RIGHT )
{
2019-06-26 17:06:46 +00:00
HCFILE fp ;
2016-10-06 20:18:20 +00:00
2019-07-01 15:27:08 +00:00
if ( hc_fopen ( & fp , combinator_ctx - > dict2 , " rb " ) = = false )
2016-10-06 20:18:20 +00:00
{
2017-02-04 01:53:50 +00:00
event_log_error ( hashcat_ctx , " %s: %s " , combinator_ctx - > dict2 , strerror ( errno ) ) ;
2016-10-06 20:18:20 +00:00
return - 1 ;
}
2019-06-26 17:06:46 +00:00
const int rc = count_words ( hashcat_ctx , & fp , combinator_ctx - > dict2 , & status_ctx - > words_cnt ) ;
2017-02-22 15:33:23 +00:00
2019-06-26 17:06:46 +00:00
hc_fclose ( & fp ) ;
2017-11-05 06:20:36 +00:00
2017-02-22 15:33:23 +00:00
if ( rc = = - 1 )
{
event_log_error ( hashcat_ctx , " Integer overflow detected in keyspace of wordlist: %s " , combinator_ctx - > dict2 ) ;
return - 1 ;
}
2016-10-06 20:18:20 +00:00
}
if ( status_ctx - > words_cnt = = 0 )
{
logfile_sub_msg ( " STOP " ) ;
return 0 ;
}
}
else if ( user_options - > attack_mode = = ATTACK_MODE_BF )
{
logfile_sub_string ( mask_ctx - > mask ) ;
}
else if ( ( user_options - > attack_mode = = ATTACK_MODE_HYBRID1 ) | | ( user_options - > attack_mode = = ATTACK_MODE_HYBRID2 ) )
{
if ( induct_ctx - > induction_dictionaries_cnt )
{
straight_ctx - > dict = induct_ctx - > induction_dictionaries [ induct_ctx - > induction_dictionaries_pos ] ;
}
else
{
straight_ctx - > dict = straight_ctx - > dicts [ straight_ctx - > dicts_pos ] ;
}
logfile_sub_string ( straight_ctx - > dict ) ;
logfile_sub_string ( mask_ctx - > mask ) ;
2019-06-26 17:06:46 +00:00
HCFILE fp ;
2016-10-06 20:18:20 +00:00
2019-07-01 15:27:08 +00:00
if ( hc_fopen ( & fp , straight_ctx - > dict , " rb " ) = = false )
2016-10-06 20:18:20 +00:00
{
2017-02-04 01:53:50 +00:00
event_log_error ( hashcat_ctx , " %s: %s " , straight_ctx - > dict , strerror ( errno ) ) ;
2016-10-06 20:18:20 +00:00
return - 1 ;
}
2019-06-26 17:06:46 +00:00
const int rc = count_words ( hashcat_ctx , & fp , straight_ctx - > dict , & status_ctx - > words_cnt ) ;
2017-02-22 15:33:23 +00:00
2019-06-26 17:06:46 +00:00
hc_fclose ( & fp ) ;
2017-11-05 06:20:36 +00:00
2017-02-22 15:33:23 +00:00
if ( rc = = - 1 )
{
event_log_error ( hashcat_ctx , " Integer overflow detected in keyspace of wordlist: %s " , straight_ctx - > dict ) ;
return - 1 ;
}
2016-10-06 20:18:20 +00:00
if ( status_ctx - > words_cnt = = 0 )
{
logfile_sub_msg ( " STOP " ) ;
return 0 ;
}
}
2020-09-29 13:56:32 +00:00
else if ( user_options - > attack_mode = = ATTACK_MODE_ASSOCIATION )
{
if ( user_options_extra - > wordlist_mode = = WL_MODE_FILE )
{
straight_ctx - > dict = straight_ctx - > dicts [ straight_ctx - > dicts_pos ] ;
logfile_sub_string ( straight_ctx - > dict ) ;
for ( u32 i = 0 ; i < user_options - > rp_files_cnt ; i + + )
{
logfile_sub_var_string ( " rulefile " , user_options - > rp_files [ i ] ) ;
}
HCFILE fp ;
if ( hc_fopen ( & fp , straight_ctx - > dict , " rb " ) = = false )
{
event_log_error ( hashcat_ctx , " %s: %s " , straight_ctx - > dict , strerror ( errno ) ) ;
return - 1 ;
}
const int rc = count_words ( hashcat_ctx , & fp , straight_ctx - > dict , & status_ctx - > words_cnt ) ;
hc_fclose ( & fp ) ;
if ( rc = = - 1 )
{
event_log_error ( hashcat_ctx , " Integer overflow detected in keyspace of wordlist: %s " , straight_ctx - > dict ) ;
return - 1 ;
}
if ( ( status_ctx - > words_cnt / straight_ctx - > kernel_rules_cnt ) ! = hashes - > salts_cnt )
{
event_log_error ( hashcat_ctx , " Number of words in wordlist %s is not in sync with number of salts (words: % " PRIu64 " , salts: %d) " , straight_ctx - > dict , status_ctx - > words_cnt , hashes - > salts_cnt ) ;
return - 1 ;
}
if ( status_ctx - > words_cnt = = 0 )
{
logfile_sub_msg ( " STOP " ) ;
return 0 ;
}
}
}
2016-10-06 20:18:20 +00:00
return 0 ;
}
2016-10-06 08:21:39 +00:00
int straight_ctx_init ( hashcat_ctx_t * hashcat_ctx )
{
straight_ctx_t * straight_ctx = hashcat_ctx - > straight_ctx ;
user_options_extra_t * user_options_extra = hashcat_ctx - > user_options_extra ;
user_options_t * user_options = hashcat_ctx - > user_options ;
2016-09-27 11:13:07 +00:00
straight_ctx - > enabled = false ;
2017-08-22 09:09:46 +00:00
if ( user_options - > example_hashes = = true ) return 0 ;
if ( user_options - > left = = true ) return 0 ;
2019-05-01 13:52:56 +00:00
if ( user_options - > backend_info = = true ) return 0 ;
2017-08-22 09:09:46 +00:00
if ( user_options - > show = = true ) return 0 ;
if ( user_options - > usage = = true ) return 0 ;
if ( user_options - > version = = true ) return 0 ;
2016-09-30 10:45:10 +00:00
if ( user_options - > attack_mode = = ATTACK_MODE_BF ) return 0 ;
2016-09-27 11:13:07 +00:00
straight_ctx - > enabled = true ;
2016-10-03 14:27:34 +00:00
/**
* generate NOP rules
*/
2016-09-27 11:13:07 +00:00
if ( ( user_options - > rp_files_cnt = = 0 ) & & ( user_options - > rp_gen = = 0 ) )
{
2016-11-20 21:54:52 +00:00
straight_ctx - > kernel_rules_buf = ( kernel_rule_t * ) hcmalloc ( sizeof ( kernel_rule_t ) ) ;
2016-09-27 11:13:07 +00:00
straight_ctx - > kernel_rules_buf [ 0 ] . cmds [ 0 ] = RULE_OP_MANGLE_NOOP ;
straight_ctx - > kernel_rules_cnt = 1 ;
}
else
{
if ( user_options - > rp_files_cnt )
{
2019-07-13 05:16:03 +00:00
if ( kernel_rules_load ( hashcat_ctx , & straight_ctx - > kernel_rules_buf , & straight_ctx - > kernel_rules_cnt ) = = - 1 ) return - 1 ;
2016-09-27 11:13:07 +00:00
}
else if ( user_options - > rp_gen )
{
2019-07-13 05:16:03 +00:00
if ( kernel_rules_generate ( hashcat_ctx , & straight_ctx - > kernel_rules_buf , & straight_ctx - > kernel_rules_cnt ) = = - 1 ) return - 1 ;
2016-09-27 11:13:07 +00:00
}
}
/**
2016-10-03 14:27:34 +00:00
* wordlist based work
2016-09-27 11:13:07 +00:00
*/
2020-09-29 13:56:32 +00:00
if ( ( user_options - > attack_mode = = ATTACK_MODE_STRAIGHT ) | | ( user_options - > attack_mode = = ATTACK_MODE_ASSOCIATION ) )
2016-10-03 14:27:34 +00:00
{
if ( user_options_extra - > wordlist_mode = = WL_MODE_FILE )
{
for ( int i = 0 ; i < user_options_extra - > hc_workc ; i + + )
{
char * l0_filename = user_options_extra - > hc_workv [ i ] ;
2017-01-27 10:46:45 +00:00
// at this point we already verified the path actually exist and is readable
2016-10-03 14:27:34 +00:00
2017-01-27 10:46:45 +00:00
if ( hc_path_is_directory ( l0_filename ) = = true )
2016-10-03 14:27:34 +00:00
{
2017-02-11 00:09:58 +00:00
char * * dictionary_files ;
2016-10-03 14:27:34 +00:00
2016-11-20 21:54:52 +00:00
dictionary_files = scan_directory ( l0_filename ) ;
2016-10-03 14:27:34 +00:00
if ( dictionary_files ! = NULL )
{
qsort ( dictionary_files , ( size_t ) count_dictionaries ( dictionary_files ) , sizeof ( char * ) , sort_by_stringptr ) ;
for ( int d = 0 ; dictionary_files [ d ] ! = NULL ; d + + )
{
char * l1_filename = dictionary_files [ d ] ;
2017-01-27 10:46:45 +00:00
if ( hc_path_read ( l1_filename ) = = false )
2016-10-03 14:27:34 +00:00
{
2017-02-04 01:53:50 +00:00
event_log_error ( hashcat_ctx , " %s: %s " , l1_filename , strerror ( errno ) ) ;
2016-10-03 14:27:34 +00:00
2017-02-14 14:47:41 +00:00
hcfree ( dictionary_files ) ;
2016-10-03 14:27:34 +00:00
return - 1 ;
}
2017-01-27 10:46:45 +00:00
if ( hc_path_is_file ( l1_filename ) = = true )
2016-10-03 14:27:34 +00:00
{
2019-07-13 05:16:03 +00:00
if ( straight_ctx_add_wl ( hashcat_ctx , l1_filename ) = = - 1 )
2017-02-14 14:47:41 +00:00
{
hcfree ( dictionary_files ) ;
return - 1 ;
}
2016-10-03 14:27:34 +00:00
}
}
}
2016-10-10 09:03:11 +00:00
hcfree ( dictionary_files ) ;
2016-10-03 14:27:34 +00:00
}
else
{
2019-07-13 05:16:03 +00:00
if ( straight_ctx_add_wl ( hashcat_ctx , l0_filename ) = = - 1 ) return - 1 ;
2016-10-03 14:27:34 +00:00
}
}
if ( straight_ctx - > dicts_cnt = = 0 )
{
2016-10-11 08:55:02 +00:00
event_log_error ( hashcat_ctx , " No usable dictionary file found. " ) ;
2016-10-03 14:27:34 +00:00
return - 1 ;
}
}
}
else if ( user_options - > attack_mode = = ATTACK_MODE_COMBI )
{
}
else if ( user_options - > attack_mode = = ATTACK_MODE_BF )
{
}
else if ( user_options - > attack_mode = = ATTACK_MODE_HYBRID1 )
{
for ( int i = 0 ; i < user_options_extra - > hc_workc - 1 ; i + + )
{
char * l0_filename = user_options_extra - > hc_workv [ i ] ;
2017-01-27 10:46:45 +00:00
// at this point we already verified the path actually exist and is readable
2016-10-03 14:27:34 +00:00
2017-01-27 10:46:45 +00:00
if ( hc_path_is_directory ( l0_filename ) = = true )
2016-10-03 14:27:34 +00:00
{
2017-02-11 00:09:58 +00:00
char * * dictionary_files ;
2016-10-03 14:27:34 +00:00
2016-11-20 21:54:52 +00:00
dictionary_files = scan_directory ( l0_filename ) ;
2016-10-03 14:27:34 +00:00
if ( dictionary_files ! = NULL )
{
qsort ( dictionary_files , ( size_t ) count_dictionaries ( dictionary_files ) , sizeof ( char * ) , sort_by_stringptr ) ;
for ( int d = 0 ; dictionary_files [ d ] ! = NULL ; d + + )
{
char * l1_filename = dictionary_files [ d ] ;
2017-01-27 10:46:45 +00:00
if ( hc_path_read ( l1_filename ) = = false )
2016-10-03 14:27:34 +00:00
{
2017-02-04 01:53:50 +00:00
event_log_error ( hashcat_ctx , " %s: %s " , l1_filename , strerror ( errno ) ) ;
2016-10-03 14:27:34 +00:00
2017-02-14 14:47:41 +00:00
hcfree ( dictionary_files ) ;
2016-10-03 14:27:34 +00:00
return - 1 ;
}
2017-01-27 10:46:45 +00:00
if ( hc_path_is_file ( l1_filename ) = = true )
2016-10-03 14:27:34 +00:00
{
2019-07-13 05:16:03 +00:00
if ( straight_ctx_add_wl ( hashcat_ctx , l1_filename ) = = - 1 )
2017-02-14 14:47:41 +00:00
{
hcfree ( dictionary_files ) ;
return - 1 ;
}
2016-10-03 14:27:34 +00:00
}
}
}
2016-10-10 09:03:11 +00:00
hcfree ( dictionary_files ) ;
2016-10-03 14:27:34 +00:00
}
else
{
2019-07-13 05:16:03 +00:00
if ( straight_ctx_add_wl ( hashcat_ctx , l0_filename ) = = - 1 ) return - 1 ;
2016-10-03 14:27:34 +00:00
}
}
if ( straight_ctx - > dicts_cnt = = 0 )
{
2016-10-11 08:55:02 +00:00
event_log_error ( hashcat_ctx , " No usable dictionary file found. " ) ;
2016-10-03 14:27:34 +00:00
return - 1 ;
}
}
else if ( user_options - > attack_mode = = ATTACK_MODE_HYBRID2 )
{
for ( int i = 1 ; i < user_options_extra - > hc_workc ; i + + )
{
char * l0_filename = user_options_extra - > hc_workv [ i ] ;
2017-01-27 10:46:45 +00:00
// at this point we already verified the path actually exist and is readable
2016-10-03 14:27:34 +00:00
2017-01-27 10:46:45 +00:00
if ( hc_path_is_directory ( l0_filename ) = = true )
2016-10-03 14:27:34 +00:00
{
2017-02-11 00:09:58 +00:00
char * * dictionary_files ;
2016-10-03 14:27:34 +00:00
2016-11-20 21:54:52 +00:00
dictionary_files = scan_directory ( l0_filename ) ;
2016-10-03 14:27:34 +00:00
if ( dictionary_files ! = NULL )
{
qsort ( dictionary_files , ( size_t ) count_dictionaries ( dictionary_files ) , sizeof ( char * ) , sort_by_stringptr ) ;
for ( int d = 0 ; dictionary_files [ d ] ! = NULL ; d + + )
{
char * l1_filename = dictionary_files [ d ] ;
2017-01-27 10:46:45 +00:00
if ( hc_path_read ( l1_filename ) = = false )
2016-10-03 14:27:34 +00:00
{
2017-02-04 01:53:50 +00:00
event_log_error ( hashcat_ctx , " %s: %s " , l1_filename , strerror ( errno ) ) ;
2016-10-03 14:27:34 +00:00
2017-02-14 14:47:41 +00:00
hcfree ( dictionary_files ) ;
2016-10-03 14:27:34 +00:00
return - 1 ;
}
2017-01-27 10:46:45 +00:00
if ( hc_path_is_file ( l1_filename ) = = true )
2016-10-03 14:27:34 +00:00
{
2019-07-13 05:16:03 +00:00
if ( straight_ctx_add_wl ( hashcat_ctx , l1_filename ) = = - 1 )
2017-02-14 14:47:41 +00:00
{
hcfree ( dictionary_files ) ;
return - 1 ;
}
2016-10-03 14:27:34 +00:00
}
}
}
2016-10-10 09:03:11 +00:00
hcfree ( dictionary_files ) ;
2016-10-03 14:27:34 +00:00
}
else
{
2019-07-13 05:16:03 +00:00
if ( straight_ctx_add_wl ( hashcat_ctx , l0_filename ) = = - 1 ) return - 1 ;
2016-10-03 14:27:34 +00:00
}
}
if ( straight_ctx - > dicts_cnt = = 0 )
{
2016-10-11 08:55:02 +00:00
event_log_error ( hashcat_ctx , " No usable dictionary file found. " ) ;
2016-10-03 14:27:34 +00:00
return - 1 ;
}
}
2016-09-27 11:13:07 +00:00
return 0 ;
}
2016-10-06 08:21:39 +00:00
void straight_ctx_destroy ( hashcat_ctx_t * hashcat_ctx )
2016-09-27 11:13:07 +00:00
{
2016-10-06 08:21:39 +00:00
straight_ctx_t * straight_ctx = hashcat_ctx - > straight_ctx ;
2016-10-01 22:00:21 +00:00
if ( straight_ctx - > enabled = = false ) return ;
2016-09-27 11:13:07 +00:00
2016-09-29 12:46:51 +00:00
for ( u32 dict_pos = 0 ; dict_pos < straight_ctx - > dicts_cnt ; dict_pos + + )
{
2016-10-10 09:03:11 +00:00
hcfree ( straight_ctx - > dicts [ dict_pos ] ) ;
2016-09-29 12:46:51 +00:00
}
2016-10-10 09:03:11 +00:00
hcfree ( straight_ctx - > dicts ) ;
2016-09-29 12:46:51 +00:00
2016-10-10 09:03:11 +00:00
hcfree ( straight_ctx - > kernel_rules_buf ) ;
2016-09-27 11:13:07 +00:00
2016-10-01 22:00:21 +00:00
memset ( straight_ctx , 0 , sizeof ( straight_ctx_t ) ) ;
2016-09-27 11:13:07 +00:00
}