在建立網路列舉工具時,了解網域結構如何對應是安全愛好者或開發者的一項核心技能。在本篇文章中,我們將探討一個使用 dnspython 函式庫、以 Python 撰寫的簡單遞迴 DNS 探索腳本。
先決條件若要在本機或行動終端環境(如 Termux)中執行此腳本,您需要先安裝 dnspython 套件:
pip install dnspython
腳本程式碼
以下是用於基於字典的子網域列舉與反向 DNS 查詢的完整腳本:
import dns
import dns.resolver
import socket
domains = {}
subs = "dns_search.txt"
res = dns.resolver.Resolver()
res.nameservers = ["8.8.8.8"]
res.port = 53
domain = "google.com"
nums = True
def ReverseDNS(ip):
try:
result = socket.gethostbyaddr(ip)
return [result[0]]+result[1]
except socket.herror:
return []
def DNSRequest(domain):
ips = []
try:
result = res.resolve(domain)
if result:
addresses = [a.to_text() for a in result]
if domain in domains:
domains[domain] = list(set(domains[domain]+addresses))
else:
domains[domain] = addresses
for a in addresses:
rd = ReverseDNS(a)
for d in rd:
if d not in domains:
domains[d] = [a]
DNSRequest(d)
else:
domains[d] = [a]
except (dns.resolver.NXDOMAIN,dns.exception.Timeout):
return []
return ips
def HostSearch(domain,dictionary,nums):
successes = []
for word in dictionary:
d = word+"."+domain
DNSRequest(d)
if nums:
for i in range (0,10):
s = word+str(i)+"."+domain
DNSRequest(s)
dictionary = []
with open(subs,"r") as f:
dictionary = f.read().splitlines()
HostSearch(domain,dictionary,nums)
for domain in domains:
print("%s:%s" % (domain, domains[domain]))
運作原理
自訂解析器設定:腳本會建立一個自訂解析器,明確指向 Google 的公用 DNS 名稱伺服器 (8.8.8.8) 並使用連接埠 53。
字典迭代與數字附加:腳本會讀取單字清單 (dns_search.txt),並將每個單字與目標網域結合。若 nums = True,則會額外附加數字 0 到 9,以最大化涵蓋範圍。
遞迴反向查詢:當解析出有效的 A 記錄後,腳本會對該 IP 位址執行反向 DNS 查詢 (PTR),並將任何發現的主機名稱回饋至探索迴圈中。
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.