-
Notifications
You must be signed in to change notification settings - Fork 2
/
SingleByteHistogram.py
220 lines (177 loc) · 6.69 KB
/
SingleByteHistogram.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
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
#Copyright 2020 Battelle Energy Alliance, LLC, ALL RIGHTS RESERVED.
#python ByteHistogram.py <Input File> <Output Directory> | <Start Postion in Bytes> | <Length in Bytes> | <Max:Min Entropy> | <Block Size>
import sys
import os
import binascii
import subprocess
no00FF = False
writeVectorToFile = True
plotGraphs = False
showScreenOutput = False
CRED = '\033[91m'
CEND = '\033[0m'
knownArchitectures = ['amd64','mips','mipsel','powerpc','i386','armel','armhf','ppc64el','java','python27','python35','gcc','clang','tcc','AVR','dotNet']
bytecount = 0
processedByteCount = 0
#Argument Error Checking
try:
fileSelected = sys.argv[2]
file = open(sys.argv[2], "rb")
bytesInFile = os.path.getsize(sys.argv[2])
except:
print(CRED + 'Argument Error' + CEND)
sys.exit()
try:
offsetStart = sys.argv[4]
except:
offsetStart = 0
try:
length = sys.argv[5]
except:
length = bytesInFile - int(offsetStart)
try:
maxEntropy = float(sys.argv[6].split(':')[0])
minEntropy = float(sys.argv[6].split(':')[1])
except:
maxEntropy = 1.0
minEntropy = 0.0
try:
blockSize = int(sys.argv[7])
except:
blockSize = 256
try:
outPath = sys.argv[3]
except:
print(CRED + 'Argument Error' + CEND)
sys.exit()
if not os.path.exists(sys.argv[3]):
os.makedirs(sys.argv[3])
#Binwalk Section
#Get Verbose Binwalk Entropy Scan Results
entropy = subprocess.check_output(['binwalk', '-E', '-v', '--block=' + str(blockSize), '--nplot', fileSelected]).split()
entropy = entropy[entropy.index(b'ENTROPY')+2:len(entropy)]
del entropy[1::3]
#Loop through Binwalk elements to decode them
for index in range(0,len(entropy)):
entropy[index] = entropy[index].decode()
#Bytes to Read and throw away (Offset)
trashBytes = file.read(int(offsetStart))
#Read in 2 bytes
bytes = file.read(2)
#Make 260 Element Array
#histogram = [0] * 260
histogram = [0] * 258
#Make 254 Element Array
index = list(range(0,255))
print('Processing ' + fileSelected.split(os.sep)[-1])
#count = 0
while bytes:
print((bytecount / bytesInFile) * 100, end='\r')
if int(length) > bytecount:
#Format bytes
hex2bytes = binascii.hexlify(bytearray(bytes))
#Index into array and increment count Endian checks
if hex2bytes == b'0001':
histogram[256] = histogram[256] + 1
if hex2bytes == b'0100':
histogram[257] = histogram[257] + 1
#Split Bytes
hexbyte1 = hex2bytes[0:2]
hexbyte2 = hex2bytes[2:4]
#Check Entropy to see if bytes should be ignored
entropyValue = float(entropy[entropy.index(str((int(bytecount / blockSize)) * blockSize)) + 1])
#Index into array and increment count
if entropyValue <= maxEntropy and entropyValue >= minEntropy:
histogram[int(hexbyte1,16)] = histogram[int(hexbyte1,16)] + 1
processedByteCount = processedByteCount + 1
bytecount = bytecount + 1
if hexbyte2 != b'': #Handles last value not 2 Bytes long
if entropyValue <= maxEntropy and entropyValue >= minEntropy:
histogram[int(hexbyte2,16)] = histogram[int(hexbyte2,16)] + 1
processedByteCount = processedByteCount + 1
bytecount = bytecount + 1
#Read another 2 bytes
bytes = file.read(2)
#Save a copy of the non-Normalized count
normalCount = histogram[:]
#Normalize Histogram
try:
histogram[:256] = [x / processedByteCount for x in histogram[:256]]
except:
histogram[:256] = [0 for x in histogram[:256]]
#Calc Endianness
if histogram[256] < histogram[257]:
histogram[256] = 0
histogram[257] = 1
else:
histogram[256] = 1
histogram[257] = 0
if showScreenOutput == True:
#Print AI Vector
print(histogram)
print()
#Print Histogram
for h in range (0,64):
print(str(hex(h)) + " : " + str(histogram[h]) + '\t' + str(hex(h+64)) + " : " + str(histogram[h+64]) + '\t' + str(hex(h+2*64)) + " : " + str(histogram[h+2*64]) + '\t' + str(hex(h+3*64)) + " : " + str(histogram[h+3*64]))
#Print Top n Bytes
print("\nMost Frequent Bytes")
n=50
topValues = sorted(zip(histogram[0:255],index,normalCount[0:255]), reverse=True)[:n]
for t in range(0,n):
print(str(hex(topValues[t][1])) + "\t" + str(topValues[t][1]) + ' \tNormalized Count: ' + str(topValues[t][0]) + '\t Count: ' + str(topValues[t][2]))
#Check for Endianness and Print Result
maxEndianIndex = histogram.index(max(histogram[256:260]))
if maxEndianIndex == 256 or maxEndianIndex == 258:
print("\nBig Endian")
else:
print("\nLittle Endian")
#Print bytes processed
print('\n' + str(processedByteCount) + ' bytes Processed')
file.close()
#Write Vector to File
if writeVectorToFile == True:
if sys.argv[1] == 'Directory':
outputFile = open(outPath + sys.argv[2].split(os.sep)[-2] + '.csv','a')
else:
outputFile = open(outPath + sys.argv[2].split(os.sep)[-1] + '.csv','a')
try:
arch = str(fileSelected).split(os.sep)[-1].split('_')[1]
if arch in knownArchitectures:
outputFile.write(arch + ',' + str(histogram)[1:-1] + '\n')
else:
raise
except:
#outputFile.write('mips' + ',' + str(histogram)[1:-1] + '\n')
outputFile.write(sys.argv[2] + ',' + str(histogram)[1:-1] + '\n')
#outputFile.write('?' + ',' + str(histogram)[1:-1] + '\n')
outputFile.close()
#Plot Graph
if plotGraphs == True:
if no00FF:
import matplotlib.pyplot as plot
fig, ax = plot.subplots()
ax.plot(range(1,255), histogram[1:255])
ax.set(xlabel='Bytes', ylabel='Normalized Count',title='Byte Histogram (No 0x00 or 0xFF)')
major_ticks = range(1,255)
ax.set_xticks(major_ticks)
ax.tick_params(axis='x', labelsize='10')
ax.grid(which='both', alpha=0.1)
ax2 = ax.twinx()
ax2.plot(range(1,255), normalCount[1:255])
ax2.set_ylabel('Count')
fig.tight_layout()
plot.show()
else:
import matplotlib.pyplot as plot
fig, ax = plot.subplots()
ax.plot(range(0,256), histogram[0:256])
ax.set(xlabel='Bytes', ylabel='Normalized Count',title='Byte Histogram')
major_ticks = range(0,256)
ax.set_xticks(major_ticks)
ax.tick_params(axis='x', labelsize='10')
ax.grid(which='both', alpha=0.1)
ax2 = ax.twinx()
ax2.plot(range(0,256), normalCount[0:256])
ax2.set_ylabel('Count')
fig.tight_layout()
plot.show()