如何用 C# 发送原始以太网数据包?

发布于 2024-09-28 04:50:30 字数 1436 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱娆 2024-10-05 04:50:31

根据 Saint_pl 的建议:

我找到了可能更好的解决方案 - 类似于 SharpPcap。它是 Pcap.Net - WinPcap 的 .NET 包装器。现在我可以随意修改我的数据包。


我有一些资源可能对您有帮助。我不会在 Windows 7 中尝试该解决方案,但也许它包含一些好的开始信息。

原始以太网数据包操作CodeProject 上的镜像

本文的目的是解释如何在 Microsoft 平台上使用 C# 发送原始以太网数据包。原始以太网数据包是发送到物理线路的完整第 2 层网络帧。发送这样的帧允许您操纵目标和源 MAC 地址以及第 3 层协议字段。

还有一些有关原始套接字的信息(以防万一您也感兴趣):

客户端(和服务器)套接字通信 看一下整个章节,但这里是关键部分:

不发送数据包,但可能很有趣:C# 中的网络嗅探器SharpPcap - .NET 数据包捕获框架

Based on suggestion by Saint_pl:

I found probably better solution - similar to SharpPcap. It's Pcap.Net - .NET wrapper for WinPcap. Now I can modify my packets whatever I want.


I have some resources for you that maybe helpful. I don't try that solutions in Windows 7 but maybe it contains some good info to start.

Raw Ethernet Packet Manipulation or mirror on CodeProject

This purpose of this article is to explain how to send a raw Ethernet packet using C# on a Microsoft platform. A raw Ethernet packet is the complete Layer 2 network frame that is sent to the physical wire. Sending a frame like this allows you to manipulate the target and source MAC addresses and the Layer 3 protocol fields.

Also some info on raw sockets (just in case you interesting too):

Client (and Server) Sockets Communication take a look on whole chapter but here key parts:

Not sending packets but maybe interesting: A Network Sniffer in C#, SharpPcap - A Packet Capture Framework for .NET

东京女 2024-10-05 04:50:31

iphelper API 有一些低级的东西 - 但可能没有你想要的那么低

iphelper API has some low level stuff - but probably not quite as low as you want to get

青丝拂面 2024-10-05 04:50:31
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("10.25.184.11"), 4456);

server.Connect(ip);

byte[] sendData = new byte[] { 0, 8, 32, 64 };
server.Send(sendData);

//done. now let's listen for data

byte[] receiveData = new byte[1024];
int receivedDataLength = server.Receive(receiveData);

//if the response is a string message
string stringData = Encoding.ASCII.GetString(receiveData, 0, receivedDataLength);
Console.WriteLine(stringData);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("10.25.184.11"), 4456);

server.Connect(ip);

byte[] sendData = new byte[] { 0, 8, 32, 64 };
server.Send(sendData);

//done. now let's listen for data

byte[] receiveData = new byte[1024];
int receivedDataLength = server.Receive(receiveData);

//if the response is a string message
string stringData = Encoding.ASCII.GetString(receiveData, 0, receivedDataLength);
Console.WriteLine(stringData);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文