コード例 #1
0
        public override IEnumerable <AbstractPacket> GetSubPackets(bool includeSelfReference)
        {
            if (includeSelfReference)
            {
                yield return(this);
            }
            if (PacketStartIndex + 40 < PacketEndIndex)
            {
                AbstractPacket packet;
                try {
                    if (this.nextHeader == (byte)IPv4Packet.RFC1700Protocols.TCP)
                    {
                        //TCP packet
                        if (PacketStartIndex + 40 + 20 > PacketEndIndex + 1)
                        {
                            yield break;//too little room for a TCP packet
                        }
                        else
                        {
                            packet = new TcpPacket(this.ParentFrame, PacketStartIndex + 40, PacketEndIndex);//bugg?
                        }
                    }
                    else if (this.nextHeader == (byte)IPv4Packet.RFC1700Protocols.UDP)
                    {
                        //UDP packet
                        if (PacketStartIndex + 40 + 8 > PacketEndIndex + 1)
                        {
                            yield break;//too little room for a UDP packet
                        }
                        else
                        {
                            packet = new UdpPacket(this.ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                        }
                    }
                    else if (this.nextHeader == (byte)IPv4Packet.RFC1700Protocols.SCTP)
                    {
                        //SCTP packet
                        packet = new SctpPacket(this.ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                    }
                    else if (this.nextHeader == (byte)IPv4Packet.RFC1700Protocols.GRE)
                    {
                        //GRE packet
                        packet = new GrePacket(this.ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                    }
                    else
                    {
                        packet = new RawPacket(ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                    }
                }
                catch (Exception) {
                    packet = new RawPacket(ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                }
                yield return(packet);

                foreach (AbstractPacket subPacket in packet.GetSubPackets(false))
                {
                    yield return(subPacket);
                }
            }
        }
コード例 #2
0
ファイル: IPv4Packet.cs プロジェクト: hatnetsec/NetworkMiner
        public override IEnumerable <AbstractPacket> GetSubPackets(bool includeSelfReference)
        {
            if (includeSelfReference)
            {
                yield return(this);
            }
            if (this.fragmentOffset != 0 || this.moreFragmentsFlag)
            {
                if (!this.ParentFrame.QuickParse)
                {
                    byte[] reassembledIpFrameData = null;
                    string fragmentID             = this.GetFragmentIdentifier();
                    lock (PacketHandler.Ipv4Fragments) {
                        List <IPv4Packet> ipPacketList;
                        if (!PacketHandler.Ipv4Fragments.ContainsKey(fragmentID))
                        {
                            ipPacketList = new List <IPv4Packet>();
                            PacketHandler.Ipv4Fragments.Add(fragmentID, ipPacketList);
                        }
                        else
                        {
                            ipPacketList = PacketHandler.Ipv4Fragments[fragmentID];
                        }
                        ipPacketList.Add(this);
                        //see if we have all fragments of a complete IP packet
                        bool allFragmentsHaveMoreFragmentsFlag = true;
                        int  completeIpPacketPayloadLength     = 0;
                        foreach (IPv4Packet p in ipPacketList)
                        {
                            completeIpPacketPayloadLength += p.PayloadLength;
                            if (!p.moreFragmentsFlag)
                            {
                                allFragmentsHaveMoreFragmentsFlag = false;
                            }
                        }
                        if (!allFragmentsHaveMoreFragmentsFlag)
                        {
                            //we might actually have all the fragments!
                            reassembledIpFrameData = new byte[this.HeaderLength + completeIpPacketPayloadLength];
                            if (reassembledIpFrameData.Length > UInt16.MaxValue)
                            {
                                PacketHandler.Ipv4Fragments.Remove(fragmentID);
                                yield break;
                            }

                            foreach (IPv4Packet p in ipPacketList)
                            {
                                if (p.fragmentOffset + this.HeaderLength + p.PayloadLength > reassembledIpFrameData.Length)
                                {
                                    yield break;
                                }
                                Array.Copy(p.ParentFrame.Data, p.PacketStartIndex + p.HeaderLength, reassembledIpFrameData, p.fragmentOffset + this.HeaderLength, p.PayloadLength);
                            }
                            PacketHandler.Ipv4Fragments.Remove(fragmentID);//we don't want to reassemble this IP frame any more
                            //we now need to create a fake frame and run GetSubPackets(false) on it
                        }
                    }//Release lock on PacketHandler.Ipv4Fragments
                    if (reassembledIpFrameData != null && reassembledIpFrameData.Length > this.HeaderLength)
                    {
                        Array.Copy(this.ParentFrame.Data, this.PacketStartIndex, reassembledIpFrameData, 0, this.headerLength);

                        //totalLength = (ushort)reassembledIpFrameData.Length;
                        Utils.ByteConverter.ToByteArray((ushort)reassembledIpFrameData.Length, reassembledIpFrameData, 2);
                        //moreFragmentsFlag = false;
                        //fragmentOffset = 0;
                        reassembledIpFrameData[6] = 0;
                        reassembledIpFrameData[7] = 0;



                        Frame      reassembledFrame    = new Frame(this.ParentFrame.Timestamp, reassembledIpFrameData, ParentFrame.FrameNumber);
                        IPv4Packet reassembledIpPacket = new IPv4Packet(reassembledFrame, 0, reassembledFrame.Data.Length - 1);
                        reassembledIpPacket.fragmentOffset    = 0;
                        reassembledIpPacket.moreFragmentsFlag = false;
                        reassembledIpPacket.totalLength       = (ushort)reassembledIpFrameData.Length;

                        foreach (AbstractPacket subPacket in reassembledIpPacket.GetSubPackets(false))
                        {
                            yield return(subPacket);
                        }
                    }
                }
            }
            else if (PacketStartIndex + headerLength < PacketEndIndex && this.fragmentOffset == 0)
            {
                AbstractPacket packet;
                try {
                    if (this.protocol == (byte)IPv4Packet.RFC1700Protocols.TCP)
                    {
                        //TCP packet
                        if (PacketStartIndex + headerLength + 20 > PacketEndIndex + 1)
                        {
                            yield break;//too little room for a TCP packet
                        }
                        else
                        {
                            packet = new TcpPacket(this.ParentFrame, PacketStartIndex + headerLength, PacketEndIndex);
                        }
                    }
                    else if (this.protocol == (byte)IPv4Packet.RFC1700Protocols.UDP)
                    {
                        //UDP packet
                        if (PacketStartIndex + headerLength + 8 > PacketEndIndex + 1)
                        {
                            yield break;//too little room for a UDP packet
                        }
                        else
                        {
                            packet = new UdpPacket(this.ParentFrame, PacketStartIndex + headerLength, PacketEndIndex);
                        }
                    }
                    else if (this.protocol == (byte)IPv4Packet.RFC1700Protocols.SCTP)
                    {
                        //SCTP packet
                        packet = new SctpPacket(this.ParentFrame, PacketStartIndex + headerLength, PacketEndIndex);
                    }
                    else if (this.protocol == (byte)IPv4Packet.RFC1700Protocols.IPv6)
                    {
                        packet = new IPv6Packet(this.ParentFrame, PacketStartIndex + headerLength, PacketEndIndex);
                    }
                    else if (this.protocol == (byte)IPv4Packet.RFC1700Protocols.GRE)
                    {
                        packet = new GrePacket(this.ParentFrame, PacketStartIndex + headerLength, PacketEndIndex);
                    }
                    else if (this.protocol == (byte)IPv4Packet.RFC1700Protocols.ICMP)
                    {
                        packet = new IcmpPacket(this.ParentFrame, PacketStartIndex + headerLength, PacketEndIndex);
                    }
                    else
                    {
                        packet = new RawPacket(ParentFrame, PacketStartIndex + headerLength, PacketEndIndex);
                    }
                }
                catch (Exception) {
                    packet = new RawPacket(ParentFrame, PacketStartIndex + headerLength, PacketEndIndex);
                }
                yield return(packet);

                foreach (AbstractPacket subPacket in packet.GetSubPackets(false))
                {
                    yield return(subPacket);
                }
            }
        }
コード例 #3
0
ファイル: IPv6Packet.cs プロジェクト: mammo0/networkminer-cli
        public override IEnumerable <AbstractPacket> GetSubPackets(bool includeSelfReference)
        {
            if (includeSelfReference)
            {
                yield return(this);
            }
            if (PacketStartIndex + 40 < PacketEndIndex)
            {
                AbstractPacket packet;
                try {
                    if (this.nextHeader == (byte)IPv4Packet.RFC1700Protocols.TCP)
                    {
                        //TCP packet
                        if (PacketStartIndex + 40 + 20 > PacketEndIndex + 1)
                        {
                            yield break;//too little room for a TCP packet
                        }
                        else
                        {
                            packet = new TcpPacket(this.ParentFrame, PacketStartIndex + 40, PacketEndIndex);//bugg?
                        }
                    }
                    else if (this.nextHeader == (byte)IPv4Packet.RFC1700Protocols.UDP)
                    {
                        //UDP packet
                        if (PacketStartIndex + 40 + 8 > PacketEndIndex + 1)
                        {
                            yield break;//too little room for a UDP packet
                        }
                        else
                        {
                            packet = new UdpPacket(this.ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                        }
                    }
                    else if (this.nextHeader == (byte)IPv4Packet.RFC1700Protocols.SCTP)
                    {
                        //SCTP packet
                        packet = new SctpPacket(this.ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                    }
                    else if (this.nextHeader == (byte)IPv4Packet.RFC1700Protocols.GRE)
                    {
                        //GRE packet
                        packet = new GrePacket(this.ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                    }
                    else
                    {
                        packet = new RawPacket(ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                    }
                }
                catch (Exception e) {
                    SharedUtils.Logger.Log("Error parsing packet in IPv6 payload in " + this.ParentFrame.ToString() + ". " + e.ToString(), SharedUtils.Logger.EventLogEntryType.Warning);
                    packet = new RawPacket(ParentFrame, PacketStartIndex + 40, PacketEndIndex);
                }
                yield return(packet);

                foreach (AbstractPacket subPacket in packet.GetSubPackets(false))
                {
                    yield return(subPacket);
                }
            }
        }