如何在C#中广播ARP数据包

发布于 2024-09-09 21:05:59 字数 105 浏览 7 评论 0原文

我想通过C#发送ARP数据包。 我不知道如何在 C# 中形成 ARP 数据包(格式)。有人可以帮忙吗? 加上如何发送arp数据包或广播它。

任何示例代码都受到高度赞赏。 提前致谢。

I want to send ARP packet through C#.
I dont know how to form ARP packet(format) in C# . Can anybody help on this ?
Plus how to send arp packet or broadcast it.

Any sample code is highly appreciated.
Thanks in Advance.

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

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

发布评论

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

评论(1

对你而言 2024-09-16 21:05:59

这是您可能感兴趣的库:http://www.beesync。 com/packetx/docs/html/index.html

以及 send.cs 文件中的片段。

// Get adapter hardware address and IP address
  Adapter oAdapter = (Adapter)oPktX.Adapter;
  string sHWAddr = oAdapter.HWAddress;
  string sIPAddr = oAdapter.NetIP;
  string sIPMask = oAdapter.NetMask;
  Console.WriteLine("MAC Addr = " + sHWAddr);
  Console.WriteLine("IP  Addr = " + sIPAddr);

  // Send ARP request for this IP address
  string sIPReso = "11.12.13.14";
  char [] aDelimiter = {'.'};
  string[] aIPReso = sIPReso.Split(aDelimiter, 4);
  string[] aIPAddr = sIPAddr.Split(aDelimiter, 4);

  // Build ARP packet
  Object[] oPacket = new Object[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    Convert.ToByte("0x" + sHWAddr.Substring(0,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(2,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(4,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(6,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(8,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(10,2), 16),
    0x08, 0x06, 0x00, 0x01,
    0x08, 0x00, 0x06, 0x04, 0x00, 0x01,
    Convert.ToByte("0x" + sHWAddr.Substring(0,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(2,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(4,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(6,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(8,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(10,2), 16),
    Convert.ToByte(aIPAddr[0], 10),
    Convert.ToByte(aIPAddr[1], 10),
    Convert.ToByte(aIPAddr[2], 10),
    Convert.ToByte(aIPAddr[3], 10),
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    Convert.ToByte(aIPReso[0], 10),
    Convert.ToByte(aIPReso[1], 10),
    Convert.ToByte(aIPReso[2], 10),
    Convert.ToByte(aIPReso[3], 10),
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

  // Send 100 ARP requests      
  oAdapter.SendPacket(oPacket, 100);

编辑:

最好的方法是使用 WinPCap 库,现在它们正式以 C 语言而不是 C# 语言提供,但如果您已经安装了 winpcap,则可以导入 wpcap.ddl,这是您可能想要查看的其他一些资源:

链接

http://bytes.com/topic/c- Sharp/answers/278941-how-wrap-winpcap

这是一个带有源代码的 GUI 嗅探器:

http://www.codeproject.com/KB/IP/dotnetwinpcap.aspx

Here is a library you might be interested in: http://www.beesync.com/packetx/docs/html/index.html

And a Snippit from the send.cs File.

// Get adapter hardware address and IP address
  Adapter oAdapter = (Adapter)oPktX.Adapter;
  string sHWAddr = oAdapter.HWAddress;
  string sIPAddr = oAdapter.NetIP;
  string sIPMask = oAdapter.NetMask;
  Console.WriteLine("MAC Addr = " + sHWAddr);
  Console.WriteLine("IP  Addr = " + sIPAddr);

  // Send ARP request for this IP address
  string sIPReso = "11.12.13.14";
  char [] aDelimiter = {'.'};
  string[] aIPReso = sIPReso.Split(aDelimiter, 4);
  string[] aIPAddr = sIPAddr.Split(aDelimiter, 4);

  // Build ARP packet
  Object[] oPacket = new Object[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    Convert.ToByte("0x" + sHWAddr.Substring(0,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(2,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(4,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(6,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(8,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(10,2), 16),
    0x08, 0x06, 0x00, 0x01,
    0x08, 0x00, 0x06, 0x04, 0x00, 0x01,
    Convert.ToByte("0x" + sHWAddr.Substring(0,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(2,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(4,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(6,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(8,2), 16),
    Convert.ToByte("0x" + sHWAddr.Substring(10,2), 16),
    Convert.ToByte(aIPAddr[0], 10),
    Convert.ToByte(aIPAddr[1], 10),
    Convert.ToByte(aIPAddr[2], 10),
    Convert.ToByte(aIPAddr[3], 10),
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    Convert.ToByte(aIPReso[0], 10),
    Convert.ToByte(aIPReso[1], 10),
    Convert.ToByte(aIPReso[2], 10),
    Convert.ToByte(aIPReso[3], 10),
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

  // Send 100 ARP requests      
  oAdapter.SendPacket(oPacket, 100);

Edit:

The best way to do this is by using the WinPCap Libraries now they officially come in C and not C# but you can import the wpcap.ddl if you have installed winpcap, Heres some other resources that you may want view:

Link

http://bytes.com/topic/c-sharp/answers/278941-how-wrap-winpcap

And here is a GUI sniffer with source:

http://www.codeproject.com/KB/IP/dotnetwinpcap.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文