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

大家好
請問板上有大大用過 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;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值