Spaces:
Runtime error
Runtime error
import tensorflow as tf | |
import gradio as gr | |
from joblib import load | |
import numpy as np | |
model_multi= tf.keras.models.load_model('bestmodel_attack.h5') | |
model_bin = load("neural.joblib") | |
# protocol icmp tcp udp | |
# service IRC X11 Z3950 aol auth bgp courier csnet_ns ctf daytime discard domain domain_u echo eco_i ecr_i | |
# efs exec finger ftp ftp_data gopher harvest hostnames http http_2784 http_443 http_8001 imap4 | |
# iso_tsap klogin kshell ldap link login mtp name netbios_dgm netbios_ns netbios_ssn | |
# netstat nnsp nntp ntp_u other pm_dump pop_2 pop_3 printer private red_i remote_job rje | |
# shell smtp sql_net ssh sunrpc supdup systat telnet tftp_u tim_i time urh_i urp_i uucp | |
# uucp_path vmnet whois | |
# flag | |
# OTH RSTO RSTOS0 RSTR S0 S1 S2 S3 SF SH | |
# duration | |
# src_bytes | |
# dst_bytes | |
# urgent | |
# root_shell | |
# su_attempted | |
# num_file_creations | |
# num_shells | |
# num_access_files | |
# is_guest_login | |
data_mining = { | |
"protocol": ["icmp", "tcp", "udp"], | |
"service":['IRC', 'X11', 'Z39_50', 'aol','auth', 'bgp', 'courier', 'csnet_ns','ctf', 'daytime', | |
'discard', 'domain','domain_u', 'echo', 'eco_i', 'ecr_i','efs', 'exec', 'finger', 'ftp', | |
'ftp_data', 'gopher', 'harvest','hostnames', 'http', 'http_2784','http_443', 'http_8001', | |
'imap4','iso_tsap', 'klogin', 'kshell', 'ldap','link', 'login', 'mtp', 'name','netbios_dgm', | |
'netbios_ns', 'netbios_ssn','netstat', 'nnsp', 'nntp', 'ntp_u','other', 'pm_dump', | |
'pop_2', 'pop_3','printer', 'private', 'red_i','remote_job', 'rje', 'shell', 'smtp', | |
'sql_net', 'ssh', 'sunrpc', 'supdup','systat', 'telnet', 'tftp_u', 'tim_i','time', | |
'urh_i', 'urp_i', 'uucp','uucp_path', 'vmnet', 'whois'], | |
'flag':['OTH', 'REJ', 'RSTO', 'RSTOS0', 'RSTR','S0', 'S1', 'S2', 'S3', 'SF', 'SH'] | |
} | |
def greet(choix,protocol,services, flag,duration,src_bytes,dst_bytes,land,wrong_fragment,urgent,hot, # ok | |
num_failed_logins,logged_in,num_compromised,root_shell,su_attempted,num_root,num_file_creations,num_shells # ok | |
,num_access_files,num_outbound_cmds,is_host_login,is_guest_login,count,srv_count,serror_rate,srv_serror_rate #ok | |
,rerror_rate,srv_rerror_rate,same_srv_rate,diff_srv_rate,srv_diff_host_rate,dst_host_count,dst_host_srv_count #ok | |
,dst_host_same_srv_rate,dst_host_diff_srv_rate,dst_host_same_src_port_rate,dst_host_srv_diff_host_rate,dst_host_serror_rate | |
,dst_host_srv_serror_rate,dst_host_rerror_rate,dst_host_srv_rerror_rate): | |
liste_head = [0]* len(data_mining["protocol"]) | |
liste_head[data_mining["protocol"].index(protocol)] = 1 | |
liste_head2 = [0]* len(data_mining["service"]) | |
liste_head2[data_mining["service"].index(services)] = 1 | |
liste_head3 = [0]* len(data_mining["flag"]) | |
liste_head3[data_mining["flag"].index(flag)] = 1 | |
list1 = list() | |
pred = None | |
name = str() | |
list1 = liste_head + liste_head2 + liste_head3 + [duration,src_bytes,dst_bytes,land,wrong_fragment,urgent,hot, | |
num_failed_logins,logged_in,num_compromised,root_shell,su_attempted,num_root,num_file_creations,num_shells | |
,num_access_files,num_outbound_cmds,is_host_login,is_guest_login,count,srv_count,serror_rate,srv_serror_rate | |
,rerror_rate,srv_rerror_rate,same_srv_rate,diff_srv_rate,srv_diff_host_rate,dst_host_count,dst_host_srv_count | |
,dst_host_same_srv_rate,dst_host_diff_srv_rate,dst_host_same_src_port_rate,dst_host_srv_diff_host_rate,dst_host_serror_rate | |
,dst_host_srv_serror_rate,dst_host_rerror_rate,dst_host_srv_rerror_rate] | |
arr = np.array(list1) | |
if choix == "Binaire": | |
pred = int(model_bin.predict(arr.reshape(1,arr.shape[0])).argmax()) | |
if pred == 0: | |
name = "Aucune Attaque détectée" | |
else: | |
name= "Attaque détectée" | |
else: | |
pred = int(model_multi.predict(arr.reshape(1,arr.shape[0])).argmax()) | |
if pred == 0: | |
name = "Aucune Attaque détectée" | |
elif pred == 1: | |
name = "Attaque DOS détecté" | |
elif pred == 2: | |
name = "Attaque Probe détecté" | |
elif pred == 3: | |
name = "Attaque de Privilège détecté" | |
else: | |
name = "Attaque d'accès détecter" | |
return f"La prédiction est : {name} " | |
iface = gr.Interface( | |
fn=greet, | |
title="API de Test de pénétration", | |
description="Cette API est utilisé pour deux actions binaires et multidetection assisté d'un environnement test continuel", | |
inputs=[ | |
gr.inputs.Radio(["Binaire", "Catégorisation"],label="Choix de Prediction"), # choix | |
gr.inputs.Radio(["icmp", "tcp", "udp"],label="protocol de communication"), # protocol | |
gr.inputs.Radio(['IRC', 'X11', 'Z39_50', 'aol','auth', 'bgp', 'courier', 'csnet_ns','ctf', 'daytime', | |
'discard', 'domain','domain_u', 'echo', 'eco_i', 'ecr_i','efs', 'exec', 'finger', 'ftp', | |
'ftp_data', 'gopher', 'harvest','hostnames', 'http', 'http_2784','http_443', 'http_8001', | |
'imap4','iso_tsap', 'klogin', 'kshell', 'ldap','link', 'login', 'mtp', 'name','netbios_dgm', | |
'netbios_ns', 'netbios_ssn','netstat', 'nnsp', 'nntp', 'ntp_u','other', 'pm_dump', | |
'pop_2', 'pop_3','printer', 'private', 'red_i','remote_job', 'rje', 'shell', 'smtp', | |
'sql_net', 'ssh', 'sunrpc', 'supdup','systat', 'telnet', 'tftp_u', 'tim_i','time', | |
'urh_i', 'urp_i', 'uucp','uucp_path', 'vmnet', 'whois'] | |
,label="services de communication"), # service | |
gr.inputs.Radio(['OTH', 'REJ', 'RSTO', 'RSTOS0', 'RSTR','S0', 'S1', 'S2', 'S3', 'SF', 'SH'], # 10 | |
label="drapeau de communication"), # flag | |
gr.inputs.Slider(minimum=0, maximum=360,label="durée de l'execution"), # duration | |
gr.inputs.Slider(minimum=0, maximum=360,label="nombre de bits sources"), # src_bytes | |
gr.inputs.Slider(minimum=0, maximum=360,label="nombre de bits destination"), # dst_bytes | |
gr.inputs.Slider(minimum=0, maximum=360,label="land"), # land | |
gr.inputs.Slider(minimum=0, maximum=360,label="wrong_fragment"), # wrong_fragment | |
gr.inputs.Slider(minimum=0, maximum=150,step=1,label="l'urgence de l'execution"), # urgent | |
gr.inputs.Slider(minimum=0, maximum=45,step=1,label="hot"), # hot | |
gr.inputs.Slider(minimum=0, maximum=100,step=1,label="num_failed_logins"), # num_failed_logins | |
gr.inputs.Slider(minimum=0, maximum=3,step=1,label="logged_in"), # logged_in | |
gr.inputs.Slider(minimum=0, maximum=350,step=1,label="num_compromised"), # num_compromised | |
gr.inputs.Slider(minimum=0, maximum=35,step=1,label="ouverture du shell en super utilisateur"), # root_shell | |
gr.inputs.Slider(minimum=0, maximum=50,step=1,label="utilisation du mode super utilisateur"), # su_attempted | |
gr.inputs.Slider(minimum=0, maximum=315,step=1,label="num_root"), # num_root | |
gr.inputs.Slider(minimum=0, maximum=200,step=1,label="nombre de fichier en création"), # num_file_creations | |
gr.inputs.Slider(minimum=0, maximum=200,step=1,label="nombre de shell"), # num_shells | |
gr.inputs.Slider(minimum=0, maximum=200,step=1,label="nombre d'accès de fichier"), # num_access_files | |
gr.inputs.Slider(minimum=0, maximum=1,step=1,label="num_outbound_cmds"), # num_outbound_cmds | |
gr.inputs.Slider(minimum=0, maximum=400,step=1,label="is_host_login"), # is_host_login | |
gr.inputs.Slider(minimum=0, maximum=15,step=1,label="is_guest_login"), # is_guest_login | |
gr.inputs.Slider(minimum=0, maximum=5,step=1,label="count"), # count | |
gr.inputs.Slider(minimum=0, maximum=7,step=1,label="srv_count"), # srv_count | |
gr.inputs.Slider(minimum=0, maximum=1.6,step=.1,label="serror_rate"), # serror_rate | |
gr.inputs.Slider(minimum=0, maximum=2,step=.1,label="srv_serror_rate"), # srv_rerror_rate | |
gr.inputs.Slider(minimum=0, maximum=3,step=.1,label="rerror_rate"), # rerror_rate | |
gr.inputs.Slider(minimum=0, maximum=4,step=.1,label="srv_rerror_rate"), # srv_rerror_rate | |
gr.inputs.Slider(minimum=0, maximum=1,step=.1,label="same_srv_rate"), # same_srv_rate | |
gr.inputs.Slider(minimum=0, maximum=6,step=1,label="diff_srv_rate"), # diff_srv_rate | |
gr.inputs.Slider(minimum=0, maximum=6,step=1,label="srv_diff_host_rate"), # srv_diff_host_rate | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="dst_host_count"), # dst_host_count | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="dst_host_srv_count"), # dst_host_srv_count | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="d,st_host_same_srv_rate"), # dst_host_same_srv_rate | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="dst_host_same_srv_rate"), # dst_host_diff_srv_rate | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="dst_host_same_src_port_rate"), # dst_host_same_src_port_rate | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="dst_host_srv_diff_host_rate"), # dst_host_srv_diff_host_rate | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="dst_host_serror_rate"), # dst_host_serror_rate | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="dst_host_srv_serror_rate"), # dst_host_srv_serror_rate | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="dst_host_rerror_rate"), # dst_host_rerror_rate | |
gr.inputs.Slider(minimum=0, maximum=10,step=1,label="dst_host_srv_rerror_rate") #, # dst_host_srv_rerror_rate | |
# gr.inputs.Number(label="sources bytes de la chaine d'execution"), | |
# gr.inputs.Number(label="destination bytes de la chaine d'execution"), | |
# gr.inputs.Slider(minimum=0, maximum=80,step=1,label="land"), # land | |
# gr.inputs.Number(label="wrong fragment"), | |
# gr.inputs.Slider(minimum=0, maximum=150,step=1,label="l'urgence de l'execution"), | |
# gr.inputs.Slider(minimum=0, maximum=45,step=1,label="hot"), | |
# gr.inputs.Slider(minimum=0, maximum=100,step=1,label="num_failed_logins"), | |
# gr.inputs.Slider(minimum=0, maximum=3,step=1,label="logged_in"), | |
# gr.inputs.Slider(minimum=0, maximum=350,step=1,label="num_compromised"), | |
# gr.inputs.Slider(minimum=0, maximum=35,step=1,label="ouverture du shell en super utilisateur"), | |
# gr.inputs.Slider(minimum=0, maximum=50,step=1,label="utilisation du mode super utilisateur"), | |
# gr.inputs.Slider(minimum=0, maximum=315,step=1,label=",num_root"), | |
# gr.inputs.Slider(minimum=0, maximum=200,step=1,label=",nombre de fichier en création"), | |
# gr.inputs.Slider(minimum=0, maximum=200,step=1,label=",nombre de shell"), | |
# gr.inputs.Slider(minimum=0, maximum=200,step=1,label=",nombre d'accès de fichier"), | |
# gr.inputs.Slider(minimum=0, maximum=1,step=1,label="num_outbound_cmds"), | |
# gr.inputs.Slider(minimum=0, maximum=400,step=1,label=",is_host_login"), | |
# gr.inputs.Slider(minimum=0, maximum=15,step=1,label="i,s_guest_login"), | |
# gr.inputs.Slider(minimum=0, maximum=5,step=1,label="co,unt"), | |
# gr.inputs.Slider(minimum=0, maximum=7,step=1,label="sr,v_count"), | |
# gr.inputs.Slider(minimum=0, maximum=1.6,step=.1,label=,"serror_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=2,step=.1,label="s,rv_serror_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=3,step=.1,label="r,error_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=4,step=.1,label="s,rv_rerror_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=1,step=.1,label="s,ame_srv_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=6,step=1,label="di,ff_srv_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=6,step=1,label="sr,v_diff_host_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=10,step=1,label="d,st_host_count"), | |
# gr.inputs.Slider(minimum=0, maximum=10,step=1,label="d,st_host_srv_count"), | |
# gr.inputs.Slider(minimum=0, maximum=10,step=1,label="d,st_host_same_srv_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=10,step=1,label="d,st_host_same_src_port_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=10,step=1,label="d,st_host_srv_diff_host_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=10,step=1,label="d,st_host_serror_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=10,step=1,label="d,st_host_srv_serror_rate"), | |
# gr.inputs.Slider(minimum=0, maximum=10,step=1,label="d,st_host_srv_rerror_rate")#, | |
# gr.inputs.Slider(minimum=0, maximum=15,step=1,label=,"nombre de shell crée"), | |
# gr.inputs.Slider(minimum=0, maximum=15,step=1,label=,"accès aux fichiers systèmes"), | |
# gr.inputs.Slider(minimum=0, maximum=1,step=1,label="est en login") | |
], # ok | |
outputs="text", | |
examples=[ | |
["Binaire", "icmp", 'IRC','OTH',4000,45,35,2,0,1,20,5,7,0], | |
["Catégorisation", "tcp", 'netstat','REJ',3500,25,15,0,1,0,17,10,2,0], | |
["Catégorisation", "udp", 'login','S0',150,30,5,0,1,20,2,15,1,1] | |
], | |
flagging_options=["this", "or", "that"], | |
theme="dark-seafoam", | |
thumbnail='images.jpg' | |
) | |
iface.launch()# share= True,auth=("admin", "pass1234")) | |