static void charset_filter_plaintexts(struct db_main *db) { struct list_entry *current, *last; unsigned char *ptr; char key[PLAINTEXT_BUFFER_SIZE]; last = NULL; if ((current = db->plaintexts->head)) do { if (!current->data[0]) { list_del_next(db->plaintexts, last); continue; } for (ptr = (unsigned char *)current->data; *ptr; ptr++) if (*ptr < CHARSET_MIN || *ptr > CHARSET_MAX) { list_del_next(db->plaintexts, last); break; } if (*ptr) continue; strnzcpy(key, current->data, PLAINTEXT_BUFFER_SIZE); if (ext_filter(key)) { if (strlen(key) <= strlen(current->data)) strcpy(current->data, key); } else { list_del_next(db->plaintexts, last); continue; } last = current; } while ((current = current->next)); }
void do_regex_crack(struct db_main *db, const char *regex) { c_simplestring_ptr buffer = c_simplestring_new(); c_iterator_ptr iter = NULL; charset encoding = CHARSET_UTF8; int ignore_case = 0; char word[WORDSIZE]; if (john_main_process) fprintf(stderr, "Warning: regex mode currently can't be " "resumed if aborted\n"); rexgen_setlocale(); status_init(&get_progress, 0); rec_restore_mode(restore_state); rec_init(db, save_state); crk_init(db, fix_state, NULL); iter = c_regex_iterator_cb(regex, ignore_case, encoding, callback); if (!iter) { fprintf(stderr, "Error, invalid regex expression. John exiting now\n"); exit(1); } while (c_iterator_next(iter)) { c_iterator_value(iter, buffer); c_simplestring_to_binary_string(buffer, &word[0], sizeof(word)); c_simplestring_clear(buffer); if (ext_filter((char*)word)) { if (crk_process_key((char*)word)) break; } } c_simplestring_delete(buffer); c_iterator_delete(iter); crk_done(); rec_done(event_abort); }
void do_wordlist_crack(struct db_main *db, char *name, int rules) { union { char buffer[2][LINE_BUFFER_SIZE + CACHE_BANK_SHIFT]; ARCH_WORD dummy; } aligned; char *line = aligned.buffer[0], *last = aligned.buffer[1]; struct rpp_context ctx; char *prerule, *rule, *word; char *(*apply)(char *word, char *rule, int split, char *last); int dist_rules, dist_switch; unsigned long my_words, their_words, my_words_left; log_event("Proceeding with wordlist mode"); if (name) { if (!(word_file = fopen(path_expand(name), "r"))) pexit("fopen: %s", path_expand(name)); log_event("- Wordlist file: %.100s", path_expand(name)); } else { word_file = stdin; log_event("- Reading candidate passwords from stdin"); } length = db->format->params.plaintext_length; if (rules) { if (rpp_init(rule_ctx = &ctx, SUBSECTION_WORDLIST)) { log_event("! No wordlist mode rules found"); if (john_main_process) fprintf(stderr, "No wordlist mode rules found in %s\n", cfg_name); error(); } rules_init(length); rule_count = rules_count(&ctx, -1); log_event("- %d preprocessed word mangling rules", rule_count); apply = rules_apply; } else { rule_ctx = NULL; rule_count = 1; log_event("- No word mangling rules"); apply = dummy_rules_apply; } rule_number = 0; line_number = 0; status_init(get_progress, 0); rec_restore_mode(restore_state); rec_init(db, save_state); crk_init(db, fix_state, NULL); prerule = rule = ""; if (rules) prerule = rpp_next(&ctx); /* A string that can't be produced by fgetl(). */ last[0] = '\n'; last[1] = 0; dist_rules = 0; dist_switch = rule_count; /* never */ my_words = ~0UL; /* all */ their_words = 0; if (options.node_count) { int rule_rem = rule_count % options.node_count; const char *now, *later = ""; dist_switch = rule_count - rule_rem; if (!rule_rem || rule_number < dist_switch) { dist_rules = 1; now = "rules"; if (rule_rem) later = ", then switch to distributing words"; } else { dist_switch = rule_count; /* never */ my_words = options.node_max - options.node_min + 1; their_words = options.node_count - my_words; now = "words"; } log_event("- Will distribute %s across nodes%s", now, later); } my_words_left = my_words; if (their_words) { if (line_number) { /* Restored session. line_number is right after a word we've actually used. */ int for_node = line_number % options.node_count + 1; if (for_node < options.node_min || for_node > options.node_max) { /* We assume that line_number is at the beginning of other nodes' block */ if (skip_lines(their_words, line) && /* Check for error since a mere EOF means next rule (the loop below should see * the EOF again, and it will skip to next rule if applicable) */ ferror(word_file)) prerule = NULL; } else { my_words_left = options.node_max - for_node + 1; } } else { /* New session. Skip lower-numbered nodes' lines. */ if (skip_lines(options.node_min - 1, line)) prerule = NULL; } } if (prerule) do { if (rules) { if (dist_rules) { int for_node = rule_number % options.node_count + 1; if (for_node < options.node_min || for_node > options.node_max) goto next_rule; } if ((rule = rules_reject(prerule, -1, last, db))) { if (strcmp(prerule, rule)) log_event("- Rule #%d: '%.100s'" " accepted as '%.100s'", rule_number + 1, prerule, rule); else log_event("- Rule #%d: '%.100s'" " accepted", rule_number + 1, prerule); } else { log_event("- Rule #%d: '%.100s' rejected", rule_number + 1, prerule); goto next_rule; } } while (fgetl(line, LINE_BUFFER_SIZE, word_file)) { line_number++; if (line[0] != '#') { process_word: if ((word = apply(line, rule, -1, last))) { last = word; if (ext_filter(word)) if (crk_process_key(word)) { rules = 0; break; } } next_word: if (--my_words_left) continue; if (skip_lines(their_words, line)) break; my_words_left = my_words; continue; } if (strncmp(line, "#!comment", 9)) goto process_word; goto next_word; } if (ferror(word_file)) break; if (rules) { next_rule: if (!(rule = rpp_next(&ctx))) break; rule_number++; if (rule_number >= dist_switch) { log_event("- Switching to distributing words"); dist_rules = 0; dist_switch = rule_count; /* not anymore */ my_words = options.node_max - options.node_min + 1; their_words = options.node_count - my_words; } line_number = 0; if (fseek(word_file, 0, SEEK_SET)) pexit("fseek"); if (their_words && skip_lines(options.node_min - 1, line)) break; } my_words_left = my_words; } while (rules); crk_done(); rec_done(event_abort || (status.pass && db->salts)); if (ferror(word_file)) pexit("fgets"); if (name) { if (event_abort) progress = get_progress(); else progress = 100; if (fclose(word_file)) pexit("fclose"); word_file = NULL; } }
static int single_process_pw(struct db_salt *salt, struct db_password *pw, char *rule) { struct list_entry *first, *second; int first_number, second_number; char pair[RULE_WORD_SIZE]; int split; char *key; if (!(first = pw->words->head)) return -1; first_number = 0; do { if ((key = rules_apply(first->data, rule, 0, NULL))) if (ext_filter(key)) if (single_add_key(salt, key, 0)) return 1; if (!salt->list) return 2; if (!pw->binary) return 0; if (++first_number > words_pair_max) continue; if (!CP_isLetter[(unsigned char)first->data[0]]) continue; second_number = 0; second = pw->words->head; do if (first != second) { if ((split = strlen(first->data)) < length) { strnzcpy(pair, first->data, RULE_WORD_SIZE); strnzcat(pair, second->data, RULE_WORD_SIZE); if ((key = rules_apply(pair, rule, split, NULL))) if (ext_filter(key)) if (single_add_key(salt, key, 0)) return 1; if (!salt->list) return 2; if (!pw->binary) return 0; } if (first->data[1]) { pair[0] = first->data[0]; pair[1] = 0; strnzcat(pair, second->data, RULE_WORD_SIZE); if ((key = rules_apply(pair, rule, 1, NULL))) if (ext_filter(key)) if (single_add_key(salt, key, 0)) return 1; if (!salt->list) return 2; if (!pw->binary) return 0; } } while (++second_number <= words_pair_max && (second = second->next)); } while ((first = first->next)); return 0; }
static int single_process_pw(struct db_salt *salt, struct db_password *pw, char *rule) { struct db_keys *keys; struct list_entry *first, *second; int first_number, second_number; char pair[RULE_WORD_SIZE]; int split; char *key; unsigned int c; if (!(first = pw->words->head)) return -1; keys = salt->keys; first_number = 0; do { if ((key = rules_apply(first->data, rule, 0, NULL))) if (ext_filter(key)) if (single_add_key(keys, key)) if (single_process_buffer(salt)) return 1; if (!salt->list) return 2; if (!pw->binary) return 0; if (++first_number > SINGLE_WORDS_PAIR_MAX) continue; c = (unsigned int)first->data[0] | 0x20; if (c < 'a' || c > 'z') continue; second_number = 0; second = pw->words->head; do if (first != second) { if ((split = strlen(first->data)) < length) { strnzcpy(pair, first->data, RULE_WORD_SIZE); strnzcat(pair, second->data, RULE_WORD_SIZE); if ((key = rules_apply(pair, rule, split, NULL))) if (ext_filter(key)) if (single_add_key(keys, key)) if (single_process_buffer(salt)) return 1; if (!salt->list) return 2; if (!pw->binary) return 0; } if (first->data[1]) { pair[0] = first->data[0]; pair[1] = 0; strnzcat(pair, second->data, RULE_WORD_SIZE); if ((key = rules_apply(pair, rule, 1, NULL))) if (ext_filter(key)) if (single_add_key(keys, key)) if (single_process_buffer(salt)) return 1; if (!salt->list) return 2; if (!pw->binary) return 0; } } while (++second_number <= SINGLE_WORDS_PAIR_MAX && (second = second->next)); } while ((first = first->next)); return 0; }
int do_regex_crack_as_rules(const char *regex, const char *base_word, int regex_case, const char *regex_alpha) { c_simplestring_ptr buffer = c_simplestring_new(); c_iterator_ptr iter = NULL; charset encoding = CHARSET_UTF8; char word[WORDSIZE]; static int bFirst=1; static int bALPHA=0; if (bFirst) { bFirst = 0; rexgen_setlocale(); if (regex_alpha && !strncmp(regex_alpha, "alpha", 5)) { bALPHA = 1; SetupAlpha(regex_alpha); } } if (bALPHA) { // Ok, we do our own elete of the word, static char Buf[4096]; char *cp = Buf; const char *cpi = base_word; while (*cpi) { cp += strnzcpyn (cp, rexgen_alphabets[(unsigned char)(*cpi)], 100); ++cpi; if (cp - Buf > sizeof(Buf)-101) break; } *cp = 0; printf ("buf=%s\n", Buf); if (*regex == 0) regex = Buf; else { static char final_Buf[16384]; int len = strlen(Buf)+1; cpi = regex; cp = final_Buf; while (*cpi) { if (*cpi == '\\' && cpi[1] == '0') { cp += strnzcpyn (cp, Buf, len); cpi += 2; } else *cp++ = *cpi++; } regex = final_Buf; } } strcpy(BaseWord, base_word); if (!regex[0]) { if (ext_filter("")) { if (crk_process_key("")) return 1; } return 0; } iter = c_regex_iterator_cb(regex, regex_case, encoding, callback); if (!iter) { fprintf(stderr, "Error, invalid regex expression. John exiting now base_word=%s Regex= %s\n", base_word, regex); exit(1); } while (c_iterator_next(iter)) { c_iterator_value(iter, buffer); c_simplestring_to_binary_string(buffer, &word[0], sizeof(word)); c_simplestring_clear(buffer); if (ext_filter((char*)word)) { if (crk_process_key((char*)word)) { c_simplestring_delete(buffer); c_iterator_delete(iter); return 1; } } } c_simplestring_delete(buffer); c_iterator_delete(iter); return 0; }