Spaces:
Sleeping
Sleeping
update since gradio was updated
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
# import io
|
| 2 |
-
# import base64
|
| 3 |
from PIL import Image
|
| 4 |
from fastai.vision.all import load_learner
|
| 5 |
from binary2image import get_size, save_file, get_binary_data
|
|
@@ -13,18 +11,30 @@ import pickle
|
|
| 13 |
|
| 14 |
## Loading the models
|
| 15 |
entropy_classifier = pickle.load(open('entropy_tester_classifier.pkl', 'rb'))
|
| 16 |
-
model_NonObf = load_learner("model_non_obfuscated.pkl", cpu=True)
|
| 17 |
-
model_Obf = load_learner("resnet18_obfuscated_samples.pkl", cpu=True)
|
|
|
|
| 18 |
|
| 19 |
def entropy_tester(bin_data):
|
| 20 |
-
entropy = stats.entropy(bin_data,base=2)
|
| 21 |
pred = entropy_classifier.predict(
|
| 22 |
-
pd.DataFrame(data
|
| 23 |
-
|
| 24 |
return pred[0]
|
| 25 |
|
|
|
|
| 26 |
def process_file(file):
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
pred_entropy = entropy_tester(greyscale_data)
|
| 30 |
|
|
@@ -38,34 +48,50 @@ def process_file(file):
|
|
| 38 |
prediction, _, probas = model_NonObf.predict(converted_filename)
|
| 39 |
elif pred_entropy == "Obfuscated":
|
| 40 |
prediction, _, probas = model_Obf.predict(converted_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
message = f"Your file is {prediction}!!!"
|
| 43 |
-
|
| 44 |
-
if pred_entropy == "NonObf":
|
| 45 |
-
|
|
|
|
| 46 |
# Convert probas to percentages
|
| 47 |
probas_percentage = [float(prob) * 100 for prob in probas]
|
| 48 |
-
|
| 49 |
-
fig = px.bar(x=["Goodware", "Malware"],
|
| 50 |
-
y=probas_percentage, labels={'x':'Type', 'y':'Probability (%)'},
|
| 51 |
-
height=300)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return pred_entropy, message, fig, Image.open("tempfile.png")
|
| 55 |
|
|
|
|
| 56 |
# Define the layout using Blocks, Row, and Column
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
with gr.Column(scale=1):
|
| 59 |
-
file = gr.File(label="Upload Executable File")
|
| 60 |
with gr.Row():
|
| 61 |
-
with gr.Column():
|
| 62 |
text1 = gr.Textbox(label="Prediction")
|
| 63 |
prob_dist_img = gr.Plot(label="Probability Distribution")
|
| 64 |
with gr.Column():
|
| 65 |
text0 = gr.Textbox(label="Type of Obfuscation")
|
| 66 |
converted_img = gr.Image(label="Converted Image", height=300, width=300)
|
| 67 |
-
|
| 68 |
button = gr.Button(value="Process File")
|
| 69 |
-
button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
from PIL import Image
|
| 2 |
from fastai.vision.all import load_learner
|
| 3 |
from binary2image import get_size, save_file, get_binary_data
|
|
|
|
| 11 |
|
| 12 |
## Loading the models
|
| 13 |
entropy_classifier = pickle.load(open('entropy_tester_classifier.pkl', 'rb'))
|
| 14 |
+
model_NonObf = load_learner("model_non_obfuscated.pkl", cpu=True) # change to "model_NonObf.pkl" if needed
|
| 15 |
+
model_Obf = load_learner("resnet18_obfuscated_samples.pkl", cpu=True) # change to "model_Shikata.pkl" if needed
|
| 16 |
+
|
| 17 |
|
| 18 |
def entropy_tester(bin_data):
|
| 19 |
+
entropy = stats.entropy(bin_data, base=2)
|
| 20 |
pred = entropy_classifier.predict(
|
| 21 |
+
pd.DataFrame(data=entropy.reshape(1, -1), columns=['Entropy'])
|
| 22 |
+
)
|
| 23 |
return pred[0]
|
| 24 |
|
| 25 |
+
|
| 26 |
def process_file(file):
|
| 27 |
+
# Gradio 4 with type="filepath" returns a string path.
|
| 28 |
+
# Older Gradio could return a file object with `.name`.
|
| 29 |
+
if file is None:
|
| 30 |
+
return "No file", "Please upload a file.", None, None
|
| 31 |
+
|
| 32 |
+
if isinstance(file, str):
|
| 33 |
+
file_path = file
|
| 34 |
+
else:
|
| 35 |
+
file_path = file.name
|
| 36 |
+
|
| 37 |
+
greyscale_data = get_binary_data(file_path)
|
| 38 |
|
| 39 |
pred_entropy = entropy_tester(greyscale_data)
|
| 40 |
|
|
|
|
| 48 |
prediction, _, probas = model_NonObf.predict(converted_filename)
|
| 49 |
elif pred_entropy == "Obfuscated":
|
| 50 |
prediction, _, probas = model_Obf.predict(converted_filename)
|
| 51 |
+
else:
|
| 52 |
+
# If some unexpected label appears, return a safe message
|
| 53 |
+
return (
|
| 54 |
+
"Unknown",
|
| 55 |
+
"Could not determine obfuscation type.",
|
| 56 |
+
None,
|
| 57 |
+
Image.open(converted_filename),
|
| 58 |
+
)
|
| 59 |
|
| 60 |
message = f"Your file is {prediction}!!!"
|
| 61 |
+
|
| 62 |
+
if pred_entropy == "NonObf":
|
| 63 |
+
pred_entropy = "Non-Obfuscated"
|
| 64 |
+
|
| 65 |
# Convert probas to percentages
|
| 66 |
probas_percentage = [float(prob) * 100 for prob in probas]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
fig = px.bar(
|
| 69 |
+
x=["Goodware", "Malware"],
|
| 70 |
+
y=probas_percentage,
|
| 71 |
+
labels={'x': 'Type', 'y': 'Probability (%)'},
|
| 72 |
+
height=300,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
return pred_entropy, message, fig, Image.open("tempfile.png")
|
| 76 |
|
| 77 |
+
|
| 78 |
# Define the layout using Blocks, Row, and Column
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
with gr.Column(scale=1):
|
| 81 |
+
file = gr.File(label="Upload Executable File", type="filepath")
|
| 82 |
with gr.Row():
|
| 83 |
+
with gr.Column():
|
| 84 |
text1 = gr.Textbox(label="Prediction")
|
| 85 |
prob_dist_img = gr.Plot(label="Probability Distribution")
|
| 86 |
with gr.Column():
|
| 87 |
text0 = gr.Textbox(label="Type of Obfuscation")
|
| 88 |
converted_img = gr.Image(label="Converted Image", height=300, width=300)
|
| 89 |
+
|
| 90 |
button = gr.Button(value="Process File")
|
| 91 |
+
button.click(
|
| 92 |
+
process_file,
|
| 93 |
+
inputs=[file],
|
| 94 |
+
outputs=[text0, text1, prob_dist_img, converted_img],
|
| 95 |
+
)
|
| 96 |
|
| 97 |
demo.launch()
|