欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探

 更新時(shí)間:2018年03月19日 17:07:26   作者:Alan.hsiang  
這篇文章主要為大家詳細(xì)介紹了C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文是利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包的捕獲的小例子,實(shí)現(xiàn)了端口監(jiān)控,數(shù)據(jù)包捕獲等功能,主要用于學(xué)習(xí)分享。

什么是SharpPcap?

SharpPcap 是一個(gè).NET 環(huán)境下的網(wǎng)絡(luò)包捕獲框架,基于著名的 pcap/WinPcap 庫開發(fā)。提供了捕獲、注入、分析和構(gòu)建的功能,適用于 C# 和 VB NET 開發(fā)語言。

SharpPcap有兩部分組成:1> SharpPcap.dll 負(fù)責(zé)數(shù)據(jù)的捕獲  2> PacketDotNet.dll負(fù)責(zé)數(shù)據(jù)包的解析

思路:

通過進(jìn)程名字獲取對應(yīng)的端口號。
SharpPcap獲取對應(yīng)的數(shù)據(jù)包,通過解析數(shù)據(jù)包過濾相關(guān)的端口。

涉及知識點(diǎn):

Process 獲取相關(guān)進(jìn)程信息。
netstat命令:netstat -ano|find "3844" 獲取進(jìn)程對應(yīng)的端口
SharpPcap相關(guān)信息:

       通過CaptureDeviceList的靜態(tài)方法獲取設(shè)備列表。
       通過OnPacketArrival事件接收數(shù)據(jù)包。
       通過PacketDotNet來解析數(shù)據(jù)包

效果圖下:

SharpPcap核心代碼:

/// <summary>
  /// 開始捕捉
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void btnStart_Click(object sender, EventArgs e)
  {
   if (this.combDevice.SelectedIndex > -1)
   {
    StartCapture(this.combDevice.SelectedIndex);
    this.btnStart.Enabled = false;
    this.btnStop.Enabled = true;
   }
   else {
    MessageBox.Show(this,"請選擇一個(gè)設(shè)備","提示",MessageBoxButtons.OK);
   }
  }

  /// <summary>
  /// 停止捕捉
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void btnStop_Click(object sender, EventArgs e)
  {
   Shutdown();
   this.btnStop.Enabled = false;
   this.btnStart.Enabled = true;
  }

  private void StartCapture(int itemIndex)
  {
   packetCount = 0;
   device = CaptureDeviceList.Instance[itemIndex];
   packetStrings = new Queue<PacketWrapper>();
   bs = new BindingSource();
   dgvData.DataSource = bs;
   LastStatisticsOutput = DateTime.Now;

   // start the background thread
   backgroundThreadStop = false;
   backgroundThread = new Thread(BackgroundThread);
   backgroundThread.Start();

   
   // setup background capture
   device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);
   device.OnCaptureStopped += new CaptureStoppedEventHandler(device_OnCaptureStopped);
   device.Open();

   // tcpdump filter to capture only TCP/IP packets
   string filter = "ip and tcp";
   device.Filter = filter;

   // force an initial statistics update
   captureStatistics = device.Statistics;
   UpdateCaptureStatistics();

   // start the background capture
   device.StartCapture();

   btnStop.Enabled = true;
  }

  /// <summary>
  /// 設(shè)備接收事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void device_OnPacketArrival(object sender, CaptureEventArgs e)
  {
   // print out periodic statistics about this device
   var Now = DateTime.Now;
   var interval = Now - LastStatisticsOutput;
   if (interval > new TimeSpan(0, 0, 2))
   {
    Console.WriteLine("device_OnPacketArrival: " + e.Device.Statistics);
    captureStatistics = e.Device.Statistics;
    statisticsUiNeedsUpdate = true;
    LastStatisticsOutput = Now;
   }
   
   lock (QueueLock)
   {
    PacketQueue.Add(e.Packet);
   }
  }

  /// <summary>
  /// 設(shè)備停止事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="status"></param>
  private void device_OnCaptureStopped(object sender, CaptureStoppedEventStatus status)
  {
   if (status != CaptureStoppedEventStatus.CompletedWithoutError)
   {
    MessageBox.Show("Error stopping capture", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
  }

  private void UpdateCaptureStatistics()
  {
   tlblStatistic.Text = string.Format("接收包: {0}, 丟棄包: {1}, 接口丟棄包: {2}", captureStatistics.ReceivedPackets,captureStatistics.DroppedPackets, captureStatistics.InterfaceDroppedPackets);
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論