1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-12-23 07:08:19 +00:00

Created build_plain(), build_crackpos() and build_debugdata() out of code from check_hash()

This commit is contained in:
jsteube 2016-10-04 16:18:49 +02:00
parent 3ac0737b19
commit 6d3277ab17
2 changed files with 257 additions and 204 deletions

View File

@ -11,6 +11,10 @@ int sort_by_salt (const void *v1, const void *v2);
int sort_by_hash (const void *v1, const void *v2, void *v3); int sort_by_hash (const void *v1, const void *v2, void *v3);
int sort_by_hash_no_salt (const void *v1, const void *v2, void *v3); int sort_by_hash_no_salt (const void *v1, const void *v2, void *v3);
void build_plain (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain, u32 *plain_buf, int *out_len);
void build_crackpos (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain, u64 *out_pos);
void build_debugdata (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain, u8 *debug_rule_buf, int *debug_rule_len, u8 *debug_plain_ptr, int *debug_plain_len);
void save_hash (const user_options_t *user_options, const hashconfig_t *hashconfig, const hashes_t *hashes); void save_hash (const user_options_t *user_options, const hashconfig_t *hashconfig, const hashes_t *hashes);
void check_hash (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain); void check_hash (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain);

View File

@ -117,6 +117,249 @@ int sort_by_hash_no_salt (const void *v1, const void *v2, void *v3)
return sort_by_digest_p0p1 (d1, d2, v3); return sort_by_digest_p0p1 (d1, d2, v3);
} }
void build_plain (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain, u32 *plain_buf, int *out_len)
{
combinator_ctx_t *combinator_ctx = hashcat_ctx->combinator_ctx;
hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
hashes_t *hashes = hashcat_ctx->hashes;
mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
opencl_ctx_t *opencl_ctx = hashcat_ctx->opencl_ctx;
straight_ctx_t *straight_ctx = hashcat_ctx->straight_ctx;
user_options_t *user_options = hashcat_ctx->user_options;
const u32 gidvid = plain->gidvid;
const u32 il_pos = plain->il_pos;
int plain_len = 0;
u8 *plain_ptr = (u8 *) plain_buf;
if (user_options->attack_mode == ATTACK_MODE_STRAIGHT)
{
pw_t pw;
gidd_to_pw_t (opencl_ctx, device_param, gidvid, &pw);
for (int i = 0; i < 16; i++)
{
plain_buf[i] = pw.i[i];
}
plain_len = (int) pw.pw_len;
const u32 off = device_param->innerloop_pos + il_pos;
plain_len = (int) apply_rules (straight_ctx->kernel_rules_buf[off].cmds, &plain_buf[0], &plain_buf[4], (u32) plain_len);
if (plain_len > (int) hashconfig->pw_max) plain_len = (int) hashconfig->pw_max;
}
else if (user_options->attack_mode == ATTACK_MODE_COMBI)
{
pw_t pw;
gidd_to_pw_t (opencl_ctx, device_param, gidvid, &pw);
for (int i = 0; i < 16; i++)
{
plain_buf[i] = pw.i[i];
}
plain_len = (int) pw.pw_len;
char *comb_buf = (char *) device_param->combs_buf[il_pos].i;
u32 comb_len = device_param->combs_buf[il_pos].pw_len;
if (combinator_ctx->combs_mode == COMBINATOR_MODE_BASE_LEFT)
{
memcpy (plain_ptr + plain_len, comb_buf, (size_t) comb_len);
}
else
{
memmove (plain_ptr + comb_len, plain_ptr, (size_t) plain_len);
memcpy (plain_ptr, comb_buf, comb_len);
}
plain_len += comb_len;
if (hashconfig->pw_max != PW_DICTMAX1)
{
if (plain_len > (int) hashconfig->pw_max) plain_len = (int) hashconfig->pw_max;
}
}
else if (user_options->attack_mode == ATTACK_MODE_BF)
{
u64 l_off = device_param->kernel_params_mp_l_buf64[3] + gidvid;
u64 r_off = device_param->kernel_params_mp_r_buf64[3] + il_pos;
u32 l_start = device_param->kernel_params_mp_l_buf32[5];
u32 r_start = device_param->kernel_params_mp_r_buf32[5];
u32 l_stop = device_param->kernel_params_mp_l_buf32[4];
u32 r_stop = device_param->kernel_params_mp_r_buf32[4];
sp_exec (l_off, (char *) plain_ptr + l_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, l_start, l_start + l_stop);
sp_exec (r_off, (char *) plain_ptr + r_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, r_start, r_start + r_stop);
plain_len = (int) mask_ctx->css_cnt;
}
else if (user_options->attack_mode == ATTACK_MODE_HYBRID1)
{
pw_t pw;
gidd_to_pw_t (opencl_ctx, device_param, gidvid, &pw);
for (int i = 0; i < 16; i++)
{
plain_buf[i] = pw.i[i];
}
plain_len = (int) pw.pw_len;
u64 off = device_param->kernel_params_mp_buf64[3] + il_pos;
u32 start = 0;
u32 stop = device_param->kernel_params_mp_buf32[4];
sp_exec (off, (char *) plain_ptr + plain_len, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, start, start + stop);
plain_len += start + stop;
if (hashconfig->pw_max != PW_DICTMAX1)
{
if (plain_len > (int) hashconfig->pw_max) plain_len = (int) hashconfig->pw_max;
}
}
else if (user_options->attack_mode == ATTACK_MODE_HYBRID2)
{
pw_t pw;
gidd_to_pw_t (opencl_ctx, device_param, gidvid, &pw);
for (int i = 0; i < 16; i++)
{
plain_buf[i] = pw.i[i];
}
plain_len = (int) pw.pw_len;
u64 off = device_param->kernel_params_mp_buf64[3] + il_pos;
u32 start = 0;
u32 stop = device_param->kernel_params_mp_buf32[4];
memmove (plain_ptr + stop, plain_ptr, plain_len);
sp_exec (off, (char *) plain_ptr, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, start, start + stop);
plain_len += start + stop;
if (hashconfig->pw_max != PW_DICTMAX1)
{
if (plain_len > (int) hashconfig->pw_max) plain_len = (int) hashconfig->pw_max;
}
}
if (user_options->attack_mode == ATTACK_MODE_BF)
{
if (hashconfig->opti_type & OPTI_TYPE_BRUTE_FORCE) // lots of optimizations can happen here
{
if (hashconfig->opti_type & OPTI_TYPE_SINGLE_HASH)
{
if (hashconfig->opti_type & OPTI_TYPE_APPENDED_SALT)
{
plain_len = plain_len - hashes->salts_buf[0].salt_len;
}
}
if (hashconfig->opts_type & OPTS_TYPE_PT_UNICODE)
{
for (int i = 0, j = 0; i < plain_len; i += 2, j += 1)
{
plain_ptr[j] = plain_ptr[i];
}
plain_len = plain_len / 2;
}
}
}
*out_len = plain_len;
}
void build_crackpos (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain, u64 *out_pos)
{
combinator_ctx_t *combinator_ctx = hashcat_ctx->combinator_ctx;
mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
straight_ctx_t *straight_ctx = hashcat_ctx->straight_ctx;
user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
const u32 gidvid = plain->gidvid;
const u32 il_pos = plain->il_pos;
u64 crackpos = device_param->words_off;
if (user_options_extra->attack_kern == ATTACK_KERN_STRAIGHT)
{
crackpos += gidvid;
crackpos *= straight_ctx->kernel_rules_cnt;
crackpos += device_param->innerloop_pos + il_pos;
}
else if (user_options_extra->attack_kern == ATTACK_KERN_COMBI)
{
crackpos += gidvid;
crackpos *= combinator_ctx->combs_cnt;
crackpos += device_param->innerloop_pos + il_pos;
}
else if (user_options_extra->attack_kern == ATTACK_MODE_BF)
{
crackpos += gidvid;
crackpos *= mask_ctx->bfs_cnt;
crackpos += device_param->innerloop_pos + il_pos;
}
*out_pos = crackpos;
}
void build_debugdata (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain, u8 *debug_rule_buf, int *debug_rule_len, u8 *debug_plain_ptr, int *debug_plain_len)
{
debugfile_ctx_t *debugfile_ctx = hashcat_ctx->debugfile_ctx;
opencl_ctx_t *opencl_ctx = hashcat_ctx->opencl_ctx;
straight_ctx_t *straight_ctx = hashcat_ctx->straight_ctx;
user_options_t *user_options = hashcat_ctx->user_options;
const u32 gidvid = plain->gidvid;
const u32 il_pos = plain->il_pos;
if (user_options->attack_mode != ATTACK_MODE_STRAIGHT) return;
const u32 debug_mode = debugfile_ctx->mode;
if (debug_mode == 0) return;
pw_t pw;
gidd_to_pw_t (opencl_ctx, device_param, gidvid, &pw);
int plain_len = (int) pw.pw_len;
const u32 off = device_param->innerloop_pos + il_pos;
// save rule
if ((debug_mode == 1) || (debug_mode == 3) || (debug_mode == 4))
{
*debug_rule_len = kernel_rule_to_cpu_rule ((char *) debug_rule_buf, &straight_ctx->kernel_rules_buf[off]);
}
// save plain
if ((debug_mode == 2) || (debug_mode == 3) || (debug_mode == 4))
{
memcpy (debug_plain_ptr, (char *) pw.i, (size_t) plain_len);
*debug_plain_len = plain_len;
}
}
void save_hash (const user_options_t *user_options, const hashconfig_t *hashconfig, const hashes_t *hashes) void save_hash (const user_options_t *user_options, const hashconfig_t *hashconfig, const hashes_t *hashes)
{ {
char *hashfile = hashes->hashfile; char *hashfile = hashes->hashfile;
@ -213,32 +456,18 @@ void save_hash (const user_options_t *user_options, const hashconfig_t *hashconf
void check_hash (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain) void check_hash (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, plain_t *plain)
{ {
combinator_ctx_t *combinator_ctx = hashcat_ctx->combinator_ctx;
debugfile_ctx_t *debugfile_ctx = hashcat_ctx->debugfile_ctx; debugfile_ctx_t *debugfile_ctx = hashcat_ctx->debugfile_ctx;
hashconfig_t *hashconfig = hashcat_ctx->hashconfig; hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
hashes_t *hashes = hashcat_ctx->hashes; hashes_t *hashes = hashcat_ctx->hashes;
loopback_ctx_t *loopback_ctx = hashcat_ctx->loopback_ctx; loopback_ctx_t *loopback_ctx = hashcat_ctx->loopback_ctx;
mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
opencl_ctx_t *opencl_ctx = hashcat_ctx->opencl_ctx;
outfile_ctx_t *outfile_ctx = hashcat_ctx->outfile_ctx; outfile_ctx_t *outfile_ctx = hashcat_ctx->outfile_ctx;
potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx;
status_ctx_t *status_ctx = hashcat_ctx->status_ctx; status_ctx_t *status_ctx = hashcat_ctx->status_ctx;
straight_ctx_t *straight_ctx = hashcat_ctx->straight_ctx;
user_options_t *user_options = hashcat_ctx->user_options; user_options_t *user_options = hashcat_ctx->user_options;
user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra; user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
const u32 salt_pos = plain->salt_pos; const u32 salt_pos = plain->salt_pos;
const u32 digest_pos = plain->digest_pos; // relative const u32 digest_pos = plain->digest_pos; // relative
const u32 gidvid = plain->gidvid;
const u32 il_pos = plain->il_pos;
// debugfile
u8 debug_rule_buf[BLOCK_SIZE] = { 0 };
int debug_rule_len = 0; // -1 error
u8 debug_plain_ptr[BLOCK_SIZE] = { 0 };
int debug_plain_len = 0;
// hash // hash
@ -248,207 +477,28 @@ void check_hash (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, pl
// plain // plain
u64 crackpos = device_param->words_off;
u32 plain_buf[16] = { 0 }; u32 plain_buf[16] = { 0 };
u8 *plain_ptr = (u8 *) plain_buf; u8 *plain_ptr = (u8 *) plain_buf;
int plain_len = 0; int plain_len = 0;
if (user_options->attack_mode == ATTACK_MODE_STRAIGHT) build_plain (hashcat_ctx, device_param, plain, plain_buf, &plain_len);
{
pw_t pw;
gidd_to_pw_t (opencl_ctx, device_param, gidvid, &pw); // crackpos
for (int i = 0; i < 16; i++) u64 crackpos = 0;
{
plain_buf[i] = pw.i[i];
}
plain_len = (int) pw.pw_len; build_crackpos (hashcat_ctx, device_param, plain, &crackpos);
const u32 off = device_param->innerloop_pos + il_pos; // debug
const u32 debug_mode = debugfile_ctx->mode; u8 debug_rule_buf[BLOCK_SIZE] = { 0 };
int debug_rule_len = 0; // -1 error
if (debug_mode > 0) u8 debug_plain_ptr[BLOCK_SIZE] = { 0 };
{ int debug_plain_len = 0;
debug_rule_len = 0;
// save rule build_debugdata (hashcat_ctx, device_param, plain, debug_rule_buf, &debug_rule_len, debug_plain_ptr, &debug_plain_len);
if ((debug_mode == 1) || (debug_mode == 3) || (debug_mode == 4))
{
memset (debug_rule_buf, 0, sizeof (debug_rule_buf));
debug_rule_len = kernel_rule_to_cpu_rule ((char *) debug_rule_buf, &straight_ctx->kernel_rules_buf[off]);
}
// save plain
if ((debug_mode == 2) || (debug_mode == 3) || (debug_mode == 4))
{
memset (debug_plain_ptr, 0, sizeof (debug_plain_ptr));
memcpy (debug_plain_ptr, plain_ptr, (size_t) plain_len);
debug_plain_len = plain_len;
}
}
plain_len = (int) apply_rules (straight_ctx->kernel_rules_buf[off].cmds, &plain_buf[0], &plain_buf[4], (u32) plain_len);
crackpos += gidvid;
crackpos *= straight_ctx->kernel_rules_cnt;
crackpos += device_param->innerloop_pos + il_pos;
if (plain_len > (int) hashconfig->pw_max) plain_len = (int) hashconfig->pw_max;
}
else if (user_options->attack_mode == ATTACK_MODE_COMBI)
{
pw_t pw;
gidd_to_pw_t (opencl_ctx, device_param, gidvid, &pw);
for (int i = 0; i < 16; i++)
{
plain_buf[i] = pw.i[i];
}
plain_len = (int) pw.pw_len;
char *comb_buf = (char *) device_param->combs_buf[il_pos].i;
u32 comb_len = device_param->combs_buf[il_pos].pw_len;
if (combinator_ctx->combs_mode == COMBINATOR_MODE_BASE_LEFT)
{
memcpy (plain_ptr + plain_len, comb_buf, (size_t) comb_len);
}
else
{
memmove (plain_ptr + comb_len, plain_ptr, (size_t) plain_len);
memcpy (plain_ptr, comb_buf, comb_len);
}
plain_len += comb_len;
crackpos += gidvid;
crackpos *= combinator_ctx->combs_cnt;
crackpos += device_param->innerloop_pos + il_pos;
if (hashconfig->pw_max != PW_DICTMAX1)
{
if (plain_len > (int) hashconfig->pw_max) plain_len = (int) hashconfig->pw_max;
}
}
else if (user_options->attack_mode == ATTACK_MODE_BF)
{
u64 l_off = device_param->kernel_params_mp_l_buf64[3] + gidvid;
u64 r_off = device_param->kernel_params_mp_r_buf64[3] + il_pos;
u32 l_start = device_param->kernel_params_mp_l_buf32[5];
u32 r_start = device_param->kernel_params_mp_r_buf32[5];
u32 l_stop = device_param->kernel_params_mp_l_buf32[4];
u32 r_stop = device_param->kernel_params_mp_r_buf32[4];
sp_exec (l_off, (char *) plain_ptr + l_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, l_start, l_start + l_stop);
sp_exec (r_off, (char *) plain_ptr + r_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, r_start, r_start + r_stop);
plain_len = (int) mask_ctx->css_cnt;
crackpos += gidvid;
crackpos *= mask_ctx->bfs_cnt;
crackpos += device_param->innerloop_pos + il_pos;
}
else if (user_options->attack_mode == ATTACK_MODE_HYBRID1)
{
pw_t pw;
gidd_to_pw_t (opencl_ctx, device_param, gidvid, &pw);
for (int i = 0; i < 16; i++)
{
plain_buf[i] = pw.i[i];
}
plain_len = (int) pw.pw_len;
u64 off = device_param->kernel_params_mp_buf64[3] + il_pos;
u32 start = 0;
u32 stop = device_param->kernel_params_mp_buf32[4];
sp_exec (off, (char *) plain_ptr + plain_len, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, start, start + stop);
plain_len += start + stop;
crackpos += gidvid;
crackpos *= combinator_ctx->combs_cnt;
crackpos += device_param->innerloop_pos + il_pos;
if (hashconfig->pw_max != PW_DICTMAX1)
{
if (plain_len > (int) hashconfig->pw_max) plain_len = (int) hashconfig->pw_max;
}
}
else if (user_options->attack_mode == ATTACK_MODE_HYBRID2)
{
pw_t pw;
gidd_to_pw_t (opencl_ctx, device_param, gidvid, &pw);
for (int i = 0; i < 16; i++)
{
plain_buf[i] = pw.i[i];
}
plain_len = (int) pw.pw_len;
u64 off = device_param->kernel_params_mp_buf64[3] + il_pos;
u32 start = 0;
u32 stop = device_param->kernel_params_mp_buf32[4];
memmove (plain_ptr + stop, plain_ptr, plain_len);
sp_exec (off, (char *) plain_ptr, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, start, start + stop);
plain_len += start + stop;
crackpos += gidvid;
crackpos *= combinator_ctx->combs_cnt;
crackpos += device_param->innerloop_pos + il_pos;
if (hashconfig->pw_max != PW_DICTMAX1)
{
if (plain_len > (int) hashconfig->pw_max) plain_len = (int) hashconfig->pw_max;
}
}
if (user_options->attack_mode == ATTACK_MODE_BF)
{
if (hashconfig->opti_type & OPTI_TYPE_BRUTE_FORCE) // lots of optimizations can happen here
{
if (hashconfig->opti_type & OPTI_TYPE_SINGLE_HASH)
{
if (hashconfig->opti_type & OPTI_TYPE_APPENDED_SALT)
{
plain_len = plain_len - hashes->salts_buf[0].salt_len;
}
}
if (hashconfig->opts_type & OPTS_TYPE_PT_UNICODE)
{
for (int i = 0, j = 0; i < plain_len; i += 2, j += 1)
{
plain_ptr[j] = plain_ptr[i];
}
plain_len = plain_len / 2;
}
}
}
// no need for locking, we're in a mutex protected function // no need for locking, we're in a mutex protected function
@ -458,7 +508,6 @@ void check_hash (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, pl
// if an error occurs opening the file, send to stdout as fallback // if an error occurs opening the file, send to stdout as fallback
// the fp gets opened for each cracked hash so that the user can modify (move) the outfile while hashcat runs // the fp gets opened for each cracked hash so that the user can modify (move) the outfile while hashcat runs
outfile_write_open (outfile_ctx); outfile_write_open (outfile_ctx);
if (outfile_ctx->filename == NULL) if (user_options->quiet == false) clear_prompt (); if (outfile_ctx->filename == NULL) if (user_options->quiet == false) clear_prompt ();