子網域枚舉概述
子網域枚舉(Subdomain Enumeration)是滲透測試和漏洞賞金獵人在偵察階段的核心技術之一。透過發現目標組織的所有子網域,攻擊者可以擴大攻擊面,找出潛在的薄弱環節,例如:被遺忘的測試環境、未更新的舊系統、或是配置錯誤的服務。
為什麼子網域枚舉很重要?
- 擴大攻擊面:主網域通常受到較多保護,但子網域可能存在安全漏洞
- 發現隱藏資產:staging、dev、test 等子網域常被忽視
- 識別技術堆疊:不同子網域可能使用不同的技術,提供更多攻擊向量
- 繞過 WAF/CDN:某些子網域可能未受保護,可直接存取後端伺服器
被動枚舉技術
被動枚舉不直接與目標伺服器互動,而是利用公開資源蒐集資訊,降低被發現的風險。
使用線上資料庫
1
2
3
4
5
6
7
8
9
10
| # 使用 crt.sh 查詢證書透明度日誌
curl -s "https://crt.sh/?q=%.example.com&output=json" | jq -r '.[].name_value' | sort -u
# 使用 SecurityTrails API(需要 API Key)
curl -s "https://api.securitytrails.com/v1/domain/example.com/subdomains" \
-H "APIKEY: YOUR_API_KEY" | jq -r '.subdomains[]' | sed 's/$/.example.com/'
# 使用 VirusTotal API
curl -s "https://www.virustotal.com/vtapi/v2/domain/report?apikey=YOUR_API_KEY&domain=example.com" \
| jq -r '.subdomains[]'
|
歷史 DNS 記錄查詢
1
2
3
| # 使用 Wayback Machine 查詢歷史子網域
curl -s "http://web.archive.org/cdx/search/cdx?url=*.example.com/*&output=text&fl=original&collapse=urlkey" \
| sed -e 's_https*://__' -e "s/\/.*//g" | sort -u
|
主動枚舉技術
主動枚舉會直接與目標互動,可能被 IDS/IPS 偵測到,但通常能獲得更完整的結果。
DNS 暴力破解
1
2
3
4
5
6
7
8
| # 使用字典檔進行子網域暴力破解
# 常用字典:SecLists/Discovery/DNS/subdomains-top1million-5000.txt
# 使用 gobuster 進行 DNS 暴力破解
gobuster dns -d example.com -w /usr/share/seclists/Discovery/DNS/subdomains-top1million-5000.txt -t 50
# 使用 massdns 進行高速 DNS 解析
massdns -r resolvers.txt -t A -o S subdomains.txt | grep -v "NXDOMAIN"
|
常用工具
Subfinder
Subfinder 是一個快速的被動子網域發現工具,支援多種資料來源。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 安裝 Subfinder
go install -v github.com/projectdiscovery/subfinder/v2/cmd/subfinder@latest
# 基本用法
subfinder -d example.com -o subdomains.txt
# 使用所有來源並顯示詳細資訊
subfinder -d example.com -all -v
# 從檔案讀取多個目標
subfinder -dL domains.txt -o all_subdomains.txt
# 設定 API Keys 以獲得更多結果
# 編輯 ~/.config/subfinder/provider-config.yaml
|
Amass
Amass 是 OWASP 專案,提供強大的資產發現能力。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 安裝 Amass
go install -v github.com/owasp-amass/amass/v4/...@master
# 被動模式枚舉
amass enum -passive -d example.com -o amass_passive.txt
# 主動模式枚舉(包含 DNS 暴力破解)
amass enum -active -d example.com -brute -w wordlist.txt -o amass_active.txt
# 使用圖形化輸出
amass viz -d3 -d example.com
# 列舉歷史資料
amass intel -whois -d example.com
|
Assetfinder
Assetfinder 是一個輕量級工具,專注於快速發現資產。
1
2
3
4
5
6
7
8
9
10
11
| # 安裝 Assetfinder
go install -v github.com/tomnomnom/assetfinder@latest
# 基本用法
assetfinder example.com
# 只顯示包含目標網域的結果
assetfinder --subs-only example.com
# 結合其他工具使用
assetfinder --subs-only example.com | httprobe | tee live_subdomains.txt
|
DNS 區域傳輸測試
DNS 區域傳輸(Zone Transfer)配置不當可能洩露所有 DNS 記錄。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 查詢目標的 Name Server
dig NS example.com +short
# 嘗試區域傳輸
dig axfr example.com @ns1.example.com
# 使用 host 命令
host -l example.com ns1.example.com
# 批次測試多個 NS
for ns in $(dig NS example.com +short); do
echo "Testing $ns..."
dig axfr example.com @$ns
done
|
證書透明度日誌
證書透明度(Certificate Transparency, CT)日誌是發現子網域的重要來源。
1
2
3
4
5
6
7
8
9
10
11
12
| # 使用 crt.sh
curl -s "https://crt.sh/?q=%.example.com&output=json" | \
jq -r '.[].name_value' | \
sed 's/\*\.//g' | \
sort -u > ct_subdomains.txt
# 使用 ctfr 工具
python3 ctfr.py -d example.com -o ct_results.txt
# 使用 certspotter
curl -s "https://api.certspotter.com/v1/issuances?domain=example.com&include_subdomains=true&expand=dns_names" | \
jq -r '.[].dns_names[]' | sort -u
|
搜尋引擎 Dorking
利用搜尋引擎的進階搜尋語法發現子網域。
Google Dorks
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 基本子網域搜尋
site:*.example.com -www
# 尋找特定類型的子網域
site:*.example.com inurl:admin
site:*.example.com inurl:dev
site:*.example.com inurl:staging
site:*.example.com inurl:api
# 尋找敏感檔案
site:*.example.com filetype:pdf
site:*.example.com filetype:xls
site:*.example.com filetype:conf
|
Shodan
1
2
3
4
5
| # 使用 Shodan CLI
shodan search "hostname:*.example.com" --fields hostnames
# 尋找 SSL 證書中的子網域
shodan search "ssl.cert.subject.CN:example.com" --fields hostnames
|
自動化枚舉腳本
整合多個工具的自動化腳本:
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
| #!/bin/bash
# subdomain_enum.sh - 自動化子網域枚舉腳本
DOMAIN=$1
OUTPUT_DIR="./recon/$DOMAIN"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
# 檢查參數
if [ -z "$DOMAIN" ]; then
echo "Usage: $0 <domain>"
exit 1
fi
# 建立輸出目錄
mkdir -p "$OUTPUT_DIR"
echo "[*] Starting subdomain enumeration for $DOMAIN"
# Subfinder
echo "[+] Running Subfinder..."
subfinder -d "$DOMAIN" -silent -o "$OUTPUT_DIR/subfinder.txt"
# Assetfinder
echo "[+] Running Assetfinder..."
assetfinder --subs-only "$DOMAIN" > "$OUTPUT_DIR/assetfinder.txt"
# Amass (被動模式)
echo "[+] Running Amass (passive)..."
amass enum -passive -d "$DOMAIN" -o "$OUTPUT_DIR/amass.txt" 2>/dev/null
# 證書透明度
echo "[+] Querying Certificate Transparency logs..."
curl -s "https://crt.sh/?q=%.$DOMAIN&output=json" | \
jq -r '.[].name_value' 2>/dev/null | \
sed 's/\*\.//g' | sort -u > "$OUTPUT_DIR/crtsh.txt"
# 合併結果並去重
echo "[+] Merging and deduplicating results..."
cat "$OUTPUT_DIR"/*.txt | sort -u > "$OUTPUT_DIR/all_subdomains.txt"
TOTAL=$(wc -l < "$OUTPUT_DIR/all_subdomains.txt")
echo "[*] Found $TOTAL unique subdomains"
echo "[*] Results saved to $OUTPUT_DIR/all_subdomains.txt"
|
驗證存活子網域
發現子網域後,需要驗證哪些是存活的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # 使用 httprobe 檢查 HTTP/HTTPS 服務
cat subdomains.txt | httprobe -c 50 | tee live_http.txt
# 使用 httpx(更多功能)
cat subdomains.txt | httpx -silent -status-code -title -tech-detect -o httpx_results.txt
# 使用 massdns 驗證 DNS 解析
massdns -r resolvers.txt -t A -o S subdomains.txt > resolved.txt
# 使用 nmap 進行端口掃描
nmap -iL live_subdomains.txt -p 80,443,8080,8443 -T4 -oA nmap_scan
# 篩選有效的結果
httpx -l subdomains.txt -status-code -content-length -title \
-follow-redirects -o detailed_results.txt
|
完整驗證流程
1
2
3
4
5
6
7
8
9
10
11
| # 1. DNS 解析驗證
cat all_subdomains.txt | dnsx -silent -a -resp -o dns_resolved.txt
# 2. HTTP 服務探測
cat dns_resolved.txt | cut -d' ' -f1 | httpx -silent -title -status-code -tech-detect
# 3. 截圖存活網站
cat live_http.txt | aquatone -out screenshots/
# 4. 識別潛在的接管漏洞
subjack -w subdomains.txt -t 100 -timeout 30 -ssl -c fingerprints.json -v
|
參考資料