准备:
pip install dnspython
dnspython版本为2.1.0
思路:
1 根据域名查询NS服务器
2 尝试对每个NS服务器发起区域传输请求
该过程包含对NS域名的解析
代码:
#!/usr/bin/python3
import dns.resolver
import dns.query
import sys
ns=[]
def line():
flags='-'*70
print(flags)
def getNS(domain):
# answer is class <dns.resolver.Answer>
try:
# 返回结果是 类 <dns.resolver.Answer> 对象,查看dnspython手册了解类成员
# 解析失败触发异常
answer=dns.resolver.resolve(domain,'NS')
# 提取节点
for i in answer.rrset.items:
#print(i)
# i is class <Rdata>
ns.append(str(i))
print(' '+str(i))
except Exception as e:
print('!!!!get ns faile!!!!!\n')
return True
return False
def zoneTransef(nameServer,domain):
# 定义区域
zone=dns.zone.Zone(domain)
print("**********************")
try:
# 解析NS域名,解析失败则触发异常
answer=dns.resolver.resolve(nameServer,'A')
try:
# 提取解析的IP
for ip in answer.rrset.items:
print(nameServer+':'+str(ip))
# 请求区域传输,传输失败则触发异常
dns.query.inbound_xfr(str(ip),zone)
# 解析区域传输内容
for sub in zone.nodes:
if str(sub) != '@':
print(str(sub)+'.'+domain)
except Exception as e:
print(nameServer+' transfer faile\n')
except Exception as e:
print(nameServer+' resolve faile\n')
print('\n')
if __name__=="__main__":
if len(sys.argv) != 2:
print('usage: '+sys.argv[0]+' megacorpone.com')
exit()
line()
print("%%start%%")
line()
print("%%get nameservers%%")
print("Name Servers:")
if getNS(sys.argv[1]):
exit()
line()
print("%%zone transfer%%")
for n in ns:
zoneTransef(n,sys.argv[1])
line()
print("%%end%%")
## 有时间优化,并尝试使用scapy生成dns数据包进行区域传输