Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace LoginAnalyzer
- {
- struct LoginAttempt
- {
- public string IP;
- public DateTime Timestamp;
- public LoginAttempt(string ip, DateTime timestamp)
- {
- IP = ip;
- Timestamp = timestamp;
- }
- }
- class Program
- {
- const int Threshold = 5;
- static readonly TimeSpan TimeWindow = TimeSpan.FromMinutes(10);
- static void Main(string[] args)
- {
- List<LoginAttempt> loginAttempts = new List<LoginAttempt>
- {
- new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-9)),
- new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-8)),
- new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-7)),
- new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-6)),
- new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-5)),
- new LoginAttempt("192.168.1.1", DateTime.Now.AddMinutes(-4)),
- new LoginAttempt("10.0.0.2", DateTime.Now.AddMinutes(-1)),
- new LoginAttempt("10.0.0.2", DateTime.Now.AddMinutes(-1)),
- new LoginAttempt("10.0.0.2", DateTime.Now.AddMinutes(-1)),
- };
- DetectSuspiciousActivity(loginAttempts);
- }
- static void DetectSuspiciousActivity(List<LoginAttempt> attempts)
- {
- var groupedByIP = attempts.GroupBy(a => a.IP);
- foreach (var group in groupedByIP)
- {
- string ip = group.Key;
- DateTime now = DateTime.Now;
- var recentAttempts = group
- .Where(a => now - a.Timestamp <= TimeWindow)
- .ToList();
- if (recentAttempts.Count >= Threshold)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine($"Обнаружено {recentAttempts.Count} попыток входа с IP {ip} за последние 10 минут!");
- Console.ResetColor();
- }
- else
- {
- Console.WriteLine($"IP {ip}: {recentAttempts.Count} попыток входа за последние 10 минут.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement