【问题标题】:Using pcapdotnet to send raw ethernet packet使用 pcapdotnet 发送原始以太网数据包
【发布时间】:2011-12-02 06:34:53
【问题描述】:

我正在使用 pcapdotnet 并想发送一个原始以太网数据包。 我正在使用此处找到的示例:http://pcapdotnet.codeplex.com/wikipage?title=Pcap.Net%20Tutorial%20-%20Sending%20Packets

我想知道两件事:

  1. 为了修改它以发送 mac 级别数据包,我需要离开 PacketBuilder 构造函数中只有 ethernetLayer?
  2. 如何将要在以太网数据包中发送的原始位/字节数据加载到数据包中?

谢谢!

【问题讨论】:

    标签: c# pcap.net


    【解决方案1】:

    我相信你需要使用 2 层:

    1. EthernetLayer(用于以太网标头)。
    2. PayloadLayer(用于原始字节数据 - 以太网负载)。

    【讨论】:

      【解决方案2】:
      class Program
      {
          static void Main(string[] args)
          {
              // Retrieve the device list from the local machine
              IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
      
              if (allDevices.Count == 0)
              {
                  Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
                  return;
              }
      
              // Print the list
              for (int i = 0; i != allDevices.Count; ++i)
              {
                  LivePacketDevice device = allDevices[i];
                  Console.Write((i + 1) + ". " + device.Name);
                  if (device.Description != null)
                      Console.WriteLine(" (" + device.Description + ")");
                  else
                      Console.WriteLine(" (No description available)");
              }
      
              int deviceIndex = 0;
              do
              {
                  Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
                  string deviceIndexString = Console.ReadLine();
                  if (!int.TryParse(deviceIndexString, out deviceIndex) ||
                      deviceIndex < 1 || deviceIndex > allDevices.Count)
                  {
                      deviceIndex = 0;
                  }
              } while (deviceIndex == 0);
      
              // Take the selected adapter
              PacketDevice selectedDevice = allDevices[deviceIndex - 1];
      
              // Open the output device
              using (PacketCommunicator communicator = selectedDevice.Open(100, // name of the device
                                                                           PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                                                                           1000)) // read timeout
              {
                  // Supposing to be on ethernet, set mac source to 01:01:01:01:01:01
                  MacAddress source = new MacAddress("01:01:01:01:01:01");
      
                  // set mac destination to 02:02:02:02:02:02
                  MacAddress destination = new MacAddress("02:02:02:02:02:02");
      
                  // Create the packets layers
      
                  // Ethernet Layer
                  EthernetLayer ethernetLayer = new EthernetLayer
                  {
                      Source = source,
                      Destination = destination
                  };
      
                  // IPv4 Layer
                  IpV4Layer ipV4Layer = new IpV4Layer
                  {
                      Source = new IpV4Address("1.2.3.4"),
                      Ttl = 128,
                      // The rest of the important parameters will be set for each packet
                  };
      
                  // ICMP Layer
                  IcmpEchoLayer icmpLayer = new IcmpEchoLayer();
      
                  // Create the builder that will build our packets
                  PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer);
      
                  // Send 100 Pings to different destination with different parameters
                  for (int i = 0; i != 1; ++i)
                  {
                      // Set IPv4 parameters
                      ipV4Layer.CurrentDestination = new IpV4Address("2.3.4.1" );
                      ipV4Layer.Identification = (ushort)i;
      
                      // Set ICMP parameters
                      icmpLayer.SequenceNumber = (ushort)i;
                      icmpLayer.Identifier = (ushort)i;
                      // Build the packet
                      Packet packet = builder.Build(DateTime.Now);
      
                      // Send down the packet
                     communicator.SendPacket(packet);
                  }
      
              }
              Console.ReadLine();
          }
      

      【讨论】:

        猜你喜欢
        • 2011-04-27
        • 2010-12-24
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-10
        • 2012-10-10
        • 2011-08-04
        相关资源
        最近更新 更多