1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-12-20 21:58:11 +00:00
hashcat/tools/rules_test/kernel2cpu_rule_test.c

211 lines
3.8 KiB
C
Raw Normal View History

2015-12-04 14:47:52 +00:00
/**
* Author......: See docs/credits.txt
2015-12-04 14:47:52 +00:00
* License.....: MIT
*/
2016-05-26 10:21:08 +00:00
#define RULES_PER_PLAIN_MIN 1
2015-12-04 14:47:52 +00:00
#define RULES_PER_PLAIN_MAX 99
2016-05-26 10:21:08 +00:00
#define RP_GEN_FUNC_MIN 1
#define RP_GEN_FUNC_MAX 4
2015-12-04 14:47:52 +00:00
#define PW_MAX 32
#define LINE_SIG_LEN RP_GEN_FUNC_MAX * 2 + 1
2016-05-26 10:21:08 +00:00
2015-12-04 14:47:52 +00:00
int max_len = 0;
#include "cpu_rules.h"
2016-01-15 16:16:43 +00:00
#include "rp_kernel_on_cpu.h"
2015-12-04 14:47:52 +00:00
void print_plain (char *plain, int plain_len)
{
int need_hexifly = 0;
2016-01-15 16:16:43 +00:00
u8 *plain_ptr = (u8*) plain;
2015-12-04 14:47:52 +00:00
int k;
for (k = 0; k < plain_len; k++)
{
if ((plain_ptr[k] < 0x20) || (plain_ptr[k] > 0x7f))
{
need_hexifly = 1;
break;
}
}
if (need_hexifly)
{
printf ("$HEX[");
for (k = 0; k < plain_len; k++)
{
printf ("%02x", plain_ptr[k]);
}
printf ("]");
}
else
{
for (k = 0; k < plain_len; k++)
{
printf ("%c", plain_ptr[k]);
}
}
}
int main (int argc, char **argv)
{
FILE *fp = stdin;
2016-09-08 10:17:56 +00:00
char *rule_buf = (char *) malloc (HCBUFSIZ_LARGE);
2016-09-08 10:17:56 +00:00
char *line_buf = (char *) malloc (HCBUFSIZ_LARGE);
2015-12-04 14:47:52 +00:00
int rp_gen_func_min = RP_GEN_FUNC_MIN;
int rp_gen_func_max = RP_GEN_FUNC_MAX;
while (1)
{
/*
* line
*/
if (feof (fp)) break;
2016-09-08 10:17:56 +00:00
char *line_ptr = fgets (line_buf, HCBUFSIZ_LARGE - 1, fp);
2015-12-04 14:47:52 +00:00
if (line_ptr == NULL) continue;
int line_len = strlen (line_ptr);
line_len--;
if (line_len < 0) continue;
if (line_len > PW_MAX) continue;
memset (line_ptr + line_len, 0, PW_MAX - line_len);
/*
* generate random rule and apply it afterwards
*/
2016-10-04 09:22:08 +00:00
u32 max;
2015-12-04 14:47:52 +00:00
if (argc < 2)
{
max = get_random_num (RULES_PER_PLAIN_MIN, RULES_PER_PLAIN_MAX);
}
else
{
max = 1;
}
2016-10-04 09:22:08 +00:00
u32 i;
2015-12-04 14:47:52 +00:00
for (i = 0; i < max; i++)
{
int rule_len;
memset (rule_buf, 0, BLOCK_SIZE);
if (argc < 2)
{
rule_len = (int) generate_random_rule (rule_buf, rp_gen_func_min, rp_gen_func_max);
}
else
{
2016-09-08 10:17:56 +00:00
strncpy (rule_buf, argv[1], HCBUFSIZ_LARGE - 1);
2015-12-04 14:47:52 +00:00
rule_len = strlen (rule_buf);
}
2016-01-15 16:16:43 +00:00
kernel_rule_t kernel_rule_buf;
2015-12-04 14:47:52 +00:00
2016-01-15 16:16:43 +00:00
memset (&kernel_rule_buf, 0, sizeof (kernel_rule_t));
2015-12-04 14:47:52 +00:00
2016-01-15 16:16:43 +00:00
if (cpu_rule_to_kernel_rule (rule_buf, rule_len, &kernel_rule_buf) == -1) continue;
2015-12-04 14:47:52 +00:00
// cpu
char rule_buf_cpu[BLOCK_SIZE];
memset (rule_buf_cpu, 0, BLOCK_SIZE);
max_len = 0;
int out_len_cpu = apply_rule_cpu (rule_buf, rule_len, line_ptr, line_len, rule_buf_cpu);
if (max_len >= 32) continue;
// gpu
2016-01-15 16:16:43 +00:00
char rule_buf_kernel[BLOCK_SIZE];
2015-12-04 14:47:52 +00:00
2016-01-15 16:16:43 +00:00
memset (rule_buf_kernel, 0, sizeof (rule_buf_kernel));
2015-12-04 14:47:52 +00:00
2016-01-15 16:16:43 +00:00
memcpy (rule_buf_kernel, line_buf, line_len);
2015-12-04 14:47:52 +00:00
2016-01-15 16:16:43 +00:00
u32 *plain_ptr = (u32 *) rule_buf_kernel;
2015-12-04 14:47:52 +00:00
2016-01-15 16:16:43 +00:00
int out_len_kernel = apply_rules (kernel_rule_buf.cmds, &plain_ptr[0], &plain_ptr[4], line_len);
2015-12-04 14:47:52 +00:00
/*
* compare
*/
if (out_len_cpu >= 0 && out_len_cpu < 32)
{
int failed = 1;
2016-01-15 16:16:43 +00:00
if (out_len_kernel == out_len_cpu)
2015-12-04 14:47:52 +00:00
{
2016-01-15 16:16:43 +00:00
if (memcmp (rule_buf_kernel, rule_buf_cpu, out_len_kernel) == 0)
2015-12-04 14:47:52 +00:00
{
failed = 0;
}
}
/*
* print if failed
*/
if (failed == 1)
{
printf ("Rule: %s", rule_buf);
// nicer output
int spaces = LINE_SIG_LEN - rule_len;
if (rule_len > 10) spaces++;
if (rule_len > 100) spaces++;
while (spaces--) printf (".");
printf (": ");
// initial line
print_plain (line_buf, line_len);
printf (" %i => ", line_len);
// modified by cpu
print_plain (rule_buf_cpu, out_len_cpu);
printf (" %i vs ", out_len_cpu);
// modified by gpu
2016-01-15 16:16:43 +00:00
print_plain (rule_buf_kernel, out_len_kernel);
2015-12-04 14:47:52 +00:00
2016-01-15 16:16:43 +00:00
printf (" %i\n", out_len_kernel);
2015-12-04 14:47:52 +00:00
}
}
}
}
fclose (fp);
free (line_buf);
free (rule_buf);
2015-12-04 14:47:52 +00:00
return 0;
}