-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathzip2john.c
More file actions
1190 lines (1103 loc) · 39.2 KB
/
Copy pathzip2john.c
File metadata and controls
1190 lines (1103 loc) · 39.2 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* zip2john processes input ZIP files into a format suitable for use with JtR.
*
* This software is
* Copyright (c) 2011-2018 Dhiru Kholia <dhiru.kholia at gmail.com>,
* Copyright (c) 2011-2018 JimF, Copyright (c) 2020 Simon Rettberg,
* Copyright (c) 2013-2021 magnum,
* 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.
*
* References:
*
* 1. http://www.winzip.com/aes_info.htm
* 2. http://www.winzip.com/aes_tips.htm
* 4. ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
* 5. Nathan Moinvaziri's work in extending minizip to support AES.
* 6. http://oldhome.schmorp.de/marc/fcrackzip.html (coding hints)
* 7. http://www.pkware.com/documents/casestudies/APPNOTE.TXT
* 8. http://gladman.plushost.co.uk/oldsite/cryptography_technology/fileencrypt/index.php
* (borrowed files have "gladman_" prepended to them). This gladman code has been removed from JtR source tree.
*
* Usage:
*
* 1. Run zip2john on zip file(s) as "zip2john [zip files]".
* Output is written to standard output.
* 2. Run JtR on the output generated by zip2john as "john [output file]".
*
* Output Line Format:
*
* For type = 0, for ZIP files encrypted using AES
* filename:$zip$*type*hex(CRC)*encryption_strength*hex(salt)*hex(password_verfication_value):hex(authentication_code)
*
* Original $pkzip$ only had CS, fixed to be part of CRC, which was found out sometimes inappropriate.
* filename:$pkzip$C*B*[DT*MT{CL*UL*CR*OF*OX}*CT*DL*CS*DA]*$/pkzip$
* CS 2 bytes of checksum data.
*
* The newer $pkzip2$ addressed the problem by adding TC (as in timestamp), but still neither zip2john or
* the format really knew when to use which (resulting in suboptimal early rejection).
* filename:$pkzip2$C*B*[DT*MT{CL*UL*CR*OF*OX}*CT*DL*CS*TC*DA]*$/pkzip2$ (with 2 checksums: CS & TC)
* CS 2 bytes of checksum data.
* TC 2 bytes of checksum data (from timestamp)
*
* Current version (Feb 2021) reverted to original $pkzip$ but now with the one correct value put in CS: Sometimes
* it's taken from timestamp, sometimes part of the CRC.
* filename:$pkzip$C*B*[DT*MT{CL*UL*CR*OF*OX}*CT*DL*CS*DA]*$/pkzip$
* CS Depending on archive, 2 bytes of either checksum data or timestamp.
*
* All numeric and 'binary data' fields are stored in hex.
*
* C is the count of hashes present (the array of items, inside the [] C can be 1 to 8.).
* B is number of valid bytes in the CS. For ZIP version [needed to extract] < 2.0, this is 2, otherwise it's 1.
* The B value should actually be defined within the ARRAY below, not per archive. For now we set it to 1 if any
* of the files limits it.
* ARRAY of data starts here
* DT is a "Data Type enum". This will be 1 2 or 3. 1 is 'partial'. 2 and 3 are full file data (2 is inline, 3 is load from file).
* MT Magic Type enum. 0 is no 'type' and other values are no longer valid.
* NOTE, CL, DL, CRC, OFF are only present if DT != 1
* CL Compressed length of file blob data (includes 12 byte IV).
* UL Uncompressed length of the file.
* CR CRC32 of the 'final' file.
* OF Offset to the PK\x3\x4 record for this file data. If DT == 2, then this will be a 0, as it is not needed, all of the data is already included in the line.
* OX Additional offset (past OF), to get to the zip data within the file.
* END OF 'optional' fields.
* CT Compression type (0 or 8) 0 is stored, 8 is imploded.
* DL Length of the DA data.
* CS 2 bytes of checksum data, from CRC, *or* from either CRC or timestamp (see above).
* TC 2 bytes of checksum data, from timestamp (only $pkzip2$, see above).
* DA This is the 'data'. It will be hex data if DT == 1 or 2. If DT == 3, then it is a filename (name of the .zip file).
* END of array item. There will be C (count) array items.
* The format string will end with $/pkzip$
*
* The AES-zip format redone by JimF, Summer 2014. Spent some time to understand the AES authentication code,
* and now have placed code to do this. However, this required format change. The old AES format was:
*
* For type = 0, for ZIP files encrypted using AES
* filename:$zip$*type*hex(CRC)*encryption_strength*hex(salt)*hex(password_verfication_value):hex(authentication_code)
* NOTE, the authentication code was NOT part of this, even though documented in this file. nor is hex(CRC) a part.
*
* The new format is: (and the $zip$ is deprecated)
*
* filename:$zip2$*Ty*Mo*Ma*Sa*Va*Le*DF*Au*$/zip2$
* Ty = type (0) and ignored.
* Mo = mode (1 2 3 for 128/192/256 bit)
* Ma = magic (file magic). This is reservered for now. See pkzip_fmt_plug.c or zip2john.c for information.
* For now, this must be a '0'
* Sa = salt(hex). 8, 12 or 16 bytes of salt (depends on mode)
* Va = Verification bytes(hex) (2 byte quick checker)
* Le = real compr len (hex) length of compressed/encrypted data (field DF)
* DF = compressed data DF can be Le*2 hex bytes, and if so, then it is the ENTIRE file blob written 'inline'.
* However, if the data blob is too long, then a .zip ZIPDATA_FILE_PTR_RECORD structure will be the 'contents' of DF
* Au = Authentication code (hex) a 10 byte hex value that is the hmac-sha1 of data over DF. This is the binary() value
*
* ZIPDATA_FILE_PTR_RECORD (this can be the 'DF' of this above hash line).
* *ZFILE*Fn*Oh*Ob* (Note, the leading and trailing * are the * that 'wrap' the DF object.
* ZFILE This is the literal string ZFILE
* Fn This is the name of the .zip file. NOTE the user will need to keep the .zip file in proper locations (same as
* was seen when running zip2john. If the file is removed, this hash line will no longer be valid.
* Oh Offset to the zip local header record for this blob.
* Ob Offset to the start of the blob data
*
*
* The new format for PKWARE's Strong Encryption Specification is:
*
* filename:$zip3$*Ty*Al*Bi*Ma*Sa*Erd*Le*DF*Au*Fn
* Ty = type (0) and ignored.
* Al = algorithm (1 for AES)
* Bi = bit length (128/192/256 bit)
* Ma = magic (file magic), reserved, must be '0' now
* Sa = salt(hex), 12 or 16 bytes of IV data
* Erd = encrypted random data (max. 256 bytes)
* Le = real compr len (hex) length of compressed/encrypted data (field DF), unused currently
* DF = compressed data DF can be Le*2 hex bytes, and if so, then it is the ENTIRE file blob written 'inline', unused currently
* Au = authentication code, a 8 byte hex value that contains a CRC32 checksum, unused currently
* Fn = filename within zip file
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "arch.h"
#if !AC_BUILT || HAVE_LIMITS_H
#include <limits.h>
#endif
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#if (!AC_BUILT || HAVE_UNISTD_H) && !_MSC_VER
#include <unistd.h>
#endif
#include "common.h"
#include "jumbo.h"
#include "formats.h"
#include "memory.h"
#include "pkzip.h"
#ifdef _MSC_VER
#include "missing_getopt.h"
#endif
#include "johnswap.h"
#define _STR_VALUE(arg) #arg
#define STR_MACRO(n) _STR_VALUE(n)
#define MAX_FILES STR_MACRO(MAX_PKZ_FILES)
#define MAX_BLOB_INLINE_SIZE 0x400000000ULL // 16 GB
#define FLAG_ENCRYPTED 1
#define FLAG_LOCAL_SIZE_UNKNOWN 8
#define FLAG_STRONG_ENCRYPTION 64
#define AES_EXTRA_DATA_LENGTH 7 // https://www.winzip.com/aes_info.html#extra-data
#define AES_AUTHENTICATION_CODE_LENGTH 10 // http://www.winzip.com/aes_info.htm#authentication-code
#define AES_PASSWORD_VERIFICATION_LENGTH 2 // https://www.winzip.com/aes_info.html#pwd-verify
// TODO: Add macro for fprintf(stderr, ...) and make DEBUG a command line switch
// Simple wrappers
static void xfseek(FILE *stream, long offset, int whence)
{
if (fseek(stream, offset, whence) == -1) {
error(1, errno, "fseek");
}
}
static size_t xfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fread(ptr, size, nmemb, stream);
if (ret != nmemb) {
if (feof(stream)) {
error(1, 0, "Reached end of stream on fread");
} else if (ferror(stream)) {
error(1, 0, "ferror triggered on fread");
} else {
error(1, 0, "No reason for short read on fread (%"PRIu64"/%"PRIu64" records)",
(uint64_t)ret, (uint64_t)nmemb);
}
}
return ret;
}
/**
* Read a char from stream, push it back if it doesn't have the expected value.
* This is so we can properly look for header magic (32bit).
*/
static int fexpect(FILE *stream, int c)
{
int d = fgetc(stream);
if (d == EOF)
return 0;
if (d != c) {
ungetc(d, stream);
}
return d == c;
}
static int checksum_only;
static char *ascii_fname, *only_fname;
static void print_hex_inline(unsigned char *str, int len)
{
int i;
for (i = 0; i < len; ++i)
printf("%02x", str[i]);
}
// Represents a file in the archive
typedef struct _zip_ptr
{
uint16_t version, flags;
uint16_t cmptype;
char *hash_data;
char *file_name;
uint64_t offset, offex;
uint64_t cmp_len, decomp_len;
uint32_t crc;
char cs[5]; // High-order word of either crc or timestamp
int zip64; // Has extended header with 64bit data
uint16_t lastmod_date, lastmod_time;
uint16_t extrafield_length;
struct {
uint16_t vendor_version, vendor_id, cmptype;
char strength;
int found;
} aes;
} zip_ptr;
// Represents a zip archive
typedef struct _zip_file
{
FILE *fp;
const char *fname;
int check_bytes; // number of valid bytes in checksum
int zip64; // Is ZIP64 format (has ZIP64 EOCD)
} zip_file;
// Wrapper struct so we don't have to pass three pointers all the time
typedef struct _zip_context {
zip_ptr best_files[MAX_PKZ_FILES]; // Up to 8 candidates for handling old encryption type archives
zip_ptr curzip; // Meta data of file in archive we're currently processing
zip_file archive; // The zip file being processed
int num_candidates; // Number of candidates in best_files array
} zip_context;
/**
* Read 64bit fields from zip64 extended file header.
* fp is expected to point right after the tag and size of the efh
*/
static void handle_zip64_ef(FILE *fp, zip_ptr *p, uint16_t len, int is_local)
{
uint64_t decomp_len = UINT64_MAX, cmp_len = UINT64_MAX, offset = UINT64_MAX;
long rem_len = len;
#ifdef DEBUG
fprintf(stderr, "Handling ZIP64 ef (local = %d)\n", is_local);
#endif
// Only read this if we didn't get it from the Central Directory yet, otherwise
// we need to store both, local and central values in the zip_ptr struct, as the
// handle_zip64_ef function needs to know whether the fields of the header this
// extra field belongs to were 0xff..ff, because only in that case the according
// 64bit fields will be present here.
// See "4.5.3 -Zip64 Extended Information Extra Field (0x0001)" in APPNOTE.TXT
// The additional checks that reset the read values to UINT64_MAX (=not present)
// is a workaround for archives that have valid values in their normal headers
// (ie. fitting into 32bit) but additionally supply a bogus ZIP64 extended field
// in their local header, where the according fields are 0.
if (p->zip64) {
xfseek(fp, len, SEEK_CUR);
return;
}
if (p->decomp_len == UINT32_MAX || is_local) {
decomp_len = fget64LE(fp);
if (p->decomp_len != UINT32_MAX && decomp_len == 0) {
decomp_len = UINT64_MAX;
}
rem_len -= 8;
}
if (p->cmp_len == UINT32_MAX || is_local) {
cmp_len = fget64LE(fp);
if (p->cmp_len != UINT32_MAX && cmp_len == 0) {
cmp_len = UINT64_MAX;
}
rem_len -= 8;
}
if (p->offset == UINT32_MAX) {
offset = fget64LE(fp);
if (p->offset != UINT32_MAX && offset == 0) {
offset = UINT64_MAX;
}
rem_len -= 8;
}
// Other fields we don't care about follow...
// Now only apply what we read if there was enough bytes in the field
if (rem_len >= 0) {
p->zip64 = 1;
if (decomp_len != UINT64_MAX) {
p->decomp_len = decomp_len;
}
if (cmp_len != UINT64_MAX) {
p->cmp_len = cmp_len;
}
if (offset != UINT64_MAX) {
p->offset = offset;
}
} else {
fprintf(stderr, "Ignoring short zip64 extended field for %s\n", p->file_name);
}
xfseek(fp, rem_len, SEEK_CUR);
#if DEBUG
fprintf(stderr,
"ZIP64 EXTRA FIELD; NEW: decomp_len=%"PRIu64" cmp_len=%"PRIu64" offset=%"PRIu64"\n",
p->decomp_len, p->cmp_len, p->offset);
#endif
}
/**
* Handle the extended field for AES encrypted files (99)
*/
static void handle_aes_ef(FILE *fp, zip_ptr *p, uint16_t len)
{
// Data size: this value is currently 7, but because it is possible that this
// specification will be modified in the future to store additional data in
// this extra field, vendors should not assume that it will always remain 7.
if (len != AES_EXTRA_DATA_LENGTH) {
fprintf(stderr, "AES_EXTRA_DATA_LENGTH is not 7 for %s, please report this to us!\n", p->file_name);
xfseek(fp, len, SEEK_CUR);
return;
}
p->aes.found = 1;
p->aes.vendor_version = fget16LE(fp);
p->aes.vendor_id = fget16LE(fp);
p->aes.strength = fgetc(fp);
p->aes.cmptype = fget16LE(fp);
}
static void scan_for_data_descriptor(zip_file *zip, zip_ptr *p);
/**
* Process AES encrypted file in zip and produce according hash.
* file pointer in archive is assumed to be at the beginning of
* the local extra fields.
*/
static int process_aes(zip_file *zip, zip_ptr *p)
{
FILE *fp = zip->fp;
if (p->cmptype == 99) { /* AES encryption */
uint64_t real_cmpr_len;
uint16_t efh_id;
uint16_t efh_datasize;
unsigned char salt[16];
char *bname;
int d;
uint64_t i;
uint32_t salt_length;
uint16_t ef_remaining = p->extrafield_length;
// There could be multiple extra fields, so need to process them all.
while (!ferror(fp) && !feof(fp) && ef_remaining > 0) {
efh_id = fget16LE(fp);
efh_datasize = fget16LE(fp);
// Adjust the bytes processed for id, size and acutal data so the
// file pointer is moved on correctly,
// - 2 bytes for the efh_id
// - 2 bytes for the efh_datasize
ef_remaining = ef_remaining - 2 - 2 - efh_datasize;
if (efh_id == 0x9901) {
handle_aes_ef(fp, p, efh_datasize);
} else if (efh_id == 0x0001) {
handle_zip64_ef(fp, p, efh_datasize, 1);
} else {
xfseek(fp, efh_datasize, SEEK_CUR);
}
}
if (!p->aes.found) {
fprintf(stderr, "Couldn't find (valid) extra header for type 99 AES entry for %s in %s.\n", p->file_name, zip->fname);
return 0;
}
bname = jtr_basename(zip->fname);
#if DEBUG
fprintf(stderr,
"%s/%s is using AES encryption (AE-%"PRIu16"), extrafield_length is %d\n",
bname, p->file_name, p->aes.vendor_version, p->extrafield_length);
#endif
if (p->aes.vendor_version < 1 || p->aes.vendor_version > 2) {
fprintf(stderr, "! Unknown AES encryption version: %"PRIu16"\n", p->aes.vendor_version);
return 0;
}
if (p->aes.strength < 1 || p->aes.strength > 3) {
fprintf(stderr, "! Unknown AES encryption strength: %u\n", p->aes.strength);
return 0;
}
#if DEBUG
if (p->aes.vendor_version == 2 && p->crc != 0) {
fprintf(stderr, "Odd: crc is not zero for AE-2 encrypted file.\n");
}
#endif
if (p->cmp_len == 0 && (p->flags & FLAG_LOCAL_SIZE_UNKNOWN)) {
scan_for_data_descriptor(zip, p);
}
salt_length = 4 + 4 * p->aes.strength; // 128 -> 8, 192 -> 12, 256 -> 16
if (sizeof(salt) < salt_length ||
fread(salt, 1, salt_length, fp) != salt_length) {
fprintf(stderr, "Error, in fread of salt!\n");
return 0;
}
printf("%s/%s:$zip2$*0*%x*%x*",
bname, p->file_name, p->aes.strength, 0);
// Print salt value
for (i = 0; i < salt_length; i++) {
printf("%c%c",
itoa16[ARCH_INDEX(salt[i] >> 4)],
itoa16[ARCH_INDEX(salt[i] & 0x0f)]);
}
printf("*");
// Password verification value
// since in the format we read/compare this one, we do it char by
// char, so there is no endianity swapping needed. (validator)
for (i = 0; i < 2; i++) {
d = fgetc(fp);
if (d == EOF)
break;
printf("%c%c",
itoa16[ARCH_INDEX(d >> 4)],
itoa16[ARCH_INDEX(d & 0x0f)]);
}
if (p->cmp_len <= AES_PASSWORD_VERIFICATION_LENGTH + salt_length + AES_AUTHENTICATION_CODE_LENGTH) {
real_cmpr_len = 0;
fprintf(stderr, "!? compressed length of AES entry too short.\n");
} else {
// Password verification value -> 2 bytes
// Salt value -> salt_length
// Authentication code -> 10 bytes
real_cmpr_len = p->cmp_len - AES_PASSWORD_VERIFICATION_LENGTH
- salt_length - AES_AUTHENTICATION_CODE_LENGTH;
}
printf("*%"PRIx64"*", real_cmpr_len);
// Actual encrypted data
if (real_cmpr_len > MAX_BLOB_INLINE_SIZE) {
printf("ZFILE*%s*%"PRIx64"*%lx", zip->fname, p->offset, ftell(fp));
xfseek(fp, real_cmpr_len, SEEK_CUR);
} else {
for (i = 0; i < real_cmpr_len; i++) {
d = fgetc(fp);
if (d == EOF)
break;
printf("%c%c",
itoa16[ARCH_INDEX(d >> 4)],
itoa16[ARCH_INDEX(d & 0x0f)]);
}
}
printf("*");
// Authentication code
for (i = 0; i < AES_AUTHENTICATION_CODE_LENGTH; i++) {
d = fgetc(fp);
if (d == EOF)
break;
printf("%c%c",
itoa16[ARCH_INDEX(d >> 4)],
itoa16[ARCH_INDEX(d & 0x0f)]);
}
printf("*$/zip2$:%s:%s:%s\n",
p->file_name, bname, zip->fname);
return 1;
}
if ((p->flags & FLAG_ENCRYPTED) /* Try to detect Strong Encryption, bit 6 is sometimes not set */
&& ((p->flags & FLAG_STRONG_ENCRYPTION) || p->version == 51 || p->version == 52 || p->version >= 61)) {
// fseek(fp, filename_length, SEEK_CUR);
// fseek(fp, extrafield_length, SEEK_CUR);
// continue;
unsigned char iv[16];
unsigned char Erd[256];
uint32_t Format;
uint16_t AlgId;
uint16_t Bitlen;
uint16_t ErdSize;
uint32_t Reserved1;
uint16_t VSize;
uint16_t IVSize;
char *bname;
fprintf(stderr, "Poking at %s in %s to see if it's indeed strong encryption...\n", p->file_name, zip->fname);
bname = jtr_basename(zip->fname);
IVSize = fget16LE(fp);
if (IVSize > sizeof(iv)) {
fprintf(stderr, "No (IV too long: %"PRIu16")\n", IVSize);
return 0;
}
if (fread(iv, 1, IVSize, fp) != IVSize) {
fprintf(stderr, "No (Error reading IV)\n");
return 0;
}
(void) fget32LE(fp); /* Size */
Format = fget16LE(fp);
if (Format != 3) {
fprintf(stderr, "No (Format == %"PRIu32")\n", Format);
return 0;
}
AlgId = fget16LE(fp);
if (AlgId == 0x660E || AlgId == 0x660F || AlgId == 0x6610) {
AlgId = 1;
} else if (AlgId == 0x6603 || AlgId == 0x6609 || AlgId == 0x6720 || AlgId == 0x6721 || AlgId == 0x6801) {
fprintf(stderr, "AlgId (%x) is currently unsupported, please report this to us!\n", AlgId);
return 0;
} else {
fprintf(stderr, "No (Unknown AlgId)\n");
return 0;
}
if (IVSize == 0) {
uint32_t crc = p->crc;
uint64_t decomp_len = p->decomp_len;
#if DEBUG
fprintf(stderr, "IV size is 0, assuming it is crc + decomp_len...\n");
#endif
memset(iv, 0, 16);
#if !ARCH_LITTLE_ENDIAN
crc = JOHNSWAP(crc);
decomp_len = JOHNSWAP64(decomp_len);
#endif
memcpy(iv, &crc, 4);
memcpy(iv + 4, &decomp_len, 8);
IVSize = 12;
}
Bitlen = fget16LE(fp);
(void) fget16LE(fp); /* Flags */
ErdSize = fget16LE(fp);
if (ErdSize > sizeof(Erd)) {
fprintf(stderr, "No (ErdSize too large: %"PRIu16")\n", ErdSize);
return 0;
}
if (fread(Erd, 1, ErdSize, fp) != ErdSize) {
fprintf(stderr, "! Could not fread Erd from file\n");
return 0;
}
Reserved1 = fget32LE(fp);
if (Reserved1 != 0) {
fprintf(stderr, "Reserved1 is %u (non-zero), please report this bug to us!\n", Reserved1);
return 0;
}
VSize = fget16LE(fp);
xfseek(fp, VSize, SEEK_CUR);
printf("%s:$zip3$*%d*%d*%d*%d*", bname, 0, AlgId, Bitlen, 0);
print_hex_inline(iv, IVSize); // getting this right isn't important ;)
printf("*");
print_hex_inline(Erd, ErdSize);
printf("*0*0*0*%s\n", p->file_name);
return 1;
}
return 0;
}
static void print_hex(unsigned char *p, uint64_t len) {
while (len--)
printf("%02x", *p++);
printf("*");
}
// If archive was created from a non-seekable stream, we need to find CRC and
// sizes AFTER file data which means we're in a hen-and-egg situation since we
// don't know the size... I think the below is enough but there may be edge
// cases where we need to also recognize some other kind of start-of-whatever
// and seek back 16 bytes.
// This would also fail if there is some garbage bytes between the end of the
// current file and the next one, but you can't cover everything I guess.
static void scan_for_data_descriptor(zip_file *zip, zip_ptr *p)
{
FILE *fp = zip->fp;
long saved_pos = ftell(fp);
uint32_t crc = 0;
uint64_t cmp_len = 0, decomp_len = 0;
// All values known? Nothing to do.
if (p->cmp_len && p->decomp_len && p->crc)
return;
// Flag not set, and both sizes are 0: accept as valid
if (p->cmp_len == 0 && p->decomp_len == 0 && !(p->flags & FLAG_LOCAL_SIZE_UNKNOWN))
return;
// Likewise, no flag set and at least one field filled: accept
if ((p->cmp_len || p->decomp_len || p->crc) && !(p->flags & FLAG_LOCAL_SIZE_UNKNOWN))
return;
fprintf(stderr, "Scanning for EOD... ");
while (!feof(fp)) {
if (fgetc(fp) != 0x50 || !fexpect(fp, 0x4b))
continue;
if (fexpect(fp, 0x07) && fexpect(fp, 0x08)) {
// Best case: Found the optional header
fprintf(stderr, "FOUND Extended local header\n");
crc = fget32LE(fp);
if (zip->zip64) {
cmp_len = fget64LE(fp);
decomp_len = fget64LE(fp);
} else {
cmp_len = fget32LE(fp);
decomp_len = fget32LE(fp);
}
break;
}
if ((fexpect(fp, 0x03) && fexpect(fp, 0x04)
&& fprintf(stderr, "FOUND next Local file header\n"))
|| (fexpect(fp, 0x01) && fexpect(fp, 0x02)
&& fprintf(stderr, "FOUND Central directory\n"))
) {
// No optional header, but we found something that looks like the
// start of another file, or the central directory. Assume that
// the data we're interested in lies directly before this header.
if (zip->zip64)
xfseek(fp, -24, SEEK_CUR);
else
xfseek(fp, -16, SEEK_CUR);
crc = fget32LE(fp);
if (zip->zip64) {
cmp_len = fget64LE(fp);
decomp_len = fget64LE(fp);
} else {
cmp_len = fget32LE(fp);
decomp_len = fget32LE(fp);
}
break;
}
}
if (feof(fp)) {
fprintf(stderr, "Nothing found.\n");
} else {
if (cmp_len > ftell(fp) - p->offset) {
fprintf(stderr, "!? Compressed len stored after compessed data (%"PRIu64") is"
" larger than offset between current and next file (%"PRIu64")\n",
cmp_len, ftell(fp) - p->offset);
}
//fprintf(stderr, "cmp_len: %"PRIu64", decomp_len: %"PRIu64", crc32: %"PRIu32"\n",
// cmp_len, decomp_len, crc);
if (p->cmp_len == 0) {
p->cmp_len = cmp_len;
}
if (p->decomp_len == 0) {
p->decomp_len = decomp_len;
}
if (p->crc == 0) {
p->crc = crc;
}
}
xfseek(fp, saved_pos, SEEK_SET);
}
static int load_local_header(zip_file *zfp, zip_ptr *p)
{
FILE *fp = zfp->fp;
uint16_t filename_length;
p->offset = ftell(fp)-4;
// Ignore mismatches for these for now and trust local over central, but we warn about crc/size below
p->version = fget16LE(fp) & 0xff; // Remove OS compatibility byte, don't care
p->flags = fget16LE(fp);
p->cmptype = fget16LE(fp);
p->lastmod_time = fget16LE(fp);
p->lastmod_date = fget16LE(fp);
if (!(p->flags & FLAG_LOCAL_SIZE_UNKNOWN)) {
uint32_t crc, cmp_len, decomp_len;
crc = fget32LE(fp);
cmp_len = fget32LE(fp);
decomp_len = fget32LE(fp);
if (!p->zip64) {
// If we already got 64bit values from the central directory, don't overwrite them again with 32bit ones
if (crc == 0 && p->crc != 0) {
fprintf(stderr, "Local CRC field is 0, but central one is %"PRIx32". Weird.\n", p->crc);
} else {
p->crc = crc;
}
if (cmp_len == 0 && p->cmp_len != 0) {
fprintf(stderr, "Local cmp_len field is 0, but central one is %"PRIx64". Weird.\n", p->cmp_len);
} else {
p->cmp_len = cmp_len;
}
if (decomp_len == 0 && p->decomp_len != 0) {
fprintf(stderr, "Local decomp_len field is 0, but central one is %"PRIx64". Weird.\n", p->decomp_len);
} else {
p->decomp_len = decomp_len;
}
}
} else { // Local size unknown, skip according fields
xfseek(fp, 12, SEEK_CUR);
}
filename_length = fget16LE(fp);
p->extrafield_length = fget16LE(fp);
if (p->file_name != NULL) {
xfseek(fp, filename_length, SEEK_CUR);
} else {
p->file_name = mem_alloc(filename_length + 1);
xfread(p->file_name, filename_length, 1, fp);
p->file_name[filename_length] = 0;
}
if (only_fname != NULL && strcmp(only_fname, (char*)p->file_name) != 0)
return 0; // Not interested in this one
p->offex = 30 + filename_length + p->extrafield_length;
return 1;
}
/**
* Process file in archive with legacy encryption. File pointer
* is assumed to be at the start of local extra fields.
*/
static int process_legacy(zip_file *zfp, zip_ptr *p)
{
FILE *fp = zfp->fp;
fprintf(stderr, "ver %d.%d ", p->version / 10, p->version % 10);
if ( (p->flags & FLAG_ENCRYPTED) &&
(p->version == 10 || p->version == 20 || p->version == 45)) {
uint16_t extra_len_used = 0;
while (extra_len_used < p->extrafield_length) {
uint16_t efh_id = fget16LE(fp);
uint16_t efh_datasize = fget16LE(fp);
fprintf(stderr, "efh %04x ", efh_id);
if (efh_id == 0x0001) {
handle_zip64_ef(fp, p, efh_datasize, 1);
extra_len_used += efh_datasize;
efh_datasize = 0;
}
xfseek(fp, efh_datasize, SEEK_CUR);
extra_len_used += 4 + efh_datasize;
}
if (p->version >= 20)
zfp->check_bytes = 1;
else if (zfp->check_bytes == 1)
fprintf(stderr, "** 2b ** ");
scan_for_data_descriptor(zfp, p);
if (p->cmptype != 0 && p->cmptype != 8) {
fprintf(stderr, "%s/%s is not encrypted, or stored with non-handled compression type=%"PRIu16"\n",
zfp->fname, p->file_name, p->cmptype);
return 0;
}
// Ok, now set checksum bytes. This will depend upon if from crc, or from timestamp
if (p->flags & FLAG_LOCAL_SIZE_UNKNOWN)
sprintf(p->cs, "%02x%02x", p->lastmod_time >> 8, p->lastmod_time & 0xFF);
else
sprintf(p->cs, "%02x%02x", (p->crc >> 24) & 0xFF, (p->crc >> 16) & 0xFF);
fprintf(stderr,
"%s/%s PKZIP%s Encr: %s%scmplen=%"PRIu64", decmplen=%"PRIu64", crc=%08X ts=%04X cs=%s type=%"PRIu16"\n",
jtr_basename(zfp->fname), p->file_name,
zfp->zip64 ? "64" : "",
zfp->check_bytes == 2 ? "2b chk, " : "",
p->flags & FLAG_LOCAL_SIZE_UNKNOWN ? "TS_chk, " : "",
p->cmp_len, p->decomp_len, p->crc, p->lastmod_time, p->cs, p->cmptype);
MEM_FREE(p->hash_data);
p->hash_data = mem_alloc(p->cmp_len + 1);
if (fread(p->hash_data, 1, p->cmp_len, fp) != p->cmp_len) {
fprintf(stderr, "Error, fread could not read the data from the file: %s\n", zfp->fname);
return 0;
}
return 1;
}
scan_for_data_descriptor(zfp, p);
fprintf(stderr, "%s/%s is not encrypted, or stored with non-handled compression type\n", zfp->fname, p->file_name);
xfseek(fp, p->extrafield_length, SEEK_CUR);
xfseek(fp, p->cmp_len, SEEK_CUR);
return 0;
}
static void move_entry(zip_ptr *dst, zip_ptr *src)
{
MEM_FREE(dst->file_name);
MEM_FREE(dst->hash_data);
memcpy(dst, src, sizeof(zip_ptr));
src->file_name = NULL;
src->hash_data = NULL;
}
/**
* Process next file in zip archive. ctx->curzip is assumed to
* be empty or populated with data from the central directory,
* and the position in the zip file is at the beginning of the
* local header of the according file.
*/
static void handle_file_entry(zip_context *ctx)
{
if (!load_local_header(&ctx->archive, &ctx->curzip)) {
//fprintf(stderr, "Skipping bad entry\n");
return;
}
if (ctx->curzip.cmptype == 99) {
// AES
if (process_aes(&ctx->archive, &ctx->curzip))
return;
fprintf(stderr, "Skipping bad AES entry\n");
return;
}
// Legacy
if (!process_legacy(&ctx->archive, &ctx->curzip))
return;
if (ctx->curzip.decomp_len < 4) {
fprintf(stderr, "Skipping short file %s\n", ctx->curzip.file_name);
return;
}
// Suitable file with legacy encryption
int i, j;
for (i = 0; i < ctx->num_candidates; i++) {
if (ctx->curzip.cmp_len < ctx->best_files[i].cmp_len) {
for (j = ctx->num_candidates; j > i; j--)
if (j < MAX_PKZ_FILES)
move_entry(&(ctx->best_files[j]), &(ctx->best_files[j - 1]));
break;
}
}
move_entry(&(ctx->best_files[i]), &ctx->curzip);
if (ctx->num_candidates < MAX_PKZ_FILES)
ctx->num_candidates++;
}
static void print_and_cleanup(zip_context *ctx);
static void init_zip_context(zip_context *ctx, const char *fname, FILE *fp)
{
memset(ctx, 0, sizeof(*ctx));
ctx->archive.fname = fname;
ctx->archive.check_bytes = 2;
ctx->archive.fp = fp;
}
/**
* Forward scan file for local file header signatures and try to
* process each of these entries. Might yield false positives, or
* have minor trouble with entries that don't have the size field
* of the header filled out (see handle_file_entry for details).
*/
static void scan_from_start(const char *fname)
{
FILE *fp;
zip_context ctx;
if (!(fp = fopen(fname, "rb"))) {
fprintf(stderr, "! %s : %s\n", fname, strerror(errno));
return;
}
init_zip_context(&ctx, fname, fp);
while (!feof(fp)) {
if (fgetc(fp) != 0x50 || !fexpect(fp, 0x4b))
continue;
if (fexpect(fp, 0x03) && fexpect(fp, 0x04)) { /* local header */
memset(&ctx.curzip, 0, sizeof(ctx.curzip));
handle_file_entry(&ctx);
#if 0 // Maybe not worth it - best case we skip over uninteresting parts, but might bail out too early on a false positive
} else if (fexpect(fp, 0x07) && fexpect(fp, 0x08)) { /* data descriptor */
xfseek(fp, 12, SEEK_CUR);
} else if ((fexpect(fp, 0x01) && fexpect(fp, 0x02))
|| (fexpect(fp, 0x05) && fexpect(fp, 0x06))) { /* central directory structures */
break;
#endif
}
}
fclose(fp);
// This will only print stuff if we found at least one old type encrypted file
print_and_cleanup(&ctx);
}
static void print_and_cleanup(zip_context *ctx)
{
int i = 1;
char *bname;
static int once;
char *filenames;
if (ctx->num_candidates == 0)
return;
filenames = xstrdup(ctx->best_files[0].file_name);
bname = jtr_basename(ctx->archive.fname);
if (ctx->num_candidates == 1) {
/* sanitize here, as we don't need the raw file_name any more when exiting this function */
replace(ctx->best_files[0].file_name, ':', ' ');
printf("%s/%s:$pkzip$%x*%x*", bname,
ctx->best_files[0].file_name,
ctx->num_candidates,
ctx->archive.check_bytes);
} else {
printf("%s:$pkzip$%x*%x*", bname, ctx->num_candidates, ctx->archive.check_bytes);
}
if (checksum_only)
i = 0;
for (; i < ctx->num_candidates; ++i) {
uint64_t len = 12+24;
if (i) {
filenames = mem_realloc(filenames,
strlen(filenames) +
strlen(ctx->best_files[i].file_name) + 3);
strcat(filenames, ", ");
strcat(filenames, ctx->best_files[i].file_name);
}
if (len > ctx->best_files[i].cmp_len)
len = ctx->best_files[i].cmp_len; // even though we 'could' output a '2', we do not. We only need one full inflate CRC check file.
printf("1*%x*%x*%"PRIx64"*%s*", 0, ctx->best_files[i].cmptype, (uint64_t)len, ctx->best_files[i].cs);
print_hex((unsigned char*)ctx->best_files[i].hash_data, len);
}
// Ok, now output the 'little' one (the first).
if (!checksum_only) {
printf("%x*%x*%"PRIx64"*%"PRIx64"*%x*%"PRIx64"*%"PRIx64"*%x*", 2, 0, ctx->best_files[0].cmp_len, ctx->best_files[0].decomp_len, ctx->best_files[0].crc, ctx->best_files[0].offset, ctx->best_files[0].offex, ctx->best_files[0].cmptype);
printf("%"PRIx64"*%s*", ctx->best_files[0].cmp_len, ctx->best_files[0].cs);
print_hex((unsigned char*)ctx->best_files[0].hash_data, ctx->best_files[0].cmp_len);
}
/* Don't allow our delimiter in there! */
replace(filenames, ':', ' ');
if (ctx->num_candidates > 1)
printf("$/pkzip$::%s:%s:%s\n", bname, filenames, ctx->archive.fname);
else
printf("$/pkzip$:%s:%s::%s\n", filenames, bname, ctx->archive.fname);
if (ctx->num_candidates > 1 && !once++)
fprintf(stderr,
"Note: It is assumed that all files in each archive have the same password.\n"
"If that is not the case, the hash may be uncrackable. To avoid this, use\n"
"option -o to pick a file at a time.\n");
// Give warning to user for potentially large output of zip2john
fprintf(stderr,
"Note: It is normal for some outputs to be very large\n");
for (i = 0; i < ctx->num_candidates; ++i) {
MEM_FREE(ctx->best_files[i].hash_data);
MEM_FREE(ctx->best_files[i].file_name);
}
MEM_FREE(filenames);
}
/**
* Open given file and scan for the central directory. From there,
* process every file in the archive that has a central directory
* entry.
*/
static void scan_central_index(const char *fname)
{
FILE *fp;
long filesize;
uint32_t this_disk, cd_start_disk;
uint64_t num_records, num_records_total;
uint64_t cd_size, cd_start_offset;
zip_context ctx = {};
if (!(fp = fopen(fname, "rb"))) {
fprintf(stderr, "! %s : %s\n", fname, strerror(errno));
return;
}
// File is now open, jump to cleanup on error
xfseek(fp, 0, SEEK_END);
filesize = ftell(fp);
if (filesize == -1) {
perror("ftell (trying to get filesize)");
goto cleanup;
}
// First, scan for end of central directory. Its length is 22+n bytes, where n can be up to 64k
if (filesize > 22 + 65535) {
xfseek(fp, -(22 + 65535), SEEK_END);
} else {
xfseek(fp, 0, SEEK_SET);
}
while (!feof(fp)) {
if (fgetc(fp) == 0x50 && fexpect(fp, 0x4b)) {
long found = 0;
int zip64 = 0;
if (fexpect(fp, 0x05) && fexpect(fp, 0x06)) {
// Old EOCD header
found = ftell(fp);
this_disk = fget16LE(fp);
cd_start_disk = fget16LE(fp);
}
else if (fexpect(fp, 0x06) && fexpect(fp, 0x06)) {