-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal.c
293 lines (243 loc) · 6 KB
/
external.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
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2001,2003,2004,2006,2011,2013 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.
*/
#include <stdio.h>
#include <string.h>
#include "misc.h"
#include "params.h"
#include "signals.h"
#include "compiler.h"
#include "loader.h"
#include "logger.h"
#include "status.h"
#include "recovery.h"
#include "options.h"
#include "config.h"
#include "cracker.h"
#include "john.h"
#include "external.h"
static char int_word[PLAINTEXT_BUFFER_SIZE];
static char rec_word[PLAINTEXT_BUFFER_SIZE];
/*
* A "sequence number" for distributing the candidate passwords across nodes.
* It is OK if this number overflows once in a while, as long as this happens
* in the same way for all nodes (must be same size unsigned integer type).
*/
static unsigned int seq, rec_seq;
unsigned int ext_flags = 0;
static char *ext_mode;
static c_int ext_word[PLAINTEXT_BUFFER_SIZE];
c_int ext_abort, ext_status;
static struct c_ident ext_ident_status = {
NULL,
"status",
&ext_status
};
static struct c_ident ext_ident_abort = {
&ext_ident_status,
"abort",
&ext_abort
};
static struct c_ident ext_globals = {
&ext_ident_abort,
"word",
ext_word
};
static void *f_generate;
void *f_filter = NULL;
static struct cfg_list *ext_source;
static struct cfg_line *ext_line;
static int ext_pos;
static int ext_getchar(void)
{
unsigned char c;
if (!ext_line || !ext_line->data) return -1;
if ((c = (unsigned char)ext_line->data[ext_pos++])) return c;
ext_line = ext_line->next;
ext_pos = 0;
return '\n';
}
static void ext_rewind(void)
{
ext_line = ext_source->head;
ext_pos = 0;
}
void ext_init(char *mode)
{
if (!(ext_source = cfg_get_list(SECTION_EXT, mode))) {
if (john_main_process)
fprintf(stderr, "Unknown external mode: %s\n", mode);
error();
}
if (c_compile(ext_getchar, ext_rewind, &ext_globals)) {
if (!ext_line) ext_line = ext_source->tail;
if (john_main_process)
fprintf(stderr,
"Compiler error in %s at line %d: %s\n",
cfg_name, ext_line->number,
c_errors[c_errno]);
error();
}
ext_word[0] = 0;
c_execute(c_lookup("init"));
f_generate = c_lookup("generate");
f_filter = c_lookup("filter");
if ((ext_flags & EXT_REQ_GENERATE) && !f_generate) {
if (john_main_process)
fprintf(stderr,
"No generate() for external mode: %s\n", mode);
error();
}
if ((ext_flags & EXT_REQ_FILTER) && !f_filter) {
if (john_main_process)
fprintf(stderr,
"No filter() for external mode: %s\n", mode);
error();
}
if (john_main_process &&
(ext_flags & (EXT_USES_GENERATE | EXT_USES_FILTER)) ==
EXT_USES_FILTER && f_generate)
fprintf(stderr, "Warning: external mode defines generate(), "
"but is only used for filter()\n");
ext_mode = mode;
}
int ext_filter_body(char *in, char *out)
{
unsigned char *internal;
c_int *external;
internal = (unsigned char *)in;
external = ext_word;
external[0] = internal[0];
external[1] = internal[1];
external[2] = internal[2];
external[3] = internal[3];
if (external[0] && external[1] && external[2] && external[3])
do {
if (!(external[4] = internal[4]))
break;
if (!(external[5] = internal[5]))
break;
if (!(external[6] = internal[6]))
break;
if (!(external[7] = internal[7]))
break;
internal += 4;
external += 4;
} while (1);
c_execute_fast(f_filter);
if (!ext_word[0] && in[0]) return 0;
internal = (unsigned char *)out;
external = ext_word;
internal[0] = external[0];
internal[1] = external[1];
internal[2] = external[2];
internal[3] = external[3];
if (external[0] && external[1] && external[2] && external[3])
do {
if (!(internal[4] = external[4]))
break;
if (!(internal[5] = external[5]))
break;
if (!(internal[6] = external[6]))
break;
if (!(internal[7] = external[7]))
break;
internal += 4;
external += 4;
} while (1);
return 1;
}
static void save_state(FILE *file)
{
unsigned char *ptr;
fprintf(file, "%u\n", rec_seq);
ptr = (unsigned char *)rec_word;
do {
fprintf(file, "%d\n", (int)*ptr);
} while (*ptr++);
}
static int restore_state(FILE *file)
{
int c;
unsigned char *internal;
c_int *external;
int count;
if (rec_version >= 4 && fscanf(file, "%u\n", &seq) != 1)
return 1;
internal = (unsigned char *)int_word;
external = ext_word;
count = 0;
do {
if (fscanf(file, "%d\n", &c) != 1) return 1;
if (++count >= PLAINTEXT_BUFFER_SIZE) return 1;
} while ((*internal++ = *external++ = c));
c_execute(c_lookup("restore"));
return 0;
}
static void fix_state(void)
{
strcpy(rec_word, int_word);
rec_seq = seq;
}
void do_external_crack(struct db_main *db)
{
unsigned char *internal;
c_int *external;
log_event("Proceeding with external mode: %.100s", ext_mode);
internal = (unsigned char *)int_word;
external = ext_word;
while (*external)
*internal++ = *external++;
*internal = 0;
seq = 0;
status_init(NULL, 0);
rec_restore_mode(restore_state);
rec_init(db, save_state);
crk_init(db, fix_state, NULL);
do {
c_execute_fast(f_generate);
if (!ext_word[0])
break;
/*
* The skipping of other nodes' candidate passwords can be optimized, such as
* to avoid the modulo division like it's done for dist_words in wordlist.c.
*/
if (options.node_count) {
int for_node = seq++ % options.node_count + 1;
if (for_node < options.node_min ||
for_node > options.node_max)
continue;
}
if (f_filter) {
c_execute_fast(f_filter);
if (!ext_word[0])
continue;
}
int_word[0] = ext_word[0];
if ((int_word[1] = ext_word[1])) {
internal = (unsigned char *)&int_word[2];
external = &ext_word[2];
do {
if (!(internal[0] = external[0]))
break;
if (!(internal[1] = external[1]))
break;
if (!(internal[2] = external[2]))
break;
if (!(internal[3] = external[3]))
break;
internal += 4;
external += 4;
} while (1);
}
if (crk_process_key(int_word)) break;
} while (1);
crk_done();
rec_done(event_abort);
}