def resolve(self, request, handler): idna = request.q.qname.idna() self.prune_stale() reply = request.reply() reply.header.rcode = RCODE.NXDOMAIN if request.q.qtype == QTYPE.A: try: record = self.update_record(idna) except models.NoRecordsFoundError: record = self.add_record(idna) if record: addr = ipaddress.ip_address(record.address) reply.add_answer( RR(idna, rclass=CLASS.IN, rtype=QTYPE.A, rdata=A(str(addr)))) else: reply.header.rcode = RCODE.SERVFAIL elif request.q.qtype == QTYPE.PTR: addr = self.get_addr_from_reverse_pointer(idna) for rec in self.get_name_by_addr(addr): reply.add_answer( RR(idna, rclass=CLASS.IN, rtype=QTYPE.PTR, rdata=PTR(rec))) if reply.rr: reply.header.rcode = RCODE.NOERROR return reply
def resolve(self,request,handler): print(request) reply=request.reply() reply.add_answer(*RR.fromZone("abc.def. 6000 A 1.2.3.4")) reply.add_answer(*RR.fromZone("blabla 6000 TXT 1.2.3.4")) return reply
def parseAnswers(self,a,auth,ar,dns): sect_map = {'a':'add_answer','auth':'add_auth','ar':'add_ar'} for sect in 'a','auth','ar': f = getattr(dns,sect_map[sect]) for rr in locals()[sect]: rname,ttl,rclass,rtype = rr[:4] rdata = rr[4:] rd = RDMAP.get(rtype,RD) try: if rd == RD and \ any([ x not in string.hexdigits for x in rdata[-1]]): # Only support hex encoded data for fallback RD pass else: f(RR(rname=rname, ttl=int(ttl), rtype=getattr(QTYPE,rtype), rclass=getattr(CLASS,rclass), rdata=rd.fromZone(rdata))) except DNSError as e: if self.debug: print("DNSError:",e,rr) else: # Skip records we dont understand pass
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 local_resolve(self, qname, qtype): now = datetime.utcnow() key = (qname, qtype) ip = None resp_ttl = self.ttl if key in self.cache: ttl_diff = (now - self.cache[key][KEY_TIME]).total_seconds() if ttl_diff <= self.ttl: ip = self.cache[key][KEY_VALUE] resp_ttl = int(self.ttl - ttl_diff) if not ip: try: aname = DNSLabel(str(qname)) if self.mappings: for fr, to in self.mappings.items(): if aname.matchSuffix(fr): aname = DNSLabel(str(aname).replace(fr, to)) log.debug(f'mapping {qname} to {aname}') break ip = self.ask_api_server(aname, qtype) ipaddress.ip_address(ip) self.cache[key] = { KEY_TIME: now, KEY_VALUE: ip } except ValueError: return None return RR.fromZone(f'{qname} {resp_ttl} {qtype} {ip}')
def resolve(self, request, handler): reply = request.reply() if request.questions[0].qname == QUESTION_ADDRESS: fake_rr = RR(rname=QUESTION_ADDRESS, ttl=5, rdata=A(HardcodedResolver.ANSWER)) reply.add_answer(fake_rr) else: reply.header.rcode = dnslib.RCODE.REFUSED return reply
def sendmsg(m_type, message, request, agent): logging.debug("Sending response message") update_last_ping(agent) reply = request.reply() qname = request.q.qname l_msg = s_msg - 1 logging.debug("message: %s " % message) logging.debug("msg len: %i " % len(message)) reply.add_answer( RR(qname, QTYPE.TXT, ttl=1, rdata=TXT(cryptmsg(message, Agents[agent][0])))) reply.header.rcode = RCODE.NOERROR return (reply)
def resolve(self, request, handler): qname = str(request.q.qname) reply = request.reply() if request.q.qtype != QTYPE.TXT: return reply if qname.endswith(f"{TRANSFER_HOSTNAME}."): encoded_filepath, encoded_chunk = qname.replace( f"{TRANSFER_HOSTNAME}.", "").split(".") decoded_filepath = base58.b58decode(encoded_filepath) print(f"received chunk for file: {decoded_filepath}") output_filepath = os.path.join(OUTPUT_DIRECTORY, encoded_filepath) with open(output_filepath, "ab") as f: decoded_chunk = base58.b58decode(encoded_chunk) f.write(decoded_chunk) reply.add_answer(*RR.fromZone(f"{qname} 1 TXT OK")) return reply
def query(self, request, origin=None): tracking_chain = [] # Prevent infinite recursion region = self._get_or_set_cached_region(origin) records, checker_records = [], [] for index, record in enumerate( self._query(request.questions, region, tracking_chain)): if index > settings.DNSKEY_MAXIMUM_QUERY_DEPTH: break if record.subdomain: checker_records.append(record) if record.status == 1: rr = RR.fromZone("{rr} {ttl} {rclass} {rtype} {rdata}".format( rr=record.full_subdomain, ttl=record.ttl, rclass=CLASS.get(record.rclass), rtype=QTYPE.get(record.rtype), rdata=record.content)) records.append(record) request.add_answer(*rr) if len(checker_records) > 0: query_records.send(sender=LocalQueryProxy, records=checker_records) return request
def _resolve_mx(self, request, handler): reply = request.reply() qname = request.q.qname # If we are luck, load the anwer from cache rr = self._answer_from_cache(qname) if rr: a = copy.copy(rr) reply.add_answer(a) return reply # Forward the request to upstream reply = self._resolve_other(request, handler) if not self._is_noerror(reply): return reply a = reply.get_a() (pref, host) = self._parse_rdata(a.rdata) if not self._is_ipaddress(host): reply2 = self._resolve_record(host, qtype='A') # if we cannot reply MX host to IP address, do nothing if not self._is_noerror(reply2): return reply (pref2, host) = self._parse_rdata(reply2.get_a().rdata) loc = self._location_from_client(host) gw = self.DEFAULT_OVERSEA_OUTBOUND_GATEWAY if loc == 'CN': gw = self.CN_OVERSEA_OUTBOUND_GATEWAY reply = request.reply() zone = "%s\t%s\t%s\t%s\t%s\t%s" % \ (qname, a.ttl, CLASS[a.rclass], QTYPE[a.rtype], pref,gw) rr = RR.fromZone(zone)[0] reply.add_answer(rr) # cache the reply self._answer_to_cache(qname, reply.get_a()) return reply
def resolve(self, request, handler): domain_query = request.get_q().get_qname() ip_address = dummy_sql_query(domain_query) reply = request.reply() reply.add_answer(*RR.fromZone(f"{domain_query} 60 A {ip_address}")) return reply
def resolve(self, request, handler): reply = request.reply() reply.add_answer(*RR.fromZone(self.zone)) return reply