Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import time
- import datetime
- import dns
- import dns.name
- import dns.query
- def mydig(domain, where, timer):
- domain = str(domain)
- qname = dns.name.from_text(domain)
- if domain.startswith('www.'):
- domain = domain[4:]
- dns_start = time.perf_counter()
- q = dns.message.make_query(qname, dns.rdatatype.A)
- dns_end = time.perf_counter()
- timer = timer + ((dns_end - dns_start) * 1000)
- # print('The query is:')
- # print(q)
- # print('')
- where = str(where)
- dns_start = time.perf_counter()
- r = dns.query.udp(q, where)
- # print(r)
- dns_end = time.perf_counter()
- timer = timer + ((dns_end - dns_start) * 1000)
- rToString = str(r)
- # parsing answer section
- answer_position = rToString.find("ANSWER")
- answer = rToString[answer_position:]
- answer = answer.split('\n')
- # parsing additional section
- additional_position = rToString.find(";ADDITIONAL")
- additional = rToString[additional_position:]
- additional = additional.split('\n')
- # print("THIS IS THE ANSWER")
- # print(answer)
- first_answer = answer[1]
- # print("THIS IS THE ADDITONAL")
- # print(additional)
- additional.pop(0) # remove first element in additional section
- # print("THIS IS THE ADDITONAL without heading")
- # print(additional)
- # print("THIS IS THE ONE - ANSWER")
- # print(first_answer)
- if first_answer[-1].isdigit(): # looks at answer section
- answer.remove('ANSWER')
- #ip_list = []
- print("QUESTION SECTION:")
- print("%s IN A" % domain)
- print("ANSWER SECTION:")
- print(first_answer)
- # for elem in answer:
- # if elem == ";AUTHORITY":
- # break
- # elem = elem.split(" A ")
- # elem.pop(0)
- # ip_list.append(elem)
- # print("The Domain's IP Addresses are...")
- # ip_list = sum(ip_list, []) # to flatten list of lists
- # print(ip_list)
- timer = float(timer)
- print("Query time: %.2f ms" % timer)
- print("WHEN: %s" % str(datetime.datetime.now()))
- elif additional != []: # looks at additional section
- # print("Test:")
- additional_position= rToString.find("ADDITIONAL")
- additional = rToString[additional_position:]
- # print("printing Additional:")
- # print(additional)
- additional_list = additional.split(" A ")
- # print("TESTING")
- # print(additional_list)
- firstIP= additional_list[1].split('\n')
- firstIP = firstIP[0]
- # print("The first ip is...")
- # print(firstIP)
- mydig(domain,firstIP,timer)
- else: # looks at authority section
- # print("Authority testing")
- authority_position = rToString.find("AUTHORITY")
- authority = rToString[authority_position:]
- # print("Printing authority")
- # print(authority)
- if " SOA " in authority:
- authority_list = authority.split(" SOA ")
- authority_list.pop(0)
- # find position of ".com" + 1
- first_authoritative_NS = authority_list[0]
- authoritative_NS_position = first_authoritative_NS.find(".com")
- authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
- elif " NS " in authority:
- authority_list = authority.split(" NS ")
- # print("PRINTING AUTHORITY_LIST")
- # print(authority_list)
- authority_list.pop(0)
- # print("PRINTING AUTHORITY_LIST popped")
- # print(authority_list)
- # find position of ".com" + 1
- first_authoritative_NS = authority_list[0]
- if ".com" in first_authoritative_NS:
- authoritative_NS_position = first_authoritative_NS.find(".com")
- authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
- if ".net" in first_authoritative_NS:
- authoritative_NS_position = first_authoritative_NS.find(".net")
- authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
- if ".org" in first_authoritative_NS:
- authoritative_NS_position = first_authoritative_NS.find(".org")
- authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
- if ".gov" in first_authoritative_NS:
- authoritative_NS_position = first_authoritative_NS.find(".gov")
- authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
- if ".edu" in first_authoritative_NS:
- authoritative_NS_position = first_authoritative_NS.find(".edu")
- authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
- if ".cn" in first_authoritative_NS:
- authoritative_NS_position = first_authoritative_NS.find(".cn")
- authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 3]
- if ".uk" in first_authoritative_NS:
- authoritative_NS_position = first_authoritative_NS.find(".uk")
- authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 3]
- # print("The authoritative_NS is...")
- # print(authoritative_NS)
- mydig(authoritative_NS,where,timer)
- def run_mydig(timer):
- mydig(sys.argv[1], sys.argv[2], timer)
- run_mydig(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement