Repository navigation
Expand file tree
/
Copy pathDeXRAY.pl
More file actions
3023 lines (2674 loc) · 124 KB
/
Copy pathDeXRAY.pl
File metadata and controls
3023 lines (2674 loc) · 124 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
#!/usr/bin/perl
#
# dexray v2.36, copyright by Hexacorn.com, 2010-2025
#
# This is a simple script that attempts to decrypt:
# - Quarantine files from various AV / security products
# - Log and metadata encrypted files from various AV companies/products
# - Portable Executable files embedded in an encrypted form within
# other files (using encryption relying on a one-byte xor key).
# The decryption is generic and is based on the X-RAY algorithm.
#
# Note: the tool scans directories recursively
#
# File types that dexray attempts to handle:
# * AhnLab (V3B)
# * Amiti (IFC)
# * ASquared (EQF)
# * Avast/AVG (Magic@0='-chest- ')
# * Avira (QUA)
# * Baidu (QV)
# * BitDefender (BDQ)
# * BullGuard (Q)
# * Cisco AMP
# * CMC Antivirus (CMC)
# * Comodo <GUID> (not really; Quarantined files are not encrypted :)
# * ESafe (VIR)
# * ESET (NQF)
# * F-Prot (TMP) (Magic@0='KSS')
# * G-Data (Q) (Magic@0=0xCAFEBABE)
# * K7 Antivirus (<md5>.QNT)
# * Kaspersky (KLQ, System Watcher's <md5>.bin)
# * Lavasoft AdAware (BDQ) /BitDefender files really/
# * Lumension LEMSS (lqf)
# * MalwareBytes Data files (DATA) - 2 versions
# * MalwareBytes Quarantine files (QUAR) - 2 versions
# * McAfee Quarantine files (BUP) /full support for OLE format/
# * Microsoft Antimalware / Microsoft Security Essentials
# * Microsoft Defender PC (Magic@0=0B AD|D3 45) - D3 45 C5 99 metadata + 0B AD malicious content
# * Microsoft Defender Mac (Magic@0=75 6E)
# * Panda <GUID> Zip files
# * Sentinel One (MAL)
# * Spybot - Search & Destroy 2 'recovery'
# * SUPERAntiSpyware (SDB)
# * Symantec ccSubSdk files: {GUID} files and submissions.idx
# * Symantec Quarantine Data files (QBD)
# * Symantec Quarantine files (VBN), including from SEP on Linux
# * Symantec Quarantine Index files (QBI)
# * Symantec Quarantine files on MAC (quarantine.qtn)
# * Total AV ({GUID}.dat) 'infected'
# * Total Defense (BDQ) /BitDefender files really/
# * TrendMicro (Magic@0=A9 AC BD A7 which is a 'VSBX' string ^ 0xFF)
# * QuickHeal <hash> files
# * Vipre (<GUID>_ENC2)
# * Zemana <hash> files+quarantine.db
# * Any binary file (using X-RAY scanning)
#
# Usage:
# perl DeXRAY.pl <filename or directory>
#
# History
# 2025-12-03 - Brian added yet another fix, this time for Windows Defender files. thx!
# 2025-02-24 - Added Fortinet (thx to TheMythologist who implemented it based on https://github.com/NUKIB/maldump)
# 2024-09-11 - Fixed some architecture-related bugs (hopefully!)
# 2023-10-13 - Kaspersky Security for Windows Server Fixed -> 2.33; thx RevD17!
# 2022-01-21 - Fixed support for Defender for Mac (thx to Oskar)
# 2021-11-10 - Added support for Defender for Mac (thx to Jordan Meurer)
# 2021-04-12 - Tony pointed out bug/feature with recursive directory scan and offered a fix (thx!)
# 2020-11-23 - Brian added yet another type of symantec files on Linux (different offset)
# 2020-11-18 - Added Total Defence AV (
# 2020-11-17 - Added Total AV {GUID}.dat 'infected'
# 2020-11-16 - Added K7 QNT files (<md5>.QNT)
# 2020-11-10 - Added G-Data Q files (Magic@0=0xCAFEBABE)
# 2020-10-17 - Added Amiti IFC files
# 2020-09-18 - Thanks to @r0ns3n added Cisco AMP decryption & updated X-Ray detection
# 2020-09-16 - Brian did it again -- lots of new code to handle all known variants of VBNs
# 2020-01-03 - Brian revisited the VBN routine and rewritten big part of it
# 2019-11-09 - added a quick hack to always extract 2 alternative versions of VBN Quarantine files
# one of them should be always right; not sure what causes it, needs further research
# 2019-07-18 - added full support for Windows Defender (now handles both malicious code & meta)
# 2018-05-12 - added Microsoft Antimalware / Microsoft Security Essentials (thx Corey Forman /fetchered/).
# 2018-04-24 - added decryption for MAL files from Sentinel One (thx @MrAdz350)
# 2018-04-23 - Brian added extraction of metadata for Linux VBN files (thx @bmmaloney97!)
# 2018-04-21 - yet another fix for VBN files (thx @bmmaloney97 & @shotgunner101)
# 2018-03-26 - better fix for VBN files (thx @bmmaloney97)
# 2018-03-08 - temp. fix for SEP decryption to handle some corner cases (needs more research)
# 2018-03-01 - Brian Maloney did an incredible work adding a much better support for VBN files (thx @bmmaloney97)
# 2018-01-26 - added Kaspersky System Watcher (thx @countuponsec)
# 2017-11-09 - added new MBAM
# 2017-10-07 - added Symantec for MAC
# 2017-09-29 - added Zemana
# 2017-09-23 - added BullGuard
# 2017-03-09 - added generic carving routine to allow extraction of troublesome files
# 2017-03-06 - added better code to handle McAfee BUP files (thx @bmmaloney97) /v1.9 not published/
# 2017-01-28 - added Lumension LEMSS (thx @JamesHabben) /v1.8 not published/
# 2016-09-18 - added a buggy routine that attempts to interpret ccSubSdk files; it does work relatively well on some files,
# and on others it fails miserably; pure static file-format based analysis, that's why :)
# 2016-09-15 - added Symantec ccSubSdk files: {GUID} files and submissions.idx
# 2016-06-25 - added Microsoft Defender (D3 45 C5 99 header)
# 2016-05-20 - added ESafe VIR
# 2016-05-20 - added Spybot - Search & Destroy 2 'recovery'
# 2016-04-06 - confirmed Comodo stores Quarantine files w/o encryption :)
# 2016-04-05 - added CMC Antivirus
# 2016-04-04 - added Baidu Antivirus
# 2016-04-04 - confirmed Lavasoft AdAware to be identical with BitDefender
# 2016-04-03 - added F-Prot
# 2016-04-01 - added QuickHeal
# 2016-03-31 - added Panda
# 2016-03-29 - added AhnLab
# 2016-03-28 - added Vipre
# 2016-03-26 - added Bitdefender & Avira
# 2016-03-20 - added Avast/AVG
# 2012-2016 - added ESET, ASquared, Symantec QBD/QBI, Trend, etc.
# 2012-09-22 - added support for new VBN files (xor A5 + F6 xx xx FF FF chunks)
# 2012-01-23 - fixed minor bug in X-RAY loop
# 2012-01-05 - first public release
# 2010-2012 - private version handling a couple of formats, including
# QUAR/DATA, VBN, BUP, X-RAY
#
# References:
# Trend Micro decrypter
# https://docs.trendmicro.com/all/ent/iwsva/v5.5/en-us/iwsva_5.5_olh/decrypt_encrypted_quarantine_files.htm
# http://solutionfile.trendmicro.com/SolutionFile/11435/en/vsencode.zip
# http://docs.trendmicro.com/all/ent/tmcm/v3.5/en-us/tmcm_3.5_olh/Template_Files/decrypt_encrypted_quarantine_files.htm
# Symantec VBN extractor
# https://github.com/conix-security/VBNExtract/blob/master/extractVBN.c
# BUP extractor
# https://github.com/herrcore/punbup/blob/master/punbup.py
# Various Quarantine formats
# https://github.com/brad-accuvant/cuckoo-modified/blob/master/lib/cuckoo/common/quarantine.py
use strict;
use warnings;
use Crypt::RC4;
use Digest::CRC qw(crc32);
use Digest::MD5 qw (md5 );
use Crypt::Blowfish;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use Compress::Raw::Zlib;
use MIME::Base64;
use OLE::Storage_Lite;
use Encode qw(decode);
$| = 1;
my %output_files;
print STDERR "
=================================================================
dexray v2.37, copyright by Hexacorn.com, 2010-2025
Trend&Kaspersky decryption based on code by Optiv
McAfee BUP decryption code by Brian Maloney
Much better Symantec VBN support code by Brian Maloney
Kaspersky System Watcher decryption by Luis Rocha&Antonio Monaca
Sentinel One decryption research by MrAdz350
Microsoft AV/Security Essentials by Corey Forman /fetchered/
Cisco AMP research by \@r0ns3n
Fortinet code by TheMythologist
Thx to Brian Baskin, James Habben, Brian Maloney, Luis Rocha,
Antonio Monaca, MrAdz350, Corey Forman /fetchered/, \@r0ns3n
Tony, Jordan Meurer, Oskar, RevD17, Roman D., TheMythologist,
Maldump team
=================================================================
";
my $target = shift or die "\n\nError: Gimme a filename or dir (use '.' for a current directory)\n";
if (-d $target)
{
scan ($target);
}
elsif (-f $target)
{
processonefile ($target);
}
else
{
print "\n\nError: Don't know what to do with '$target'\n";
}
exit(0);
######################
sub scan
{
my $subdir = shift;
$subdir =~ s/^\.\///;
print STDERR "Processing directory: '$subdir'\n";
opendir(DIR, $subdir);
my @sorted_subdir = sort readdir DIR;
closedir DIR;
foreach my $filename (@sorted_subdir)
{
next if $filename =~ /^\.{1,2}$/;
my $fullpath = $subdir.'/'.$filename;
if (-d $fullpath)
{
scan ($fullpath);
next;
}
processonefile ($fullpath);
}
}
######################
sub processonefile
{
my $file = shift;
$file =~ s/^\.\///;
return if defined $output_files{$file};
return if $file=~/out_[0-9a-f]{8}_[0-9a-f]{8}/i;
# Filter .rpt, .log and .db files
return if $file=~/\.(log|rpt|db|db-wal)$/;
print STDERR "Processing file: '$file'\n";
if (! -f $file)
{
print STDERR " -> Can't be found! (check attributes/access rights)\n";
return;
}
my $filesize = -s $file;
if ($filesize == 0)
{
print STDERR " -> Skipping cuz it's empty \n";
return;
}
my $data = readfile ($file, 0, $filesize);
my $datalen = length($data);
if ($filesize != $datalen)
{
print STDERR " -> Skipping cuz something funny happened during data reading (investigate)\n";
return;
}
## --> AhnLab V3B files
if ($file =~ /\.v3b$/i || substr($data,0,16) eq 'AhnLab Inc. 2006')
{
extract_ahnlab ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> ASquared EQF Files
if ($file =~ /\.eqf$/i)
{
extract_asquared ($file, $data, 0x0000000, $filesize);
goto skipxray;
}
## --> Avast/AVG chest files
if (substr($data,0,8) eq '-chest- ')
{
extract_avast_avg ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Avira QUA Files
if ($file =~ /\.qua$/i || substr($data,0,11) eq 'AntiVir Qua')
{
extract_avira ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Baidu QV Files
if ($file =~ /\.qv$/i)
{
extract_baidu ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> BitDefender, Lavasoft AdAware, Total Defence BDQ Files
if ($file =~ /\.bdq$/i)
{
extract_bitdefender ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
if ($file =~ /\.q$/i)
{
## --> G-Data Q Files
if ($data=~/\xCA\xFE\xBA\xBE/)
{
extract_gdata ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> BullGuard Q Files
extract_bullguard ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Cisco AMP
if ($file =~ /(^|[\/\\])qrt[a-z0-9]{16}\.[0-9]{3}/i)
{
extract_cisco_amp ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> CMC Antivirus CMC Files
if ($file =~ /\.cmc$/i && substr($data,0,23) eq 'CMC Quarantined Malware')
{
extract_cmc ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> ESafe VIR Files
if ($file =~ /\.vir$/i)
{
extract_esafe ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Amiti IFC Files
if ($file =~ /\.ifc$/i)
{
extract_amiti ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> ESET NQF Files
if ($file =~ /\.nqf$/i)
{
extract_eset ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> F-Prot TMP Files
if ($file =~ /\.tmp$/i||$data=~/^KSS/)
{
extract_fprot ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Kaspersky KLQ files
if ($file =~ /\.klq$/i || (substr($data,0,4)) eq 'KLQB')
{
extract_kaspersky ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> K7 QNT files
if ($file =~ /(^|[\/\\])[0-9a-f]{32}\.QNT$/i)
{
extract_k7 ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Kaspersky System Watcher files
if ($file =~ /(^|[\/\\])[0-9a-f]{32}\.bin$/i)
{
extract_kaspersky_system_watcher ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Kaspersky Security for Windows Server files
if ($file =~ /{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}$/i && $data =~ /^\xAF\x1F/)
{
extract_kaspersky_security_for_windows_server ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Lumension LEMSS
if ($file =~ /\.lqf$/i)
{
extract_lumension ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> MalwareBytes DATA and QUAR Files
if ($file =~ /\.(quar|data)$/i)
{
extract_malwarebytes ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> McAfee BUP Files
if ($file =~ /\.bup$/i&&$data=~/^\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1/)
{
extract_mcafee ($file);
goto skipxray;
}
## --> Microsoft Antimalware / Microsoft Security Essentials
if ($file=~/\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}-.{1,}/i)
{
extract_ma_mse ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Microsoft Defender MAC
if ($file=~/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i)
{
extract_defender_mac ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Microsoft Defender PC - partially supported (D3 45 C5 99 header);
if ($data=~/^(\xD3\x45|\x0B\xAD)/)
{
extract_defender ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Panda <GUID> Zip Files
if ($file =~ /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i && $data=~/^PK/)
{
extract_panda ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Sentinel One MAL files
if ($file =~ /\.mal$/i)
{
extract_sentinelone ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
if ($file =~ /-\d+/i && $data=~/^PK/)
{
## --> Total AV {GUID}.dat
if ($file =~ /(^|[\/\\])[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.dat$/i && $data=~/^PK/)
{
extract_zip ($file, $data, 0x00000000, $filesize, 'Total AV', 'infected');
goto skipxray;
}
## --> Spybot - Search & Destroy 2 Zip Files
extract_zip ($file, $data, 0x00000000, $filesize, 'Search & Destroy 2', 'recovery');
goto skipxray;
}
## --> SUPERAntiSpyware (SDB)
if ($file =~ /\.sdb$/i)
{
extractdata ($file, $data, 0x00000000, $filesize, 0xED, 1);
goto skipxray;
}
## --> Symantec QBD and QBI Files
## Note: I don't know what data is stored inside QBI files
if ($file =~ /\.qb[di]$/i)
{
extractdata ($file, $data, 0x00000000, $filesize, 0xB3, 1);
goto skipxray;
}
## --> Symantec ccSubSDK {GUID} Files
if ($file =~ /\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}$/i)
{
extract_sym_ccSubSDK ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Symantec ccSubSDK submissions.idx Files
if ($file =~ /submissions\.idx$/i)
{
extract_sym_submissionsidx ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Symantec quarantine.qtn
if ($file =~ /quarantine\.qtn/i && $data=~/^PK/)
{
extract_sym_qtn ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> Symantec VBN Files
if ($file =~ /\.vbn$/i)
{
extract_sep ($file, $data, $filesize);
goto skipxray;
}
## --> TrendMicro VSBX files
if ((unpack("I",substr($data,0,4))^0xFFFFFFFF) == 0x58425356) # VSBX
{
extract_trend ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> QuickHeal <hash> Files
## --> Zemana <hash> Files
if ($file =~ /(^|[\/\\])[0-9a-f]{32}$/i)
{
if (-f 'quarantine.db')
{
extract_zemana ($file, $data, 0x00000000, $filesize);
}
else
{
extract_quickheal ($file, $data, 0x00000000, $filesize);
}
goto skipxray;
}
## --> Vipre <GUID>_ENC2 Files
if ($file =~ /\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}_ENC2$/i)
{
extract_vipre ($file, $data, 0x00000000, $filesize);
goto skipxray;
}
## --> FortiClient files
if ($data =~ /^\x51\x55\x41\x52\x46\x00\x00\x00/)
{
extract_forticlient ($file, $data, 0x00000000, $filesize);
goto skipxray
}
if ($file =~ /[0-9a-f]{2,}.*?\.out$/i)
{
goto skipxray;
}
## --> X-RAY scan
my $progress_delta = 100/$datalen; # $datalen is never 0
my $progress = 0;
my $lastprogress = 0;
my $cnt = 0;
print STDERR " Attempting x-ray scan ($datalen bytes)\n";
print STDERR " (may take quite some time !!!)\n" if $datalen>2000000;
if ($data !~ /^MZ/)
{
for (my $ofs=0; $ofs<$datalen; $ofs++)
{
print STDERR int($progress)."%\r" if $progress != $lastprogress;
if ( (ord(substr($data, $ofs, 1)) ^ ord(substr($data, $ofs+1, 1))) == 0x17)
{
my $key = ord(substr($data, $ofs, 1)) ^ 0x4D;
next if ( ord(substr($data, $ofs+1, 1)) ^ $key ) != 0x5A;
my $MZPE = dexor(substr($data,$ofs,16384),$key);
if ($MZPE =~ /^MZ.+PE\x00\x00/s)
{
$cnt+=extractdata ($file, $data, $ofs,$filesize-$ofs, $key, 0);
}
}
$lastprogress = $progress;
$progress+=$progress_delta;
}
}
print STDERR " -> Nothing found via X-RAY\n" if $cnt == 0;
print STDERR " -> $cnt potential file(s) found via X-RAY\n" if $cnt > 0;
skipxray:
}
sub extractdata
{
my $file = shift;
my $data = shift;
my $ofs = shift;
my $size = shift;
my $key = shift;
my $flag = shift;
my $newfilename = sprintf($file.'.%08d.%02X.out',$ofs,$key);
my $newdata = dexor(substr($data,$ofs,$size),$key);
if ($newdata =~ /^MZ.+?PE\x00\x00/si)
{
print STDERR " -> '$newfilename' - Possible PE\n -> ofs='$ofs' (".sprintf("%08lX",$ofs)."), key = 0x".sprintf("%02X",$key)." ($key)\n";
writefile ($newfilename, substr($newdata,0,length($newdata)));
return 1;
}
elsif ($newdata =~ /^(PK\x03\x04|Cr24|Rar!|\xCA\xFE\xBA\xBE|CAB|SZDD)/si)
{
print STDERR " -> '$newfilename' - Possible Archive\n -> ofs='$ofs' (".sprintf("%08lX",$ofs)."), key = 0x".sprintf("%02X",$key)." ($key)\n";
writefile ($newfilename, substr($newdata,0,length($newdata)));
return 1;
}
elsif ($newdata =~ /^\%PDF/si)
{
print STDERR " -> '$newfilename' - Possible PDF\n -> ofs='$ofs' (".sprintf("%08lX",$ofs)."), key = 0x".sprintf("%02X",$key)." ($key)\n";
writefile ($newfilename, substr($newdata,0,length($newdata)));
return 1;
}
elsif ($flag == 1)
{
print STDERR " -> '$newfilename' - Decrypted data\n -> ofs='$ofs' (".sprintf("%08lX",$ofs)."), key = 0x".sprintf("%02X",$key)." ($key)\n";
writefile ($newfilename, $newdata);
return 1;
}
return 0;
}
sub extract_sep
{
my $file = shift;
my $data = shift;
my $filesize = shift;
my $ofs = unpack("I64", $data);
my $recordtype;
my $qdataheader;
my $switch = ord(substr($data, $ofs, 1));
my $qsize;
#printf ("### DEBUG: The offset is @ %08lX (%d)\n",$ofs,$ofs);
#printf ("### DEBUG: The qsize is @ %08lX (%d)\n",$qsize,$qsize);
#printf ("### DEBUG: The switch is %08lX (%d)\n",$switch,$switch);
if ($ofs == 3676)
{
$recordtype = unpack("L",(substr($data,2656,4)));
$qsize = unpack("L",(substr($data,2348, 4)));
}
if ($ofs == 4752)
{
$recordtype = unpack("L",(substr($data,3732,4)));
$qsize = unpack("L",(substr($data,3412, 4)));
}
if ($ofs == 15100)
{
$recordtype = unpack("L",(substr($data,14080,4)));
$qsize = unpack("L",(substr($data,9936, 4)));
}
if ($ofs == 15108)
{
$recordtype = unpack("L",(substr($data,14088,4)));
$qsize = unpack("L",(substr($data,9940, 4)));
}
if ($recordtype == 0)
{
$qdataheader = unpack("L",(dexor(substr($data, $ofs+1, 8),0x5A)));
if ($qdataheader == 0x06AAAA20)
{
$filesize = unpack("L",(dexor(substr($data, $ofs + 16, 8),0x5A)));
$filesize -= unpack("L",(dexor(substr($data, $ofs + 8, 8),0x5A)));
$ofs += unpack("L",(dexor(substr($data, $ofs + 8, 8),0x5A)));
sep_meta($file, $data, $ofs, 0xA5);
extractdata ($file, $data, $ofs, $filesize, 0x5A, 1);
}
else
{
sep_meta($file, $data, $ofs, 0xA5);
extractdata ($file, $data, $ofs, $filesize-$ofs, 0x5A, 1);
}
}
if ($recordtype == 1)
{
print STDERR " -> Extracting metadata only. Does not contain quarantine file.\n";
sep_meta($file, $data, $ofs, 0x5A);
}
if ($recordtype == 2)
{
sep_meta($file, $data, $ofs, 0xA5);
extract_sept2($file, $data, $ofs,$filesize,$qsize);
}
}
sub extract_sept2
{
my $file = shift;
my $data = shift;
my $ofs = shift;
my $filesize = shift;
my $qsize = shift;
my $newfilename=sprintf($file.'.%08d.0xA5.out',$ofs);
my $newdata;
my $garbage =1;
my $pos = $ofs + unpack("L",(dexor(substr($data,4776,4),0x5A))) + 117;
my $qfs = unpack("L",(dexor(substr($data,$pos - 8,8),0x5A)));
my $datatype = ord(dexor(substr($data,$pos,1),0x5A));
if ($datatype == 0x8)
{
$garbage = 0;
my $sdsize = unpack("L",(dexor(substr($data,$pos + 1,4),0x5A)));
$pos = $pos + $sdsize + 19;
$datatype = ord(dexor(substr($data,$pos,1),0x5A));
}
if ($datatype == 0x9)
{
my $junk = $qfs - $qsize;
my $chunktype = ord(dexor(substr($data,$pos,1),0x5A));
$ofs = $pos;
while ($chunktype == 0x9)
{
my $chunksize = unpack("L",(dexor(substr($data,$pos + 1,4),0x5A)));
$newdata .= dexor(substr($data,$pos + 5,$chunksize),0xA5);
$pos = $pos + 5 + $chunksize;
$chunktype = ord(dexor(substr($data,$pos,1),0x5A));
}
if ($newdata =~ /MZ.+?PE\x00\x00/si)
{
print STDERR " -> '$newfilename' - Possible PE\n -> ofs='$ofs' (".sprintf("%08lX",$ofs+$-[0])."), key = 0xA5 (165)\n";
$ofs=$-[0];
}
else
{
print STDERR " -> '$newfilename' - Decrypted data\n -> ofs='$ofs' (".sprintf("%08lX",$ofs)."), key = 0xA5 (165)\n";
}
if ($garbage == 1)
{
my $header = unpack("L",substr($newdata,8,8)) + 40;
writefile ($newfilename, substr($newdata,$header,$qsize));
}
if ($garbage == 0)
{
writefile ($newfilename,$newdata);
}
}
else
{
print STDERR " -> Does not contain quarantine file.\n";
}
}
sub extract_mcafee
{
my $file = shift;
my $oOl = OLE::Storage_Lite->new($file);
my $oPps = $oOl->getPpsTree(1);
die ( $file. "Must be a OLE file") unless($oPps);
my $iTtl = 0;
olestream ($oPps, 0, \$iTtl, 1, $file);
}
sub extract_ma_mse
{
my $file = shift;
my $data = shift;
my $ofs = shift;
my $size = shift;
my $newfilename = sprintf($file.'.%08d_ma_mse.out',$ofs);
my $newdata = dexor($data,255);
print STDERR " -> '$newfilename' - Microsoft Antimalware/Security Essentials File\n -> ofs='$ofs' (".sprintf("%08lX",$ofs).")\n";
writefile ($newfilename, $newdata);
}
sub extract_defender_mac
{
my $file = shift;
my $data = shift;
my $ofs = shift;
my $size = shift;
my $newfilename = sprintf($file.'.%08d_defender_mac.out',$ofs);
my $newdata = dexor($data,0x25);
print STDERR " -> '$newfilename' - Microsoft Defender for Mac File\n -> ofs='$ofs' (".sprintf("%08lX",$ofs).")\n";
writefile ($newfilename, $newdata);
}
sub extract_defender
{
my $file = shift;
my $data = shift;
my $filesize = shift;
my $ofs = shift;
my $size = shift;
return if length($data)<0x3C;
my $newfilename = sprintf($file.'.%08d_defender.out',$ofs);
# this doesn't work for the samples I have atm
#my $newdata = dexor(substr($data,$ofs,$size),0xFF);
# this works for a subset of Windows Defender samples (D3 45 C5 99)
# these files contain metadata
my $hdrlen=0x3C;
my $key ="\x1E\x87\x78\x1B\x8D\xBA\xA8\x44\xCE\x69\x70\x2C\x0C\x78\xB7\x86\xA3\xF6\x23\xB7\x38\xF5\xED\xF9\xAF\x83\x53\x0F\xB3\xFC\x54\xFA\xA2\x1E\xB9\xCF\x13\x31\xFD\x0F\x0D\xA9\x54\xF6\x87\xCB\x9E\x18\x27\x96\x97\x90\x0E\x53\xFB\x31\x7C\x9C\xBC\xE4\x8E\x23\xD0\x53\x71\xEC\xC1\x59\x51\xB8\xF3\x64\x9D\x7C\xA3\x3E\xD6\x8D\xC9\x04\x7E\x82\xC9\xBA\xAD\x97\x99\xD0\xD4\x58\xCB\x84\x7C\xA9\xFF\xBE\x3C\x8A\x77\x52\x33\x55\x7D\xDE\x13\xA8\xB1\x40\x87\xCC\x1B\xC8\xF1\x0F\x6E\xCD\xD0\x83\xA9\x59\xCF\xF8\x4A\x9D\x1D\x50\x75\x5E\x3E\x19\x18\x18\xAF\x23\xE2\x29\x35\x58\x76\x6D\x2C\x07\xE2\x57\x12\xB2\xCA\x0B\x53\x5E\xD8\xF6\xC5\x6C\xE7\x3D\x24\xBD\xD0\x29\x17\x71\x86\x1A\x54\xB4\xC2\x85\xA9\xA3\xDB\x7A\xCA\x6D\x22\x4A\xEA\xCD\x62\x1D\xB9\xF2\xA2\x2E\xD1\xE9\xE1\x1D\x75\xBE\xD7\xDC\x0E\xCB\x0A\x8E\x68\xA2\xFF\x12\x63\x40\x8D\xC8\x08\xDF\xFD\x16\x4B\x11\x67\x74\xCD\x0B\x9B\x8D\x05\x41\x1E\xD6\x26\x2E\x42\x9B\xA4\x95\x67\x6B\x83\x98\xDB\x2F\x35\xD3\xC1\xB9\xCE\xD5\x26\x36\xF2\x76\x5E\x1A\x95\xCB\x7C\xA4\xC3\xDD\xAB\xDD\xBF\xF3\x82\x53";
my $rc4 = Crypt::RC4->new($key);
my $hdr = $rc4->RC4(substr($data,0,$hdrlen));
if (substr($hdr,0,4) eq "\xDB\xE8\xC5\x01")
{
$data=substr($data,$hdrlen,length($data)-$hdrlen);
my $len1 = unpack("I",substr($hdr,0x28,4));
my $len2 = unpack("I",substr($hdr,0x2C,4));
$rc4 = Crypt::RC4->new($key);
my $dec1=$rc4->RC4( substr($data,0,$len1) );
$data=substr($data,$len1,length($data)-$len1);
$rc4 = Crypt::RC4->new($key);
my $dec2=$rc4->RC4( substr($data,0,$len2) );
print STDERR " -> '$newfilename' - Defender File\n -> ofs='$ofs' (".sprintf("%08lX",$ofs).")\n";
writefile ($newfilename, $hdr.$dec1.$dec2);
}
# and this works for a subset of Windows Defender samples (0B AD)
# these files contain malicious code
else
{
my $rc4 = Crypt::RC4->new($key);
my $data = $rc4->RC4($data);
my $ofs = 0x28 + unpack("I",substr($data,0x08,4));
my $filesize = unpack("L",(substr($data,$ofs - 0xC,8)));
$newfilename = sprintf($file.'.%08d_Defender.out',$ofs);
print STDERR " -> '$newfilename' - Defender File\n -> ofs='$ofs' (".sprintf("%08lX",$ofs).")\n";
writefile ($newfilename, substr($data,$ofs,$filesize));
}
}
sub extract_sym_submissionsidx
{
my $file = shift;
my $data = shift;
my $ofs = shift;
my $size = shift;
$data=substr($data,0x30,length($data)-0x30);
my $cnt=0;
while (substr($data,0,4) eq "\x40\x99\xC6\x89")
{
my $len1=unpack("I",substr($data,24,4));
my $len2=unpack("I",substr($data,28,4));
print STDERR "Submission [$cnt] len1=$len1 len2=$len2\n";
my $newfilename = sprintf($file.'.%08d_Symantec_submission_[%04d]_idx.out',$ofs,$cnt);
print STDERR " -> '$newfilename' - Symantec submission_".sprintf("[%04d]",$cnt).".idx File\n";
my $dec=blowfishit(substr($data,56,$len1),substr($data,40,16),1);
writefile ($newfilename, $dec);
$newfilename = sprintf($file.'.%08d_Symantec_submission_[%04d]_idx.met',$ofs,$cnt);
my $dataout=parsesym($dec);
writefile ($newfilename, $dataout);
$data=substr($data,40+$len1);
$cnt++;
}
}
# 8EF95B94E971E842BAC952B02E79FB74 AVModule.dll
# 2B5CA624B61E3F408B994BF679001DC2 BHSvcPlg.dll
# 6AB68FC93C09E744B828A598179EFC83
# 5E6E81A4A77338449805BB2B7AB12FB4
sub extract_sym_ccSubSDK
{
my $file = shift;
my $data = shift;
my $ofs = shift;
my $size = shift;
my $newfilename = sprintf($file.'.%08d_Symantec_ccSubSDK.out',$ofs);
print STDERR " -> '$newfilename' - Symantec_ccSubSDK File\n";
print "GUID = ".hexdump (substr($data,0,16))."\n";
my $dec=blowfishit(substr($data,32,length($data)-32),substr($data,16,16),1);
writefile ($newfilename, $dec);
carve ($newfilename);
$newfilename = sprintf($file.'.%08d_Symantec_ccSubSDK.met',$ofs);
my $dataout=parsesym($dec);
writefile ($newfilename, $dataout);
}
sub parsesym
{
my $data = shift;
my $ret='';
my $lasttoken=0;
my $contlen=0;
while (1)
{
my $token = ord(substr($data,0,1)) or last;
$ret.=sprintf("%02X",$token)."\n";
if ($token==0x00) { $ret.=hexdump (substr($data,1,8))."\n"; $data=substr($data,1+8,length($data)-9); }
elsif ($token==0x01) { $ret.=hexdump (substr($data,1,1))."\n"; $data=substr($data,1+1,length($data)-2); }
elsif ($token==0x02) { $ret.=hexdump (substr($data,1,2))."\n"; $data=substr($data,1+2,length($data)-3); }
elsif ($token==0x03) { $ret.=hexdump (substr($data,1,4))."\n"; $data=substr($data,1+4,length($data)-5); }
elsif ($token==0x04) { $ret.=hexdump (substr($data,1,8))."\n"; $data=substr($data,1+8,length($data)-9); }
elsif ($token==0x06) { $ret.=hexdump (substr($data,1,4))."\n"; $data=substr($data,1+4,length($data)-5); }
elsif ($token==0x07) {
$ret.=hexdump (substr($data,1,4))."\n";
my $strlen=unpack("I",substr($data,1,4));
my $string=substr($data,5,$strlen);
$ret.=hexdump (substr($data,5,$strlen))."\n### STRING-A\n $string\n\n";
$data=substr($data,1+4+$strlen,length($data)-5-$strlen);
}
elsif ($token==0x08) {
$ret.=hexdump (substr($data,1,4))."\n";
my $strlen=unpack("I",substr($data,1,4));
my $string=substr($data,5,$strlen);
$string =~ s/\x00//gs;
$ret.=hexdump (substr($data,5,$strlen))."\n### STRING-W\n $string\n\n";
$data=substr($data,1+4+$strlen,length($data)-5-$strlen);
}
elsif ($token==0x09) {
$ret.=hexdump (substr($data,1,4))."\n";
$contlen=unpack("I",substr($data,1,4));
$data=substr($data,1+4,length($data)-5);
}
elsif ($token==0x0A) { $ret.=hexdump (substr($data,1,1)) ."\n";$data=substr($data,1+1,length($data)-2); }
elsif ($token==0x0F) {
$ret.="\n### GUID\n".hexdump (substr($data,1,16))."\n";
$data=substr($data,1+16,length($data)-17);
}
elsif ($token==0x10) {
if ($lasttoken==9)
{
$ret.=hexdump(substr($data,1,16))."\n";
$data=substr($data,1+16,length($data)-17);
}
else
{
$data=substr($data,1+16,length($data)-17);
}
}
else
{
if ($lasttoken!=9)
{
$ret.="### Error: $token [$lasttoken]!\n.[".hexdump(substr($data,0,16))."]\n";
#### very stupid fix, but at least it produces some output
#### when the routine gets stuck
if ($data=~/(
\x01\x35\xDB\x62\x9E\xE4\x0A\x40\x80\x27\x90\xA6\x7C\x64\x9F\x2E|
\x12\x90\x16\x29\xB1\xB6\x28\x40\xA5\x4A\x81\xC9\x1E\x79\xFD\x8C|
\x20\xE4\x52\xD3\x86\x9E\x7F\x4A\x82\x6C\xA8\x78\x26\x62\xB2\x46|
\x21\xA3\x05\x3F\xB7\x43\x78\x45\x93\xC8\xCD\xC5\xF6\x4A\x14\x9A|
\x25\x7B\x09\x10\xB0\xB2\xD7\x42\xBF\x9F\xA8\xB7\x48\x86\x75\x72|
\x3B\x3F\xFF\x0B\x21\x8D\x56\x4E\x84\xFF\x8E\x1C\x6D\xCF\xD7\x45|
\x56\xA9\x7B\xD9\x75\x7A\x08\x41\x8D\xCB\xE1\xD3\xAD\x6B\x55\x08|
\x6B\xC4\x70\x6E\xD6\x7F\x71\x4D\xA2\x33\xB1\x01\xE9\xAD\x72\xF2|
\x84\x12\x69\x0E\x06\xF4\x81\x45\xBD\x8F\xC6\x66\xC9\xFD\xEC\x84|
\x91\xD2\x5F\x94\x65\xF5\x44\x4A\x88\xEB\x23\xB6\x5C\xBB\xF9\x6B|
\xBF\xCC\x5C\x4E\xCB\x10\xBC\x45\x84\xAE\x94\x00\x03\x4A\xF8\xC4|
\x21\xA3\x05\x3F\xB7\x43\x78\x45\x93\xC8\xCD\xC5\xF6\x4A\x14\x9A|
\x63\x47\x39\xF6\x0E\x6A\xCF\x05\xD0\x5F\x1D\xFF\xD9\xA4\xCB\x16|
\x64\x45\x32\x31\x13\x3B\x33\x45\x89\x99\x33\x99\x06\x88\xF5\xA9|
\x6A\xB6\x8F\xC9\x3C\x09\xE7\x44\xB8\x28\xA5\x98\x17\x9E\xFC\x83|
\x7C\x62\xD3\x37\x34\x34\x45\x32\x3C\x37\x43\x33\xCB\xCC\x32\x33|
\xBB\x81\x1A\x3A\x8F\xC1\xBE\x48\x82\x2C\x8B\x62\x63\xA5\x20\x4D
)/sgx)
{
$data=substr($data,(pos $data),length($data)-(pos $data));
$ret.="### Resuming search after the GUID!\n";
}
else
{
last;
}
}
else
{
my $cont=substr($data,0,$contlen);
if (length($cont)==16)
{
$ret.="\n### GUID\n".hexdump($cont);
}
else
{
$ret.="\n### BLOB\n".hexdump($cont);
}
$contlen=length($data) if $contlen>length($data);
$data=substr($data,$contlen,length($data)-$contlen);
}
}
$lasttoken=$token;
}
return $ret;
}
sub extract_panda
{
my $file = shift;
my $data = shift;
my $ofs = shift;
my $size = shift;
my $zip = Archive::Zip->new();
if ($zip->read( $file ) != AZ_OK)
{
print STDERR "Error reading zip file $file\n" ;
}
my @members = $zip->memberNames();
foreach my $onefile (sort @members)
{
my $newfilename = '';
if ($onefile =~ /QEINFO/i)
{