def get_reply_record(self, data): request = dnslib.DNSRecord.parse(data) qname = str(request.q.qname).lower() qtype = request.q.qtype dnsservers = self.dns_servers if qname.endswith('.in-addr.arpa'): ipaddr = '.'.join(reversed(qname[:-13].split('.'))) record = dnslib.DNSRecord(header=dnslib.DNSHeader(id=request.header.id, qr=1,aa=1,ra=1), a=dnslib.RR(qname, rdata=dnslib.A(ipaddr))) return record if 'USERDNSDOMAIN' in os.environ: user_dnsdomain = '.' + os.environ['USERDNSDOMAIN'].lower() if qname.endswith(user_dnsdomain): qname = qname[:-len(user_dnsdomain)] if '.' not in qname: if not self.dns_intranet_servers: logging.warning('qname=%r is a plain hostname, need intranet dns server!!!', qname) return dnslib.DNSRecord(header=dnslib.DNSHeader(id=request.header.id, rcode=3)) qname += user_dnsdomain dnsservers = self.dns_intranet_servers try: return self.dns_cache.get((qname, qtype)) except KeyError: pass try: dns_resolve = dnslib_resolve_over_tcp if qname.endswith(self.dns_tcpover) else dnslib_resolve_over_udp kwargs = {'blacklist': self.dns_blacklist, 'turstservers': self.dns_trust_servers} record = dns_resolve(request, dnsservers, self.dns_timeout, **kwargs) ttl = max(x.ttl for x in record.rr) if record.rr else 600 self.dns_cache.set((qname, qtype), record, ttl * 2) return record except socket.gaierror as e: logging.warning('resolve %r failed: %r', qname, e) return dnslib.DNSRecord(header=dnslib.DNSHeader(id=request.header.id, rcode=3))
def _record(self, domain, qtype): if isinstance(qtype, str): request = dnslib.DNSRecord.question(domain, qtype=qtype) else: request = dnslib.DNSRecord(q=dnslib.DNSQuestion(domain, qtype)) while request.header.id in self.event_dict: if isinstance(qtype, str): request = dnslib.DNSRecord.question(domain, qtype=qtype) else: request = dnslib.DNSRecord(q=dnslib.DNSQuestion(domain, qtype)) data = request.pack() self.sock.sendto(data, self.dnsserver) flag = 1 result = None while 1: try: flag = self.event_dict[request.header.id].wait(self.timeout) assert isinstance(flag, dnslib.DNSRecord) except Exception as e: if not isinstance(e, AssertionError): traceback.print_exc(file=sys.stderr) sys.stderr.flush() del self.event_dict[request.header.id] if not result: raise IOError(0, 'reliable udp resolve failed! %s' % domain) return result else: result = flag
def prepare_reply(answer, request): #pack anwsers qname = request.q.qname qtype = request.q.qtype qt = dnslib.QTYPE[qtype] rcode = 0 reply = dnslib.DNSRecord(\ dnslib.DNSHeader(id=request.header.id, qr=1, aa=1, ra=1),\ q=request.q) bad_reply = dnslib.DNSRecord(dnslib.DNSHeader(\ id=request.header.id, qr=1, aa=1, ra=1,\ rcode=rcode), q=request.q) record_class = getattr(dnslib, str(qt)) empty_ans = True if rcode == 0: rcode = answer[1] for a in answer[0]: empty_ans = False reply.add_answer(dnslib.RR(rname=qname, rtype=qtype,\ rclass=1, ttl=10, rdata=record_class(a))) #print "---- Reply:\n", reply # if failed, send back error code if empty_ans and rcode > 0: reply = bad_reply return reply.pack()
def dnslib_resolve_over_udp(query, dnsservers, timeout, **kwargs): """ http://gfwrev.blogspot.com/2009/11/gfwdns.html http://zh.wikipedia.org/wiki/%E5%9F%9F%E5%90%8D%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%BC%93%E5%AD%98%E6%B1%A1%E6%9F%93 http://support.microsoft.com/kb/241352 """ if not isinstance(query, (basestring, dnslib.DNSRecord)): raise TypeError('query argument requires string/DNSRecord') blacklist = kwargs.get('blacklist', ()) turstservers = kwargs.get('turstservers', ()) dns_v4_servers = [x for x in dnsservers if ':' not in x] dns_v6_servers = [x for x in dnsservers if ':' in x] sock_v4 = sock_v6 = None socks = [] if dns_v4_servers: sock_v4 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) socks.append(sock_v4) if dns_v6_servers: sock_v6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) socks.append(sock_v6) timeout_at = time.time() + timeout try: for _ in xrange(4): try: for dnsserver in dns_v4_servers: if isinstance(query, basestring): query = dnslib.DNSRecord(q=dnslib.DNSQuestion(query)) query_data = query.pack() sock_v4.sendto(query_data, parse_hostport(dnsserver, 53)) for dnsserver in dns_v6_servers: if isinstance(query, basestring): query = dnslib.DNSRecord(q=dnslib.DNSQuestion(query, qtype=dnslib.QTYPE.AAAA)) query_data = query.pack() sock_v6.sendto(query_data, parse_hostport(dnsserver, 53)) while time.time() < timeout_at: ins, _, _ = select.select(socks, [], [], 0.1) for sock in ins: reply_data, reply_address = sock.recvfrom(512) reply_server = reply_address[0] record = dnslib.DNSRecord.parse(reply_data) iplist = [str(x.rdata) for x in record.rr if x.rtype in (1, 28, 255)] if any(x in blacklist for x in iplist): logging.warning('query=%r dnsservers=%r record bad iplist=%r', query, dnsservers, iplist) elif record.header.rcode and not iplist and reply_server in turstservers: logging.info('query=%r trust reply_server=%r record rcode=%s', query, reply_server, record.header.rcode) return record elif iplist: logging.debug('query=%r reply_server=%r record iplist=%s', query, reply_server, iplist) return record else: logging.debug('query=%r reply_server=%r record null iplist=%s', query, reply_server, iplist) continue except socket.error as e: logging.warning('handle dns query=%s socket: %r', query, e) raise socket.gaierror(11004, 'getaddrinfo %r from %r failed' % (query, dnsservers)) finally: for sock in socks: sock.close()
def put_in_cache(self, query): self.cache.update( {str(query.q.qname) + str(query.q.qtype): (time.time()+query.a.ttl ,dnslib.DNSRecord(rr=query.rr))}) if len(query.auth) != 0: self.cache.update({str(query.auth[0].rname) + str(query.auth[0].rtype): (time.time() + query.auth[0].ttl,dnslib.DNSRecord(rr=query.auth))}) if len(query.ar) != 0: for ar in query.ar: nil = list() nil.append(ar) self.cache.update( {str(ar.rname) + str(ar.rtype): (time.time()+ar.ttl, dnslib.DNSRecord(rr=nil))})
def resolve(self, request, handler): qname = request.q.qname qtype = request.q.qtype try: answer = self.answer(qname, qtype) # We answered the question without an error, so we are exfiltrating successfully # When this happens, we want to proxy back the real domain name to get back real data # with our fake response inconspicuously at the end. real_domain_name = '.'.join(str(qname).split('.')[-3:]) if self.use_upstream_cache: try: real_records = self.upstream_cache[(real_domain_name, qtype)] except KeyError: real_request = dnslib.DNSRecord() real_request.add_question( dnslib.DNSQuestion(real_domain_name, qtype)) real_reply = self.interceptor.resolve( real_request, handler) self.upstream_cache[(real_domain_name, qtype)] = real_reply.rr real_records = real_reply.rr else: real_reply = self.interceptor.resolve(real_request, handler) real_records = real_reply.rr return_reply = request.reply() for record in real_records: return_reply.add_answer(record) return_reply.add_answer(answer) except (RecordTypeNotDefined, CannotExfiltrateError): # if we are here, we did not exfiltrate data. # Lets assume it's a real domain and just return a valid response return_reply = self.interceptor.resolve(request, handler) return return_reply
def command(self, command): record = dnslib.DNSRecord() record.add_question(dnslib.DNSQuestion(self.domain, dnslib.QTYPE.A)) for condition, match in self.context['header_conditions'].items(): setattr(record.header, condition, match) record.header.id = self.context['command_map'][command] record.send(self.connect)
def _udp_dns_records(host, qtype, server): if isinstance(qtype, str): query = dnslib.DNSRecord.question(host, qtype=qtype) else: query = dnslib.DNSRecord(q=dnslib.DNSQuestion(host, qtype)) query_data = query.pack() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(query_data, server) record_list = [] try: (ins, _, _) = select.select([sock], [], [], 2) if ins: reply_data, reply_address = sock.recvfrom(8192) record_list.append(dnslib.DNSRecord.parse(reply_data)) finally: while 1: try: (ins, _, _) = select.select([sock], [], [], 2) if not ins: break reply_data, reply_address = sock.recvfrom(8192) record_list.append(dnslib.DNSRecord.parse(reply_data)) except: break return record_list
async def service_client(self, reader: aio.StreamReader, writer: aio.StreamWriter) -> None: try: while True: # Parse DNS query packet into a request prefix = await reader.readexactly(2) query = await reader.readexactly(struct.unpack('!H', prefix)[0]) request = dns.DNSRecord.parse(query) response = await self._resolver.resolve(request) # Pack DNS response into answer answer = response.pack() writer.write(struct.pack('!H', len(answer)) + answer) await writer.drain() # Connection likely closed or reset by client except aio.IncompleteReadError: pass # Failed to parse DNS query except dns.DNSError: writer.write(dns.DNSRecord(dns.DNSHeader(rcode=getattr(dns.RCODE, 'FORMERR'))).pack()) await writer.drain() # Cleanly close client connection finally: if not writer.is_closing(): writer.close() await writer.wait_closed()
def get_chunk(self, chunk_no, chunk_size, filename, tries=3): query_string = 'c{cn}.s{cs}.{fn}.{d}'.format(cn=chunk_no, cs=chunk_size, fn=filename, d=self.domain) record = dnslib.DNSRecord() record.add_question(dnslib.DNSQuestion(query_string, dnslib.QTYPE.MX)) reply = dnslib.DNSRecord.parse(record.send(self.connect)) def retry(): time.sleep(0.5) if tries > 0: return self.get_chunk(chunk_no, chunk_size, filename, tries - 1) else: raise CannotReadChunk if reply.header.rcode != dnslib.RCODE.NOERROR: # Server reported an error in the response. return retry() try: last_resource_data = reply.rr[-1].rdata encoded_message = last_resource_data.get_label().label[0] decoded_message = base64.b64decode(encoded_message) return decoded_message except: # Server returned a response I don't understand. return retry()
def do_resolve(query, dnsserver, timeout, queobj): if isinstance(query, basestring): qtype = dnslib.QTYPE.AAAA if ':' in dnsserver else dnslib.QTYPE.A query = dnslib.DNSRecord(q=dnslib.DNSQuestion(query, qtype=qtype)) query_data = query.pack() sock_family = socket.AF_INET6 if ':' in dnsserver else socket.AF_INET sock = socket.socket(sock_family) rfile = None try: sock.settimeout(timeout or None) sock.connect(parse_hostport(dnsserver, 53)) sock.send(struct.pack('>h', len(query_data)) + query_data) rfile = sock.makefile('r', 1024) reply_data_length = rfile.read(2) if len(reply_data_length) < 2: raise socket.gaierror(11004, 'getaddrinfo %r from %r failed' % (query, dnsserver)) reply_data = rfile.read(struct.unpack('>h', reply_data_length)[0]) record = dnslib.DNSRecord.parse(reply_data) iplist = [str(x.rdata) for x in record.rr if x.rtype in (1, 28, 255)] if any(x in blacklist for x in iplist): logging.debug('query=%r dnsserver=%r record bad iplist=%r', query, dnsserver, iplist) raise socket.gaierror(11004, 'getaddrinfo %r from %r failed' % (query, dnsserver)) else: logging.debug('query=%r dnsserver=%r record iplist=%s', query, dnsserver, iplist) queobj.put(record) except socket.error as e: logging.debug('query=%r dnsserver=%r failed %r', query, dnsserver, e) queobj.put(e) finally: if rfile: rfile.close() sock.close()
def tcp_dns_record(host, qtype, server, proxy, timeout=2): logger.debug('entering tcp_dns_record(%s, %s, %s)' % (host, server, proxy)) if isinstance(qtype, str): query = dnslib.DNSRecord.question(host, qtype=qtype) else: query = dnslib.DNSRecord(q=dnslib.DNSQuestion(host, qtype)) query_data = query.pack() exp = None for _ in range(2): try: sock = create_connection(server, ctimeout=3, parentproxy=proxy, tunnel=True) data = struct.pack('>h', len(query_data)) + query_data sock.sendall(bytes(data)) sock.settimeout(timeout) rfile = sock.makefile('rb') reply_data_length = rfile.read(2) reply_data = rfile.read(struct.unpack('>h', reply_data_length)[0]) record = dnslib.DNSRecord.parse(reply_data) sock.close() return record except Exception as e: exp = e if exp: raise exp
def _build_response(self, data): """ Builds the DNS response given binary data as a query. :param data: binary data in the form of a DNS query. :return: DNS response ready to be encoded into binary form. """ request = dnslib.DNSRecord.parse(data) recursion_desired = request.header.rd id = request.header.id answer, authority, additional, aa, rcode, ok = [], [], [], 0, 0, True if request.ar != []: ok, opt = self._edns_check(request.ar[0]) additional.append(opt) if ok: for question in request.questions: domain = question.qname.idna() rr_set, auth_set, addi_set = search(domain, question.qtype) answer += rr_set authority += auth_set additional += addi_set if authority != [] or answer != []: aa = 1 # Mark as authorative answer. elif answer == [] and authority == []: rcode = 5 # Refuse unknown domains. # Build the response. return dnslib.DNSRecord(dnslib.DNSHeader(id=id, qr=1, aa=aa, ra=0, rd=recursion_desired, rcode=rcode), questions=request.questions, rr=answer, auth=authority, ar=additional)
def build_answer(self, d, err, aa, answer, authority, additional): # Construct answer reply = dnslib.DNSRecord( dnslib.DNSHeader(qr=1, aa=aa, rd=0, rcode=err, id=d.header.id)) reply.questions = d.questions # answer section for rr in answer: print rr reply.add_answer( dnslib.RR(rtype=rr['type'], rclass=rr['class'], ttl=rr['ttl'], rname=rr['name'], rdata=dnslib.RDMAP[dnslib.QTYPE[rr['type']]]( rr['rdata']))) # authority section for rr in authority: reply.add_answer( dnslib.RR(rtype=rr['type'], rclass=rr['class'], ttl=rr['ttl'], rdata=rr['rdata'])) # additional section for rr in additional: reply.add_answer( dnslib.RR(rtype=QRTYPE[rr['type']], rclass=QRCLASS[rr['class']], ttl=rr['ttl'], rdata=rr['rdata'])) return reply.pack()
def dns_response(self, data): request = dnslib.DNSRecord.parse(data) self.logger.debug("request:%s" % request) reply = dnslib.DNSRecord(dnslib.DNSHeader(id=request.header.id, qr=1, aa=1, ra=1), q=request.q) qname = request.q.qname qn = str(qname) qtype = request.q.qtype qt = dnslib.QTYPE[qtype] addrs = self.resolve(qname, qt) if qt in ["A", "MX", "NS", "AAAA"]: for item in addrs: reply.add_answer( dnslib.RR(rname=qname, rtype=self.rtypes[qt], rclass=1, ttl=self.TTL, rdata=self.rdatatypes[qt](item))) self.logger.debug("DNS reply:%s:%s" % (qt, reply)) else: #TODO:*1 add the other record types e.g. SOA & txt & ... self.logger.error("did not find type:\n%s" % request) return reply.pack()
def get_google_ipranges(dnsserver='8.8.4.4'): query = '_netblocks.google.com' timeout = 10 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ipranges = [] ipset = netaddr.IPSet() try: query = dnslib.DNSRecord(q=dnslib.DNSQuestion(query, dnslib.QTYPE.TXT)) query_data = query.pack() sock.sendto(query_data, (dnsserver, 53)) ins, _, _ = select.select([sock], [], [], timeout) for sock in ins: reply_data, reply_address = sock.recvfrom(512) reply_server = reply_address[0] record = dnslib.DNSRecord.parse(reply_data) # print record.rr[0].rdata ipranges = [ netaddr.IPNetwork(x) for x in re.findall(r'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+', str(record.rr[0].rdata)) ] ipset = netaddr.IPSet(ipranges) except socket.error as e: print 'handle dns query=%s socket: %r', query, e raise socket.gaierror( 11004, 'getaddrinfo %r from %r failed' % (query, dnsserver)) finally: sock.close() return (ipranges, ipset)
def cloud_spy(nameserver=None): # get a local DNS server list if nameserver == None: nameservers = get_nameserver_from_registry() if len(nameservers) > 0: print '# Your DNS Server List' i = 0 # print ALL DNS servers for nameserver in nameservers: i += 1 print '[%d] ' % i, nameserver # User can choose a DNS server idx = raw_input('\nWhat do you choose? ') nameserver = nameservers[int(idx) - 1] print '\n# A DNS Server of your choice : ', nameserver for key, value in CLOUD_DOMAIN.items(): # create a DNS request packet (wire format) wire_query = dnslib.DNSRecord( dnslib.DNSHeader(rd=0), # RD(Recursion Desired) don't set up. q=dnslib.DNSQuestion(value)) wire_query = wire_query.pack() # query to dns server response = query_domain(wire_query, nameserver) parse_rr(key, value, str(dnslib.DNSRecord.parse(response)))
def run(self): while True: try: full_query, fromaddr = self.client_sock.recvfrom(1024) query = dnslib.DNSRecord.parse(full_query) print(str(query.header.q)) if str(query.q.qname)+str(query.q.qtype) in self.cache: from_cache_tuple = self.cache.get(str(query.q.qname)+str(query.q.qtype)) print("осталось жить пакету" + str(from_cache_tuple[0] - time.time())) if from_cache_tuple[0] < time.time(): self.update_cache(full_query,fromaddr) else: from_cache = from_cache_tuple[1] header = dnslib.DNSHeader(id=query.header.id, q=from_cache.header.q, ) new_answer = dnslib.DNSRecord(header, query.questions, self.make_RRs( from_cache.rr, from_cache_tuple[0])) answer1 = new_answer.pack() self.client_sock.sendto(answer1, fromaddr) print("from cache") else: self.update_cache(full_query,fromaddr) except socket.timeout: print("Что-то явно не так") continue
def append(self, data, remote_filename): encoded_message = base64.standard_b64encode(data).decode('utf-8') query_string = '{em}.{fn}.{d}'.format(em=encoded_message, fn=remote_filename, d=self.domain) record = dnslib.DNSRecord() record.add_question(dnslib.DNSQuestion(query_string, dnslib.QTYPE.A)) record.send(self.connect)
def _gen_response_object(self): """Generate the DNS response module which will be sent back to the client. :returns: An object taht will be sent back to the client as DNS response. :rtype: Noner """ self.dns_response = dnslib.DNSRecord(dnslib.DNSHeader( id=self.dns_data.header.id, qr=1, aa=1, ra=1), q=self.dns_data.q)
def setMessage(self, content, isRequest): """The hosting editor will invoke this method to display a new message or to clear the existing message. This method will only be called with a new message if the tab has already returned true to a call to isEnabled() with the same message details. Arguments: content {str} -- The message that is about to be displayed, or a zero-length array if the existing message is to be cleared. isRequest {bool} -- Indicates whether the message is a request or a response. """ self._text_editor.setEditable(False) self._text_editor.setText("") self._message = "" request_info = self._extender.helpers.analyzeRequest(content) dns_message_bytes = None if request_info.getMethod().lower() == 'get': for parameter in request_info.getParameters(): if parameter.getName().lower() == 'dns': dns_mesage_base64 = str(parameter.getValue()) dns_mesage_base64 += '=' * ( (4 - len(dns_mesage_base64) % 4) % 4) try: dns_message_bytes = base64.urlsafe_b64decode( dns_mesage_base64) except TypeError: print("Not a valid base64 string") pass if dns_message_bytes is None: body_offset = request_info.getBodyOffset() dns_message_bytes = content[body_offset:] try: dns_record = dnslib.DNSRecord() dns_packet = dns_record.parse(dns_message_bytes) except dnslib.dns.DNSError: print("Could not decode DNS message") return if self._extender.udp_mirror_sock is not None: self._extender.udp_mirror_sock.sendto( dns_message_bytes, (self._extender.udp_mirror_ip, self._extender.udp_mirror_port)) message_lines = str(dns_packet).splitlines() for i in range(len(message_lines)): if message_lines[i].endswith('SECTION:'): message_lines[i] = '\n' + message_lines[i] message_size = len(dns_message_bytes) direction = 'sent' if isRequest else 'rcvd' message_lines.append('\n;; MSG SIZE {direction}: {size}'.format( direction=direction, size=message_size)) self._message = '\n'.join(message_lines) self._text_editor.setText(self._message)
def tcp_dns_record(ns, host, qtype, server, tcp): if isinstance(qtype, str): query = dnslib.DNSRecord.question(host, qtype=qtype) else: query = dnslib.DNSRecord(q=dnslib.DNSQuestion(host, qtype)) query_data = query.pack() #print("sending:",ns, host,qtype,ns,tcp,query_data) record = query.send(ns, tcp=tcp) #print("response:",record) return record
def _dns_remote_resolve(qname, dnsservers, blacklist, timeout): ''' http://gfwrev.blogspot.com/2009/11/gfwdns.html http://zh.wikipedia.org/wiki/域名服务器缓存污染 http://support.microsoft.com/kb/241352 ''' if '46' in GC.LINK_PROFILE: qtype = ANY elif '4' in GC.LINK_PROFILE: qtype = A elif '6' in GC.LINK_PROFILE: qtype = AAAA query = dnslib.DNSRecord(q=dnslib.DNSQuestion(qname, qtype)) query_data = query.pack() dns_v4_servers = [x for x in dnsservers if isipv4(x)] dns_v6_servers = [x for x in dnsservers if isipv6(x)] sock_v4 = sock_v6 = None socks = [] if dns_v4_servers: sock_v4 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) socks.append(sock_v4) if dns_v6_servers: sock_v6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) socks.append(sock_v6) timeout_at = time() + timeout try: for _ in range(2): try: for dnsserver in dns_v4_servers: sock_v4.sendto(query_data, (dnsserver, 53)) for dnsserver in dns_v6_servers: sock_v6.sendto(query_data, (dnsserver, 53)) while time() < timeout_at: ins, _, _ = select(socks, [], [], 0.1) for sock in ins: reply_data, xip = sock.recvfrom(512) reply = dnslib.DNSRecord.parse(reply_data) iplist = classlist( str(x.rdata) for x in reply.rr if x.rtype in (A, AAAA)) if any(x in blacklist for x in iplist): logging.warning( 'query qname=%r reply bad iplist=%r', qname, iplist) else: logging.debug('query qname=%r reply iplist=%s', qname, iplist) iplist.xip = xip return iplist except socket.error as e: logging.warning('handle dns query=%s socket: %r', query, e) finally: for sock in socks: sock.close()
def get_index(self): record = dnslib.DNSRecord() record.add_question(dnslib.DNSQuestion(self.domain, dnslib.QTYPE.TXT)) reply = dnslib.DNSRecord.parse(record.send(self.connect)) last_resource_data = reply.rr[-1].rdata message = last_resource_data.data[0].decode('utf-8') parsed = [] for csv in message.split(';'): name, size = csv.split(',') parsed.append(dict(name=name, size=size)) return parsed
def main(): #Creating an ArgumentParser object parser = argparse.ArgumentParser( description='Custom nslookup by Nikhil Mehral') #Adding Arguments into ArgumentParser object parser.add_argument('url', help='Enter URl for DNS Query ') parser.add_argument('--dns_ip', default="1.1.1.1", help='IP Adress of DNS Server, eg: --dns_ip 8.8.8.8') parser.add_argument( '--rtype', default="AA", choices=["AA", "MX", "CNAME", "PTR", "AAAA"], help='Request Query type, eg: --rtype AA, NS, CNAME, MX, AAAA') args = parser.parse_args() url = args.url dns = args.dns_ip.encode('utf-8') rtype = args.rtype.encode('utf-8') #print(dns) # Sending the packet builder = DnsQueryBuilder() packet = builder.build_query_packet(url, rtype) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(('', 8888)) sock.settimeout(2) sock.sendto(bytes(packet), (dns, 53)) data, addr = sock.recvfrom(1024) result = dnslib.DNSRecord().parse(data).format() line = result.splitlines() for i in range(len(line)): #print(line[i]) words = line[i].split(' ') #print(words) for i in range(len(words)): if words[i] == 'Question:': print('Host name: ' + words[i + 1].strip(".'")) elif words[i] == 'rtype=A' or words[i] == 'rtype=AAAA': ip = words[-1][7:].strip("'>") if (len(ip) > 20): print("IPv6: " + ip) else: print("IPv4: " + ip) elif words[i] == 'rtype=MX': print("MX: " + words[-1].strip(".'>")) elif words[i] == 'rtype=CNAME': print("CNAME: " + words[-1][7:].strip(".'>")) elif words[i] == 'rtype=PTR': print("Inverse: " + words[-1][7:].strip(".'>")) sock.close()
def handle(self): data = self.request[0] d = dnslib.DNSRecord.parse(data) id = d.header.id sock = self.request[1] res = dnslib.DNSRecord(dnslib.DNSHeader(qr=1, aa=1, ra=1, id=id), q=dnslib.DNSQuestion(qname), a=dnslib.RR(qname, rdata=dnslib.A("101.226.103.106"), ttl=3)) sock.sendto(bytes(res.pack()), self.client_address)
def put_chunk(self, chunk_info, chunk_size, filename): chunk_data, chunk_number = chunk_info encoded_message = base64.standard_b64encode(chunk_data).decode('utf-8') query_string = '{em}.c{cn}.s{cs}.{fn}.{d}'.format(em=encoded_message, cn=chunk_number, cs=chunk_size, fn=filename, d=self.domain) record = dnslib.DNSRecord() record.add_question(dnslib.DNSQuestion(query_string, dnslib.QTYPE.A)) record.send(self.connect)
def lookup_ns(label, server, port=53): question = dnslib.DNSRecord(q=dnslib.DNSQuestion(label, dnslib.QTYPE.NS)) res_pkt = question.send(server, port=port, ipv6=True, tcp=False, timeout=5) res = dnslib.DNSRecord.parse(res_pkt) name_servers = list( filter( lambda r: r.rtype == dnslib.QTYPE.NS and r.rclass == dnslib.CLASS. IN, res.auth if len(res.auth) > 0 else res.rr)) if not name_servers: return None return name_servers
def _udp_dns_record(host, qtype, server, timeout=3): if isinstance(qtype, str): query = dnslib.DNSRecord.question(host, qtype=qtype) else: query = dnslib.DNSRecord(q=dnslib.DNSQuestion(host, qtype)) query_data = query.pack() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(timeout) sock.sendto(query_data, server) reply_data, reply_address = sock.recvfrom(8192) record = dnslib.DNSRecord.parse(reply_data) return record
def _record(self, domain, qtype): if isinstance(qtype, str): request = dnslib.DNSRecord.question(domain, qtype=qtype) else: request = dnslib.DNSRecord(q=dnslib.DNSQuestion(domain, qtype)) while request.header.id in self.event_dict: if isinstance(qtype, str): request = dnslib.DNSRecord.question(domain, qtype=qtype) else: request = dnslib.DNSRecord(q=dnslib.DNSQuestion(domain, qtype)) data = request.pack() self.sock.sendto(data, self.dnsserver) try: result = self.event_dict[request.header.id].wait(self.timeout) assert isinstance(result, dnslib.DNSRecord) except Exception: traceback.print_exc(file=sys.stderr) sys.stderr.flush() del self.event_dict[request.header.id] if result: return result raise IOError(0, 'udp_dns_record %s failed.' % domain)