File: zonetransfer.py

package info (click to toggle)
knockpy 4.1.0-4
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 256 kB
  • sloc: python: 560; makefile: 3
file content (55 lines) | stat: -rw-r--r-- 1,574 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
import socket

''' set the default timeout on sockets to 5 seconds '''
if hasattr(socket, 'setdefaulttimeout'): socket.setdefaulttimeout(5)

try:
	import dns.resolver, dns.query, dns.zone
except:
	exit('ImportError: No module named python-dnspython\npip install dnspython')


def zonetransfer(target):
	zonetransfer_list = []
	my_resolver = dns.resolver.Resolver()
	my_resolver.timeout=2.0
	my_resolver.lifetime=2.0
	try:
		answers = my_resolver.query(target,'NS')
	except: 
		response = {'enabled': False, 'list': [] }
		response = json.dumps(response, indent=4, separators=(',', ': '))
		return response
	
	ip_from_nslist = []
	for name_server in answers:
		name_server = str(name_server).rstrip('.')
		ip_from_nslist.append(socket.gethostbyname(name_server))

	for ip_from_ns in ip_from_nslist:
		zone = False

		try:
			zone = dns.zone.from_xfr(dns.query.xfr(ip_from_ns, target, timeout = 1))
		except: 
			pass
		
		if zone:
			for name, node in zone.nodes.items():
				rdataset = node.rdatasets
				for record in rdataset:
					name = str(name)
					if name != '@' and name != '*':
						zonetransfer_list.append(name+'.'+target)
	
	if zonetransfer_list:
		zonetransfer_list = [item.lower() for item in zonetransfer_list]
		zonetransfer_list = list(set(zonetransfer_list))
		response = {'enabled': True, 'list': zonetransfer_list }
		response = json.dumps(response, indent=4, separators=(',', ': '))
		return response
	else:
		response = {'enabled': False, 'list': [] }
		response = json.dumps(response, indent=4, separators=(',', ': '))
		return response