3

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

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

4

1 回答 1

3

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

还有来自 send.cs 文件的 Snippit。

// 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://geekswithblogs.net/dotnetnomad/archive/2008/01/31/119140.aspx

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

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

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

于 2010-07-14T10:12:21.007 回答