mirror of
https://github.com/hashcat/hashcat.git
synced 2024-11-12 18:59:03 +00:00
350 lines
8.2 KiB
C
350 lines
8.2 KiB
C
/**
|
|
* Author......: Jens Steube <jens.steube@gmail.com>
|
|
* License.....: MIT
|
|
*/
|
|
|
|
#include "common.h"
|
|
#include "types_int.h"
|
|
#include "types.h"
|
|
#include "timer.h"
|
|
#include "logging.h"
|
|
#include "memory.h"
|
|
#include "filehandling.h"
|
|
#include "ext_OpenCL.h"
|
|
#include "tuningdb.h"
|
|
|
|
static int sort_by_tuning_db_alias (const void *v1, const void *v2)
|
|
{
|
|
const tuning_db_alias_t *t1 = (const tuning_db_alias_t *) v1;
|
|
const tuning_db_alias_t *t2 = (const tuning_db_alias_t *) v2;
|
|
|
|
const int res1 = strcmp (t1->device_name, t2->device_name);
|
|
|
|
if (res1 != 0) return (res1);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int sort_by_tuning_db_entry (const void *v1, const void *v2)
|
|
{
|
|
const tuning_db_entry_t *t1 = (const tuning_db_entry_t *) v1;
|
|
const tuning_db_entry_t *t2 = (const tuning_db_entry_t *) v2;
|
|
|
|
const int res1 = strcmp (t1->device_name, t2->device_name);
|
|
|
|
if (res1 != 0) return (res1);
|
|
|
|
const int res2 = t1->attack_mode
|
|
- t2->attack_mode;
|
|
|
|
if (res2 != 0) return (res2);
|
|
|
|
const int res3 = t1->hash_type
|
|
- t2->hash_type;
|
|
|
|
if (res3 != 0) return (res3);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void tuning_db_destroy (tuning_db_t *tuning_db)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < tuning_db->alias_cnt; i++)
|
|
{
|
|
tuning_db_alias_t *alias = &tuning_db->alias_buf[i];
|
|
|
|
myfree (alias->device_name);
|
|
myfree (alias->alias_name);
|
|
}
|
|
|
|
for (i = 0; i < tuning_db->entry_cnt; i++)
|
|
{
|
|
tuning_db_entry_t *entry = &tuning_db->entry_buf[i];
|
|
|
|
myfree (entry->device_name);
|
|
}
|
|
|
|
myfree (tuning_db->alias_buf);
|
|
myfree (tuning_db->entry_buf);
|
|
|
|
myfree (tuning_db);
|
|
}
|
|
|
|
tuning_db_t *tuning_db_alloc (FILE *fp)
|
|
{
|
|
tuning_db_t *tuning_db = (tuning_db_t *) mymalloc (sizeof (tuning_db_t));
|
|
|
|
int num_lines = count_lines (fp);
|
|
|
|
// a bit over-allocated
|
|
|
|
tuning_db->alias_buf = (tuning_db_alias_t *) mycalloc (num_lines + 1, sizeof (tuning_db_alias_t));
|
|
tuning_db->alias_cnt = 0;
|
|
|
|
tuning_db->entry_buf = (tuning_db_entry_t *) mycalloc (num_lines + 1, sizeof (tuning_db_entry_t));
|
|
tuning_db->entry_cnt = 0;
|
|
|
|
return tuning_db;
|
|
}
|
|
|
|
tuning_db_t *tuning_db_init (const char *tuning_db_file)
|
|
{
|
|
FILE *fp = fopen (tuning_db_file, "rb");
|
|
|
|
if (fp == NULL)
|
|
{
|
|
log_error ("%s: %s", tuning_db_file, strerror (errno));
|
|
|
|
exit (-1);
|
|
}
|
|
|
|
tuning_db_t *tuning_db = tuning_db_alloc (fp);
|
|
|
|
rewind (fp);
|
|
|
|
int line_num = 0;
|
|
|
|
char *buf = (char *) mymalloc (HCBUFSIZ);
|
|
|
|
while (!feof (fp))
|
|
{
|
|
char *line_buf = fgets (buf, HCBUFSIZ - 1, fp);
|
|
|
|
if (line_buf == NULL) break;
|
|
|
|
line_num++;
|
|
|
|
const int line_len = in_superchop (line_buf);
|
|
|
|
if (line_len == 0) continue;
|
|
|
|
if (line_buf[0] == '#') continue;
|
|
|
|
// start processing
|
|
|
|
char *token_ptr[7] = { NULL };
|
|
|
|
int token_cnt = 0;
|
|
|
|
char *next = strtok (line_buf, "\t ");
|
|
|
|
token_ptr[token_cnt] = next;
|
|
|
|
token_cnt++;
|
|
|
|
while ((next = strtok (NULL, "\t ")) != NULL)
|
|
{
|
|
token_ptr[token_cnt] = next;
|
|
|
|
token_cnt++;
|
|
}
|
|
|
|
if (token_cnt == 2)
|
|
{
|
|
char *device_name = token_ptr[0];
|
|
char *alias_name = token_ptr[1];
|
|
|
|
tuning_db_alias_t *alias = &tuning_db->alias_buf[tuning_db->alias_cnt];
|
|
|
|
alias->device_name = mystrdup (device_name);
|
|
alias->alias_name = mystrdup (alias_name);
|
|
|
|
tuning_db->alias_cnt++;
|
|
}
|
|
else if (token_cnt == 6)
|
|
{
|
|
if ((token_ptr[1][0] != '0') &&
|
|
(token_ptr[1][0] != '1') &&
|
|
(token_ptr[1][0] != '3') &&
|
|
(token_ptr[1][0] != '*'))
|
|
{
|
|
log_info ("WARNING: Tuning-db: Invalid attack_mode '%c' in Line '%u'", token_ptr[1][0], line_num);
|
|
|
|
continue;
|
|
}
|
|
|
|
if ((token_ptr[3][0] != '1') &&
|
|
(token_ptr[3][0] != '2') &&
|
|
(token_ptr[3][0] != '4') &&
|
|
(token_ptr[3][0] != '8') &&
|
|
(token_ptr[3][0] != 'N'))
|
|
{
|
|
log_info ("WARNING: Tuning-db: Invalid vector_width '%c' in Line '%u'", token_ptr[3][0], line_num);
|
|
|
|
continue;
|
|
}
|
|
|
|
char *device_name = token_ptr[0];
|
|
|
|
int attack_mode = -1;
|
|
int hash_type = -1;
|
|
int vector_width = -1;
|
|
int kernel_accel = -1;
|
|
int kernel_loops = -1;
|
|
|
|
if (token_ptr[1][0] != '*') attack_mode = atoi (token_ptr[1]);
|
|
if (token_ptr[2][0] != '*') hash_type = atoi (token_ptr[2]);
|
|
if (token_ptr[3][0] != 'N') vector_width = atoi (token_ptr[3]);
|
|
|
|
if (token_ptr[4][0] != 'A')
|
|
{
|
|
kernel_accel = atoi (token_ptr[4]);
|
|
|
|
if ((kernel_accel < 1) || (kernel_accel > 1024))
|
|
{
|
|
log_info ("WARNING: Tuning-db: Invalid kernel_accel '%d' in Line '%u'", kernel_accel, line_num);
|
|
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
kernel_accel = 0;
|
|
}
|
|
|
|
if (token_ptr[5][0] != 'A')
|
|
{
|
|
kernel_loops = atoi (token_ptr[5]);
|
|
|
|
if ((kernel_loops < 1) || (kernel_loops > 1024))
|
|
{
|
|
log_info ("WARNING: Tuning-db: Invalid kernel_loops '%d' in Line '%u'", kernel_loops, line_num);
|
|
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
kernel_loops = 0;
|
|
}
|
|
|
|
tuning_db_entry_t *entry = &tuning_db->entry_buf[tuning_db->entry_cnt];
|
|
|
|
entry->device_name = mystrdup (device_name);
|
|
entry->attack_mode = attack_mode;
|
|
entry->hash_type = hash_type;
|
|
entry->vector_width = vector_width;
|
|
entry->kernel_accel = kernel_accel;
|
|
entry->kernel_loops = kernel_loops;
|
|
|
|
tuning_db->entry_cnt++;
|
|
}
|
|
else
|
|
{
|
|
log_info ("WARNING: Tuning-db: Invalid number of token in Line '%u'", line_num);
|
|
|
|
continue;
|
|
}
|
|
}
|
|
|
|
myfree (buf);
|
|
|
|
fclose (fp);
|
|
|
|
// todo: print loaded 'cnt' message
|
|
|
|
// sort the database
|
|
|
|
qsort (tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias);
|
|
qsort (tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
|
|
|
|
return tuning_db;
|
|
}
|
|
|
|
tuning_db_entry_t *tuning_db_search (tuning_db_t *tuning_db, hc_device_param_t *device_param, int attack_mode, int hash_type)
|
|
{
|
|
static tuning_db_entry_t s;
|
|
|
|
// first we need to convert all spaces in the device_name to underscore
|
|
|
|
char *device_name_nospace = mystrdup (device_param->device_name);
|
|
|
|
int device_name_length = strlen (device_name_nospace);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < device_name_length; i++)
|
|
{
|
|
if (device_name_nospace[i] == ' ') device_name_nospace[i] = '_';
|
|
}
|
|
|
|
// find out if there's an alias configured
|
|
|
|
tuning_db_alias_t a;
|
|
|
|
a.device_name = device_name_nospace;
|
|
|
|
tuning_db_alias_t *alias = bsearch (&a, tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias);
|
|
|
|
char *alias_name = (alias == NULL) ? NULL : alias->alias_name;
|
|
|
|
// attack-mode 6 and 7 are attack-mode 1 basically
|
|
|
|
if (attack_mode == 6) attack_mode = 1;
|
|
if (attack_mode == 7) attack_mode = 1;
|
|
|
|
// bsearch is not ideal but fast enough
|
|
|
|
s.device_name = device_name_nospace;
|
|
s.attack_mode = attack_mode;
|
|
s.hash_type = hash_type;
|
|
|
|
tuning_db_entry_t *entry = NULL;
|
|
|
|
// this will produce all 2^3 combinations required
|
|
|
|
for (i = 0; i < 8; i++)
|
|
{
|
|
s.device_name = (i & 1) ? "*" : device_name_nospace;
|
|
s.attack_mode = (i & 2) ? -1 : attack_mode;
|
|
s.hash_type = (i & 4) ? -1 : hash_type;
|
|
|
|
entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
|
|
|
|
if (entry != NULL) break;
|
|
|
|
// in non-wildcard mode do some additional checks:
|
|
|
|
if ((i & 1) == 0)
|
|
{
|
|
// in case we have an alias-name
|
|
|
|
if (alias_name != NULL)
|
|
{
|
|
s.device_name = alias_name;
|
|
|
|
entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
|
|
|
|
if (entry != NULL) break;
|
|
}
|
|
|
|
// or by device type
|
|
|
|
if (device_param->device_type & CL_DEVICE_TYPE_CPU)
|
|
{
|
|
s.device_name = "DEVICE_TYPE_CPU";
|
|
}
|
|
else if (device_param->device_type & CL_DEVICE_TYPE_GPU)
|
|
{
|
|
s.device_name = "DEVICE_TYPE_GPU";
|
|
}
|
|
else if (device_param->device_type & CL_DEVICE_TYPE_ACCELERATOR)
|
|
{
|
|
s.device_name = "DEVICE_TYPE_ACCELERATOR";
|
|
}
|
|
|
|
entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
|
|
|
|
if (entry != NULL) break;
|
|
}
|
|
}
|
|
|
|
// free converted device_name
|
|
|
|
myfree (device_name_nospace);
|
|
|
|
return entry;
|
|
}
|