-
Notifications
You must be signed in to change notification settings - Fork 2
/
WiiBin.py
394 lines (308 loc) · 14.9 KB
/
WiiBin.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#Copyright 2020 Battelle Energy Alliance, LLC, ALL RIGHTS RESERVED.
#Buffered File with 0x00's
#Offset measured from center of chunk
#Adjustable slide %
#Added .Net Bytecode
#Added startup Notes
#Additional Error checking
#Added DotNet Bytecode
#Added Compiler Detection
#Added Percent Compressed or Encrypted
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import shutil
import os
import sys
import subprocess
import math
import zipfile
programPath = '.' + os.sep
selectedFilename = ''
def clearDirectories():
if os.path.exists(programPath + 'Input'):
shutil.rmtree(programPath + 'Input')
if os.path.exists(programPath + 'Output'):
shutil.rmtree(programPath + 'Output')
if os.path.exists(programPath + 'OutputTemp'):
shutil.rmtree(programPath + 'OutputTemp')
os.mkdir(programPath + 'Input')
os.mkdir(programPath + 'Output')
os.mkdir(programPath + 'OutputTemp')
def getEndianess():
inputResults = open(programPath + 'Output' + os.sep + 'Input.csv','r')
contents = inputResults.read()
inputResults.close()
#Read last Entry (Big Endian Status bit)
contents = contents[-2:-1]
if '0' in contents:
contents = 'Big'
else:
contents = 'Little'
return contents
def goButtonArch():
#Clear resultsbox
resultsBox.delete(1.0, END)
#Check if Arch or Compiler Radio Button Selected
if radioValue.get() == 0:
resultsBox.insert(END, 'Radio Button Not Selected')
return None
#Clear Current PickleFiles
modelPath = programPath + 'PickledSKLearnModels'
models = os.listdir(modelPath)
for model in models:
if model.endswith(".sav"):
os.remove(os.path.join(modelPath, model))
#Unzip Selected PickledModels
if radioValue.get() == 1:
zip = zipfile.ZipFile(programPath + 'PickledSKLearnModels' + os.sep + 'Architectures.zip')
zip.extractall(programPath + 'PickledSKLearnModels')
if radioValue.get() == 2:
zip = zipfile.ZipFile(programPath + 'PickledSKLearnModels' + os.sep + 'Compilers.zip')
zip.extractall(programPath + 'PickledSKLearnModels')
if radioValue.get() == 3:
zip = zipfile.ZipFile(programPath + 'PickledSKLearnModels' + os.sep + 'Bytecode.zip')
zip.extractall(programPath + 'PickledSKLearnModels')
if radioValue.get() == 4:
zip = zipfile.ZipFile(programPath + 'PickledSKLearnModels' + os.sep + 'Architectures_and_Bytecode.zip')
zip.extractall(programPath + 'PickledSKLearnModels')
#Copy file to Input Dir
global selectedFilename
try:
shutil.copy(selectedFilename, programPath + 'Input')
except:
resultsBox.insert(END, 'File not selected. Select a file and try again.')
return None
try:
os.system('python3 GenerateByteHistogram.py a ' + programPath + 'Input' + os.sep + ' ' + programPath + 'Output' + os.sep + ' 0 100000000000000000000 ' + entropyEntryText.get() + ' ' + blocksizeEntryText.get() + ' 0')
except:
os.system('python GenerateByteHistogram.py a ' + programPath + 'Input' + os.sep + ' ' + programPath + 'Output' + os.sep + ' 0 100000000000000000000 ' + entropyEntryText.get() + ' ' + blocksizeEntryText.get() + ' 0')
results = open(programPath + 'Output' + os.sep + 'Results.txt','r')
contents = results.read()
header = 'Type\tProbability\t\tAlgorthim\n------------------------------------------\n'
resultsBox.insert(END,header + contents)
#Set Endianess GUI Box
endianLabelText.set(getEndianess())
clearDirectories()
def goButtonData():
#Clear resultsbox
resultsBox.delete(1.0, END)
#Check if Arch or Compiler Radio Button Selected
if radioValue.get() == 0:
resultsBox.insert(END, 'Radio Button Not Selected')
return None
#Clear Current PickleFiles
modelPath = programPath + 'PickledSKLearnModels'
models = os.listdir(modelPath)
for model in models:
if model.endswith(".sav"):
os.remove(os.path.join(modelPath, model))
#Unzip Selected PickledModels
if radioValue.get() == 1:
zip = zipfile.ZipFile(programPath + 'PickledSKLearnModels' + os.sep + 'Architectures.zip')
zip.extractall(programPath + 'PickledSKLearnModels')
if radioValue.get() == 2:
zip = zipfile.ZipFile(programPath + 'PickledSKLearnModels' + os.sep + 'Compilers.zip')
zip.extractall(programPath + 'PickledSKLearnModels')
if radioValue.get() == 3:
zip = zipfile.ZipFile(programPath + 'PickledSKLearnModels' + os.sep + 'Bytecode.zip')
zip.extractall(programPath + 'PickledSKLearnModels')
if radioValue.get() == 4:
zip = zipfile.ZipFile(programPath + 'PickledSKLearnModels' + os.sep + 'Architectures_and_Bytecode.zip')
zip.extractall(programPath + 'PickledSKLearnModels')
#Copy file to Input Dir
global selectedFilename
try:
shutil.copy(selectedFilename, programPath + 'Input')
except:
resultsBox.insert(END, 'File not selected. Select a file and try again.')
return None
#ZeroBuffer
zero=[0]*int((int(chunkEntryText.get())/2)-1)
zeroBuffer=bytearray(zero)
#Pad Original File with Leading and Trailing Zeros
old = open('Input' + os.sep + selectedFilename.split(os.sep)[-1], 'rb')
new = open('Input' + os.sep + 'temp__' + selectedFilename.split(os.sep)[-1], 'wb')
new.write(zeroBuffer)
new.write(old.read())
new.write(zeroBuffer)
old.close()
new.close()
os.remove('Input' + os.sep + selectedFilename.split(os.sep)[-1])
os.rename('Input' + os.sep + 'temp__' + selectedFilename.split(os.sep)[-1], 'Input' + os.sep + selectedFilename.split(os.sep)[-1])
#Split File Via Rolling Window
try:
os.system('python3 RollingWindowExtractor.py ' + programPath + 'Input' + os.sep + ' ' + programPath + 'OutputTemp' + os.sep + ' ' + chunkEntryText.get() + ' ' + slideEntryText.get())
os.system('python3 GenerateByteHistogram.py d ' + programPath + 'OutputTemp' + os.sep + ' ' + programPath + 'Output' + os.sep + ' 0 100000000000000000000 ' + entropyEntryText.get() + ' ' + blocksizeEntryText.get() + ' ' + votesEntryText.get())
except:
os.system('python RollingWindowExtractor.py ' + programPath + 'Input' + os.sep + ' ' + programPath + 'OutputTemp' + os.sep + ' ' + chunkEntryText.get() + ' ' + slideEntryText.get())
os.system('python GenerateByteHistogram.py d ' + programPath + 'OutputTemp' + os.sep + ' ' + programPath + 'Output' + os.sep + ' 0 100000000000000000000 ' + entropyEntryText.get() + ' ' + blocksizeEntryText.get() + ' ' + votesEntryText.get())
results = open(programPath + 'Output' + os.sep + 'Results.txt','r')
contents = results.readlines()
#Convert to list
finalList = []
for i in range(0,len(contents)):
finalList.append(str(contents[i].rstrip('\n')))
#Generate final String
finalString = 'File\tByte Offset\t\tAgreement\n'\
+ '-------------------------------------------------------\n'
for i in range(0,len(finalList)):
finalString = finalString + str(int(finalList[i].split(',')[0])+1) + '\t' + str(math.trunc((int(finalList[i].split(',')[0]))*(int(chunkEntryText.get())*(int(slideEntryText.get())/100)))).zfill(7) + '\t\t' + str(finalList[i].split(',')[1]) + '\n'
if len(finalList) == 0:
finalString = 'No Data Offset Matches Found'
resultsBox.insert(END,finalString)
#Set Endianess GUI Box
endianLabelText.set('<????>')
clearDirectories()
def entropyPercentage(passedFile):
upperLimit = entropyEntryText.get().split(':')[0]
fileSize = os.path.getsize(passedFile)
entropy = subprocess.check_output(['binwalk', '-E', '-v', '--block=' + str(blocksizeEntryText.get()), '--nplot', passedFile]).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()
entropy.append(str(fileSize))
byteTotal = 0
for index in range(1,len(entropy),2):
if float(entropy[index]) > float(upperLimit):
byteTotal = byteTotal + (float(entropy[index+1])-float(entropy[index-1]))
percentage = str(round((byteTotal / fileSize) * 100, 2)) + '%'
return percentage
def fileSelectButton():
global selectedFilename
fileSelectedLabelText.set('Loading Selected File...')
selectedFilename = filedialog.askopenfilename(initialdir = programPath,title = "Select Binary")
print(selectedFilename)
#Check for Zero size files
##############################################
while os.path.getsize(selectedFilename) <= 0:
print("Zero-byte file detected. Please select a different file.")
selectedFilename = filedialog.askopenfilename(initialdir = programPath,title = "Select Binary")
#Check Entropy Percentage
entropyPercent = entropyPercentage(selectedFilename)
percentLabelText.set(entropyPercent)
fileSelectedLabelText.set(selectedFilename)
entropyNumber = float(entropyPercent.split('%')[0])
if entropyNumber >= 66:
percentLabel.config(fg="red")
messagebox.showwarning("Entropy Warning", "Warning: Selected Binary is " + str(entropyPercent) + " Compressed or Encrypted. WiiBin Results Should Not Be Trusted.")
elif entropyNumber >= 33:
percentLabel.config(fg="orange")
messagebox.showwarning("Entropy Warning", "Warning: Selected Binary is " + str(entropyPercent) + " Compressed or Encrypted. WiiBin Results Might Not Be Reliable.")
elif entropyNumber >= 0:
percentLabel.config(fg="green")
def entropyLabelClicked(event):
resultsBox.delete(1.0, END)
resultsBox.insert(END,"The window of entropy that will be considered when generating byte histograms. Anything outside of that window will be ignored. Syntax=Max:Min")
def blocksizeLabelClicked(event):
resultsBox.delete(1.0, END)
resultsBox.insert(END,"The size of individual block (in bytes) considered during entropy analysis. Default=512")
def chunksizeLabelClicked(event):
resultsBox.delete(1.0, END)
resultsBox.insert(END,"The size of chunk in bytes that the data offset process will break the inputed file into. Default=10000")
def slidesizeLabelClicked(event):
resultsBox.delete(1.0, END)
resultsBox.insert(END,"The percent of the chunk size that the sliding window is slid Default=50")
def reqVotesLabelClicked(event):
resultsBox.delete(1.0, END)
resultsBox.insert(END,"Number of ML algorithms that must agree on an file for it to be reported as part of the data offset output. Default=5 (Simple Majority)")
def radioArchSelected():
entropyEntryText.set('0.9:0.1')
resultsBox.delete(1.0, END)
resultsBox.insert(END,"Mode Changed to Architecture")
def radioCompSelected():
entropyEntryText.set('1.0:0.0')
resultsBox.delete(1.0, END)
resultsBox.insert(END,"Mode Changed to Compiler")
def radioByteSelected():
entropyEntryText.set('0.9:0.1')
resultsBox.delete(1.0, END)
resultsBox.insert(END,"Mode Changed to Bytecode")
def radioArchByteSelected():
entropyEntryText.set('0.9:0.1')
resultsBox.delete(1.0, END)
resultsBox.insert(END,"Mode Changed to Architecture & Bytecode")
#############Initialization###################
root = Tk()
root.configure(width=40)
root.title("WiiBin")
root.geometry("655x390")
root.resizable(0, 0)
radioValue = IntVar()
buttonText = StringVar()
buttonText.set('Select File...')
fileSelectButton = Button(root, textvariable=buttonText, command=fileSelectButton)
fileSelectButton.place(x=10,y=10)
fileSelectedLabelText = StringVar()
fileSelectedLabel = Entry(root,textvariable=fileSelectedLabelText,bd=0,bg='#D9D9D9',width=62)
fileSelectedLabel.place(x=130,y=15)
fileSelectedLabelText.set('<File Path>')
entropyLabel = Label(root,text='Entropy Span:')
entropyLabel.place(x=10,y=45)
entropyLabel.bind("<Button>", entropyLabelClicked)
entropyEntryText = StringVar()
entropyEntry = Entry(root,width=7,textvariable=entropyEntryText)
entropyEntry.place(x=108,y=45)
entropyEntryText.set('0.9:0.1')
blocksizeLabel = Label(root,text='Block Size (b):')
blocksizeLabel.place(x=180,y=45)
blocksizeLabel.bind("<Button>", blocksizeLabelClicked)
blocksizeEntryText = StringVar()
blocksizeEntry = Entry(root,width=4,textvariable=blocksizeEntryText)
blocksizeEntry.place(x=279,y=45)
blocksizeEntryText.set('512')
chunkLabel = Label(root,text='Chunk Size (b):')
chunkLabel.place(x=330,y=45)
chunkLabel.bind("<Button>", chunksizeLabelClicked)
chunkEntryText = StringVar()
chunkEntry = Entry(root,width=6,textvariable=chunkEntryText)
chunkEntry.place(x=434,y=45)
chunkEntryText.set('10000')
slideLabel = Label(root,text='Slide (%):')
slideLabel.place(x=520,y=70)
slideLabel.bind("<Button>", slidesizeLabelClicked)
slideEntryText = StringVar()
slideEntry = Entry(root,width=3,textvariable=slideEntryText)
slideEntry.place(x=590,y=70)
slideEntryText.set('50')
votesLabel = Label(root,text='Req\'d Votes: of 8')
votesLabel.place(x=500,y=45)
votesLabel.bind("<Button>", reqVotesLabelClicked)
votesEntryText = StringVar()
votesEntry = Entry(root,width=2,textvariable=votesEntryText)
votesEntry.place(x=590,y=45)
votesEntryText.set('5')
goButtonArch = Button(root,text="Determine Type",command=goButtonArch)
goButtonArch.place(x=10,y=75)
goButtonData = Button(root,text="Determine Offsets",command=goButtonData)
goButtonData.place(x=145,y=75)
radioButtonArch = Radiobutton(root, text="Architecture", variable=radioValue, value=1, command=radioArchSelected)
radioButtonArch.place(x=292,y=68)
radioButtonByte = Radiobutton(root, text="Bytecode", variable=radioValue, value=3, command=radioByteSelected)
radioButtonByte.place(x=292,y=90)
radioButtonComp = Radiobutton(root, text="Compiler", variable=radioValue, value=2, command=radioCompSelected)
radioButtonComp.place(x=400,y=68)
radioButtonArchByte = Radiobutton(root, text="Arch&Byte", variable=radioValue, value=4, command=radioArchByteSelected)
radioButtonArchByte.place(x=400,y=90)
endianLabel = Label(root,text='Endianness:', justify=RIGHT)
endianLabel.place(x=500,y=93)
endianLabelText = StringVar()
endianLabel = Label(root,width=7,textvariable=endianLabelText)
endianLabel.place(x=580,y=93)
endianLabelText.set('<????>')
percentLabel = Label(root,text='Percent Compressed/Encrypted:', justify=RIGHT)
percentLabel.place(x=370,y=112)
percentLabelText = StringVar()
percentLabel = Label(root,width=7,textvariable=percentLabelText)
percentLabel.place(x=580,y=112)
percentLabelText.set('<????>')
resultsBox = Text(root, width=75, height=14, padx=5, pady=5, borderwidth=2, relief=RIDGE)
resultsBox.place(x=10, y= 130)
scrollb = Scrollbar(root, command=resultsBox.yview)
scrollb.place(x=630, y=350)
resultsBox['yscrollcommand'] = scrollb.set
resultsBox.insert(END,'Welcome to WiiBin 1.8\n----------------------\n\nNotes:\n\nThe smaller the Slide (%) and Chunk Size, the more accurate the detection and longer the runtime.\n\nToo small a Chuck Size will cause make ML difficult and less accurate.\n\nThe minimum detectable code segment size is limited to half of the selected Chunk Size.\n\nTo detect smaller code segments the Chunk Size most be reduced.')
clearDirectories()
root.mainloop()