-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathpeentro.py
executable file
·49 lines (41 loc) · 1.36 KB
/
peentro.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
#!/usr/bin/python
import pefile
import sys
import os
# Analyse PE Section entropy
# v 0.2
# Need https://code.google.com/p/pefile et on lui doit TOUT
# Copyleft Thanat0s
# http://Thanat0s.trollprod.org
#
# Licence GNU GPL
SECTIONS = ['.text', '.bss', '.rdata', '.data', '.edata', '.idata', '.pdata', '.debug', '.xdata', '.reloc', '.rsrc', '.code', '.tls']
# Needs two arg if not... help
if len(sys.argv) != 2:
print ('Compute the entropy of PE sections')
print ('To Use: '+ sys.argv[0]+ ' filename')
sys.exit(1)
FILENAME = sys.argv[1]
# Test if file exists
if not os.path.isfile(FILENAME):
print ('File not found')
sys.exit(1)
# Load an get sectiors
try:
PE = pefile.PE(FILENAME, fast_load=True)
except:
print ("Error in loading " + FILENAME)
print ("Section\t\tEntropy\tSize\tMD5\t\t\t\t\tRemark")
for section in PE.sections:
ENTROPY = section.get_entropy()
SECTION_NAME = section.Name.strip().replace(chr(0x00), "")
REMARKS = []
SECTION_USUAL = True
for names in SECTIONS:
if names==SECTION_NAME:
SECTION_USUAL = False
if SECTION_USUAL:
REMARKS.append("Unusal Segment")
if ENTROPY > 7:
REMARKS.append("High Entropy")
print ("%s%s\t%.3f\t%s\t%s\t%s" % (SECTION_NAME, ' ' * (8 - len(SECTION_NAME)), ENTROPY, section.SizeOfRawData, section.get_hash_md5(), ','.join(REMARKS)))