Ejemplo n.º 1
0
        public void ExtractData(ref NetworkHost sourceHost, NetworkHost destinationHost, IEnumerable <Packets.AbstractPacket> packetList)
        {
            Packets.DnsPacket  dnsPacket  = null;
            Packets.IPv4Packet ipv4Packet = null;
            Packets.IPv6Packet ipv6Packet = null;
            Packets.UdpPacket  udpPacket  = null;

            foreach (Packets.AbstractPacket p in packetList)
            {
                if (p.GetType() == typeof(Packets.DnsPacket))
                {
                    dnsPacket = (Packets.DnsPacket)p;
                }
                else if (p.GetType() == typeof(Packets.IPv4Packet))
                {
                    ipv4Packet = (Packets.IPv4Packet)p;
                }

                /*else if(p.GetType()==typeof(Packets.IPv6Packet))
                 *  ipv6Packet=(Packets.IPv6Packet)p;*/
                else if (p.GetType() == typeof(Packets.UdpPacket))
                {
                    udpPacket = (Packets.UdpPacket)p;
                }
            }

            if (dnsPacket != null)
            {
                //ExtractDnsData(dnsPacket);
                if (dnsPacket.Flags.Response)
                {
                    System.Collections.Specialized.NameValueCollection cNamePointers = new System.Collections.Specialized.NameValueCollection();
                    if (dnsPacket.AnswerRecords != null && dnsPacket.AnswerRecords.Length > 0)
                    {
                        foreach (Packets.DnsPacket.ResourceRecord r in dnsPacket.AnswerRecords)
                        {
                            if (r.IP != null)
                            {
                                if (!base.MainPacketHandler.NetworkHostList.ContainsIP(r.IP))
                                {
                                    NetworkHost host = new NetworkHost(r.IP);
                                    host.AddHostName(r.DNS);
                                    base.MainPacketHandler.NetworkHostList.Add(host);
                                    MainPacketHandler.OnNetworkHostDetected(new Events.NetworkHostEventArgs(host));
                                    //base.MainPacketHandler.ParentForm.ShowDetectedHost(host);
                                }
                                else
                                {
                                    base.MainPacketHandler.NetworkHostList.GetNetworkHost(r.IP).AddHostName(r.DNS);
                                }
                                if (cNamePointers[r.DNS] != null)
                                {
                                    base.MainPacketHandler.NetworkHostList.GetNetworkHost(r.IP).AddHostName(cNamePointers[r.DNS]);
                                }
                            }
                            else if (r.Type == (ushort)Packets.DnsPacket.RRTypes.CNAME)
                            {
                                cNamePointers.Add(r.PrimaryName, r.DNS);
                            }

                            if (ipv4Packet != null)
                            {
                                MainPacketHandler.OnDnsRecordDetected(new Events.DnsRecordEventArgs(r, sourceHost, destinationHost, ipv4Packet, udpPacket));
                                //base.MainPacketHandler.ParentForm.ShowDnsRecord(r, sourceHost, destinationHost, ipPakcet, udpPacket);
                            }
                        }
                    }
                    else
                    {
                        //display the flags instead
                        //TODO : MainPacketHandler.OnDnsRecordDetected(new Events.DnsRecordEventArgs(
                        if (ipv4Packet != null && dnsPacket.QueriedDnsName != null && dnsPacket.QueriedDnsName.Length > 0)
                        {
                            MainPacketHandler.OnDnsRecordDetected(new Events.DnsRecordEventArgs(new Packets.DnsPacket.ResponseWithErrorCode(dnsPacket), sourceHost, destinationHost, ipv4Packet, udpPacket));
                        }
                    }
                }
                else  //DNS request
                {
                    if (dnsPacket.QueriedDnsName != null && dnsPacket.QueriedDnsName.Length > 0)
                    {
                        sourceHost.AddQueriedDnsName(dnsPacket.QueriedDnsName);
                    }
                }
            }
        }
        public static bool TryGetPacket(out Packets.AbstractPacket packet, System.Type packetType, Frame parentFrame, int startIndex, int endIndex)
        {
            packet = null;
            try {
                if (packetType.Equals(typeof(Packets.Ethernet2Packet)))
                {
                    packet = new Packets.Ethernet2Packet(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.IPv4Packet)))
                {
                    IPv4Packet.TryParse(parentFrame, startIndex, endIndex, out packet);
                    //packet = new Packets.IPv4Packet(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.IPv6Packet)))
                {
                    packet = new Packets.IPv6Packet(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.TcpPacket)))
                {
                    packet = new Packets.TcpPacket(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.IEEE_802_11Packet)))
                {
                    packet = new Packets.IEEE_802_11Packet(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.IEEE_802_11RadiotapPacket)))
                {
                    packet = new Packets.IEEE_802_11RadiotapPacket(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.CiscoHdlcPacket)))
                {
                    packet = new Packets.CiscoHdlcPacket(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.LinuxCookedCapture)))
                {
                    packet = new Packets.LinuxCookedCapture(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.PrismCaptureHeaderPacket)))
                {
                    packet = new Packets.PrismCaptureHeaderPacket(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.PpiPacket)))
                {
                    packet = new Packets.PpiPacket(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.PointToPointPacket)))
                {
                    packet = new Packets.PointToPointPacket(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.NullLoopbackPacket)))
                {
                    packet = new Packets.NullLoopbackPacket(parentFrame, startIndex, endIndex);
                }
                else if (packetType.Equals(typeof(Packets.ErfFrame)))
                {
                    packet = new Packets.ErfFrame(parentFrame, startIndex, endIndex);
                }

                if (packet == null)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception) {
                packet = new Packets.RawPacket(parentFrame, startIndex, endIndex);
                return(false);
            }
        }