static void Main(string[] args) { //string filePath = "../../Dataset/normal.csv"; string filePath = "../../Dataset/data_ddos.csv"; List <DataInFile> dataInFileList = File.ReadAllLines(filePath).Skip(1).Select(v => DataInFile.FromCsv(v)).ToList(); List <DataInFile> icmpDataList = dataInFileList.Where(x => x.protocol.ToUpper() == "ICMP".ToUpper()).ToList(); List <string> destinationList = icmpDataList.Select(x => x.destination).Distinct().ToList(); List <decimal> timeList = icmpDataList.Select(x => x.time).Distinct().ToList(); List <List <string> > hasilPrediksi = new List <List <string> >(); List <string> dataPrediksi = new List <string>(); dataPrediksi.Add("Time"); dataPrediksi.Add("Source"); dataPrediksi.Add("Destination"); dataPrediksi.Add("Banyak paket"); dataPrediksi.Add("Hasil Prediksi"); hasilPrediksi.Add(dataPrediksi); foreach (decimal time in timeList) { foreach (string destination in destinationList) { DataForFuzzy dataForFuzzy = new DataForFuzzy(); dataForFuzzy.jlhData = icmpDataList.Where(x => x.time == time && x.destination.ToUpper() == destination.ToUpper()).ToList().Count; List <string> dataSource = icmpDataList.Where(x => x.time == time && x.destination.ToUpper() == destination.ToUpper()).Select(y => y.source).ToList(); dataForFuzzy.jlhSource = dataSource.Count; dataForFuzzy.jlhLength = icmpDataList.Where(x => x.time == time && x.destination.ToUpper() == destination.ToUpper()).Select(y => y.length).ToList().Sum(); if (dataForFuzzy.jlhSource > 0) { decimal nilaiPrediksi = DDosDetectorUsingFuzzyLogic(dataForFuzzy); //thresholding dataPrediksi = new List <string>(); dataPrediksi.Add(time.ToString()); dataPrediksi.Add(String.Join("; ", dataSource)); dataPrediksi.Add(destination.ToString()); dataPrediksi.Add(dataForFuzzy.jlhLength.ToString()); if (nilaiPrediksi > (decimal)0.4) { dataPrediksi.Add("1"); } else { dataPrediksi.Add("0"); } hasilPrediksi.Add(dataPrediksi); } } } //Save hasil prediksi ke file //File.WriteAllLines("../../Dataset/Prediksi/hasilPrediksi_normal.csv", hasilPrediksi.Select(x => string.Join(",", x))); File.WriteAllLines("../../Dataset/Prediksi/hasilPrediksi_data_ddos.csv", hasilPrediksi.Select(x => string.Join(",", x))); }
private static decimal DDosDetectorUsingFuzzyLogic(DataForFuzzy dataForFuzzy) { decimal nilaiPrediksi = 0M; #region Fuzzifikasi List <DataFuzzy> f_jlhData = MF_JlhData(dataForFuzzy.jlhData); List <DataFuzzy> f_jlhSource = MF_JlhSource(dataForFuzzy.jlhSource); List <DataFuzzy> f_jlhLength = MF_JlhLength(dataForFuzzy.jlhLength); #endregion #region Inference : Conjunction-Disjunction (Min-Max) //Conjunction (Min) List <DataFuzzy> hasilConjunction = new List <DataFuzzy>(); foreach (DataFuzzy dt_JlhData in f_jlhData) { foreach (DataFuzzy dt_jlhSource in f_jlhSource) { foreach (DataFuzzy dt_JlhLength in f_jlhLength) { DataFuzzy dataInference = InferenceProcess(dt_JlhData, dt_jlhSource, dt_JlhLength); hasilConjunction.Add(dataInference); } } } //Disjunction (Max) List <DataFuzzy> hasilDisjuntion = new List <DataFuzzy>(); DataFuzzy rendah = new DataFuzzy(); rendah.kategori = "Rendah"; rendah.nilai = hasilConjunction.Where(x => x.kategori.ToUpper() == rendah.kategori.ToUpper()).Select(y => y.nilai).ToList().Max(); hasilDisjuntion.Add(rendah); DataFuzzy tinggi = new DataFuzzy(); tinggi.kategori = "Tinggi"; tinggi.nilai = hasilConjunction.Where(x => x.kategori.ToUpper() == tinggi.kategori.ToUpper()).Select(y => y.nilai).ToList().Max(); hasilDisjuntion.Add(tinggi); #endregion #region Defuzzifikasi : Weighted Average foreach (DataFuzzy dataDefuzzy in hasilDisjuntion) { if (dataDefuzzy.kategori.ToUpper() == "Rendah".ToUpper()) { nilaiPrediksi += dataDefuzzy.nilai * 0; } else if (dataDefuzzy.kategori.ToUpper() == "Tinggi".ToUpper()) { nilaiPrediksi += dataDefuzzy.nilai * 1; } } decimal totalValue = hasilDisjuntion.Select(x => x.nilai).Sum(); if (totalValue == 0) { nilaiPrediksi = 0; } else { nilaiPrediksi /= totalValue; } #endregion return(nilaiPrediksi); }