Skip to content

Commit 04546d1

Browse files
committed
Several formats: Colorize warnings and/or use _ONCE macros
1 parent 0881803 commit 04546d1

17 files changed

+79
-143
lines changed

src/7z_common_plug.c

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include "7z_common.h"
3939
#include "memory.h"
4040

41+
// #define DEBUG 1
42+
4143
struct fmt_tests sevenzip_tests[] = {
4244
/* CRC checks passes for this hash (4 bytes of padding) */
4345
{"$7z$128$19$0$1122$8$a264c94f2cd72bec0000000000000000$725883103$112$108$64749c0963e20c74602379ca740165b9511204619859d1914819bc427b7e5f0f8fc67f53a0b53c114f6fcf4542a28e4a9d3914b4bc76baaa616d6a7ec9efc3f051cb330b682691193e6fa48159208329460c3025fb273232b82450645f2c12a9ea38b53a2331a1d0858813c8bf25a831", "openwall"},
@@ -101,9 +103,6 @@ struct fmt_tests sevenzip_tests[] = {
101103

102104
sevenzip_salt_t *sevenzip_salt;
103105

104-
#define YEL "\x1b[0;33m"
105-
#define NRM "\x1b[0m"
106-
107106
int sevenzip_trust_padding;
108107

109108
static char *comp_type[16] = { "stored", "LZMA1", "LZMA2", "PPMD", NULL, NULL, "BZIP2", "DEFLATE" };
@@ -140,23 +139,23 @@ int sevenzip_valid(char *ciphertext, struct fmt_main *self)
140139
&& type != 128) {
141140
if (john_main_process && !warned[type]) {
142141
warned[type] = 1;
143-
fprintf(stderr, YEL "Warning: Not loading files with unsupported compression type %s (0x%02x)\n" NRM,
142+
fprintf_color(color_warning, stderr, "Warning: Not loading files with unsupported compression type %s (0x%02x)\n",
144143
comp_type[c_type] ? comp_type[c_type] : "(unknown)", type);
145144
#if !HAVE_LIBBZ2
146145
if (type == 6)
147-
fprintf(stderr, YEL "Rebuild with libbz2 to get support for that type.\n" NRM);
146+
fprintf_color(color_warning, stderr, "Rebuild with libbz2 to get support for that type.\n");
148147
#endif
149148
#if !HAVE_LIBZ
150149
if (type == 7)
151-
fprintf(stderr, YEL "Rebuild with libz (zlib) to get support for that type.\n" NRM);
150+
fprintf_color(color_warning, stderr, "Rebuild with libz (zlib) to get support for that type.\n");
152151
#endif
153152
}
154153
goto err;
155154
}
156155
if (john_main_process && !ldr_in_pot && !self_test_running &&
157156
options.verbosity > VERB_DEFAULT && !warned[type]) {
158157
warned[type] = 1;
159-
fprintf(stderr, YEL "Saw file(s) with compression type %s%s%s (0x%02x)\n" NRM,
158+
fprintf_color(color_notice, stderr, "Saw file(s) with compression type %s%s%s (0x%02x)\n",
160159
precomp_type[p_type], p_type ? "+" : "", comp_type[c_type], type);
161160
}
162161
if ((p = strtokm(NULL, "$")) == NULL) /* NumCyclesPower */
@@ -174,11 +173,7 @@ int sevenzip_valid(char *ciphertext, struct fmt_main *self)
174173
goto err;
175174
len = atoi(p);
176175
if (len > 0 && strstr(self->params.label, "-opencl")) {
177-
static int warned;
178-
179-
if (!warned++)
180-
fprintf(stderr, YEL "%s: Warning: Not loading hashes with salt due to optimizations. Please report!\n" NRM,
181-
self->params.label);
176+
WARN_ONCE(color_warning, stderr, "%s: Warning: Not loading hashes with salt due to optimizations. Please report!\n", self->params.label);
182177
goto err;
183178
}
184179
if (len > 16)
@@ -355,7 +350,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
355350

356351
#if DEBUG
357352
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
358-
fprintf(stderr, "\nType %02x (%s%s%s) AES length %zu, packed len %zu, pad size %d, crc len %zu\n",
353+
fprintf_color(color_notice, stderr, "\nType %02x (%s%s%s) AES length %zu, packed len %zu, pad size %d, crc len %zu\n",
359354
sevenzip_salt->type, precomp_type[p_type] ? precomp_type[p_type] : "",
360355
p_type ? "+" : "",
361356
comp_type[c_type] ? comp_type[c_type] : "(unknown)",
@@ -379,12 +374,11 @@ int sevenzip_decrypt(unsigned char *derived_key)
379374
if (buf[i] != 0) {
380375
#if DEBUG
381376
if (!benchmark_running && options.verbosity >= VERB_DEBUG) {
382-
fprintf(stderr, YEL "Early padding check failed, ");
377+
fprintf_color(color_warning, stderr, "Early padding check failed, ");
383378
dump_stderr_msg("padding", buf + 16 - pad_size, pad_size);
384-
fprintf(stderr, NRM);
385379
}
386380
if (sevenzip_salt->type == 0x80)
387-
fprintf(stderr, YEL "We don't have data for complete decryption\n");
381+
WARN_ONCE(color_warning, stderr, "We don't have data for complete decryption\n");
388382
break;
389383
#else
390384
return 0;
@@ -395,13 +389,13 @@ int sevenzip_decrypt(unsigned char *derived_key)
395389
}
396390
#if DEBUG
397391
if (!nbytes && !benchmark_running && options.verbosity >= VERB_DEBUG)
398-
fprintf(stderr, "Early padding check passed\n");
392+
fprintf_color(color_notice, stderr, "Early padding check passed\n");
399393
else
400394
nbytes = 0;
401-
#else
395+
if (self_test_running)
396+
#endif
402397
if (sevenzip_salt->type == 0x80) /* We only have truncated data */
403398
return 1;
404-
#endif
405399
}
406400

407401
/* Complete decryption */
@@ -417,9 +411,8 @@ int sevenzip_decrypt(unsigned char *derived_key)
417411
if (out[i] != 0) {
418412
#if DEBUG
419413
if (!benchmark_running && options.verbosity >= VERB_DEBUG) {
420-
fprintf(stderr, YEL "Full data padding check failed, ");
414+
fprintf_color(color_warning, stderr, "Full data padding check failed, ");
421415
dump_stderr_msg("padding", out + sevenzip_salt->aes_length - pad_size, pad_size);
422-
fprintf(stderr, NRM);
423416
}
424417
break;
425418
#else
@@ -431,7 +424,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
431424
}
432425
#if DEBUG
433426
if (!nbytes && !benchmark_running && options.verbosity >= VERB_DEBUG)
434-
fprintf(stderr, "Padding check passed\n");
427+
fprintf_color(color_notice, stderr, "Padding check passed\n");
435428
#endif
436429
}
437430

@@ -456,7 +449,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
456449
out_size == crc_len) {
457450
#if DEBUG
458451
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
459-
fprintf(stderr, "LZMA decoding passed, %zu/%zu -> %zu/%zu, props %02x%02x%02x%02x\n",
452+
fprintf_color(color_notice, stderr, "LZMA decoding passed, %zu/%zu -> %zu/%zu, props %02x%02x%02x%02x\n",
460453
in_size, sevenzip_salt->packed_size, out_size, crc_len, sevenzip_salt->decoder_props[0],
461454
sevenzip_salt->decoder_props[1], sevenzip_salt->decoder_props[2], sevenzip_salt->decoder_props[3]);
462455
#endif
@@ -465,7 +458,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
465458
} else {
466459
#if DEBUG
467460
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
468-
fprintf(stderr, YEL "LZMA decoding failed, %zu/%zu -> %zu/%zu, props %02x%02x%02x%02x\n" NRM,
461+
fprintf_color(color_warning, stderr, "LZMA decoding failed, %zu/%zu -> %zu/%zu, props %02x%02x%02x%02x\n",
469462
in_size, sevenzip_salt->packed_size, out_size, crc_len, sevenzip_salt->decoder_props[0],
470463
sevenzip_salt->decoder_props[1], sevenzip_salt->decoder_props[2], sevenzip_salt->decoder_props[3]);
471464
#endif
@@ -488,15 +481,15 @@ int sevenzip_decrypt(unsigned char *derived_key)
488481
out_size == crc_len) {
489482
#if DEBUG
490483
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
491-
fprintf(stderr, "LZMA2 decoding passed, %zu/%zu -> %zu/%zu, props %02x\n",
484+
fprintf_color(color_notice, stderr, "LZMA2 decoding passed, %zu/%zu -> %zu/%zu, props %02x\n",
492485
in_size, sevenzip_salt->packed_size, out_size, crc_len, sevenzip_salt->decoder_props[0]);
493486
#endif
494487
MEM_FREE(out);
495488
out = new_out;
496489
} else {
497490
#if DEBUG
498491
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
499-
fprintf(stderr, YEL "LZMA2 decoding failed, %zu/%zu -> %zu/%zu, props %02x\n" NRM,
492+
fprintf_color(color_warning, stderr, "LZMA2 decoding failed, %zu/%zu -> %zu/%zu, props %02x\n",
500493
in_size, sevenzip_salt->packed_size, out_size, crc_len, sevenzip_salt->decoder_props[0]);
501494
#endif
502495
MEM_FREE(new_out);
@@ -526,7 +519,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
526519
if (ret == BZ_STREAM_END) {
527520
#if DEBUG
528521
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
529-
fprintf(stderr, "BZIP2 decoding passed, %zu/%zu -> %zu/%zu\n",
522+
fprintf_color(color_notice, stderr, "BZIP2 decoding passed, %zu/%zu -> %zu/%zu\n",
530523
sevenzip_salt->packed_size - inf_stream.avail_in, sevenzip_salt->packed_size,
531524
crc_len - inf_stream.avail_out, crc_len);
532525
#endif
@@ -535,7 +528,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
535528
} else {
536529
#if DEBUG
537530
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
538-
fprintf(stderr, YEL "BZIP2 decoding failed, %zu/%zu -> %zu/%zu\n" NRM,
531+
fprintf_color(color_warning, stderr, "BZIP2 decoding failed, %zu/%zu -> %zu/%zu\n",
539532
sevenzip_salt->packed_size - inf_stream.avail_in, sevenzip_salt->packed_size,
540533
crc_len - inf_stream.avail_out, crc_len);
541534
#endif
@@ -564,7 +557,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
564557
if (ret == Z_STREAM_END) {
565558
#if DEBUG
566559
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
567-
fprintf(stderr, "DEFLATE decoding passed, %zu/%zu -> %zu/%zu\n",
560+
fprintf_color(color_notice, stderr, "DEFLATE decoding passed, %zu/%zu -> %zu/%zu\n",
568561
sevenzip_salt->packed_size - inf_stream.avail_in, sevenzip_salt->packed_size,
569562
crc_len - inf_stream.avail_out, crc_len);
570563
#endif
@@ -573,7 +566,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
573566
} else {
574567
#if DEBUG
575568
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
576-
fprintf(stderr, YEL "DEFLATE decoding failed, %zu/%zu -> %zu/%zu\n" NRM,
569+
fprintf_color(color_warning, stderr, "DEFLATE decoding failed, %zu/%zu -> %zu/%zu\n",
577570
sevenzip_salt->packed_size - inf_stream.avail_in, sevenzip_salt->packed_size,
578571
crc_len - inf_stream.avail_out, crc_len);
579572
#endif
@@ -586,7 +579,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
586579
if (p_type) {
587580
#if DEBUG
588581
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
589-
fprintf(stderr, "Decoding %s, props %02x\n", precomp_type[p_type], sevenzip_salt->preproc_props);
582+
fprintf_color(color_notice, stderr, "Decoding %s, props %02x\n", precomp_type[p_type], sevenzip_salt->preproc_props);
590583
#endif
591584
if (p_type == 1) {
592585
uint32_t state;
@@ -595,12 +588,8 @@ int sevenzip_decrypt(unsigned char *derived_key)
595588
x86_Convert(out, crc_len, 0, &state, 0);
596589
}
597590
else if (p_type == 2) {
598-
if (!benchmark_running && options.verbosity >= VERB_DEFAULT) {
599-
static int warned;
600-
601-
if (!warned++)
602-
fprintf(stderr, YEL "Can't decode BCJ2, so skipping CRC check" NRM);
603-
}
591+
if (!benchmark_running && options.verbosity >= VERB_DEFAULT)
592+
WARN_ONCE(color_warning, stderr, "Can't decode BCJ2, so skipping CRC check");
604593
goto exit_good;
605594
}
606595
else if (p_type == 3)
@@ -620,7 +609,7 @@ int sevenzip_decrypt(unsigned char *derived_key)
620609
Delta_Decode(state, sevenzip_salt->preproc_props + 1, out, crc_len);
621610
#if DEBUG
622611
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
623-
fprintf(stderr, YEL "DELTA decoding can't fail so result unknown\n" NRM);
612+
fprintf_color(color_notice, stderr, "DELTA decoding can't fail so result unknown\n");
624613
#endif
625614
}
626615
}
@@ -636,13 +625,13 @@ int sevenzip_decrypt(unsigned char *derived_key)
636625
if (ccrc == sevenzip_salt->crc) {
637626
#if DEBUG
638627
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
639-
fprintf(stderr, "CRC check passed (%08x)\n", ccrc);
628+
fprintf_color(color_notice, stderr, "CRC check passed (%08x)\n", ccrc);
640629
#endif
641630
goto exit_good;
642631
}
643632
#if DEBUG
644633
if (!benchmark_running && options.verbosity >= VERB_DEBUG)
645-
fprintf(stderr, YEL "CRC failed, %08x vs %08x\n" NRM, ccrc, sevenzip_salt->crc);
634+
fprintf_color(color_warning, stderr, "CRC failed, %08x vs %08x\n", ccrc, sevenzip_salt->crc);
646635
#endif
647636

648637
exit_bad:

src/mscash_common_plug.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,17 @@ int mscash1_common_valid(char *ciphertext, struct fmt_main *self)
101101
// This is tricky: Max supported salt length is 19 characters of Unicode
102102
saltlen = enc_to_utf16(realsalt, MSCASH1_MAX_SALT_LENGTH+1, (UTF8*)strnzcpy(insalt, &ciphertext[FORMAT_TAG_LEN], l - FORMAT_TAG_LEN), l - 3);
103103
if (saltlen < 0) {
104-
static int error_shown = 0;
105104
#ifdef HAVE_FUZZ
106105
if (options.flags & (FLG_FUZZ_CHK | FLG_FUZZ_DUMP_CHK))
107106
return 0;
108107
#endif
109-
if (!error_shown)
110-
fprintf(stderr, "%s: Input file is not UTF-8. Please use --input-enc to specify a codepage.\n", self->params.label);
111-
error_shown = 1;
108+
if (!ldr_in_pot)
109+
WARN_ONCE(color_warning, stderr, "%s: Input file is not UTF-8. Please use --input-enc to specify a codepage.\n", self->params.label);
112110
return 0;
113111
}
114112
if (saltlen > MSCASH1_MAX_SALT_LENGTH) {
115-
static int warned = 0;
116-
117113
if (!ldr_in_pot)
118-
if (!warned++)
119-
fprintf(stderr, "%s: One or more hashes rejected due to salt length limitation\n", self->params.label);
114+
WARN_ONCE(color_warning, stderr, "%s: One or more hashes rejected due to salt length limitation\n", self->params.label);
120115
return 0;
121116
}
122117
return 1;
@@ -330,22 +325,17 @@ int mscash2_common_valid(char *ciphertext, int max_salt_length, struct fmt_main
330325
++i;
331326
saltlen = enc_to_utf16(realsalt, max_salt_length, (UTF8*)strnzcpy(insalt, &ciphertext[i], l-i), l-(i+1));
332327
if (saltlen < 0) {
333-
static int error_shown = 0;
334328
#ifdef HAVE_FUZZ
335329
if (options.flags & (FLG_FUZZ_CHK | FLG_FUZZ_DUMP_CHK))
336330
return 0;
337331
#endif
338-
if (!error_shown)
339-
fprintf(stderr, "%s: Input file is not UTF-8. Please use --input-enc to specify a codepage.\n", self->params.label);
340-
error_shown = 1;
332+
if (!ldr_in_pot)
333+
WARN_ONCE(color_warning, stderr, "%s: Input file is not UTF-8. Please use --input-enc to specify a codepage.\n", self->params.label);
341334
return 0;
342335
}
343336
if (saltlen > max_salt_length) {
344-
static int warned = 0;
345-
346337
if (!ldr_in_pot)
347-
if (!warned++)
348-
fprintf(stderr, "%s: One or more hashes rejected due to salt length limitation\n", self->params.label);
338+
WARN_ONCE(color_warning, stderr, "%s: One or more hashes rejected due to salt length limitation\n", self->params.label);
349339

350340
return 0;
351341
}

src/ntlmv1_mschapv2_fmt_plug.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,20 +1074,17 @@ static char *get_key(int index)
10741074
static void *get_binary(char *ciphertext)
10751075
{
10761076
static uchar *binary;
1077-
static int warned = 0, loaded = 0;
1077+
static int loaded = 0;
10781078
DES_cblock *challenge = my->methods.salt(ciphertext);
10791079
int i, j;
10801080

10811081
if (!binary) binary = mem_alloc_tiny(FULL_BINARY_SIZE, BINARY_ALIGN);
10821082

1083-
if (john_main_process)
1084-
if (!warned && !ldr_in_pot && !bench_or_test_running && ++loaded > 100) {
1085-
warned = 1;
1086-
fprintf(stderr, "%s: Note: slow loading. For short runs, try "
1087-
"--format=%s-naive\ninstead. That version loads "
1088-
"faster but runs slower.\n", my->params.label,
1089-
my->params.label);
1090-
}
1083+
if (john_main_process && !ldr_in_pot && !bench_or_test_running && ++loaded > 100)
1084+
WARN_ONCE(color_warning, stderr, "%s: Note: slow loading. For short runs, try "
1085+
"--format=%s-naive\ninstead. That version loads "
1086+
"faster but runs slower.\n", my->params.label,
1087+
my->params.label);
10911088

10921089
if (chap_valid_short(ciphertext))
10931090
ciphertext += FORMAT_TAG_LEN + CHAP_CHALLENGE_LENGTH / 4 + 1;

src/oldoffice_common_plug.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,10 @@ void *oldoffice_get_salt(char *ciphertext)
207207
cs.salt[i] = atoi16[ARCH_INDEX(p[i * 2])] * 16
208208
+ atoi16[ARCH_INDEX(p[i * 2 + 1])];
209209

210-
if (cs.type == 5 && !ldr_in_pot) {
211-
static int warned;
212-
213-
if (john_main_process && !warned++) {
214-
fprintf(stderr, "Note: The support for OldOffice type 5 is experimental and may be incorrect.\n");
215-
fprintf(stderr, " For latest news see https://github.com/openwall/john/issues/4705\n");
216-
}
210+
if (cs.type == 5 && !ldr_in_pot && john_main_process) {
211+
WARN_ONCE(color_warning, stderr,
212+
"Note: The support for OldOffice type 5 is experimental and may be incorrect.\n"
213+
" For latest news see https://github.com/openwall/john/issues/4705\n");
217214
}
218215

219216
MEM_FREE(keeptr);

src/opencl_diskcryptor_aes_fmt_plug.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,11 @@ static void create_clobj(size_t kpc, struct fmt_main *self)
171171

172172
static void init(struct fmt_main *_self)
173173
{
174-
static int warned = 0;
175-
176174
self = _self;
177175
opencl_prepare_dev(gpu_id);
178176

179-
if (!warned++ && !(options.flags & FLG_TEST_CHK) && !options.listconf) {
180-
fprintf(stderr, "[ATTENTION] This format (%s) can only crack AES XTS DiskCryptor hashes.\n", FORMAT_LABEL);
181-
}
177+
if (!(options.flags & FLG_TEST_CHK) && !options.listconf)
178+
WARN_ONCE(color_warning, stderr, "Warning: %s can only crack AES XTS DiskCryptor hashes.\n", FORMAT_LABEL);
182179
}
183180

184181
static void reset(struct db_main *db)

src/opencl_diskcryptor_fmt_plug.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,13 @@ static void create_clobj(size_t kpc, struct fmt_main *self)
166166

167167
static void init(struct fmt_main *_self)
168168
{
169-
static int warned = 0;
170-
171169
self = _self;
172170
opencl_prepare_dev(gpu_id);
173171

174172
Twofish_initialise();
175173

176-
if (!warned++ && !(options.flags & FLG_TEST_CHK) && !options.listconf) {
177-
fprintf(stderr, "[ATTENTION] This format (%s) does not support cascaded cipher modes yet.\n", FORMAT_LABEL);
178-
}
174+
if (!(options.flags & FLG_TEST_CHK) && !options.listconf)
175+
WARN_ONCE(color_warning, stderr, "Warning: %s does not support cascaded cipher modes yet.\n", FORMAT_LABEL);
179176
}
180177

181178
static void reset(struct db_main *db)

src/opencl_krb5pa-sha1_fmt_plug.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,8 @@ static int valid(char *ciphertext, struct fmt_main *self)
335335

336336
// We support a max. total salt length of 52.
337337
// We could opt to emit a warning if rejected here.
338-
if (saltlen > MAX_SALTLEN) {
339-
static int warned = 0;
340-
341-
if (!ldr_in_pot)
342-
if (!warned++)
343-
fprintf(stderr, "%s: One or more hashes rejected due to salt length limitation\n", FORMAT_LABEL);
338+
if (saltlen > MAX_SALTLEN && !ldr_in_pot) {
339+
WARN_ONCE(color_warning, stderr, "%s: One or more hashes rejected due to salt length limitation\n", FORMAT_LABEL);
344340

345341
return 0;
346342
}

0 commit comments

Comments
 (0)