Advertisement
vovanhik_24

Task1.3

Apr 8th, 2025
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace LoginAnalyzer
  6. {
  7.     struct LoginAttempt
  8.     {
  9.         public string IP;
  10.         public DateTime Timestamp;
  11.  
  12.         public LoginAttempt(string ip, DateTime timestamp)
  13.         {
  14.             IP = ip;
  15.             Timestamp = timestamp;
  16.         }
  17.     }
  18.  
  19.     class Program
  20.     {
  21.         const int Threshold = 5;
  22.  
  23.         static readonly TimeSpan TimeWindow = TimeSpan.FromMinutes(10);
  24.  
  25.         static void Main(string[] args)
  26.         {
  27.             List<LoginAttempt> loginAttempts = new List<LoginAttempt>
  28.             {
  29.                 new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-9)),
  30.                 new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-8)),
  31.                 new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-7)),
  32.                 new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-6)),
  33.                 new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-5)),
  34.                 new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-4)),
  35.                 new LoginAttempt("10.0.0.2", DateTime.Now.AddMinutes(-1)),
  36.                 new LoginAttempt("10.0.0.2", DateTime.Now.AddMinutes(-1)),
  37.                 new LoginAttempt("10.0.0.2", DateTime.Now.AddMinutes(-1)),
  38.             };
  39.  
  40.             DetectSuspiciousActivity(loginAttempts);
  41.         }
  42.  
  43.         static void DetectSuspiciousActivity(List<LoginAttempt> attempts)
  44.         {
  45.             var groupedByIP = attempts.GroupBy(a => a.IP);
  46.  
  47.             foreach (var group in groupedByIP)
  48.             {
  49.                 string ip = group.Key;
  50.  
  51.                 DateTime now = DateTime.Now;
  52.                 var recentAttempts = group
  53.                     .Where(a => now - a.Timestamp <= TimeWindow)
  54.                     .ToList();
  55.  
  56.                 if (recentAttempts.Count >= Threshold)
  57.                 {
  58.                     Console.ForegroundColor = ConsoleColor.Red;
  59.                     Console.WriteLine($"Обнаружено {recentAttempts.Count} попыток входа с IP {ip} за последние 10 минут!");
  60.                     Console.ResetColor();
  61.                 }
  62.                 else
  63.                 {
  64.                     Console.WriteLine($"IP {ip}: {recentAttempts.Count} попыток входа за последние 10 минут.");
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement