Пример #1
0
        public String deserialize(dnsRequest request)
        {
            string result = "";

            //Append request count
            result += reqCount.ToString("X4");
            //Build and append flags
            int flags = 0;

            flags   = (response << 15) | (opcode << 11) | (AuthAnswer << 10) | (truncation << 9) | (recursion_desired << 8) | (recursion_available << 7) | (reserved << 4) | return_code;
            result += flags.ToString("X4");
            //Append resource record counts
            result += question_resource_record_count.ToString("X4");
            result += answer_resource_record_count.ToString("X4");
            result += authority_resource_record_count.ToString("X4");
            result += additional_resource_record_count.ToString("X4");

            //Deserialize records

            foreach (GeneralRecord gr in request.records)
            {
                if (gr.resource_type == DnsResourceType.Query)
                {
                    string name   = deserializeLabel(gr.rName);
                    string type   = gr.rType.ToString("X4");
                    string rclass = gr.rClass.ToString("X4");
                    result += name;
                    result += "00";
                    result += type;
                    result += rclass;
                }
                if (gr.resource_type == DnsResourceType.Answer)
                {
                    AnswerRecord ar      = (AnswerRecord)gr;
                    string       name    = deserializeLabel(ar.rName);
                    string       type    = ar.rType.ToString("X4");
                    string       rclass  = ar.rClass.ToString("X4");
                    string       ttl     = ar.ttl.ToString("X8");
                    string       aresult = deserializeIP(ar.result, ar.ipv6Hex);
                    string       length  = (aresult.Length / 2).ToString("X4");
                    result += name + "00";
                    result += type;
                    result += rclass;
                    result += ttl;
                    result += length;
                    result += aresult;
                }
                if (gr.resource_type == DnsResourceType.Authority)
                {
                    AuthoritiveRecord ar      = (AuthoritiveRecord)gr;
                    string            name    = deserializeLabel(ar.rName);
                    string            type    = ar.rType.ToString("X4");
                    string            rclass  = ar.rClass.ToString("X4");
                    string            ttl     = ar.ttl.ToString("X8");
                    string            pDnsSrv = deserializeLabel(ar.primaryNS);
                    string            mailbox = deserializeLabel(ar.authorityMailbox);
                    string            serial  = ar.serialNum.ToString("X8");
                    string            refresh = ar.refreshInterval.ToString("X8");
                    string            retry   = ar.retryInterval.ToString("X8");
                    string            expire  = ar.expireLimit.ToString("X8");
                    string            minttl  = ar.minttl.ToString("X8");
                    string            length  = ((pDnsSrv.Length + mailbox.Length + 40) / 2).ToString("X4");
                    result += name + "00";
                    result += type;
                    result += rclass;
                    result += ttl;
                    result += length;
                    result += pDnsSrv + "00";
                    result += mailbox + "00";
                    result += serial;
                    result += refresh;
                    result += retry;
                    result += expire;
                    result += minttl;
                }
                if (gr.resource_type == DnsResourceType.Additional)
                {
                    AdditionalRecord ar = (AdditionalRecord)gr;
                    result += ar.hexDump;
                }
            }

            //Deserialize done :)
            return(result);
        }
Пример #2
0
        private void recvAsync(IAsyncResult ar)
        {
            IPEndPoint currentClient = new IPEndPoint(IPAddress.Any, 0);

            byte[] cMsg = listener.EndReceive(ar, ref currentClient);
            Console.WriteLine("[" + cMsg.Length.ToString() + " bytes] Data Size");
            string plain = Encoding.ASCII.GetString(cMsg);

            Console.WriteLine("Data Payload: \n");
            int    counter  = 0;
            string fullDump = "";

            foreach (int byt in cMsg)
            {
                if (counter > 15)
                {
                    counter = 0;
                    Console.Write("\n");
                }
                string hex = byt.ToString("X");
                if (hex.Length == 1)
                {
                    hex = "0" + hex;
                }
                Console.Write(hex);
                Console.Write(" ");
                fullDump += hex;
                counter++;
            }
            dnsRequest request = new dnsRequest();

            request.serialize(fullDump);
            //Mofify request here
            editor.setRequest(request);
            request = editor.runXML();
            Console.WriteLine("\nDeserialize test\n");
            string payload = request.deserialize(request);

            formatHex(payload);
            byte[] entropy = request.ToArray(payload);
            Client google  = new Client();

            google.start();
            google.write(entropy);
            byte[]     resp     = google.directRead();
            dnsRequest response = new dnsRequest();
            string     hx       = byteToHex(resp);

            response.serialize(hx);
            editor.setRequest(response);
            response = editor.runXML();
            Console.WriteLine(response.ToString());
            string strResponse = response.deserialize(response);

            formatHex(strResponse);
            byte[] rsp = response.ToArray(strResponse);
            write(rsp, currentClient);
            Console.WriteLine("Dns Req - Rsp sequence done");
            if (!clients.Contains(currentClient))
            {
                clients.Add(currentClient);
            }
            listener.BeginReceive(new AsyncCallback(recvAsync), null);
        }
Пример #3
0
 public void setRequest(dnsRequest request)
 {
     req = request;
 }
Пример #4
0
        public dnsRequest runXML()
        {
            dnsRequest copy = req;


            if (authoritiveProtection)
            {
                List <int> auth = new List <int>();
                int        t    = 0;
                foreach (dnsRequest.GeneralRecord gr in copy.records)
                {
                    if (gr.resource_type == dnsRequest.DnsResourceType.Authority)
                    {
                        auth.Add(t);
                    }

                    t++;
                }

                foreach (int i in auth)
                {
                    copy.records.RemoveAt(i);
                }

                authoritiveProtection = false;
            }

            foreach (string item in blackList)
            {
                if (copy.response == (int)dnsRequest.DnsResponse.response)
                {
                    bool removeResp = true;

                    foreach (dnsRequest.GeneralRecord gr in copy.records)
                    {
                        if (gr.rType == (int)dnsRequest.DnsResourceType.Query)
                        {
                            if (gr.rName == item)
                            {
                                removeResp       = true;
                                copy.return_code = (int)dnsRequest.DnsReturnCodes.NonExistentDomain;
                            }
                        }
                    }

                    if (removeResp)
                    {
                        List <dnsRequest.GeneralRecord> lcpy = new List <dnsRequest.GeneralRecord>();
                        foreach (dnsRequest.GeneralRecord r in lcpy)
                        {
                            if (r.resource_type != (int)dnsRequest.DnsResourceType.Query)
                            {
                                copy.records.Remove(r);
                            }
                        }

                        copy.additional_resource_record_count = 0;
                        copy.answer_resource_record_count     = 0;
                        copy.authority_resource_record_count  = 0;
                        break;
                    }
                }
            }

            foreach (string item in redirect)
            {
                if (copy.response == (int)dnsRequest.DnsResponse.response)
                {
                    int        i           = 0;
                    bool       canBreak    = false;
                    List <int> authoritive = new List <int>();

                    foreach (dnsRequest.GeneralRecord gr in copy.records)
                    {
                        if (gr.resource_type == dnsRequest.DnsResourceType.Answer && copy.return_code == (int)dnsRequest.DnsReturnCodes.Success)
                        {
                            if (copy.records[i].rName == item.Split(':')[0])
                            {
                                ((dnsRequest.AnswerRecord)copy.records[i]).result = item.Split(':')[1];
                                canBreak = true;
                            }
                        }

                        i++;
                    }

                    if (canBreak)
                    {
                        authoritiveProtection = true;
                        break;
                    }
                }
            }

            return(copy);
        }
Пример #5
0
 public reqEditor(dnsRequest request)
 {
     req = request;
 }