-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntropyCalc.py
More file actions
63 lines (51 loc) · 1.88 KB
/
EntropyCalc.py
File metadata and controls
63 lines (51 loc) · 1.88 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
import sys
import math
import pefile
# output the entropy of specific data 'buffer'
def calc_entropy(buffer):
if isinstance(buffer, str):
buffer = buffer.encode()
entropy = 0
for x in range(256):
p = (float(buffer.count(bytes([x])))) / len(buffer)
if p > 0:
entropy += - p * math.log(p, 2)
return entropy
# print help
def printhelp():
print("[i] Usage:\n" +
"\t- python.exe EntropyCalc.py <filename>\n" +
"\t- python.exe EntropyCalc.py <-pe> <pe filename>")
sys.exit(1)
# output the entropy of the file as a whole
def calc_file_entropy(filename):
try:
with open(filename, "rb") as f:
buf = f.read()
entropy = calc_entropy(buf)
print(f"Entropy Of {filename} As A Whole File Is : {entropy:.5f}")
except FileNotFoundError:
print(f"[!] Error: \"{filename}\" Is Not A Valid File")
printhelp()
# if input is only the file's name
if len(sys.argv) == 2:
calc_file_entropy(sys.argv[1])
sys.exit(1)
# else if input is with the "-pe" argument
elif len(sys.argv) == 3 and sys.argv[1] == "-pe":
try:
PEfile = pefile.PE(sys.argv[2])
print(f"[i] Parsing {sys.argv[2]}'s PE Section Headers ... ")
for section in PEfile.sections:
name = section.Name.rstrip(b'\x00').decode()
entropy = calc_entropy(section.get_data())
color_code = 31 + (hash(name) % 6) # choose color based on section name
print(f"\t>>> \033[{color_code}m\"{name}\"\033[0m Scored Entropy Of Value: \033[{color_code}m{entropy:.5f}\033[0m")
except FileNotFoundError:
print(f"[!] Error: \"{sys.argv[2]}\" Is Not A Valid File")
printhelp()
except pefile.PEFormatError:
print(f"[!] Error: \"{sys.argv[2]}\" Is Not A Valid PE File")
printhelp()
else:
printhelp()