-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordlist.c
375 lines (317 loc) · 8.06 KB
/
wordlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-99,2003,2004,2006,2009,2013,2017 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
#define _POSIX_SOURCE /* for fileno(3) */
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include "misc.h"
#include "params.h"
#include "common.h"
#include "path.h"
#include "signals.h"
#include "loader.h"
#include "logger.h"
#include "status.h"
#include "recovery.h"
#include "options.h"
#include "rpp.h"
#include "rules.h"
#include "external.h"
#include "cracker.h"
#include "john.h"
static FILE *word_file = NULL;
static int progress = 0;
static int rec_rule;
static long rec_pos; /* ftell(3) is defined to return a long */
static unsigned long rec_line;
static int rule_number, rule_count;
static unsigned long line_number;
static int length;
static struct rpp_context *rule_ctx;
static void save_state(FILE *file)
{
fprintf(file, "%d\n%ld\n%lu\n", rec_rule, rec_pos, rec_line);
}
static int restore_rule_number(void)
{
if (rule_ctx)
for (rule_number = 0; rule_number < rec_rule; rule_number++)
if (!rpp_next(rule_ctx)) {
fprintf(stderr, "Restored rule number is out of range - "
"has the configuration file changed?\n");
return 1;
}
return 0;
}
static MAYBE_INLINE int skip_lines(unsigned long n, char *line)
{
if (n) {
line_number += n;
do {
if (!fgetl(line, LINE_BUFFER_SIZE, word_file))
return 1;
} while (--n);
}
return 0;
}
static void restore_line_number(void)
{
char line[LINE_BUFFER_SIZE];
if (skip_lines(rec_pos, line)) {
if (ferror(word_file))
pexit("fgets");
fprintf(stderr, "fgets: Unexpected EOF\n");
error();
}
}
static int restore_state(FILE *file)
{
if (fscanf(file, "%d\n%ld\n", &rec_rule, &rec_pos) != 2)
return 1;
rec_line = 0;
if (rec_version >= 4 && fscanf(file, "%lu\n", &rec_line) != 1)
return 1;
if (rec_rule < 0 || rec_pos < 0)
return 1;
if (restore_rule_number())
return 1;
if (word_file == stdin) {
restore_line_number();
} else {
if (fseek(word_file, rec_pos, SEEK_SET))
pexit("fseek");
line_number = rec_line;
}
return 0;
}
static void fix_state(void)
{
rec_rule = rule_number;
rec_line = line_number;
if (word_file == stdin)
rec_pos = line_number;
else
if ((rec_pos = ftell(word_file)) < 0) {
#ifdef __DJGPP__
if (rec_pos != -1)
rec_pos = 0;
else
#endif
pexit("ftell");
}
}
static int get_progress(void)
{
struct stat file_stat;
long pos;
if (!word_file) return progress;
if (word_file == stdin) return -1;
if (fstat(fileno(word_file), &file_stat)) pexit("fstat");
if ((pos = ftell(word_file)) < 0) {
#ifdef __DJGPP__
if (pos != -1)
pos = 0;
else
#endif
pexit("ftell");
}
return (rule_number * 100 +
(uint64_t)pos * 100 / (file_stat.st_size + 1)) / rule_count;
}
static char *dummy_rules_apply(char *word, char *rule, int split, char *last)
{
word[length] = 0;
if (strcmp(word, last))
return strcpy(last, word);
return NULL;
}
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;
}
}