Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.ML;
- using Microsoft.ML.Data;
- using System.Collections.Generic;
- using System.Linq;
- namespace DDoSDetection
- {
- class Program
- {
- static void Main(string[] args)
- {
- var mlContext = new MLContext();
- var trainingData = new List<TrafficData>
- {
- new TrafficData { PacketCount = 50, DataRate = 10, UniquePorts = 2 },
- new TrafficData { PacketCount = 55, DataRate = 12, UniquePorts = 2 },
- new TrafficData { PacketCount = 5000, DataRate = 900, UniquePorts = 50 },
- new TrafficData { PacketCount = 48, DataRate = 9, UniquePorts = 1 },
- new TrafficData { PacketCount = 6000, DataRate = 1100, UniquePorts = 80 },
- };
- var data = mlContext.Data.LoadFromEnumerable(trainingData);
- var pipeline = mlContext.Transforms
- .Concatenate("Features", nameof(TrafficData.PacketCount), nameof(TrafficData.DataRate), nameof(TrafficData.UniquePorts))
- .Append(mlContext.Clustering.Trainers.KMeans(featureColumnName: "Features", numberOfClusters: 2));
- var model = pipeline.Fit(data);
- var predictor = mlContext.Model.CreatePredictionEngine<TrafficData, TrafficPrediction>(model);
- var incomingTraffic = new List<TrafficData>
- {
- new TrafficData { PacketCount = 60, DataRate = 11, UniquePorts = 2 },
- new TrafficData { PacketCount = 7000, DataRate = 1200, UniquePorts = 100 },
- new TrafficData { PacketCount = 45, DataRate = 8, UniquePorts = 1 },
- };
- Console.WriteLine("Анализ входящего трафика:");
- foreach (var packet in incomingTraffic)
- {
- var prediction = predictor.Predict(packet);
- if (prediction.ClusterId == 2)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine($"[!] Подозрительный трафик обнаружен! Пакеты: {packet.PacketCount}, Скорость: {packet.DataRate}, Порты: {packet.UniquePorts}");
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine($"[OK] Нормальный трафик. Пакеты: {packet.PacketCount}, Скорость: {packet.DataRate}, Порты: {packet.UniquePorts}");
- }
- }
- Console.ResetColor();
- }
- public class TrafficData
- {
- public float PacketCount { get; set; }
- public float DataRate { get; set; }
- public float UniquePorts { get; set; }
- }
- public class TrafficPrediction
- {
- [ColumnName("PredictedLabel")]
- public uint ClusterId { get; set; }
- public float[] Distance { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement