GranularFireplace commited on
Commit
8ab568a
·
verified ·
1 Parent(s): a5f8397

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py CHANGED
@@ -6,6 +6,7 @@ import numpy as np
6
  import os
7
  import shutil
8
  from huggingface_hub import snapshot_download
 
9
 
10
  MAL_CLASSES = ['Adialer.C', 'Agent.FYI', 'Allaple.A', 'Allaple.L', 'Alueron.gen!J', 'Autorun.K', 'C2LOP.P', 'C2LOP.gen!g', 'Dialplatform.B', 'Dontovo.A', 'Fakerean', 'Instantaccess', 'Lolyda.AA1', 'Lolyda.AA2', 'Lolyda.AA3', 'Lolyda.AT', 'Malex.gen!J', 'Obfuscator.AD', 'Rbot!gen', 'Skintrim.N', 'Swizzor.gen!E', 'Swizzor.gen!I', 'VB.AT', 'Wintrim.BX', 'Yuner.A']
11
  UPLOAD_DIR = "uploads"
@@ -61,3 +62,51 @@ def analyse(file_name: str):
61
  predicted_label = model.predict(img_array)
62
 
63
  return {"result": MAL_CLASSES[np.argmax(predicted_label)]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import os
7
  import shutil
8
  from huggingface_hub import snapshot_download
9
+ import cv2
10
 
11
  MAL_CLASSES = ['Adialer.C', 'Agent.FYI', 'Allaple.A', 'Allaple.L', 'Alueron.gen!J', 'Autorun.K', 'C2LOP.P', 'C2LOP.gen!g', 'Dialplatform.B', 'Dontovo.A', 'Fakerean', 'Instantaccess', 'Lolyda.AA1', 'Lolyda.AA2', 'Lolyda.AA3', 'Lolyda.AT', 'Malex.gen!J', 'Obfuscator.AD', 'Rbot!gen', 'Skintrim.N', 'Swizzor.gen!E', 'Swizzor.gen!I', 'VB.AT', 'Wintrim.BX', 'Yuner.A']
12
  UPLOAD_DIR = "uploads"
 
62
  predicted_label = model.predict(img_array)
63
 
64
  return {"result": MAL_CLASSES[np.argmax(predicted_label)]}
65
+
66
+ def convert_binary_to_grayscale_image(binary_file_path, output_image_path, height=None, width=None):
67
+ with open(binary_file_path, "rb") as bin_file:
68
+ binary_data = bin_file.read()
69
+
70
+ grayscale_image = np.frombuffer(binary_data, dtype=np.uint8)
71
+
72
+ total_pixels = len(grayscale_image)
73
+ if height is None and width is None:
74
+ raise ValueError("Either height or width must be specified.")
75
+ elif height is None:
76
+ height = total_pixels // width
77
+ elif width is None:
78
+ width = total_pixels // height
79
+
80
+ if height * width != total_pixels:
81
+ raise ValueError(
82
+ f"The binary file size ({total_pixels}) is not compatible with the specified dimensions ({height}x{width})."
83
+ )
84
+
85
+ grayscale_image = grayscale_image.reshape((height, width))
86
+
87
+ # Save
88
+ cv2.imwrite(output_image_path, grayscale_image)
89
+
90
+ print(f"Grayscale image saved to {output_image_path} with dimensions ({height}, {width})")
91
+ return grayscale_image
92
+
93
+ @app.get("/analysebin/{file_name}")
94
+ def analyse_bin(file_name: str):
95
+ download_dir = snapshot_download("GranularFireplace/malware")
96
+ print(download_dir)
97
+ print(os.path.join(download_dir, 'model_v2_with_weight.keras'))
98
+
99
+ convert_binary_to_grayscale_image(os.path.join(UPLOAD_DIR, file_name), "image.png", 256)
100
+
101
+ img = keras.preprocessing.image.load_img(
102
+ "image.png", target_size=(64, 64)
103
+ )
104
+
105
+ img_array = keras.preprocessing.image.img_to_array(tf.image.rgb_to_grayscale(img))
106
+
107
+ img_array = tf.expand_dims(img_array, 0) # Create a batch
108
+
109
+ model = tf.keras.models.load_model(os.path.join(download_dir, 'model_v2_with_weight.keras'))
110
+ predicted_label = model.predict(img_array)
111
+
112
+ return {"result": MAL_CLASSES[np.argmax(predicted_label)]}