当前位置: 移动技术网 > IT编程>开发语言>c# > C#利用SharpPcap实现网络包捕获嗅探

C#利用SharpPcap实现网络包捕获嗅探

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文是利用sharppcap实现网络包的捕获的小例子,实现了端口监控,数据包捕获等功能,主要用于学习分享。 什么是sharppcap? sharppcap 是一个.ne

本文是利用sharppcap实现网络包的捕获的小例子,实现了端口监控,数据包捕获等功能,主要用于学习分享。

什么是sharppcap?

sharppcap 是一个.net 环境下的网络包捕获框架,基于著名的 pcap/winpcap 库开发。提供了捕获、注入、分析和构建的功能,适用于 c# 和 vb net 开发语言。

sharppcap有两部分组成:1> sharppcap.dll 负责数据的捕获  2> packetdotnet.dll负责数据包的解析

思路:

通过进程名字获取对应的端口号。
sharppcap获取对应的数据包,通过解析数据包过滤相关的端口。

涉及知识点:

process 获取相关进程信息。
netstat命令:netstat -ano|find "3844" 获取进程对应的端口
sharppcap相关信息:

       通过capturedevicelist的静态方法获取设备列表。
       通过onpacketarrival事件接收数据包。
       通过packetdotnet来解析数据包

效果图下:

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,"请选择一个设备","提示",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>
  /// 设备接收事件
  /// </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>
  /// 设备停止事件
  /// </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);
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网