Spaces:
Runtime error
Runtime error
cheikhdeme
commited on
Commit
•
399f819
1
Parent(s):
ec35913
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import joblib
|
4 |
+
import pefile
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
import gradio as gr
|
8 |
+
import hashlib
|
9 |
+
|
10 |
+
|
11 |
+
# Charger le modèle pré-entraîné
|
12 |
+
try:
|
13 |
+
model = joblib.load('random_forest_model.pkl')
|
14 |
+
except Exception as e:
|
15 |
+
print(f"Erreur de chargement du modèle : {e}")
|
16 |
+
model = None
|
17 |
+
|
18 |
+
def calculate_file_hash(file_path):
|
19 |
+
"""Calculer le hash SHA-256 du fichier."""
|
20 |
+
sha256_hash = hashlib.sha256()
|
21 |
+
with open(file_path, "rb") as f:
|
22 |
+
for byte_block in iter(lambda: f.read(4096), b""):
|
23 |
+
sha256_hash.update(byte_block)
|
24 |
+
return sha256_hash.hexdigest()
|
25 |
+
|
26 |
+
def extract_pe_attributes(file_path):
|
27 |
+
"""Extraction avancée des attributs du fichier PE."""
|
28 |
+
try:
|
29 |
+
pe = pefile.PE(file_path)
|
30 |
+
|
31 |
+
attributes = {
|
32 |
+
# Attributs PE standard
|
33 |
+
'AddressOfEntryPoint': pe.OPTIONAL_HEADER.AddressOfEntryPoint,
|
34 |
+
'MajorLinkerVersion': pe.OPTIONAL_HEADER.MajorLinkerVersion,
|
35 |
+
'MajorImageVersion': pe.OPTIONAL_HEADER.MajorImageVersion,
|
36 |
+
'MajorOperatingSystemVersion': pe.OPTIONAL_HEADER.MajorOperatingSystemVersion,
|
37 |
+
'DllCharacteristics': pe.OPTIONAL_HEADER.DllCharacteristics,
|
38 |
+
'SizeOfStackReserve': pe.OPTIONAL_HEADER.SizeOfStackReserve,
|
39 |
+
'NumberOfSections': pe.FILE_HEADER.NumberOfSections,
|
40 |
+
'ResourceSize':pe.OPTIONAL_HEADER.DATA_DIRECTORY[2].Size
|
41 |
+
}
|
42 |
+
|
43 |
+
"""## Ressources
|
44 |
+
data_directory_entries = pe.OPTIONAL_HEADER.DATA_DIRECTORY
|
45 |
+
# Parcourir la liste pour trouver l'entrée du répertoire des ressources
|
46 |
+
for entry in data_directory_entries:
|
47 |
+
if entry.name == "IMAGE_DIRECTORY_ENTRY_RESOURCE":
|
48 |
+
resource_size = entry.Size
|
49 |
+
attributes['ResourceSize'] = resource_size
|
50 |
+
break
|
51 |
+
else:
|
52 |
+
attributes['ResourceSize'] = 0"""
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
return attributes
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Erreur de traitement du fichier {file_path}: {str(e)}")
|
59 |
+
return f"Erreur de traitement du fichier {file_path}: {str(e)}"
|
60 |
+
|
61 |
+
def predict_malware(file):
|
62 |
+
"""Prédiction de malware avec gestion d'erreurs."""
|
63 |
+
if model is None:
|
64 |
+
return "Erreur : Modèle non chargé"
|
65 |
+
|
66 |
+
try:
|
67 |
+
# Extraire les attributs du fichier
|
68 |
+
attributes = extract_pe_attributes(file.name)
|
69 |
+
if "Erreur" in attributes:
|
70 |
+
return attributes
|
71 |
+
|
72 |
+
# Convertir en DataFrame
|
73 |
+
df = pd.DataFrame([attributes])
|
74 |
+
|
75 |
+
# Prédiction
|
76 |
+
prediction = model.predict(df)
|
77 |
+
proba = model.predict_proba(df)[0]
|
78 |
+
|
79 |
+
# Résultat avec probabilité
|
80 |
+
if prediction[0] == 1:
|
81 |
+
return f"🚨 MALWARE (Probabilité: {proba[1] * 100:.2f}%)"
|
82 |
+
else:
|
83 |
+
return f"✅ Fichier Légitime (Probabilité: {proba[0] * 100:.2f}%)"
|
84 |
+
except Exception as e:
|
85 |
+
return f"Erreur d'analyse : {str(e)}"
|
86 |
+
|
87 |
+
# Interface Gradio
|
88 |
+
demo = gr.Interface(
|
89 |
+
fn=predict_malware,
|
90 |
+
inputs=gr.File(file_types=['.exe', '.dll', '.sys'], label="Télécharger un fichier exécutable"),
|
91 |
+
outputs="text",
|
92 |
+
title="🛡️ Détecteur de Malwares",
|
93 |
+
theme='huggingface' # Thème moderne
|
94 |
+
)
|
95 |
+
|
96 |
+
if __name__ == "__main__":
|
97 |
+
demo.launch(share=True) # Rend l'interface accessible publiquement
|