Skip to content

Instantly share code, notes, and snippets.

  • Save bandrel/c04d1d75bb4aba285e86 to your computer and use it in GitHub Desktop.
Save bandrel/c04d1d75bb4aba285e86 to your computer and use it in GitHub Desktop.
dns lookup
def query(self, hostname, query_type = 'ANY', name_server = False, use_tcp = True):
ret = []
response = None
if name_server == False:
name_server = self.get_ns()
else:
self.wildcards = {}
self.failed_code = None
self.last_resolver = name_server
query = dnslib.DNSRecord.question(hostname, query_type.upper().strip())
try:
response_q = query.send(name_server, 53, use_tcp)
if response_q:
response = dnslib.DNSRecord.parse(response_q)
else:
raise IOError("Empty Response")
except Exception as e:
#IOErrors are all conditions that require a retry.
raise IOError(str(e))
if response:
self.rcode = dnslib.RCODE[response.header.rcode]
for r in response.rr:
try:
rtype = str(dnslib.QTYPE[r.rtype])
except:#Server sent an unknown type:
rtype = str(r.rtype)
#Fully qualified domains may cause problems for other tools that use subbrute's output.
rhost = str(r.rname).rstrip(".")
ret.append((rhost, rtype, str(r.rdata)))
#What kind of response did we get?
if self.rcode not in ['NOERROR', 'NXDOMAIN', 'SERVFAIL', 'REFUSED']:
trace('!Odd error code:', self.rcode, hostname, query_type)
#Is this a perm error? We will have to retry to find out.
if self.rcode in ['SERVFAIL', 'REFUSED', 'FORMERR', 'NOTIMP', 'NOTAUTH']:
raise IOError('DNS Failure: ' + hostname + " - " + self.rcode)
#Did we get an empty body and a non-error code?
elif not len(ret) and self.rcode != "NXDOMAIN":
raise IOError("DNS Error - " + self.rcode + " - for:" + hostname)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment