现在的位置: 首页 > 综合 > 正文

使用 SharpPcap 來產生假的 ICMP 封包並送出

2019年05月10日 ⁄ 综合 ⁄ 共 1868字 ⁄ 字号 评论关闭
大家好
請問板上有大大用過 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;
        }

抱歉!评论已关闭.