0x01 何為子域名接管
子域名接管是注冊不存在的域名以獲得對另一個域的控制權的過程送爸。此過程最常見的情況如下:
域名(例如滞欠,sub.example.com)將CNAME記錄用于另一個域(例如挟秤,sub.example.com CNAME anotherdomain.com)蒂教。在某個時間點弹沽,anotherdomain.com到期并可供任何人注冊追迟。
由于未從example.com DNS區(qū)域刪除CNAME記錄溶其,因此注冊anotherdomain.com的任何人都可以完全控制sub.example.com,直到存在DNS記錄敦间。子域名接管的影響可能非常重要瓶逃。使用子域名接管,攻擊者可以從合法域發(fā)送網(wǎng)絡釣魚電子郵件廓块,執(zhí)行跨站點腳本(XSS)或破壞與域關聯(lián)的品牌聲譽厢绝。
子域名接管不僅限于CNAME記錄。NS带猴,MX甚至A記錄也會受到影響昔汉。
image.png
0x02 常規(guī)域名
使用CNAME記錄的DNS授權對用戶完全透明,它發(fā)生在DNS解析期間的后臺拴清。下圖說明了具有CNAME記錄的域名的Web瀏覽器的行為靶病。
image.png
0x03 子域名接管漏洞批量檢測腳本
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
import sys
version = sys.version_info
if version < (3, 0):
print('The current version is not supported, you need to use python3')
sys.exit()
import dns.resolver
import tldextract
import requests
import datetime
from threading import Semaphore
import threading
import queue
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
CurrentTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
CurrentTime=str(CurrentTime).replace(' ','-').replace(':','-')
q=queue.Queue()
Multithread=10#線程數(shù)
Suddomain='domain_url.txt' #整理好的子域名文件
count=0
def outinfo(strinfo,status=0):#輸出不換行
global count
count=count+1
if status==1:
print('\n'+strinfo)
else:
sys.stdout.write("\r" + str(strinfo)+' '+str(count))
sys.stdout.flush()
def filewrite(text,lock):#運行的log記錄實時寫入文件会通。
lock.acquire()
outfile=open('Result-' + CurrentTime + '.txt', 'a', encoding='utf-8')
outfile.write(text+'\n')
outfile.close()
lock.release()
def Cname_query(q,lock):#查詢cname地址
while not q.empty():
subdomain=q.get()
try:
subdomain=subdomain.replace('http://','').replace('https://','').replace('/','')
cname = dns.resolver.query(subdomain, 'CNAME')
for i in cname.response.answer:
for j in i.items:
res = tldextract.extract(j.to_text())#提取Cname的主域名
domain=res.domain + '.' + res.suffix
regurl = 'https://checkapi.aliyun.com/check/checkdomain?domain={}&command=&token=Y3d83b57bc8aca0f156381976a6171f4a&ua=¤cy=&site=&bid=&_csrf_token=&callback=jsonp_1569557125267_14652'.format(
domain)
try:
res2 = requests.get(regurl, timeout=5, verify=False)#查詢Cname指向的域是否可注冊
if 'avail":1' in str(res2.content):
outinfo(subdomain+' ***存在子域名接管漏洞,接管地址:'+domain,1)
filewrite(subdomain+' ***存在子域名接管漏洞娄周,接管地址:'+domain,lock)#記錄實時寫入文件
else:
outinfo(subdomain+' 失敗涕侈,不存在子域名接管漏洞')
filewrite(subdomain+' 失敗,不存在子域名接管漏洞',lock)#記錄實時寫入文件
except Exception as e:
outinfo(e,lock)
except Exception as e:
outinfo(subdomain + ' 不存在cname')
filewrite(subdomain + ' 不存在cname', lock)#記錄實時寫入文件
File=open(Suddomain,'r',encoding='utf-8').read().split('\n')
lock=threading.Lock()
for Url in File:
q.put(Url)
for M in range(Multithread):
threading.Thread(target=Cname_query,args=(q,lock,)).start()
q.join()
print('程序執(zhí)行完成煤辨!')
使用
把已經(jīng)搜集好的子域名整理好裳涛,放置在當前目錄的 “domain_url.txt”,使用Python3直接運行腳本即可掷酗,結果的結果會實時保存起來调违。