我一直在遵循http://www.codeproject.com/KB/IP/sharppcap.aspx的指南来实现一个简单的包嗅探器来自动为我进行身份验证,我设法进入了过滤部分,到目前为止,我不得不对教程代码进行一些调整才能工作,但现在我很困惑。
我所收到的错误是;
'PacketDotNet.TcpPacket.GetEncapsulated(PacketDotNet.Packet)‘的最佳重载方法匹配有一些无效的参数 论点1:不能从“SharpPcap.RawCapture”转换为“PacketDotNet.Packet”
但我还没有提到PacketDotNet我自己(到目前为止,一切都是SharpPcap)。
到目前为止,我拥有的全部代码都包括在内,问题在于device_OnPacketArrival()函数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PacketDotNet;
using SharpPcap;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string ver = SharpPcap.Version.VersionString;
Console.WriteLine("SharpPcap {0}, Example1.IfList.cs", ver);
// Retrieve the device list
CaptureDeviceList devices = CaptureDeviceList.Instance;
// If no devices were found print an error
if (devices.Count < 1)
{
Console.WriteLine("No devices were found on this machine");
return;
}
// Extract a device from the list
ICaptureDevice device = devices[0];
// Register our handler function to the
// 'packet arrival' event
device.OnPacketArrival +=
new SharpPcap.PacketArrivalEventHandler(device_OnPacketArrival);
// Open the device for capturing
int readTimeoutMilliseconds = 1000;
device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
// tcpdump filter to capture only TCP/IP packets
string filter = "ip and tcp";
device.Filter = filter;
Console.WriteLine();
Console.WriteLine("-- The following tcpdump filter will be applied: \"{0}\"",
filter);
Console.WriteLine("-- Listening on {0}, hit 'Enter' to stop...",
device.Description);
// Start capturing packets indefinitely
device.Capture();
// Close the pcap device
// (Note: this line will never be called since
// we're capturing indefinitely
device.Close();
}
private static void device_OnPacketArrival(object sender, CaptureEventArgs e)
{
var tcp = TcpPacket.GetEncapsulated(e.Packet);
}
}
}发布于 2011-09-11 17:21:48
SharpPcap.RawPacket用于保存通过网络适配器捕获的原始数据,但是PacketDotNet需要在GetEncapsulated()方法工作之前解析数据包。您需要的步骤如下:
var packet = PacketDotNet.Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data);然后,您可以通过传递TcpPacket的GetEncapsulated()方法提取封装的packet。
SharpPcap源代码中的示例12显示了https://sourceforge.net/projects/sharppcap/的语法和如何修改数据包。
请记住,PacketType.GetEncapsulated()正在返回对数据包的该部分的引用,因此修改它将更改原始数据包。
发布于 2016-03-09 23:18:51
作为对Chris答案的一个更新(因为我发现我今天正在做这个),getEncapsulated()现在已经过时了,您应该使用packet.Extract()来提取封装的数据包。
发布于 2011-10-08 10:54:58
或者,您也可以使用Pcap.Net,它只有一个包类,您可以动态解析该类以获得它可能包含的任何内容,而无需执行任何包强制转换。
您只需获得一个包对象并执行(例如):
uint sequenceNumber = packet.Ethernet.IpV4.Tcp.SequenceNumber;不需要预先转换它或者知道它是什么样的数据包,所有的解析都是动态的。
https://stackoverflow.com/questions/7379516
复制相似问题