forked from openwall/john
-
Notifications
You must be signed in to change notification settings - Fork 0
/
7z2john.pl
executable file
·3953 lines (2856 loc) · 95.4 KB
/
7z2john.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use Compress::Raw::Lzma qw (LZMA_STREAM_END LZMA_DICT_SIZE_MIN);
use File::Basename;
# author:
# philsmd
# magnum (added proper handling of BCJ et. al. and adapt to JtR use)
# version:
# 1.9
# date released:
# April 2015
# date last updated:
# July 13 2022
# dependencies:
# Compress::Raw::Lzma
# File::Basename
# supported file types:
# - is able to identify and parse .7z files
# - is able to identify and parse splitted .7z files (.7z.001, .7z.002, ...)
# - is able to identify and parse regular (non-packed) .sfx files
# install dependencies like this:
# sudo cpan Compress::Raw::Lzma
# or sudo apt-get install libcompress-raw-lzma-perl
# or sudo perl -MCPAN -e 'install Compress::Raw::Lzma'
#
# Explanation of the "hash" format:
#
# the fields of this format are separate by the dollar ("$") sign:
# ("xyz" means that the string xyz is used literally, brackets indicate variables)
#
# "$"
# "7z"
# "$"
# [data type indicator] # see "Explanation of the data type indicator" below
# "$"
# [cost factor] # means: 2 ^ [cost factor] iterations
# "$"
# [length of salt]
# "$"
# [salt]
# "$"
# [length of iv] # the initialization vector length
# "$"
# [iv] # the initialization vector itself
# "$"
# [CRC32] # the actual "hash"
# "$"
# [length of encrypted data] # the encrypted data length in bytes
# "$"
# [length of decrypted data] # the decrypted data length in bytes
# "$"
# [encrypted data] # the encrypted (and possibly also compressed) data
# in case the data was not truncated and a decompression step is needed to verify the CRC32, these fields are appended:
# "$"
# [length of data for CRC32] # the length of the first "file" needed to verify the CRC32 checksum
# "$"
# [coder attributes] # most of the coders/decompressors need some attributes (e.g. encoded lc, pb, lp, dictSize values)
# in case a preprocessor is used, this field is appended:
# "$"
# [preprocessor attributes] # the preprocessor attributes are appended after the main coder attributes
#
# Explanation of the data type indicator
#
# This field is the first field after the hash signature (i.e. after "$7z$).
# Whenever the data was longer than the value of PASSWORD_RECOVERY_TOOL_DATA_LIMIT and the data could be truncated due to the padding attack,
# the value of this field will be set to 128.
#
# If no truncation is used:
# - the value will be 0 if the data doesn't need to be decompressed to check the CRC32 checksum
# - all values different from 128, but greater than 0, indicate that the data must be decompressed as follows:
#
# LOWER NIBBLE (4 bits, type & 0xf)
# - 1 means that the data must be decompressed using the LZMA1 decompressor
# - 2 means that the data must be decompressed using the LZMA2 decompressor
# - 3 means that the data must be decompressed using the PPMD decompressor
# - 4 reserved (future use)
# - 5 reserved (future use)
# - 6 means that the data must be decompressed using the BZIP2 decompressor
# - 7 means that the data must be decompressed using the DEFLATE decompressor
# - 8 .. 15 reserved (future use)
#
# UPPER NIBBLE (4 bits, (type >> 4) & 0xf)
# - 1 means that the data must be post-processed using BCJ (x86)
# - 2 means that the data must be post-processed using BCJ2 (four data streams needed)
# - 3 means that the data must be post-processed using PPC (big-endian)
# - 4 means that the data must be post-processed using IA64
# - 5 means that the data must be post-processed using ARM (little-endian)
# - 6 means that the data must be post-processed using ARMT (little-endian)
# - 7 means that the data must be post-processed using SPARC
# - 8 unavailable since this indicates TRUNCATION (128 == 0x80 == 8 << 4)
# - 9 means that the data must be post-processed using DELTA
# - 10 .. 15 reserved (future use)
# Truncated data can only be verified using the padding attack and therefore combinations between truncation + a compressor are not allowed.
# Therefore, whenever the value is 128 or 0, neither coder attributes nor the length of the data for the CRC32 check is within the output.
# On the other hand, for all values above or equal 1 and smaller than 128, both coder attributes and the length for CRC32 check is in the output.
# Whenever the data needs to be either (pre)processed by multiple (2+) filters or whenever the data needs to be decompressed by multiple (2+) decompression algorithms,
# the attribute list (the fields "preprocessor attributes" and "coder attributes" accordingly) will be a comma-separated list of fields with type, order/position and attribute indicators.
#
# The rules for this Multiple Compressor(s)/Preprocessor(s) list are as follows:
# - all (additional) methods/coders need to be specified in the output hash format (the fields "coder attributes" and "preprocessor attributes" accordingly), even if they have no attributes
# - the format is a comma-separated list of type and order indicator (for both fields: compressor and preprocessor attributes), followed by an underscore (_), followed by the attribute itself
# - the first attribute/item/codec for both fields (compressor and preprocessor attributes) needs no type and order indicator, only attributes
# - the type and position/order of the first decompressor/filter are implied (it's always the first decompressor/filter and the type is in the "data type indicator" field)
# - this also means that the field "data type indicator" (see format above) only indicates the first decompressor (if used) and (also, if used) the first preprocessing filter (this is also due to compatibility reasons with older formats)
# - for the additional (2+) decompressors/filters, the first number after the comma is the type and order indicator, after which follows an underscore (_) and the attributes themself (could be empty/no attribute value for some codecs)
# - special case: the "coder attributes" field could start with a comma (and has also a type and order indicator) in the unlikely case where during archive generation a filter was applied after the final compression of the data. Otherwise it's always the case that the decompressor needs to be applied first
#
# Multiple Compressor(s)/Preprocessor(s) type and order indicator (it is one combined field/number):
# - upper nibble (4 bits, indicator >> 4) indicates the compressor/preprocessor method/type/codec (LZMA2, Delta, BCJ etc)
# - lower nibble (4 bits, indicator & 0xf) indicates the order/position (the step at which the (de)compressor/(pre)processor needs to be applied). The values are incrementing, the counter starts with 1
#
# Constants
#
# cracker specific stuff
my $ANALYZE_ALL_STREAMS_TO_FIND_SHORTEST_DATA_BUF = 1;
my $SHOW_LIST_OF_ALL_STREAMS = 0; # $ANALYZE_ALL_STREAMS_TO_FIND_SHORTEST_DATA_BUF must be set to 1 to list/debug all streams
my $SHOW_UNSUPPORTED_CODER_WARNING = 1;
my $SHORTEN_HASH_LENGTH_TO_CRC_LENGTH = 1; # only output the bytes needed for the checksum of the first file (plus a fixed length
# header at the very beginning of the stream; plus additional +5% to cover the exception
# that the compressed file is slightly longer than the raw file)
my $SHORTEN_HASH_FIXED_HEADER = 32.5; # at the beginning of the compressed stream we have some header info
# (shortened hash can't really be shorter than the metadata needed for decompression)
# the extra +0.5 is used to round up (we use integer numbers)
my $SHORTEN_HASH_EXTRA_PERCENT = 5; # the compressed stream could be slightly longer than the underlying data (special cases)
# in percent: i.e. x % == (x / 100)
my $DISPLAY_SENSITIVE_DATA_WARNING = 1; # 0 means skip or use --skip-sensitive-data-warning
my $PASSWORD_RECOVERY_TOOL_NAME = "john";
my $PASSWORD_RECOVERY_TOOL_DATA_LIMIT = 0x80000000; # hexadecimal output value. This value should always be >= 64
my $PASSWORD_RECOVERY_TOOL_SUPPORT_PADDING_ATTACK = 1; # does the cracker support the AES-CBC padding attack (0 means no, 1 means yes)
my @PASSWORD_RECOVERY_TOOL_SUPPORTED_DECOMPRESSORS = (1, 2, 6, 7); # within this list we only need values ranging from 1 to 7
# i.e. SEVEN_ZIP_LZMA1_COMPRESSED to SEVEN_ZIP_DEFLATE_COMPRESSED
my @PASSWORD_RECOVERY_TOOL_SUPPORTED_PREPROCESSORS = (1, 2, 3, 4, 5, 6, 7, 8); # BCJ2 can be "supported" by ignoring CRC
my $PASSWORD_RECOVERY_TOOL_SUPPORT_MULTIPLE_DECOMPRESSORS = 0; # does the cracker support more than one compressing algorithms for the same file (e.g. LZMA2 + LZMA1)
my $PASSWORD_RECOVERY_TOOL_SUPPORT_MULTIPLE_PREPROCESSORS = 0; # does the cracker support more than one preprocessing filters for the same file (e.g. BCJ + Delta)
# 7-zip specific stuff
my $LZMA2_MIN_COMPRESSED_LEN = 16; # the raw data (decrypted) needs to be at least: 3 + 1 + 1, header (start + size) + at least one byte of data + end
# therefore we need to have at least one AES BLOCK (128 bits = 16 bytes)
# header
my $SEVEN_ZIP_MAGIC = "7z\xbc\xaf\x27\x1c";
my $SEVEN_ZIP_MAGIC_LEN = 6; # fixed length of $SEVEN_ZIP_MAGIC
my $SEVEN_ZIP_END = "\x00";
my $SEVEN_ZIP_HEADER = "\x01";
my $SEVEN_ZIP_ARCHIVE_PROPERTIES = "\x02";
my $SEVEN_ZIP_ADD_STREAMS_INFO = "\x03";
my $SEVEN_ZIP_MAIN_STREAMS_INFO = "\x04";
my $SEVEN_ZIP_FILES_INFO = "\x05";
my $SEVEN_ZIP_PACK_INFO = "\x06";
my $SEVEN_ZIP_UNPACK_INFO = "\x07";
my $SEVEN_ZIP_SUBSTREAMS_INFO = "\x08";
my $SEVEN_ZIP_SIZE = "\x09";
my $SEVEN_ZIP_CRC = "\x0a";
my $SEVEN_ZIP_FOLDER = "\x0b";
my $SEVEN_ZIP_UNPACK_SIZE = "\x0c";
my $SEVEN_ZIP_NUM_UNPACK_STREAM = "\x0d";
my $SEVEN_ZIP_EMPTY_STREAM = "\x0e";
my $SEVEN_ZIP_EMPTY_FILE = "\x0f";
my $SEVEN_ZIP_ANTI_FILE = "\x10";
my $SEVEN_ZIP_NAME = "\x11";
my $SEVEN_ZIP_CREATION_TIME = "\x12";
my $SEVEN_ZIP_ACCESS_TIME = "\x13";
my $SEVEN_ZIP_MODIFICATION_TIME = "\x14";
my $SEVEN_ZIP_WIN_ATTRIBUTE = "\x15";
my $SEVEN_ZIP_ENCODED_HEADER = "\x17";
my $SEVEN_ZIP_START_POS = "\x18";
my $SEVEN_ZIP_DUMMY = "\x19";
my $SEVEN_ZIP_MAX_PROPERTY_TYPE = 2 ** 30; # 1073741824
my $SEVEN_ZIP_NOT_EXTERNAL = "\x00";
my $SEVEN_ZIP_EXTERNAL = "\x01";
my $SEVEN_ZIP_ALL_DEFINED = "\x01";
my $SEVEN_ZIP_FILE_NAME_END = "\x00\x00";
# codec
my $SEVEN_ZIP_AES = "\x06\xf1\x07\x01"; # all the following codec values are from CPP/7zip/Archive/7z/7zHeader.h
my $SEVEN_ZIP_LZMA1 = "\x03\x01\x01";
my $SEVEN_ZIP_LZMA2 = "\x21";
my $SEVEN_ZIP_PPMD = "\x03\x04\x01";
my $SEVEN_ZIP_BCJ = "\x03\x03\x01\x03";
my $SEVEN_ZIP_BCJ2 = "\x03\x03\x01\x1b";
my $SEVEN_ZIP_PPC = "\x03\x03\x02\x05";
my $SEVEN_ZIP_ALPHA = "\x03\x03\x03\x01";
my $SEVEN_ZIP_IA64 = "\x03\x03\x04\x01";
my $SEVEN_ZIP_ARM = "\x03\x03\x05\x01";
my $SEVEN_ZIP_ARMT = "\x03\x03\x07\x01";
my $SEVEN_ZIP_SPARC = "\x03\x03\x08\x05";
my $SEVEN_ZIP_BZIP2 = "\x04\x02\x02";
my $SEVEN_ZIP_DEFLATE = "\x04\x01\x08";
my $SEVEN_ZIP_DELTA = "\x03";
my $SEVEN_ZIP_COPY = "\x00";
# hash format
my $SEVEN_ZIP_HASH_SIGNATURE = "\$7z\$";
my $SEVEN_ZIP_DEFAULT_POWER = 19;
my $SEVEN_ZIP_DEFAULT_IV = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
my $SEVEN_ZIP_UNCOMPRESSED = 0;
my $SEVEN_ZIP_LZMA1_COMPRESSED = 1;
my $SEVEN_ZIP_LZMA2_COMPRESSED = 2;
my $SEVEN_ZIP_PPMD_COMPRESSED = 3;
my $SEVEN_ZIP_BZIP2_COMPRESSED = 6;
my $SEVEN_ZIP_DEFLATE_COMPRESSED = 7;
my $SEVEN_ZIP_BCJ_PREPROCESSED = 1;
my $SEVEN_ZIP_BCJ2_PREPROCESSED = 2;
my $SEVEN_ZIP_PPC_PREPROCESSED = 3;
my $SEVEN_ZIP_IA64_PREPROCESSED = 4;
my $SEVEN_ZIP_ARM_PREPROCESSED = 5;
my $SEVEN_ZIP_ARMT_PREPROCESSED = 6;
my $SEVEN_ZIP_SPARC_PREPROCESSED = 7;
# 8 conflicts with SEVEN_ZIP_TRUNCATED (128 == 0x80 == 8 << 4)
my $SEVEN_ZIP_DELTA_PREPROCESSED = 9;
my $SEVEN_ZIP_TRUNCATED = 128; # (0x80 or 0b10000000)
my %SEVEN_ZIP_COMPRESSOR_NAMES = (1 => "LZMA1", 2 => "LZMA2", 3 => "PPMD", 6 => "BZIP2", 7 => "DEFLATE",
(1 << 4) => "BCJ", (2 << 4) => "BCJ2", (3 << 4) => "PPC", (4 << 4) => "IA64",
(5 << 4) => "ARM", (6 << 4) => "ARMT", (7 << 4) => "SPARC", (9 << 4) => "DELTA");
#
# Helper functions
#
sub usage
{
my $prog_name = shift;
print STDERR "Usage: $prog_name <7-Zip file>...\n";
}
my $memory_buffer_read_offset = 0;
sub my_read
{
my $input = shift;
my $length = shift;
my $type_of_input = ref ($input);
my $output_buffer = "";
if ($type_of_input eq "GLOB")
{
read $input, $output_buffer, $length;
}
elsif ($type_of_input eq "HASH")
{
my $cur_file_handle = $$input{0}{'fh'};
my $cur_file_number = $$input{0}{'num'};
my $bytes_read = 0;
while ($bytes_read != $length)
{
my $name = $$input{$cur_file_number}{'name'};
my $start = $$input{$cur_file_number}{'start'};
my $size = $$input{$cur_file_number}{'size'};
my $cur_file_bytes_avail = ($start + $size) - $memory_buffer_read_offset;
if ($cur_file_bytes_avail < 1)
{
print STDERR "ERROR: failed to get the correct file offsets of splitted archive file '$name'\n";
exit (1);
}
my $total_bytes_to_read = $length - $bytes_read;
my $bytes_to_read = $total_bytes_to_read;
if ($bytes_to_read > $cur_file_bytes_avail)
{
$bytes_to_read = $cur_file_bytes_avail;
}
# append the current bytes read from the file to the overall output buffer
my $temp_output_buffer = "";
my $bytes = read ($cur_file_handle, $temp_output_buffer, $bytes_to_read);
$output_buffer .= $temp_output_buffer;
if ($bytes != $bytes_to_read)
{
print STDERR "ERROR: could not read from splitted 7z file '$name'\n";
exit (1);
}
$bytes_read += $bytes_to_read;
$memory_buffer_read_offset += $bytes_to_read;
# the following case only happens if we need to read across 2 or more files
if ($bytes_read != $length)
{
# we exhausted the current file, move to the next one!
close ($cur_file_handle);
$cur_file_number++;
if (! exists ($$input{$cur_file_number}))
{
my $name_prefix = get_splitted_archive_raw_name ($name);
print STDERR "ERROR: could not open part #$cur_file_number of the splitted archive file '$name_prefix'\n";
exit (1);
}
my $name = $$input{$cur_file_number}{'name'};
if (! open ($cur_file_handle, "<$name"))
{
print STDERR "ERROR: could not open the splitted archive file '$name' for reading\n";
exit (1);
}
$$input{0}{'fh'} = $cur_file_handle;
$$input{0}{'num'} = $cur_file_number;
}
}
}
else
{
$output_buffer = substr ($$input, $memory_buffer_read_offset, $length);
$memory_buffer_read_offset += $length;
}
return $output_buffer;
}
sub my_tell
{
my $input = shift;
my $res = 0;
my $type_of_input = ref ($input);
if ($type_of_input eq "HASH")
{
$res = $memory_buffer_read_offset;
}
else
{
$res = tell ($input);
}
return $res;
}
sub my_seek
{
my $input = shift;
my $offset = shift;
my $whence = shift;
my $res = 0;
my $type_of_input = ref ($input);
if ($type_of_input eq "HASH")
{
# get total number of files and total/accumulated file size
my $number_of_files= 1;
# we assume that $$input{1} exists (we did already check that beforehand)
my $end = 0;
while (exists ($$input{$number_of_files}))
{
$end = $$input{$number_of_files}{'start'} + $$input{$number_of_files}{'size'};
$number_of_files++;
}
my $new_offset = 0;
# absolute (from start)
if ($whence == 0)
{
$new_offset = $offset;
}
# relative (depending on current position)
elsif ($whence == 1)
{
$new_offset = $memory_buffer_read_offset + $offset;
}
# offset from the end of the file
else
{
$new_offset = $end + $offset;
}
# sanity check
if (($new_offset < 0) || ($new_offset > $end))
{
my $name = get_splitted_archive_raw_name ($$input{1}{'name'});
print STDERR "ERROR: could not seek within the splitted archive '$name'\n";
exit (1);
}
$memory_buffer_read_offset = $new_offset;
# check if the correct file is open
# 1. determine the correct file
# 2. if the "incorrect" file is open, close it and open the correct one
my $cur_file_number = 1;
my $file_was_found = 0;
my $start = 0;
my $size = 0;
while (exists ($$input{$cur_file_number}))
{
$start = $$input{$cur_file_number}{'start'};
$size = $$input{$cur_file_number}{'size'};
my $end = $start + $size;
if ($memory_buffer_read_offset >= $start)
{
if ($memory_buffer_read_offset < $end)
{
$file_was_found = 1;
last;
}
}
$cur_file_number++;
}
if ($file_was_found == 0)
{
my $name = get_splitted_archive_raw_name ($$input{1}{'name'});
print STDERR "ERROR: could not read the splitted archive '$name' (maybe some parts are missing?)\n";
exit (1);
}
if ($$input{0}{'num'} != $cur_file_number)
{
# if we enter this block, we definitely need to "change" to another file
close ($$input{0}{'fh'});
my $name = $$input{$cur_file_number}{'name'};
my $seven_zip_file;
if (! open ($seven_zip_file, "<$name"))
{
print STDERR "ERROR: could not open the file '$name' for reading\n";
exit (1);
}
$$input{0}{'fh'} = $seven_zip_file;
$$input{0}{'num'} = $cur_file_number;
}
# always seek w/ absolute positions within the splitted part!
$res = seek ($$input{0}{'fh'}, $memory_buffer_read_offset - $start, 0);
}
else
{
$res = seek ($input, $offset, $whence);
}
return $res;
}
sub get_uint32
{
my $fp = shift;
my $bytes = my_read ($fp, 4);
return (0, 0) if (length ($bytes) != 4);
my $num = unpack ("L", $bytes);
return $num;
}
sub get_uint64
{
my $fp = shift;
my $bytes = my_read ($fp, 8);
return (0, 0) if (length ($bytes) != 8);
my ($uint1, $uint2) = unpack ("LL<", $bytes);
my $num = $uint2 << 32 | $uint1;
return $bytes, $num;
}
sub read_number
{
my $fp = shift;
my $b = ord (my_read ($fp, 1));
if (($b & 0x80) == 0)
{
return $b;
}
my $value = ord (my_read ($fp, 1));
for (my $i = 1; $i < 8; $i++)
{
my $mask = 0x80 >> $i;
if (($b & $mask) == 0)
{
my $high = $b & ($mask - 1);
$value |= ($high << ($i * 8));
return $value;
}
my $next = ord (my_read ($fp, 1));
$value |= ($next << ($i * 8));
}
return $value;
}
sub num_to_id
{
my $num = shift;
# special case:
return "\x00" if ($num == 0);
# normal case:
my $id = "";
while ($num > 0)
{
my $value = $num & 0xff;
$id = chr ($value) . $id;
$num >>= 8;
}
return $id;
}
sub read_id
{
my $fp = shift;
my $id;
my $num = read_number ($fp);
# convert number to their ASCII code correspondent byte
return num_to_id ($num);
}
sub get_boolean_vector
{
my $fp = shift;
my $number_items = shift;
my @booleans;
# get the values
my $v = 0;
my $mask = 0;
for (my $i = 0; $i < $number_items; $i++)
{
if ($mask == 0)
{
my $byte = my_read ($fp, 1);
$v = ord ($byte);
$mask = 0x80;
}
my $val = ($v & $mask) != 0;
push (@booleans, $val);
$mask >>= 1;
}
return @booleans;
}
sub get_boolean_vector_check_all
{
my $fp = shift;
my $number_items = shift;
my @booleans;
# check first byte to see if all are defined
my $all_defined = my_read ($fp, 1);
if ($all_defined eq $SEVEN_ZIP_ALL_DEFINED)
{
@booleans = (1) x $number_items;
}
else
{
@booleans = get_boolean_vector ($fp, $number_items);
}
return @booleans;
}
sub is_supported_seven_zip_file
{
my $fp = shift;
my $magic_len = length ($SEVEN_ZIP_MAGIC);
my $signature = my_read ($fp, $magic_len);
return $signature eq $SEVEN_ZIP_MAGIC;
}
sub get_decoder_properties
{
my $attributes = shift;
my $salt_len;
my $salt_buf;
my $iv_len;
my $iv_buf;
my $number_cycles_power;
# set some default values
$salt_len = 0;
$salt_buf = "";
$iv_len = length ($SEVEN_ZIP_DEFAULT_IV);
$iv_buf = $SEVEN_ZIP_DEFAULT_IV;
$number_cycles_power = $SEVEN_ZIP_DEFAULT_POWER;
# the most important information is encoded in first and second byte
# i.e. the salt/iv length, number cycle power
my $offset = 0;
my $first_byte = substr ($attributes, 0, 1);
$first_byte = ord ($first_byte);
$offset++;
$number_cycles_power = $first_byte & 0x3f;
if (($first_byte & 0xc0) == 0)
{
return ($salt_len, $salt_buf, $iv_len, $iv_buf, $number_cycles_power);
}
$salt_len = ($first_byte >> 7) & 1;
$iv_len = ($first_byte >> 6) & 1;
# combine this info with the second byte
my $second_byte = substr ($attributes, 1, 1);
$second_byte = ord ($second_byte);
$offset++;
$salt_len += ($second_byte >> 4);
$iv_len += ($second_byte & 0x0f);
$salt_buf = substr ($attributes, $offset, $salt_len);
$offset += $salt_len;
$iv_buf = substr ($attributes, $offset, $iv_len);
# pad the iv with zeros
my $iv_max_length = 16;
$iv_buf .= "\x00" x $iv_max_length;
$iv_buf = substr ($iv_buf, 0, $iv_max_length);
return ($salt_len, $salt_buf, $iv_len, $iv_buf, $number_cycles_power);
}
sub get_digest
{
my $index = shift;
my $unpack_info = shift;
my $substreams_info = shift;
my $digest;
my $digests_unpack_info = $unpack_info->{'digests'};
my $digests_substreams_info = $substreams_info->{'digests'};
my $use_unpack_info = 0;
my $use_substreams_info = 0;
if (defined ($digests_unpack_info))
{
my $digests_unpack_info_size = 0;
if (@$digests_unpack_info)
{
$digests_unpack_info_size = scalar (@$digests_unpack_info);
}
if ($index < $digests_unpack_info_size)
{
if (ref (@$digests_unpack_info[$index]) eq "HASH")
{
$use_unpack_info = 1;
}
}
}
if (defined ($digests_substreams_info))
{
my $digests_substreams_info_size = 0;
if (@$digests_substreams_info)
{
$digests_substreams_info_size = scalar (@$digests_substreams_info);
}
if ($index < $digests_substreams_info_size)
{
if (ref (@$digests_substreams_info[$index]) eq "HASH")
{
$use_substreams_info = 1;
}
}
}
if ($use_unpack_info == 1)
{
$digest = @$digests_unpack_info[$index];
}
elsif ($use_substreams_info == 1)
{
$digest = @$digests_substreams_info[$index];
}
return $digest;
}
sub has_encrypted_header
{
my $folder = shift;
my $encrypted;
# get first coder
my $coders = $folder->{'coders'};
# get attributes of the first coder
my $attributes = @$coders[0]->{'codec_id'};
if ($attributes eq $SEVEN_ZIP_AES)
{
$encrypted = 1;
}
else
{
$encrypted = 0;
}
return $encrypted;
}
sub lzma_properties_decode
{
my $attributes = shift;
my $lclppb;
$lclppb = substr ($attributes, 0, 1);
my @data;
#data[0] is the lclppb value
$data[1] = ord (substr ($attributes, 1, 1));
$data[2] = ord (substr ($attributes, 2, 1));
$data[3] = ord (substr ($attributes, 3, 1));
$data[4] = ord (substr ($attributes, 4, 1));
my $dict_size = $data[1] | $data[2] << 8 | $data[3] << 16 | $data[4] << 24;
if ($dict_size < LZMA_DICT_SIZE_MIN)
{
$dict_size = LZMA_DICT_SIZE_MIN;
}
my $d = ord ($lclppb);
my $lc = int ($d % 9);
$d = int ($d / 9);
my $pb = int ($d / 5);
my $lp = int ($d % 5);
return ($lclppb, $dict_size, $lc, $pb, $lp);
}
sub lzma_alone_header_field_encode
{
my $num = shift;
my $length = shift;
my $value;
my $length_doubled = $length * 2;
my $big_endian_val = pack ("H*", sprintf ("%0${length_doubled}x", $num));
# what follows is just some easy way to convert endianess (there might be better ways of course)
$value = "";
for (my $i = $length - 1; $i >= 0; $i--)
{
$value .= substr ($big_endian_val, $i, 1);
}
return $value;
}
sub show_empty_streams_info_warning
{
my $file_path = shift;
print STDERR "WARNING: the file '" . $file_path . "' does not contain any meaningful data (the so-called streams info), it might only contain a list of empty files.\n";
}
sub fill_additional_attribute_list
{
my $coder = shift;
my $file_path = shift;
my $coder_attributes = shift;
my $error = 0;
my $codec_id = $$coder->{'codec_id'};
my $type = $SEVEN_ZIP_UNCOMPRESSED;
my $is_preprocessor = 0;
if ($codec_id eq $SEVEN_ZIP_LZMA1)
{
$type = $SEVEN_ZIP_LZMA1_COMPRESSED;
}
elsif ($codec_id eq $SEVEN_ZIP_LZMA2)
{
$type = $SEVEN_ZIP_LZMA2_COMPRESSED;
}
elsif ($codec_id eq $SEVEN_ZIP_PPMD)
{
$type = $SEVEN_ZIP_PPMD_COMPRESSED;
}
elsif ($codec_id eq $SEVEN_ZIP_BZIP2)
{
$type = $SEVEN_ZIP_BZIP2_COMPRESSED;
}
elsif ($codec_id eq $SEVEN_ZIP_DEFLATE)
{
$type = $SEVEN_ZIP_DEFLATE_COMPRESSED;
}
elsif ($codec_id eq $SEVEN_ZIP_COPY)
{
return 0; # don't add it to our coder_attributes list, it's a NO-OP
}
elsif ($codec_id eq $SEVEN_ZIP_BCJ)
{
$type = $SEVEN_ZIP_BCJ_PREPROCESSED;
$is_preprocessor = 1;
}
elsif ($codec_id eq $SEVEN_ZIP_BCJ2)
{
$type = $SEVEN_ZIP_BCJ2_PREPROCESSED;
$is_preprocessor = 1;
}
elsif ($codec_id eq $SEVEN_ZIP_PPC)
{
$type = $SEVEN_ZIP_PPC_PREPROCESSED;
$is_preprocessor = 1;
}
elsif ($codec_id eq $SEVEN_ZIP_IA64)
{
$type = $SEVEN_ZIP_IA64_PREPROCESSED;
$is_preprocessor = 1;
}
elsif ($codec_id eq $SEVEN_ZIP_ARM)
{
$type = $SEVEN_ZIP_ARM_PREPROCESSED;
$is_preprocessor = 1;
}
elsif ($codec_id eq $SEVEN_ZIP_ARMT)
{
$type = $SEVEN_ZIP_ARMT_PREPROCESSED;
$is_preprocessor = 1;
}
elsif ($codec_id eq $SEVEN_ZIP_SPARC)
{
$type = $SEVEN_ZIP_SPARC_PREPROCESSED;
$is_preprocessor = 1;
}
elsif ($codec_id eq $SEVEN_ZIP_DELTA)
{
$type = $SEVEN_ZIP_DELTA_PREPROCESSED;
$is_preprocessor = 1;
}
else
{
print STDERR "WARNING: unsupported coder with codec id '0x" . unpack ("H*", $codec_id) . "' in file '" . $file_path . "' found.\n";
$error = 1;
return $error;
}
my $attributes = undef;
if ($type != $SEVEN_ZIP_UNCOMPRESSED)