using System; using System.Collections.Generic; using System.Linq; using System.Net; public class DDoSDetector { private Dictionary<IPAddress, List<DateTime>> requestLog; private readonly int threshold; private readonly TimeSpan timeWindow; public DDoSDetector(int requestThreshold = 100, int timeWindowSeconds = 60) { requestLog = new Dictionary<IPAddress, List<DateTime>>(); threshold = requestThreshold; timeWindow = TimeSpan.FromSeconds(timeWindowSeconds); } public bool IsAttackDetected(IPAddress ipAddress) { var now = DateTime.UtcNow; if (!requestLog.ContainsKey(ipAddress)) { requestLog[ipAddress] = new List<DateTime>(); } // Add current request requestLog[ipAddress].Add(now); // Remove old requests outside time window requestLog[ipAddress] = requestLog[ipAddress] .ToList(); // Check if the number of requests exceeds the threshold return requestLog[ipAddress].Count > threshold; } public void CleanupOldEntries() { var now = DateTime.UtcNow; var keysToRemove = new List<IPAddress>(); foreach (var entry in requestLog) { // Remove keys that have no requests in the time window { keysToRemove.Add(entry.Key); } } // Remove the keys outside the time window foreach (var key in keysToRemove) { requestLog.Remove(key); } } // The Main method is the entry point for the program public static void Main(string[] args) { // Create an instance of the DDoSDetector var detector = new DDoSDetector(requestThreshold: 100, timeWindowSeconds: 60); // Example usage of the DDoSDetector IPAddress testIp = IPAddress.Parse("192.168.1.1"); // Simulate requests from the IP address for (int i = 0; i < 120; i++) // Simulate 120 requests { if (detector.IsAttackDetected(testIp)) { Console.WriteLine("DDoS attack detected from IP: " + testIp); break; // Exit the loop if an attack is detected } else { Console.WriteLine($"Request {i + 1} from {testIp} - No attack detected"); } } // Optionally, cleanup old entries (this would typically run periodically) detector.CleanupOldEntries(); } }
Standard input is empty
Request 1 from 192.168.1.1 - No attack detected Request 2 from 192.168.1.1 - No attack detected Request 3 from 192.168.1.1 - No attack detected Request 4 from 192.168.1.1 - No attack detected Request 5 from 192.168.1.1 - No attack detected Request 6 from 192.168.1.1 - No attack detected Request 7 from 192.168.1.1 - No attack detected Request 8 from 192.168.1.1 - No attack detected Request 9 from 192.168.1.1 - No attack detected Request 10 from 192.168.1.1 - No attack detected Request 11 from 192.168.1.1 - No attack detected Request 12 from 192.168.1.1 - No attack detected Request 13 from 192.168.1.1 - No attack detected Request 14 from 192.168.1.1 - No attack detected Request 15 from 192.168.1.1 - No attack detected Request 16 from 192.168.1.1 - No attack detected Request 17 from 192.168.1.1 - No attack detected Request 18 from 192.168.1.1 - No attack detected Request 19 from 192.168.1.1 - No attack detected Request 20 from 192.168.1.1 - No attack detected Request 21 from 192.168.1.1 - No attack detected Request 22 from 192.168.1.1 - No attack detected Request 23 from 192.168.1.1 - No attack detected Request 24 from 192.168.1.1 - No attack detected Request 25 from 192.168.1.1 - No attack detected Request 26 from 192.168.1.1 - No attack detected Request 27 from 192.168.1.1 - No attack detected Request 28 from 192.168.1.1 - No attack detected Request 29 from 192.168.1.1 - No attack detected Request 30 from 192.168.1.1 - No attack detected Request 31 from 192.168.1.1 - No attack detected Request 32 from 192.168.1.1 - No attack detected Request 33 from 192.168.1.1 - No attack detected Request 34 from 192.168.1.1 - No attack detected Request 35 from 192.168.1.1 - No attack detected Request 36 from 192.168.1.1 - No attack detected Request 37 from 192.168.1.1 - No attack detected Request 38 from 192.168.1.1 - No attack detected Request 39 from 192.168.1.1 - No attack detected Request 40 from 192.168.1.1 - No attack detected Request 41 from 192.168.1.1 - No attack detected Request 42 from 192.168.1.1 - No attack detected Request 43 from 192.168.1.1 - No attack detected Request 44 from 192.168.1.1 - No attack detected Request 45 from 192.168.1.1 - No attack detected Request 46 from 192.168.1.1 - No attack detected Request 47 from 192.168.1.1 - No attack detected Request 48 from 192.168.1.1 - No attack detected Request 49 from 192.168.1.1 - No attack detected Request 50 from 192.168.1.1 - No attack detected Request 51 from 192.168.1.1 - No attack detected Request 52 from 192.168.1.1 - No attack detected Request 53 from 192.168.1.1 - No attack detected Request 54 from 192.168.1.1 - No attack detected Request 55 from 192.168.1.1 - No attack detected Request 56 from 192.168.1.1 - No attack detected Request 57 from 192.168.1.1 - No attack detected Request 58 from 192.168.1.1 - No attack detected Request 59 from 192.168.1.1 - No attack detected Request 60 from 192.168.1.1 - No attack detected Request 61 from 192.168.1.1 - No attack detected Request 62 from 192.168.1.1 - No attack detected Request 63 from 192.168.1.1 - No attack detected Request 64 from 192.168.1.1 - No attack detected Request 65 from 192.168.1.1 - No attack detected Request 66 from 192.168.1.1 - No attack detected Request 67 from 192.168.1.1 - No attack detected Request 68 from 192.168.1.1 - No attack detected Request 69 from 192.168.1.1 - No attack detected Request 70 from 192.168.1.1 - No attack detected Request 71 from 192.168.1.1 - No attack detected Request 72 from 192.168.1.1 - No attack detected Request 73 from 192.168.1.1 - No attack detected Request 74 from 192.168.1.1 - No attack detected Request 75 from 192.168.1.1 - No attack detected Request 76 from 192.168.1.1 - No attack detected Request 77 from 192.168.1.1 - No attack detected Request 78 from 192.168.1.1 - No attack detected Request 79 from 192.168.1.1 - No attack detected Request 80 from 192.168.1.1 - No attack detected Request 81 from 192.168.1.1 - No attack detected Request 82 from 192.168.1.1 - No attack detected Request 83 from 192.168.1.1 - No attack detected Request 84 from 192.168.1.1 - No attack detected Request 85 from 192.168.1.1 - No attack detected Request 86 from 192.168.1.1 - No attack detected Request 87 from 192.168.1.1 - No attack detected Request 88 from 192.168.1.1 - No attack detected Request 89 from 192.168.1.1 - No attack detected Request 90 from 192.168.1.1 - No attack detected Request 91 from 192.168.1.1 - No attack detected Request 92 from 192.168.1.1 - No attack detected Request 93 from 192.168.1.1 - No attack detected Request 94 from 192.168.1.1 - No attack detected Request 95 from 192.168.1.1 - No attack detected Request 96 from 192.168.1.1 - No attack detected Request 97 from 192.168.1.1 - No attack detected Request 98 from 192.168.1.1 - No attack detected Request 99 from 192.168.1.1 - No attack detected Request 100 from 192.168.1.1 - No attack detected DDoS attack detected from IP: 192.168.1.1