142
142
]
143
143
)
144
144
145
+ exec_magics = OrderedDict (
146
+ [
147
+ ("PE32 executable (DLL)" , "dll" ),
148
+ ("PE32+ executable (DLL)" , "dll" ),
149
+ ("MS-DOS executable PE32 executable (DLL)" , "dll" ),
150
+ ("PE32 executable" , "exe" ),
151
+ ("PE32+ executable" , "exe" ),
152
+ ("MS-DOS executable, MZ for MS-DOS" , "exe" ),
153
+ ]
154
+ )
155
+
145
156
magics = OrderedDict (
146
157
[
147
158
# ToDo msdos
@@ -297,9 +308,6 @@ def sct(f):
297
308
298
309
299
310
def xxe (f ):
300
- if is_executable (f ):
301
- return None
302
-
303
311
STRINGS = [
304
312
b"XXEncode" ,
305
313
b"begin" ,
@@ -315,9 +323,6 @@ def xxe(f):
315
323
316
324
317
325
def hta (f ):
318
- if is_executable (f ):
319
- return None
320
-
321
326
STRINGS = [
322
327
b"<head" ,
323
328
b"<title" ,
@@ -352,9 +357,6 @@ def office_one(f):
352
357
353
358
354
359
def office_webarchive (f ):
355
- if is_executable (f ):
356
- return None
357
-
358
360
STRINGS = [
359
361
b"<o:Pages>" ,
360
362
b"<o:DocumentProperties>" ,
@@ -433,9 +435,6 @@ def office_ole(f):
433
435
434
436
435
437
def powershell (f ):
436
- if is_executable (f ):
437
- return None
438
-
439
438
POWERSHELL_STRS = [
440
439
b"$PSHOME" ,
441
440
b"Get-WmiObject" ,
@@ -458,9 +457,6 @@ def powershell(f):
458
457
459
458
460
459
def javascript (f ):
461
- if is_executable (f ):
462
- return None
463
-
464
460
JS_STRS = [
465
461
b"var " ,
466
462
b"function " ,
@@ -486,18 +482,12 @@ def javascript(f):
486
482
487
483
488
484
def wsf (f ):
489
- if is_executable (f ):
490
- return None
491
-
492
485
match = re .search (b'<script\\ s+language="(J|VB|Perl)Script"' , f .contents , re .I )
493
486
if match :
494
487
return "wsf"
495
488
496
489
497
490
def pub (f ):
498
- if is_executable (f ):
499
- return None
500
-
501
491
PUB_STRS = [
502
492
b"Microsoft Publisher" ,
503
493
b"MSPublisher" ,
@@ -512,9 +502,6 @@ def pub(f):
512
502
513
503
514
504
def visualbasic (f ):
515
- if is_executable (f ):
516
- return None
517
-
518
505
VB_STRS = [
519
506
b"Dim " ,
520
507
b"\x00 D\x00 i\x00 m\x00 " ,
@@ -564,9 +551,6 @@ def dmg(f):
564
551
565
552
566
553
def vbe_jse (f ):
567
- if is_executable (f ):
568
- return None
569
-
570
554
if b"#@~^" in f .contents [:100 ]:
571
555
data = vbe_decode_file ("" , f .contents )
572
556
if data :
@@ -586,9 +570,6 @@ def udf(f):
586
570
587
571
588
572
def inf (f ):
589
- if is_executable (f ):
590
- return None
591
-
592
573
STRINGS = [
593
574
# b"[version]",
594
575
b"Signature=" ,
@@ -609,6 +590,19 @@ def identify(f, check_shellcode: bool = False):
609
590
if not f .stream .read (0x1000 ):
610
591
return
611
592
593
+ if is_executable (f ):
594
+ # to reduce number of checks
595
+ for magic_types in exec_magics :
596
+ if f .magic .startswith (magic_types ):
597
+ # MS-DOS executable PE32 executable (DLL) (GUI) Intel 80386, for MS Windows
598
+ # MZ for MS-DOS -> MS-DOS executable
599
+ # MZ for MS-DOS -> but is DLL
600
+ package = exec_magics [magic_types ]
601
+ if package in ("exe" , "dll" ):
602
+ pe = pefile .PE (data = f .contents , fast_load = True )
603
+ return "dll" if pe .is_dll () else "exe"
604
+ return None
605
+
612
606
if f .filename :
613
607
for package , extensions in file_extensions .items ():
614
608
if f .filename .endswith (extensions ) and not f .contents .startswith (b"MZ" ):
@@ -634,6 +628,7 @@ def identify(f, check_shellcode: bool = False):
634
628
package = identifier (f )
635
629
if package :
636
630
return package
631
+
637
632
for magic_types in magics :
638
633
if f .magic .startswith (magic_types ):
639
634
# MS-DOS executable PE32 executable (DLL) (GUI) Intel 80386, for MS Windows
0 commit comments