-
Notifications
You must be signed in to change notification settings - Fork 20
/
DetectPacker.py
89 lines (75 loc) · 2.61 KB
/
DetectPacker.py
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
from Utils import *
ExtendSysPathRelativeToScript('/deps/libpefile')
import pefile
import argparse
from PackerReport import *
from PEIDDetector import *
from LowImportCountDetector import *
from PackerSectionNameDetector import *
from BadEntryPointSectionDetector import *
from NonStandardSectionNameDetector import *
DEFAULT_CONFIG = {
"LowImportThreshold": 10,
"NonStandardSectionThreshold": 3,
"BadSectionNameThreshold": 2,
"OnlyPEIDEntryPointSignatures": True,
# large database has more than 3x as many signatures, but many are for non-packers
# and will create false positives. we can move signatures from the long list to the short
# list as needed, though.
"UseLargePEIDDatabase": False,
"CheckForPEIDSignatures": True,
"CheckForBadEntryPointSections": True,
"CheckForLowImportCount": True,
"CheckForPackerSections": True,
"CheckForNonStandardSections": True
}
def InitializeDetectors(config):
return [
PEIDDetector(config),
BadEntryPointSectionDetector(config),
LowImportCountDetector(config),
PackerSectionNameDetector(config),
NonStandardSectionNameDetector(config)
]
def CheckForPackersInMemory(filedata, config=DEFAULT_CONFIG):
detectors = InitializeDetectors(config)
report = PackerReport(filedata)
pe = pefile.PE(data=filedata)
for detector in detectors:
detector.Run(pe, report)
return report
def CheckForPackers(files, config=DEFAULT_CONFIG):
detectors = InitializeDetectors(config)
reports = {}
for file in files:
report = PackerReport(file)
try:
pe = pefile.PE(file)
for detector in detectors:
detector.Run(pe, report)
except FileNotFoundError:
report.IndicateParseFailed("file not found")
finally:
reports[file] = report
return reports
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Detect if a Windows PE file is packed', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
for name, value in DEFAULT_CONFIG.items():
if (type(value) == type(True)):
parser.add_argument("--" + name, metavar='<bool>', type=CmdStrToBool, nargs=1, default=value, help=" ")
elif (type(value) == type(1)):
parser.add_argument("--" + name, metavar='<number>', type=int, nargs=1, default=value, help=" ")
parser.add_argument("filenames", metavar='file', type=str, nargs='+', help='File(s) to process')
args = parser.parse_args()
config = {}
for name, value in DEFAULT_CONFIG.items():
if (name in args.__dict__):
val = args.__dict__[name]
if (type(val) == type(list())):
val = val[0]
config[name] = val
else:
config[name] = value
reports = CheckForPackers(args.filenames, config)
for file, report in reports.items():
report.Print()