-
Notifications
You must be signed in to change notification settings - Fork 32
/
nsec3_gen_fmt_plug.c
386 lines (341 loc) · 8.57 KB
/
nsec3_gen_fmt_plug.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
376
377
378
379
380
381
382
383
384
385
386
/*
* DNSSEC NSEC3 hash cracker. Developed as part of the nsec3map DNS zone
* enumerator project (https://github.com/anonion0/nsec3map).
*
* This software is Copyright (c) 2016 Ralf Sager <nsec3map at 3fnc.org>,
* and it is hereby released to the general public under the following terms:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Some of this code was inspired by sha1_gen_fmt_plug.c by Solar Designer
*/
#if FMT_EXTERNS_H
extern struct fmt_main fmt_nsec3_gen;
#elif FMT_REGISTERS_H
john_register_one(&fmt_nsec3_gen);
#else
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include "sha.h"
#include "arch.h"
#include "params.h"
#include "common.h"
#include "formats.h"
#define FORMAT_LABEL "nsec3"
#define FORMAT_NAME "DNSSEC NSEC3"
#define ALGORITHM_NAME "32/" ARCH_BITS_STR
#define BENCHMARK_COMMENT ""
#define BENCHMARK_LENGTH 0
#define PLAINTEXT_LENGTH 125
#define MIN_KEYS_PER_CRYPT 1
#define MAX_KEYS_PER_CRYPT 1
#define BINARY_SIZE 20
#define BINARY_ALIGN 4
#define N3_MAX_SALT_SIZE 255
#define N3_MAX_ZONE_SIZE 255
#define HASH_LENGTH 20
#define SALT_SIZE sizeof(struct salt_t)
#define SALT_ALIGN 1
struct salt_t {
uint16_t iterations;
size_t salt_length;
size_t zone_length;
unsigned char salt[N3_MAX_SALT_SIZE];
unsigned char zone_wf[N3_MAX_ZONE_SIZE];
};
static struct fmt_tests tests[] = {
{ "$NSEC3$100$4141414141414141$8c2d583acbe22616c69bb457e0c2111ced0a6e77$example.com.", "www" },
{ "$NSEC3$100$42424242$8fb38d13720815ed5b5fcefd973e0d7c3906ab02$example.com.", "mx" },
{ NULL }
};
static struct salt_t saved_salt;
/* length of the saved label, without the length field */
static int saved_key_length;
static unsigned char saved_key[PLAINTEXT_LENGTH + 1];
static unsigned char saved_wf_label[PLAINTEXT_LENGTH + 2];
static SHA_CTX sha_ctx;
static ARCH_WORD_32 crypt_out[5];
static void convert_label_wf(void)
{
int last_dot = saved_key_length - 1;
int i;
unsigned char *out = saved_wf_label;
if (saved_key_length == 0)
return;
++out;
for (i = last_dot ; i >= 0; ) {
if (saved_key[i] == '.') {
out[i] = (unsigned char)(last_dot - i);
last_dot = --i;
} else {
out[i] = tolower(saved_key[i]);
--i;
}
}
*(--out) = (unsigned char)(last_dot - i);
}
static size_t parse_zone(char *zone, unsigned char *zone_wf_out)
{
char *lbl_end, *lbl_start;
unsigned int lbl_len;
unsigned int index = 0;
unsigned int zone_len = strlen(zone);
/* TODO: unvis */
if (zone_len == 0) {
return 0;
} else if (zone_len > N3_MAX_ZONE_SIZE) {
return 0;
}
lbl_end = strchr(zone, '.');
lbl_start = zone;
while (lbl_end != NULL) {
lbl_len = lbl_end - lbl_start;
zone_wf_out[index] = (unsigned char) lbl_len;
if (lbl_len > 0) {
memcpy(&zone_wf_out[++index], lbl_start, lbl_len);
}
index += lbl_len;
lbl_start = lbl_end+1;
if (lbl_start - zone == zone_len) {
zone_wf_out[index] = 0;
break;
} else {
lbl_end = strchr(lbl_start, '.');
}
}
if (lbl_end == NULL)
return 0;
return index + 1;
}
/* format:
* $NSEC3$iter$salt$hash$zone
*/
static int valid(char *ciphertext, struct fmt_main *pFmt)
{
char *p, *q;
int i;
unsigned char zone[N3_MAX_ZONE_SIZE];
unsigned int iter;
if (strncmp(ciphertext, "$NSEC3$", 7))
return 0;
p = ciphertext;
for (i = 0; i < 4; ++i) {
p = strchr(p, '$');
if (p == NULL || *(++p) == 0)
return 0;
switch (i) {
case 0:
continue;
case 1:
/* iterations */
iter = atoi(p);
if (iter < 0 || iter > UINT16_MAX)
return 0;
break;
case 2:
/* salt */
q = p;
while (atoi16[ARCH_INDEX(*q)] != 0x7F)
++q;
if (*q != '$' || q-p > N3_MAX_SALT_SIZE*2 || (q-p) % 2)
return 0;
break;
case 3:
/* hash */
q = p;
while (atoi16[ARCH_INDEX(*q)] != 0x7F)
++q;
if (*q != '$' || q-p > HASH_LENGTH*2)
return 0;
p = q+1;
break;
}
}
/* zone */
if (*p== 0)
return 0;
if (parse_zone(p, zone) == 0) {
return 0;
}
return 1;
}
static void *get_binary(char *ciphertext)
{
static unsigned char out[BINARY_SIZE];
char *p;
int i;
p = ciphertext;
for (i = 0; i < 4; ++i) {
p = strchr(p, '$') + 1;
}
for (i = 0; i < sizeof(out); ++i) {
out[i] = (atoi16[ARCH_INDEX(*p)] << 4) |
atoi16[ARCH_INDEX(p[1])];
p += 2;
}
return out;
}
static void *salt(char *ciphertext)
{
static struct salt_t out;
unsigned int salt_length;
int i;
char *p, *q;
memset(&out, 0, sizeof(out));
p = ciphertext;
for (i = 0; i < 2; ++i)
p = strchr(p, '$') + 1;
out.iterations = (uint16_t) atoi(p);
p = strchr(p, '$') + 1;
q = strchr(p, '$');
salt_length = q-p;
for (i = 0; i < salt_length; i += 2) {
out.salt[i/2] = (atoi16[ARCH_INDEX(*p)] << 4 |
atoi16[ARCH_INDEX(p[1])]);
p += 2;
}
out.salt_length = (unsigned char)((salt_length)/2);
p = strchr(q+1, '$') + 1;
out.zone_length = parse_zone(p, out.zone_wf);
return &out;
}
static int salt_hash(void *salt)
{
unsigned int hash = 0;
int i;
for (i = 0; i < SALT_SIZE; ++i) {
hash <<= 1;
hash += (unsigned char)((unsigned char *)salt)[i];
if (hash >> 10) {
hash ^= hash >> 10;
hash &= 0x3FF;
}
}
hash ^= hash >> 10;
hash &= 0x3FF;
return hash;
}
static void set_salt(void *salt)
{
memcpy(&saved_salt, salt, SALT_SIZE);
}
static void set_key(char *key, int index)
{
saved_key_length = strlen(key);
if (saved_key_length > PLAINTEXT_LENGTH)
saved_key_length = PLAINTEXT_LENGTH;
memcpy(saved_key, key, saved_key_length);
convert_label_wf();
}
static char *get_key(int index)
{
saved_key[saved_key_length] = 0;
return (char *) saved_key;
}
static int crypt_all(int *pcount, struct db_salt *salt)
{
int count = *pcount;
register int i = 0;
register uint16_t iterations = saved_salt.iterations;
register size_t salt_length = saved_salt.salt_length;
SHA1_Init(&sha_ctx);
if (saved_key_length > 0)
SHA1_Update(&sha_ctx, saved_wf_label, saved_key_length+1);
SHA1_Update(&sha_ctx, saved_salt.zone_wf, saved_salt.zone_length);
SHA1_Update(&sha_ctx, saved_salt.salt, salt_length);
SHA1_Final((unsigned char *)crypt_out, &sha_ctx);
while (i++ < iterations) {
SHA1_Init(&sha_ctx);
SHA1_Update(&sha_ctx, crypt_out, BINARY_SIZE);
SHA1_Update(&sha_ctx, saved_salt.salt, salt_length);
SHA1_Final((unsigned char *)crypt_out, &sha_ctx);
}
return count;
}
static int cmp_all(void *binary, int count)
{
return !memcmp(binary, crypt_out, BINARY_SIZE);
}
static int cmp_exact(char *source, int index) { return 1; }
static int get_hash_0(int index) { return crypt_out[0] & 0xF; }
static int get_hash_1(int index) { return crypt_out[0] & 0xFF; }
static int get_hash_2(int index) { return crypt_out[0] & 0xFFF; }
static int get_hash_3(int index) { return crypt_out[0] & 0xFFFF; }
static int get_hash_4(int index) { return crypt_out[0] & 0xFFFFF; }
static int get_hash_5(int index) { return crypt_out[0] & 0xFFFFFF; }
static int get_hash_6(int index) { return crypt_out[0] & 0x7FFFFFF; }
struct fmt_main fmt_nsec3_gen = {
{
FORMAT_LABEL,
FORMAT_NAME,
ALGORITHM_NAME,
BENCHMARK_COMMENT,
BENCHMARK_LENGTH,
PLAINTEXT_LENGTH,
BINARY_SIZE,
BINARY_ALIGN,
SALT_SIZE,
SALT_ALIGN,
MIN_KEYS_PER_CRYPT,
MAX_KEYS_PER_CRYPT,
0,
#if FMT_MAIN_VERSION > 11
{ NULL },
#endif
tests
}, {
fmt_default_init,
fmt_default_done,
fmt_default_reset,
fmt_default_prepare,
valid,
fmt_default_split,
get_binary,
salt,
#if FMT_MAIN_VERSION > 11
{ NULL },
#endif
fmt_default_source,
{
fmt_default_binary_hash_0,
fmt_default_binary_hash_1,
fmt_default_binary_hash_2,
fmt_default_binary_hash_3,
fmt_default_binary_hash_4,
fmt_default_binary_hash_5,
fmt_default_binary_hash_6
},
salt_hash,
set_salt,
set_key,
get_key,
fmt_default_clear_keys,
crypt_all,
{
get_hash_0,
get_hash_1,
get_hash_2,
get_hash_3,
get_hash_4,
get_hash_5,
get_hash_6
},
cmp_all,
cmp_all,
cmp_exact
}
};
#endif /* plugin */