static int mp_setup_usr (hashcat_ctx_t *hashcat_ctx, cs_t *mp_sys, cs_t *mp_usr, char *buf, u32 index) { FILE *fp = fopen (buf, "rb"); if (fp == NULL) // feof() in case if file is empty { const int rc = mp_expand (hashcat_ctx, buf, strlen (buf), mp_sys, mp_usr, index, 1); if (rc == -1) return -1; } else { char mp_file[1024] = { 0 }; const size_t nread = fread (mp_file, 1, sizeof (mp_file) - 1, fp); if (!feof (fp)) { event_log_error (hashcat_ctx, "%s: Custom charset file is too large", buf); fclose (fp); return -1; } else { fclose (fp); } if (nread == 0) { event_log_error (hashcat_ctx, "%s: Custom charset file is empty", buf); return -1; } const size_t len = in_superchop (mp_file); if (len == 0) { event_log_error (hashcat_ctx, "%s: Custom charset file is corrupted", buf); return -1; } const int rc = mp_expand (hashcat_ctx, mp_file, len, mp_sys, mp_usr, index, 0); if (rc == -1) return -1; } return 0; }
int tuning_db_init (hashcat_ctx_t *hashcat_ctx) { folder_config_t *folder_config = hashcat_ctx->folder_config; tuning_db_t *tuning_db = hashcat_ctx->tuning_db; user_options_t *user_options = hashcat_ctx->user_options; user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra; tuning_db->enabled = false; if (user_options->example_hashes == true) return 0; if (user_options->keyspace == true) return 0; if (user_options->left == true) return 0; if (user_options->opencl_info == true) return 0; if (user_options->show == true) return 0; if (user_options->usage == true) return 0; if (user_options->version == true) return 0; tuning_db->enabled = true; char *tuning_db_file; hc_asprintf (&tuning_db_file, "%s/%s", folder_config->shared_dir, TUNING_DB_FILE); FILE *fp = fopen (tuning_db_file, "rb"); if (fp == NULL) { event_log_error (hashcat_ctx, "%s: %s", tuning_db_file, strerror (errno)); return -1; } hcfree (tuning_db_file); int num_lines = count_lines (fp); // a bit over-allocated tuning_db->alias_buf = (tuning_db_alias_t *) hccalloc (num_lines + 1, sizeof (tuning_db_alias_t)); tuning_db->alias_cnt = 0; tuning_db->entry_buf = (tuning_db_entry_t *) hccalloc (num_lines + 1, sizeof (tuning_db_entry_t)); tuning_db->entry_cnt = 0; rewind (fp); int line_num = 0; char *buf = (char *) hcmalloc (HCBUFSIZ_LARGE); while (!feof (fp)) { char *line_buf = fgets (buf, HCBUFSIZ_LARGE - 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 *saveptr = NULL; char *next = strtok_r (line_buf, "\t ", &saveptr); token_ptr[token_cnt] = next; token_cnt++; while ((next = strtok_r ((char *) NULL, "\t ", &saveptr)) != 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 = hcstrdup (device_name); alias->alias_name = hcstrdup (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] != '*')) { event_log_warning (hashcat_ctx, "Tuning-db: Invalid attack_mode '%c' in Line '%d'", 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')) { event_log_warning (hashcat_ctx, "Tuning-db: Invalid vector_width '%c' in Line '%d'", 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 = strtol (token_ptr[1], NULL, 10); if (token_ptr[2][0] != '*') hash_type = strtol (token_ptr[2], NULL, 10); if (token_ptr[3][0] != 'N') vector_width = strtol (token_ptr[3], NULL, 10); if (token_ptr[4][0] == 'A') { kernel_accel = 0; } else if (token_ptr[4][0] == 'M') { kernel_accel = 1024; } else { kernel_accel = strtol (token_ptr[4], NULL, 10); if ((kernel_accel < 1) || (kernel_accel > 1024)) { event_log_warning (hashcat_ctx, "Tuning-db: Invalid kernel_accel '%d' in Line '%d'", kernel_accel, line_num); continue; } } if (token_ptr[5][0] == 'A') { kernel_loops = 0; } else if (token_ptr[5][0] == 'M') { if (user_options_extra->attack_kern == ATTACK_KERN_STRAIGHT) { kernel_loops = KERNEL_RULES; } else if (user_options_extra->attack_kern == ATTACK_KERN_COMBI) { kernel_loops = KERNEL_COMBS; } else if (user_options_extra->attack_kern == ATTACK_KERN_BF) { kernel_loops = KERNEL_BFS; } } else { kernel_loops = strtol (token_ptr[5], NULL, 10); if (kernel_loops < 1) { event_log_warning (hashcat_ctx, "Tuning-db: Invalid kernel_loops '%d' in Line '%d'", kernel_loops, line_num); continue; } if ((user_options_extra->attack_kern == ATTACK_KERN_STRAIGHT) && (kernel_loops > KERNEL_RULES)) { event_log_warning (hashcat_ctx, "Tuning-db: Invalid kernel_loops '%d' in Line '%d'", kernel_loops, line_num); continue; } if ((user_options_extra->attack_kern == ATTACK_KERN_COMBI) && (kernel_loops > KERNEL_COMBS)) { event_log_warning (hashcat_ctx, "Tuning-db: Invalid kernel_loops '%d' in Line '%d'", kernel_loops, line_num); continue; } if ((user_options_extra->attack_kern == ATTACK_KERN_BF) && (kernel_loops > KERNEL_BFS)) { event_log_warning (hashcat_ctx, "Tuning-db: Invalid kernel_loops '%d' in Line '%d'", kernel_loops, line_num); continue; } } tuning_db_entry_t *entry = &tuning_db->entry_buf[tuning_db->entry_cnt]; entry->device_name = hcstrdup (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 { event_log_warning (hashcat_ctx, "Tuning-db: Invalid number of token in Line '%d'", line_num); continue; } } hcfree (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 0; }