diff --git a/src/Makefile b/src/Makefile index c468b31..2082d9c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -144,7 +144,8 @@ JOHN_OBJS = \ unshadow.o \ unafs.o \ undrop.o \ - unique.o + unique.o \ + mask.o OCL_OBJS = \ common-opencl.o common_opencl_pbkdf2.o opencl_mysqlsha1_fmt.o \ diff --git a/src/john.c b/src/john.c index 0a719eb..a248b64 100644 --- a/src/john.c +++ b/src/john.c @@ -48,6 +48,7 @@ #include "single.h" #include "wordlist.h" #include "inc.h" +#include "mask.h" #include "mkv.h" #include "external.h" #include "batch.h" @@ -1255,6 +1256,8 @@ static void john_run(void) if (options.flags & FLG_INC_CHK) do_incremental_crack(&database, options.charset); else + if (options.flags & FLG_MSK_CHK) + do_mask_crack(&database, options.mask_param); if (options.flags & FLG_MKV_CHK) do_markov_crack(&database, options.mkv_param); else diff --git a/src/mask.c b/src/mask.c new file mode 100644 index 0000000..3178d74 --- /dev/null +++ b/src/mask.c @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2013 myrice + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + */ + +#include +#include +#include + +#include "cracker.h" +#include "mask.h" + +static char alpha_low_set[] = { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' +}; + +static char alpha_up_set[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' +}; + +static char num_set[] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' +}; + +enum mask_t { + constant = 0, + low_char, + up_char, + number +}; + +static void fix_state() +{ + +} + +static void mask_generate(char *param) +{ + + char key[PLAINTEXT_BUFFER_SIZE+1]; + enum mask_t mask_type[PLAINTEXT_BUFFER_SIZE]; // record mask type in every position + unsigned maskset_num[PLAINTEXT_BUFFER_SIZE]; // record total mask set number in every position + unsigned maskset_pos[PLAINTEXT_BUFFER_SIZE]; // record mask set position with current key + unsigned key_len = 0; + int index; + int first_mask_index = -1, last_mask_index = -1; + + char *ch = param; + + // Initial the key + memset(maskset_pos, 0, sizeof(maskset_pos)); + while (*ch) { + if (*ch == '?') { + ch++; + if (first_mask_index == -1) + first_mask_index = key_len; + last_mask_index = key_len; + switch (*ch) { + case 'l': + mask_type[key_len] = low_char; + key[key_len] = alpha_low_set[0]; + maskset_num[key_len] = 26; + break; + case 'u': + mask_type[key_len] = up_char; + key[key_len] = alpha_up_set[0]; + maskset_num[key_len] = 26; + break; + case 'd': + mask_type[key_len] = number; + key[key_len] = num_set[0]; + maskset_num[key_len] = 10; + break; + default: + printf("mask type not recognized!\n"); + assert(0); + } + } else { // constant character + key[key_len] = *ch; + mask_type[key_len] = constant; + maskset_num[key_len] = 0; + } + ch++; + key_len++; + assert(key_len < PLAINTEXT_BUFFER_SIZE); + } + key[key_len] = '\0'; + + if (first_mask_index == -1) { //No mask + crk_process_key(key); + return; + } + + index = last_mask_index; + do { + switch (mask_type[index]) { + case constant: + break; + case low_char: + key[index] = alpha_low_set[maskset_pos[index]]; + break; + case up_char: + key[index] = alpha_up_set[maskset_pos[index]]; + break; + case number: + key[index] = num_set[maskset_pos[index]]; + break; + default: + break; + } + if (index == last_mask_index) { + crk_process_key(key); + maskset_pos[index]++; + } else + index++; + while (maskset_pos[index] == maskset_num[index]) { + maskset_pos[index] = 0; + index--; + if (index < first_mask_index) break; + if (mask_type[index] != constant) + maskset_pos[index]++; + } + } while (index <= last_mask_index && index >= first_mask_index); +} + +void do_mask_crack(struct db_main *db, char *param) +{ + crk_init(db, fix_state, NULL); + + mask_generate(param); + crk_done(); + +} \ No newline at end of file diff --git a/src/mask.h b/src/mask.h new file mode 100644 index 0000000..ffb4847 --- /dev/null +++ b/src/mask.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2013 myrice + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + */ + +/* + * Maks mode cracker. + */ + +#ifndef _JOHN_MASK_H +#define _JOHN_MASK_H + +#include "loader.h" + +/* + * Runs the mask mode cracker. + */ +extern void do_mask_crack(struct db_main *db, char *param); + +#endif diff --git a/src/options.c b/src/options.c index c4db447..24356f1 100644 --- a/src/options.c +++ b/src/options.c @@ -70,6 +70,8 @@ static struct opt_entry opt_list[] = { OPT_FMT_STR_ALLOC, &options.loader.activewordlistrules}, {"incremental", FLG_INC_SET, FLG_CRACKING_CHK, 0, 0, OPT_FMT_STR_ALLOC, &options.charset}, + {"mask", FLG_MSK_SET, FLG_CRACKING_CHK, + 0, 0, OPT_FMT_STR_ALLOC, &options.mask_param}, {"markov", FLG_MKV_SET, FLG_CRACKING_CHK, 0, 0, OPT_FMT_STR_ALLOC, &options.mkv_param}, {"markov-stats", FLG_MKV_SET, FLG_CRACKING_CHK, diff --git a/src/options.h b/src/options.h index 4b484d1..c80b040 100644 --- a/src/options.h +++ b/src/options.h @@ -119,6 +119,10 @@ #define FLG_SCALAR 0x0000020000000000ULL #endif +#define FLG_MSK_CHK 0x1000000000000000ULL +#define FLG_MSK_SET (FLG_MSK_CHK | FLG_CRACKING_SET) + + /* * Structure with option flags and all the parameters. */ @@ -147,6 +151,9 @@ struct options_main { /* Charset file name */ char *charset; +/* Mask Mode parameters */ + char *mask_param; + /* The non-default input character set (utf8, ansi, iso-8859-1, etc) as given by the user (might be with/without dash and lower/upper case or even an alias, like 'ansi' for ISO-8859-1) */