none
請問有人使用 SharpPcap 來產生假的 ICMP 封包並送出嗎?? RRS feed

  • 問題

  • 大家好
    請問板上有大大用過 SharpPcap 來產生 ICMP 的封包(要ping但欄位自填)然後用它的 NetworkDevice 來送出嗎??
    我寫了一段程式想送出, 但是不知為何我用 WireShark 都沒看到封包有送出
    icmp 封包看起來是沒有錯, 但是用 PcapSendPacket 無法傳出
    我的程式碼如下 請大家幫忙看看好嗎??

               // btnSebd 按紐按下後就會送出封包
               private void btnSend_Click(object sender, EventArgs e)
               {
                btnSend.Enabled = false;
                this.Cursor = Cursors.WaitCursor;

                ICMPPacket p = GenPingPacket(tbxDstIP.Text, tbxDstMAC.Text, "192.168.0.30", "00-1A-6B-32-32-C0");

                NetworkDevice NetDev = m_AllNetDevice[cbbNetworkDevices.SelectedIndex] as NetworkDevice;
                NetDev.PcapOpen();
                NetDev.PcapSendPacket(p);
                NetDev.PcapClose();

                this.Cursor = Cursors.Default;
                btnSend.Enabled = true;
            }

            // 這一段是產生 ICMP 封包
            public static ICMPPacket GenPingPacket(string dstIP, string dstMAC, string srcIP, string srcMAC)
            {
                int lLen = 4;
                int ByteLen = 64;
                ICMPPacket icmp = new ICMPPacket(lLen, new byte[ByteLen]);

                icmp.EthernetProtocol = EthernetProtocols_Fields.IP;

                icmp.DestinationHwAddress = dstMAC; //Set the dest MAC of the gateway  
                icmp.SourceHwAddress = srcMAC; //Set the source mac of the local device

                //IP fields
                icmp.DestinationAddress = dstIP; //The IP of the destination host
                icmp.SourceAddress = srcIP; //The IP of the local device

                icmp.IPProtocol = IPProtocols_Fields.ICMP;
                icmp.TimeToLive = 64;
                icmp.Id = 100;
                icmp.Version = 4;
                icmp.IPTotalLength = ByteLen - lLen; //Set the correct IP length
                icmp.IPHeaderLength = IPFields_Fields.IP_HEADER_LEN;
                icmp.ComputeIPChecksum(true);
                icmp.ComputeICMPChecksum();
               
                //ICMP fields
                icmp.MessageCode = 0;
                icmp.MessageType = 8;
                icmp.MessageMajorCode = 8;
                icmp.MessageMinorCode = 0;
                icmp.ComputeICMPChecksum(true);           

                return icmp;
            }

    2008年7月3日 上午 01:38

所有回覆

  •        static void Main(string[] args)
            {
                string ver = Tamir.IPLib.Version.GetVersionString();
                /* Print SharpPcap version */
                Console.WriteLine("SharpPcap {0}, Example6.DumpTCP.cs", ver);
                Console.WriteLine();

                /* Retrieve the device list */
                PcapDeviceList devices = SharpPcap.GetAllDevices();

                /*If no device exists, print error */
                if (devices.Count < 1)
                {
                    Console.WriteLine("No device found on this machine");
                    return;
                }

                Console.WriteLine("The following devices are available on this machine:");
                Console.WriteLine("----------------------------------------------------");
                Console.WriteLine();

                int i = 0;

                /* Scan the list printing every entry */
                foreach (PcapDevice dev in devices)
                {
                    /* Description */
                    Console.WriteLine("{0}) {1}", i, dev.PcapDescription);
                    i++;
                }

                Console.WriteLine();
                Console.Write("-- Please choose a device to capture: ");
                i = int.Parse(Console.ReadLine());

                PcapDevice device = devices[i];


                device.PcapOpen(true, 20);

     

                int lLen = EthernetFields_Fields.ETH_HEADER_LEN;
                byte[] bytes = new byte[74];

     

                Tamir.IPLib.Packets.ICMPPacket p = new ICMPPacket(lLen, bytes);
                //Tamir.IPLib.Packets.IPPacket p = new IPPacket(lLen, bytes);
                p.SourceHwAddress = "00-02-3F-E9-69-3A";
                p.DestinationHwAddress = "00-e0-4c-41-78-d6";

                p.EthernetProtocol = 0x800;    //arp协议为 806  ip协议为  800

     

     

                p.IPVersion = 0x04;
                p.IpHeaderLength = 20;   //数据包里面写得是5;???/
                p.IPTotalLength = 0x3c;


               
                p.FragmentOffset = 0;
                p.TimeToLive = 0x80;
                p.IPProtocol = 0x01;
               
                p.SourceAddress = "192.168.0.225";
                p.DestinationAddress = "192.168.0.182";
                p.ComputeIPChecksum();

                p.MessageType = 8;
                p.MessageCode = 0;


                // nai nai de
                bytes[64] = 0x6e;
                bytes[65] = 0x61;
                bytes[66] = 0x69;

                bytes[68] = 0x6e;
                bytes[69] = 0x61;
                bytes[70] = 0x69;

                bytes[72] = 0x64;
                bytes[73] = 0x65;
                p.ComputeICMPChecksum();
                while (true)
                {
                    Thread.Sleep(10);
                    device.PcapSendPacket(p);
                }
            }

    2009年8月18日 上午 11:01
  • 好多東西都不在ㄝ tamir還要外下載
    2015年3月8日 上午 05:43