Advertisement
dev017

Analise de DDoS utilizando aprendizagem de maquina

Sep 22nd, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | Cybersecurity | 0 0
  1. import pandas as pd
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.ensemble import RandomForestClassifier
  4. from sklearn.metrics import classification_report
  5. from scapy.all import sniff
  6. import pandas as pd
  7.  
  8. pacotes_dados = []
  9. contagem_ips = {}
  10.  
  11. def contar_pacotes(pacote):
  12.     if pacote.haslayer('IP'):
  13.         ip_src = pacote[1].src
  14.         if ip_src in contagem_ips:
  15.             contagem_ips[ip_src] += 1
  16.         else:
  17.             contagem_ips[ip_src] = 1
  18.         pacotes_dados.append({
  19.             'src_ip': ip_src,
  20.             'dst_ip': pacote[1].dst,
  21.             'length': len(pacote),
  22.             'ttl': pacote[1].ttl,
  23.         })
  24.  
  25. sniff(iface='eth0', prn=contar_pacotes, count=1000)
  26.  
  27. print("Contagem de requisições por IP:", contagem_ips)
  28.  
  29. df_pacotes = pd.DataFrame(pacotes_dados)
  30.  
  31. df_pacotes['label'] = [0 if i % 2 == 0 else 1 for i in range(len(df_pacotes))]
  32.  
  33. X = df_pacotes.drop('label', axis=1)
  34. y = df_pacotes['label']
  35.  
  36. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
  37.  
  38. modelo = RandomForestClassifier()
  39. modelo.fit(X_train, y_train)
  40.  
  41. y_pred = modelo.predict(X_test)
  42. print(classification_report(y_test, y_pred))
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement