def _recursive_query(self, questions, region, tracking_chain, records): unanswers_questions, recursive_questions = [], [] for question in questions: has_reply = False for record in records: if question.qname == record.full_subdomain \ and record.status == 1 \ and question.qclass == record.rclass: if question.qtype == record.rtype: has_reply = True elif record.rtype == QTYPE.CNAME: has_reply = True question = DNSQuestion(qname=record.content, qtype=question.qtype, qclass=record.rclass) if question in tracking_chain: continue tracking_chain.append(question) recursive_questions.append(question) if not has_reply: unanswers_questions.append(question) if len(unanswers_questions) > 0: request = DNSRecord() request.add_question(*unanswers_questions) records = [ Record(full_subdomain=r.rname, rtype=r.rtype, rclass=r.rclass, content=str(r.rdata), ttl=r.ttl, status=1) for r in self.remote.query(request).rr ] self._set_cached_records(records, region) yield from records yield from self._query(recursive_questions, region, tracking_chain)
def smart_route_dns_query(self): xlog.info("Start testing SmartRouter DNS Query") domain = "appsec.hicloud.com" d = DNSRecord(DNSHeader(123)) d.add_question(DNSQuestion(domain, 1)) req4_pack = d.pack() for port in [8053, 53]: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(req4_pack, ("127.0.0.1", port)) sock.settimeout(5) try: response, server = sock.recvfrom(8192) except Exception as e: xlog.warn("recv fail for port:%s e:%r", port, e) continue p = DNSRecord.parse(response) for r in p.rr: ip = utils.to_bytes(str(r.rdata)) xlog.info("IP:%s" % ip) self.assertEqual(utils.check_ip_valid(ip), True) xlog.info("Finished testing SmartRouter DNS Query") return
def gen_packet(self, p_id, qname, rcode, ttl, q_type, rdata): header = DNSHeader(id=p_id, qr=1, ra=1, aa=1, bitmap=rcode) packet = DNSRecord(header) packet.add_question(DNSQuestion(qname)) packet.add_answer( *RR.fromZone("{} {} {} {}".format(qname, ttl, q_type, rdata))) return packet
def query(self, c_id, m_addr): header = DNSHeader(qr=0, aa=1, ra=1) transfer_packet = DNSRecord(header) transfer_packet.add_question(DNSQuestion(c_id)) response = transfer_packet.send(dest=m_addr, port=10053) ans = DNSRecord.parse(response) rcode, rdata = ans.header.rcode, str(ans.a.rdata) return rcode, rdata
def gen_packet(self, p_id, qname, rcode, q_typeid, rdata): header = DNSHeader(id=p_id, qr=1, ra=1, aa=1, bitmap=rcode) packet = DNSRecord(header) packet.add_question(DNSQuestion(qname)) if rcode == 0: #NOERROR packet.add_answer( RR.fromZone("{} {} {} {}".format(name, answer_data.a.ttl, q_typeid, str(rdata)))) return packet else: return packet
def smart_route_dns_query(self): xlog.info("Start testing SmartRouter DNS Query") domain = "appsec.hicloud.com" d = DNSRecord(DNSHeader(123)) d.add_question(DNSQuestion(domain, 1)) req4_pack = d.pack() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(req4_pack, ("127.0.0.1", 53)) sock.sendto(req4_pack, ("127.0.0.1", 8053)) response, server = sock.recvfrom(8192) p = DNSRecord.parse(response) for r in p.rr: ip = utils.to_bytes(str(r.rdata)) xlog.info("IP:%s" % ip) self.assertEqual(utils.check_ip_valid(ip), True) xlog.info("Finished testing SmartRouter DNS Query")
def resolve(self, request, handler): qll = [[] for _ in range(len(self._targets))] a = request.reply() for q in request.questions: for name, jump in self._rules: if q.qname.matchSuffix(name) and isinstance(jump, int): qll[jump].append(q) break if q.qname == name and q.qclass == CLASS.IN: if q.qtype == QTYPE.A and isinstance(jump, IPv4Address): a.add_answer(RR(q.qname, QTYPE.A, CLASS.IN, 1, dns.A(str(jump)))) break if q.qtype == QTYPE.AAAA and isinstance(jump, IPv6Address): a.add_answer(RR(q.qname, QTYPE.AAAA, CLASS.IN, 1, dns.AAAA(str(jump)))) break else: qll[0].append(q) for i, ql in enumerate(qll): if not len(ql): continue ur = DNSRecord(header=request.header) ur.add_question(*ql) a.add_answer(*self._targets[i].resolve(ur, handler).rr) return a
def gen_error(self, p_id, qname, q_type, rcode): header = DNSHeader(id=p_id, qr=1, ra=1, aa=1, bitmap=rcode) packet = DNSRecord(header) packet.add_question(DNSQuestion(qname)) return packet
def query(self, c_id, m_addr): header = DNSHeader(qr=0, aa=1, ra=1) transfer_packet = DNSRecord(header) transfer_packet.add_question(DNSQuestion(c_id)) response = transfer_packet.send(dest=m_addr, port=10053) return response