Guest User

Untitled

a guest
Feb 28th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import subprocess
  2. import json
  3. import sys
  4. import logging
  5.  
  6. # Configure logging for debugging
  7.  
  8. def run_dig(hostname):
  9. try:
  10. # Run dig command for NS query
  11. result = subprocess.run(['dig', 'NS', hostname, '-4'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
  12. return result.stdout
  13. except Exception as e:
  14. return str(e)
  15.  
  16. def parse_dig_output(dig_output, hostname):
  17. response = {
  18.  
  19. "details": {
  20. "delegated": False
  21. }
  22. }
  23.  
  24. # Check if 'ANSWER SECTION' exists in the dig output
  25. if "ANSWER SECTION:" in dig_output:
  26. # Extract lines after 'ANSWER SECTION' and look for NS records
  27. answer_section = dig_output.split("ANSWER SECTION:")[1]
  28. ns_lines = [line.strip() for line in answer_section.splitlines() if "IN NS" in line]
  29.  
  30. # Check if any NS lines contain the hostname at the start
  31. for line in ns_lines:
  32. ns_host = line.split()[0]
  33. if ns_host.startswith(hostname):
  34. response["details"]["delegated"] = True
  35. response["details"]["delegated_zone"] = hostname
  36. break
  37.  
  38. # Check for rcode errors in the dig output
  39. error_codes = ['SERVFAIL', 'NXDOMAIN', 'REFUSED', 'NXRRSET', 'NOTAUTH', 'NOTZONE']
  40. for error_code in error_codes:
  41. if f"rcode={error_code}" in dig_output:
  42. response["details"]["delegated"] = True
  43. break
  44.  
  45. return response if response["details"]["delegated"] else None
  46.  
  47. def main():
  48. # Get the JSON input
  49. if len(sys.argv) < 2:
  50. sys.exit(1)
  51.  
  52. input_data = json.loads(sys.argv[1])
  53. hostname = input_data.get("value")
  54.  
  55. if not hostname:
  56. sys.exit(1)
  57.  
  58. # Run dig and parse the output
  59. dig_output = run_dig(hostname)
  60. result = parse_dig_output(dig_output, hostname)
  61.  
  62. # Output only the entries with delegated: true
  63. if result:
  64. print(json.dumps([result], indent=2))
  65. else:
  66. print("[]")
  67.  
  68. if __name__ == "__main__":
  69. main()
Advertisement
Add Comment
Please, Sign In to add comment