转自http://www.cnblogs.com/billmo/archive/2008/11/09/1329972.html

在做大学最后的毕业设计了,无线局域网络远程安全监控策略
那么抓包是这个系统设计的基础
以前一直都是知道用winpcap的,现在网上搜了一下,有用C#封装好了的,很好用
下面是其中的几个用法
这个类库作者的主页:http://www.tamirgal.com/home/default.aspx

PcapOpen()有下面几个方法

  • PcapOpen()
  • PcapOpen(bool promiscuous_mode)
  • PcapOpen(bool promiscuous_mode, int read_timeout)

promiscuous_mode:在普通的抓取模式下,我们只抓取那些目的地为目标网络的包,而处于promiscuous_mode时,则抓取所有的包,包括转发的包.通常我们都是开启这种模式的

下面是示例:

 

使用SharpPCap在C#下进行网络抓包//Extract a device from the list
使用SharpPCap在C#下进行网络抓包

使用SharpPCap在C#下进行网络抓包PcapDevice device 
= devices[i];
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//Register our handler function to the 
使用SharpPCap在C#下进行网络抓包使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//'packet arrival' event
使用SharpPCap在C#下进行网络抓包

使用SharpPCap在C#下进行网络抓包device.PcapOnPacketArrival 
+= 
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包  
new SharpPcap.PacketArrivalEvent(device_PcapOnPacketArrival);
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//Open the device for capturing
 
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包//true -- means promiscuous mode
使用SharpPCap在C#下进行网络抓包使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//1000 -- means a read wait of 1000ms
使用SharpPCap在C#下进行网络抓包

使用SharpPCap在C#下进行网络抓包device.PcapOpen(
true1000);
使用SharpPCap在C#下进行网络抓包使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包Console.WriteLine(
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包    
"-- Listenning on {0}, hit 'Enter' to stop...",
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包    device.PcapDescription);
使用SharpPCap在C#下进行网络抓包使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//Start the capturing process
使用SharpPCap在C#下进行网络抓包

使用SharpPCap在C#下进行网络抓包device.PcapStartCapture();
使用SharpPCap在C#下进行网络抓包使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//Wait for 'Enter' from the user.
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包Console.ReadLine();
使用SharpPCap在C#下进行网络抓包使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//Stop the capturing process
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包device.PcapStopCapture();
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//Close the pcap device
使用SharpPCap在C#下进行网络抓包
device.PcapClose();
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包

 

 

PcapStartCapture()对应PcapStopCapture()

使用PcapCapture(int packetCount)时我们可以使用SharpPcap.INFINITE,来达到持续抓包的功能

 

Note:通常CRC的数据是不在数据包的中的,因为通常错误的CRC包会被自动丢弃.

 

上面的需要注册一个event handle,这在很多时候是不可行的,所以我们推荐使用下面这个方法PcapGetNextPacket()

 

使用SharpPCap在C#下进行网络抓包//Extract a device from the list
使用SharpPCap在C#下进行网络抓包

使用SharpPCap在C#下进行网络抓包PcapDevice device 
= devices[i];
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//Open the device for capturing
使用SharpPCap在C#下进行网络抓包
//true -- means promiscuous mode
使用SharpPCap在C#下进行网络抓包
//1000 -- means a read wait of 1000ms
使用SharpPCap在C#下进行网络抓包
device.PcapOpen(true1000);
使用SharpPCap在C#下进行网络抓包Console.WriteLine();
使用SharpPCap在C#下进行网络抓包Console.WriteLine(
"-- Listenning on {0}...",
使用SharpPCap在C#下进行网络抓包device.PcapDescription);
使用SharpPCap在C#下进行网络抓包Packet packet 
= null;
使用SharpPCap在C#下进行网络抓包
使用SharpPCap在C#下进行网络抓包
//Keep capture packets using PcapGetNextPacket()
使用SharpPCap在C#下进行网络抓包
while( (packet=device.PcapGetNextPacket()) != null )
使用SharpPCap在C#下进行网络抓包

 

 

 

 

PcapSetFilter() 设置过滤条件

 

string filter = "ip and tcp";
device.PcapSetFilter( filter );

 

 

 

下面这个例子通过抓取TCP,输出他们的时间,长度,IP,源端口,目的IP,目的端口


}

相关文章:

  • 2021-05-24
  • 2021-09-19
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2022-12-23
猜你喜欢
  • 2021-12-10
  • 2021-06-21
  • 2021-11-13
  • 2021-05-20
  • 2021-08-14
  • 2021-09-15
相关资源
相似解决方案