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