Esempio n. 1
0
        public void SendMLDQuery()
        {
            EthernetPacket ePacket  = new EthernetPacket(PhysicalAddress.Parse("98-5F-D3-3F-6D-B1"), PhysicalAddress.Parse("33-33-00-00-00-01"), EthernetPacketType.IpV6);
            IPv6Packet     ipPacket = new IPv6Packet(IPAddress.Parse("fe80::d0d6:8c11:a087:3941"), IPAddress.Parse("ff02::1"));

            ePacket.PayloadPacket = ipPacket;
            ICMPv6Packet icmp = new ICMPv6Packet(new PacketDotNet.Utils.ByteArraySegment(new byte[24]));

            Array.Copy(IPAddress.Parse("ff02::1").GetAddressBytes(), 0, icmp.Header, 8, 16);
            icmp.Type              = ICMPv6Types.MulticastLostenerQuery;
            icmp.Code              = 0;
            icmp.Checksum          = 0;
            ipPacket.PayloadPacket = icmp;
            icmp.UpdateCalculatedValues();

            _ncard.SendPacket(ePacket);
        }
Esempio n. 2
0
        private void Ipv6ToIpv4(EthernetPacket orgPacket)
        {
            try
            {
                IPv6Packet ipv6 = (IPv6Packet)orgPacket.PayloadPacket;

                if (ipv6.PayloadPacket is PacketDotNet.ICMPv6Packet)
                {
                    ICMPv6Packet icmp = ipv6.PayloadPacket as ICMPv6Packet;
                    icmp.Type = ICMPv6Types.EchoReply;

                    icmp.UpdateCalculatedValues();
                    icmp.Checksum = (ushort)ChecksumUtils.OnesComplementSum(icmp.Bytes);

                    IPv6Packet ipv6Ack = new IPv6Packet(ipv6.DestinationAddress, ipv6.SourceAddress);
                    ipv6Ack.PayloadPacket = icmp;
                    EthernetPacket eth = new EthernetPacket(orgPacket.DestinationHwAddress, orgPacket.SourceHwAddress, EthernetPacketType.IpV6);
                    eth.PayloadPacket = ipv6Ack;

                    Program.CurrentProject.data.SendPacket(eth);
                }
                else if (ipv6.PayloadPacket is PacketDotNet.TcpPacket)
                {
                    TcpPacket tcp = ipv6.PayloadPacket as TcpPacket;

                    ushort          destPort      = tcp.SourcePort;
                    ushort          sourcePort    = tcp.DestinationPort;
                    IPAddress       destAddress   = ipv6.SourceAddress;
                    IPAddress       sourceAddress = ipv6.DestinationAddress;
                    PhysicalAddress sourceMac     = device.MacAddress;
                    PhysicalAddress destMac       = orgPacket.SourceHwAddress;

                    tcp.SourcePort      = sourcePort;
                    tcp.DestinationPort = destPort;

                    if (tcp.Syn && !tcp.Ack && !tcp.Rst && tcp.PayloadData.Length == 0)
                    {
                        if (sourcePort == 80)
                        {
                            tcp.Ack = true;
                            tcp.AcknowledgmentNumber = tcp.SequenceNumber + 1;
                            tcp.SequenceNumber       = tcp.SequenceNumber + 38;
                            tcp.WindowSize           = 14600;

                            IPv6Packet ipv6Ack = new IPv6Packet(sourceAddress, destAddress);
                            ipv6Ack.PayloadPacket = tcp;

                            EthernetPacket eth = new EthernetPacket(sourceMac, destMac, EthernetPacketType.IpV6);
                            eth.PayloadPacket = ipv6Ack;

                            (eth.PayloadPacket.PayloadPacket as TcpPacket).UpdateTCPChecksum();
                            Program.CurrentProject.data.SendPacket(eth);
                        }
                        else if (sourcePort == 443)
                        {
                            tcp.Syn = false;
                            tcp.Ack = true;
                            tcp.Rst = true;
                            tcp.Psh = false;
                            uint ackOrg = tcp.AcknowledgmentNumber;
                            tcp.AcknowledgmentNumber = tcp.SequenceNumber + 1;
                            tcp.SequenceNumber       = 0;
                            tcp.WindowSize           = 0;
                            tcp.PayloadData          = new byte[0];

                            IPv6Packet ipv6Ack = new IPv6Packet(sourceAddress, destAddress);
                            ipv6Ack.PayloadPacket = tcp;
                            EthernetPacket eth = new EthernetPacket(sourceMac, destMac, EthernetPacketType.IpV6);
                            eth.PayloadPacket = ipv6Ack;
                            (eth.PayloadPacket.PayloadPacket as TcpPacket).UpdateTCPChecksum();
                            Program.CurrentProject.data.SendPacket(eth);
                        }
                    }
                    else if (!tcp.Syn && tcp.Ack && tcp.PayloadData != null && !tcp.Rst && tcp.PayloadData.Length > 0 && sourcePort == 80)
                    {
                        string element = string.Format("{0}/{1}", tcp.SequenceNumber, tcp.AcknowledgmentNumber);
                        if (!Data.Data.SlaacReqList.Contains(element))
                        {
                            Data.Data.SlaacReqList.Add(element);
                            HttpPacket httpReq = new HttpPacket(tcp.PayloadData);
                            if (httpReq.IsCompleted)
                            {
                                SendHttpResponse(httpReq, orgPacket, tcp.SourcePort, tcp.DestinationPort, tcp.AcknowledgmentNumber, tcp.SequenceNumber + (uint)tcp.PayloadData.Length);
                            }
                            else
                            {
                                lock (htppLock)
                                {
                                    //incomplete packet

                                    TcpReconstructorPacket previousPacket = (from p in Program.CurrentProject.data.ReconstructedPackets
                                                                             where p.ExpectedSequenceNumber == tcp.SequenceNumber
                                                                             select p).FirstOrDefault();


                                    TcpReconstructorPacket nextPacket = (from p in Program.CurrentProject.data.ReconstructedPackets
                                                                         where p.FirstSequenceNumber == tcp.SequenceNumber + tcp.PayloadData.Length
                                                                         select p).FirstOrDefault();

                                    if (nextPacket != null || previousPacket != null)
                                    {
                                        bool packetSend = false;
                                        if (nextPacket != null)
                                        {
                                            nextPacket.InsertPreviousTcpPacket(tcp);
                                            httpReq = new HttpPacket(nextPacket.Data);
                                            if (httpReq.IsCompleted)
                                            {
                                                packetSend = true;
                                                Program.CurrentProject.data.ReconstructedPackets.Remove(nextPacket);
                                                SendHttpResponse(httpReq, orgPacket, tcp.SourcePort, tcp.DestinationPort, tcp.AcknowledgmentNumber, nextPacket.ExpectedSequenceNumber);
                                            }
                                        }
                                        if (previousPacket != null && !packetSend)
                                        {
                                            if (nextPacket != null)
                                            {
                                                previousPacket.AppendTcpPacket(nextPacket);
                                                Program.CurrentProject.data.ReconstructedPackets.Remove(nextPacket);
                                            }
                                            else
                                            {
                                                previousPacket.AppendTcpPacket(tcp);
                                            }

                                            httpReq = new HttpPacket(previousPacket.Data);
                                            if (httpReq.IsCompleted)
                                            {
                                                Program.CurrentProject.data.ReconstructedPackets.Remove(previousPacket);
                                                SendHttpResponse(httpReq, orgPacket, tcp.SourcePort, tcp.DestinationPort, tcp.AcknowledgmentNumber, previousPacket.ExpectedSequenceNumber);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        Program.CurrentProject.data.ReconstructedPackets.Add(new TcpReconstructorPacket(tcp));
                                    }


                                    var toDelete = Program.CurrentProject.data.ReconstructedPackets.Where(p => DateTime.Now.Subtract(p.CreationTime).TotalMinutes > 2);
                                    foreach (var item in toDelete)
                                    {
                                        Program.CurrentProject.data.ReconstructedPackets.Remove(item);
                                    }
                                }
                            }
                        }
                    }
                    else if (tcp.Fin && tcp.Ack && !tcp.Rst && tcp.PayloadData.Length == 0)
                    {
                        tcp.Ack = true;
                        tcp.Fin = false;
                        uint ackOrg = tcp.AcknowledgmentNumber;
                        tcp.AcknowledgmentNumber = tcp.SequenceNumber + 1;
                        tcp.SequenceNumber       = ackOrg;
                        tcp.WindowSize           = 1400;

                        IPv6Packet ipv6Ack = new IPv6Packet(sourceAddress, destAddress);
                        ipv6Ack.PayloadPacket = tcp;
                        EthernetPacket eth = new EthernetPacket(sourceMac, destMac, EthernetPacketType.IpV6);
                        eth.PayloadPacket = ipv6Ack;
                        (eth.PayloadPacket.PayloadPacket as TcpPacket).UpdateTCPChecksum();
                        Program.CurrentProject.data.SendPacket(eth);
                    }
                }
            }
            catch
            {
            }
        }