Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. import sys
  2. import time
  3. import datetime
  4. import dns
  5. import dns.name
  6. import dns.query
  7.  
  8. def mydig(domain, where, timer):
  9. domain = str(domain)
  10.  
  11. qname = dns.name.from_text(domain)
  12. if domain.startswith('www.'):
  13. domain = domain[4:]
  14.  
  15. dns_start = time.perf_counter()
  16. q = dns.message.make_query(qname, dns.rdatatype.A)
  17. dns_end = time.perf_counter()
  18. timer = timer + ((dns_end - dns_start) * 1000)
  19.  
  20. # print('The query is:')
  21. # print(q)
  22. # print('')
  23.  
  24. where = str(where)
  25. dns_start = time.perf_counter()
  26. r = dns.query.udp(q, where)
  27. # print(r)
  28. dns_end = time.perf_counter()
  29. timer = timer + ((dns_end - dns_start) * 1000)
  30.  
  31. rToString = str(r)
  32. # parsing answer section
  33. answer_position = rToString.find("ANSWER")
  34. answer = rToString[answer_position:]
  35. answer = answer.split('\n')
  36. # parsing additional section
  37. additional_position = rToString.find(";ADDITIONAL")
  38. additional = rToString[additional_position:]
  39. additional = additional.split('\n')
  40. # print("THIS IS THE ANSWER")
  41. # print(answer)
  42. first_answer = answer[1]
  43. # print("THIS IS THE ADDITONAL")
  44. # print(additional)
  45. additional.pop(0) # remove first element in additional section
  46. # print("THIS IS THE ADDITONAL without heading")
  47. # print(additional)
  48. # print("THIS IS THE ONE - ANSWER")
  49. # print(first_answer)
  50. if first_answer[-1].isdigit(): # looks at answer section
  51. answer.remove('ANSWER')
  52. #ip_list = []
  53. print("QUESTION SECTION:")
  54. print("%s IN A" % domain)
  55. print("ANSWER SECTION:")
  56. print(first_answer)
  57. # for elem in answer:
  58. # if elem == ";AUTHORITY":
  59. # break
  60. # elem = elem.split(" A ")
  61. # elem.pop(0)
  62. # ip_list.append(elem)
  63. # print("The Domain's IP Addresses are...")
  64. # ip_list = sum(ip_list, []) # to flatten list of lists
  65. # print(ip_list)
  66. timer = float(timer)
  67. print("Query time: %.2f ms" % timer)
  68. print("WHEN: %s" % str(datetime.datetime.now()))
  69. elif additional != []: # looks at additional section
  70. # print("Test:")
  71. additional_position= rToString.find("ADDITIONAL")
  72. additional = rToString[additional_position:]
  73. # print("printing Additional:")
  74. # print(additional)
  75. additional_list = additional.split(" A ")
  76. # print("TESTING")
  77. # print(additional_list)
  78. firstIP= additional_list[1].split('\n')
  79. firstIP = firstIP[0]
  80. # print("The first ip is...")
  81. # print(firstIP)
  82. mydig(domain,firstIP,timer)
  83. else: # looks at authority section
  84. # print("Authority testing")
  85. authority_position = rToString.find("AUTHORITY")
  86. authority = rToString[authority_position:]
  87. # print("Printing authority")
  88. # print(authority)
  89. if " SOA " in authority:
  90. authority_list = authority.split(" SOA ")
  91. authority_list.pop(0)
  92. # find position of ".com" + 1
  93. first_authoritative_NS = authority_list[0]
  94. authoritative_NS_position = first_authoritative_NS.find(".com")
  95. authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
  96. elif " NS " in authority:
  97. authority_list = authority.split(" NS ")
  98. # print("PRINTING AUTHORITY_LIST")
  99. # print(authority_list)
  100. authority_list.pop(0)
  101. # print("PRINTING AUTHORITY_LIST popped")
  102. # print(authority_list)
  103. # find position of ".com" + 1
  104. first_authoritative_NS = authority_list[0]
  105. if ".com" in first_authoritative_NS:
  106. authoritative_NS_position = first_authoritative_NS.find(".com")
  107. authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
  108. if ".net" in first_authoritative_NS:
  109. authoritative_NS_position = first_authoritative_NS.find(".net")
  110. authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
  111. if ".org" in first_authoritative_NS:
  112. authoritative_NS_position = first_authoritative_NS.find(".org")
  113. authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
  114. if ".gov" in first_authoritative_NS:
  115. authoritative_NS_position = first_authoritative_NS.find(".gov")
  116. authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
  117. if ".edu" in first_authoritative_NS:
  118. authoritative_NS_position = first_authoritative_NS.find(".edu")
  119. authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 4]
  120. if ".cn" in first_authoritative_NS:
  121. authoritative_NS_position = first_authoritative_NS.find(".cn")
  122. authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 3]
  123. if ".uk" in first_authoritative_NS:
  124. authoritative_NS_position = first_authoritative_NS.find(".uk")
  125. authoritative_NS = first_authoritative_NS[:authoritative_NS_position + 3]
  126. # print("The authoritative_NS is...")
  127. # print(authoritative_NS)
  128. mydig(authoritative_NS,where,timer)
  129.  
  130.  
  131. def run_mydig(timer):
  132. mydig(sys.argv[1], sys.argv[2], timer)
  133.  
  134. run_mydig(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement