如何獲得在給定時間內發送超過 X 個數據包的 IP (How do I get IPs that sent more than X packets in less than a given time)


問題描述

如何獲得在給定時間內發送超過 X 個數據包的 IP (How do I get IPs that sent more than X packets in less than a given time)

I have a C# program that detects incoming TCP/IP packets on any given ethernet device. Every packet is processed in the following struct:

struct Packet{
   String sourceIp;
   DateTime arrivalDate;
}

If I have a List of every incoming Packets (List), how do I get those IPs that have more than X packets in less than Y seconds (say 1 second)?

I have no idea how to approach this problem, any help/tip will be highly appreciated.


參考解法

方法 1:

Using Linq, it will be something like this:

  List<Packet> allPackets =
     new List<Packet>
        {
           new Packet {arrivalDate = DateTime.Parse("2000-01-01 0:00:00"), sourceIp = "a"},
           new Packet {arrivalDate = DateTime.Parse("2000-01-01 0:00:01"), sourceIp = "a"},
           new Packet {arrivalDate = DateTime.Parse("2000-01-01 0:00:01"), sourceIp = "a"},
           new Packet {arrivalDate = DateTime.Parse("2000-01-01 0:01:00"), sourceIp = "a"},
           new Packet {arrivalDate = DateTime.Parse("2000-01-01 0:00:00"), sourceIp = "b"},
           new Packet {arrivalDate = DateTime.Parse("2000-01-01 0:01:00"), sourceIp = "b"},
           new Packet {arrivalDate = DateTime.Parse("2000-01-01 0:02:00"), sourceIp = "b"},
           new Packet {arrivalDate = DateTime.Parse("2000-01-01 0:03:00"), sourceIp = "b"},
        };
  var xPackets = 2;
  var interval = TimeSpan.FromSeconds(15);

  // We group all the packets by ip, then within that, order the packets by date.
  var ips =
     allPackets
        .GroupBy(
           p => p.sourceIp,
           (ip, packets) => new
                                {
                                   ip,
                                   packets = packets.OrderBy(p => p.arrivalDate).ToList()
                                })
        .ToList();
  // Build a list of IPs with at least x packets in y interval.
  var rapidIps = new List<string>();
  foreach (var ipPacket in ips)
  {
     for (int i = 0, j = xPackets; j < ipPacket.packets.Count; i++, j++)
     {
        if (ipPacket.packets[i].arrivalDate + interval >= ipPacket.packets[j].arrivalDate)
        {
           rapidIps.Add((ipPacket.ip));
           break;
        }

     }
  }

At the end, rapidIps contains [a].

(by Guj Milagent-j)

參考文件

  1. How do I get IPs that sent more than X packets in less than a given time (CC BY-SA 3.0/4.0)

#datetime #sharppcap #winpcap #.net #C#






相關問題

NHibernate:HQL:從日期字段中刪除時間部分 (NHibernate:HQL: Remove time part from date field)

如何獲得在給定時間內發送超過 X 個數據包的 IP (How do I get IPs that sent more than X packets in less than a given time)

Памылка дадання даты пры адніманні ад 0:00 (Dateadd error when subtracting from 0:00)

查找與日曆相比缺失的日期 (Find missing date as compare to calendar)

CodeReview:java Dates diff(以天為單位) (CodeReview: java Dates diff (in day resolution))

顯示兩個給定時間之間的 15 分鐘步長 (display 15-minute steps between two given times)

如何在 C# 中獲取月份名稱? (How to get the month name in C#?)

fromtimestamp() 的反義詞是什麼? (What is the opposite of fromtimestamp()?)

構建 JavaScript 時缺少模塊 (Missing Module When Building JavaScript)

setTimeout 一天中的特定時間,然後停止直到下一個特定時間 (setTimeout for specific hours of day and then stop until next specific time)

將浮點數轉換為 datatime64[ns] (Converting float into datatime64[ns])

Python Dataframe 在連接時防止重複 (Python Dataframe prevent duplicates while concating)







留言討論