
我正在編寫一個腳本,需要使用使用者指定的 DNS 伺服器查詢 DNS 記錄。 DNS 伺服器可以採用任何協議,包括 UDP、TCP、DNS over HTTPS (DoH) 和 DNS over TLS (DoT)。
我知道dig
能夠處理 UDP 和 TCP 的 DNS(帶+tcp
標誌)。有沒有辦法使用dig
或其他工具來查詢 DoH 和 DoT 伺服器?
我更喜歡現有的流行工具,例如curl
這樣我的腳本將更加可移植,但也歡迎其他建議。
答案1
我沒有找到一個可以同時實現這兩個目的的工具,但我確實找到了使用它們的方法。
查詢DoH有兩種方式:
# json
curl -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=example.com&type=A' | jq .
# dns wireformat
curl -H 'accept: application/dns-message' 'https://dns.google/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB' | hexdump -c
對於 DoT,您可以使用kdig
提供的工具knot
。命令列類似於dig
:
apt-get install knot-dnsutils
# For macOS:
# brew install knot
kdig -d @8.8.8.8 +tls-ca +tls-host=dns.google.com example.com
其中8.8.8.8
是 tls 主機的預解析位址 ( dns.google.com
)。
更新:這是一個工具(https://github.com/ameshkov/dnslookup),它本身支援所有主要的 DNS 協議,並且能夠產生機器可讀的輸出。
答案2
捲曲有官方的衛生部自版本 7.62.0 起支援(問題是有多少目標端點已將curl更新到此版本。)
透過使用該選項來使用它--doh-url
。例子:
curl --doh-url https://cloudflare-dns.com/dns-query https://www.google.com
看: https://github.com/curl/curl/wiki/DOH-implementation https://daniel.haxx.se/blog/2018/09/06/doh-in-curl/
答案3
答案4
除了 DoT(正如其他用戶在這裡提到的)之外,最新版本dig
還支援使用該+https
標誌進行 DoH 查詢。
+https[=value], +nohttps
This option indicates whether to use DNS over HTTPS (DoH) when querying name
servers. When this option is in use, the port number defaults to 443. The
HTTP POST request mode is used when sending the query.
If value is specified, it will be used as the HTTP endpoint in the query URI;
the default is /dns-query. So, for example, dig @example.com +https will use
the URI https://example.com/dns-query.
例子,
dig @cloudflare-dns.com +https foobar.com
透過聲明+https
,將透過連接埠 443 上的 HTTPS 向預設端點dig
查詢提供的 DNS 伺服器網域 ( ) 。cloudflare-dns.com
/dns-query
實際上,上述命令將發送一個 DoH (POST) 請求foobar.com
:
有選項可以切換到 GET 請求、聲明不同的端點等。手冊頁與 DoH 相關的查詢選項:
+https
+https-get
+https-post
+http-plain
+http-plain-get
+http-plain-post