Beispiel #1
0
        public void ParsePacket(byte[] LLMNRPayloadRaw)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream(LLMNRPayloadRaw))
                {
                    using (BitReader bitReader = new BitReader(ms))
                    {
                        // transaction id
                        this.Id = new byte[] { (byte)bitReader.ReadByte(), (byte)bitReader.ReadByte() };
                        // flags
                        IsResponse = bitReader.ReadBit(); //1bit
                        OpCode = (short)bitReader.ReadLSB(4); //4bits
                        C = bitReader.ReadBit(); //1bit
                        TC = bitReader.ReadBit(); //1bit
                        T = bitReader.ReadBit(); //1bit
                        bitReader.ReadLSB(4); //4bits ZZZZ
                        RCode = (short)bitReader.ReadLSB(4); //4bits

                        // questions
                        QDCOUNT = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                        // answers RRs
                        ANCOUNT = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                        // autority RRs
                        NSCOUNT = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                        // additional RRs
                        ARCOUNT = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));

                        this.Query = new DNSQuery();

                        // query name
                        this.Query.Name = bitReader.ReadStringLSB();
                        // type
                        this.Query.Type = (DNSType?)IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                        //class
                        this.Query.Class = (DNSClass?)IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));

                        for (int i = 0; i < this.ANCOUNT; i++)
                        {
                            DNSAnswer answer = new DNSAnswer();

                            // answer name
                            answer.Name = bitReader.ReadStringLSB();
                            // type answer
                            answer.Type = (DNSType)IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                            // class answer
                            answer.Class = (DNSClass?)IPAddress.NetworkToHostOrder(bitReader.ReadLSB(16));
                            // time to live
                            answer.TTL = bitReader.ReadLSB(32);
                            // longitud RData
                            answer.RDLength = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                            List<byte> arrayList = new List<byte>();
                            for (int index = 0; index < answer.RDLength; index++)
                                arrayList.Add((byte)bitReader.ReadByte());
                            answer.RData = arrayList.ToArray();

                            this.AnswerList.Add(answer);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Beispiel #2
0
        public void ParsePacket(byte[] LLMNRPayloadRaw)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream(LLMNRPayloadRaw))
                {
                    using (BitReader bitReader = new BitReader(ms))
                    {
                        // transaction id
                        this.Id = new byte[] { (byte)bitReader.ReadByte(), (byte)bitReader.ReadByte() };
                        // flags
                        IsResponse = bitReader.ReadBit();         //1bit
                        OpCode     = (short)bitReader.ReadLSB(4); //4bits
                        C          = bitReader.ReadBit();         //1bit
                        TC         = bitReader.ReadBit();         //1bit
                        T          = bitReader.ReadBit();         //1bit
                        bitReader.ReadLSB(4);                     //4bits ZZZZ
                        RCode = (short)bitReader.ReadLSB(4);      //4bits

                        // questions
                        QDCOUNT = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                        // answers RRs
                        ANCOUNT = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                        // autority RRs
                        NSCOUNT = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                        // additional RRs
                        ARCOUNT = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));

                        this.Query = new DNSQuery();

                        // query name
                        this.Query.Name = bitReader.ReadStringLSB();
                        // type
                        this.Query.Type = (DNSType?)IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                        //class
                        this.Query.Class = (DNSClass?)IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));

                        for (int i = 0; i < this.ANCOUNT; i++)
                        {
                            DNSAnswer answer = new DNSAnswer();

                            // answer name
                            answer.Name = bitReader.ReadStringLSB();
                            // type answer
                            answer.Type = (DNSType)IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                            // class answer
                            answer.Class = (DNSClass?)IPAddress.NetworkToHostOrder(bitReader.ReadLSB(16));
                            // time to live
                            answer.TTL = bitReader.ReadLSB(32);
                            // longitud RData
                            answer.RDLength = IPAddress.NetworkToHostOrder((short)bitReader.ReadLSB(16));
                            List <byte> arrayList = new List <byte>();
                            for (int index = 0; index < answer.RDLength; index++)
                            {
                                arrayList.Add((byte)bitReader.ReadByte());
                            }
                            answer.RData = arrayList.ToArray();

                            this.AnswerList.Add(answer);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }