Using curl to Fetch DNS Results
Introduces two ways to use the curl command to obtain DNS query results.
Categories:
This article presents two methods to retrieve DNS query results using curl:
- DNS JSON format
- DNS Wire Format
1. DNS JSON Format Queries
Returns DNS responses in JSON, making them easy to parse.
curl -H 'accept: application/dns-json' "https://dns.google/resolve?name=baidu.com&type=A" | jq .
Cloudflare
curl -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=baidu.com&type=A' | jq .
Aliyun
curl -H "accept: application/dns-json" "https://223.5.5.5/resolve?name=baidu.com&type=1" | jq .
dns.pub
curl -H 'accept: application/dns-json' 'https://doh.dns.pub/dns-query?name=baidu.com&type=A' | jq .
AdGuard Private DNS
# Currently unsupported
2. DNS Wire Format Queries
Returns binary DNS responses that require further parsing.
curl -H 'accept: application/dns-message' 'https://dns.google/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB' | hexdump -c
Cloudflare
curl -H 'accept: application/dns-message' 'https://cloudflare-dns.com/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB' | hexdump -c
Aliyun
curl -H 'accept: application/dns-message' "https://dns.alidns.com/dns-query?dns=P8QBAAABAAAAAAAABWJhaWR1A2NvbQAAAQAB" | hexdump -c
dns.pub
curl -H 'accept: application/dns-message' 'https://doh.dns.pub/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB' | hexdump -c
AdGuard Private DNS
curl -H 'accept: application/dns-message' 'https://public0.adguardprivate.com/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB' | hexdump -c
Parsing DNS Responses with Python
# pip install dnspython
# pip install requests
# Parsing JSON responses
import json
import requests
def query_dns_json(domain="example.com", type="A"):
"""Query DNS using JSON format"""
url = "https://dns.google/resolve"
params = {"name": domain, "type": type}
headers = {"accept": "application/dns-json"}
response = requests.get(url, params=params, headers=headers)
return json.dumps(response.json(), indent=2)
# Parsing Wire Format responses
def query_dns_wire(domain="example.com"):
"""Query DNS using Wire Format"""
import dns.message
import requests
import base64
# Create DNS query message
query = dns.message.make_query(domain, 'A')
wire_format = query.to_wire()
dns_query = base64.b64encode(wire_format).decode('utf-8')
# Send request
url = "https://dns.google/dns-query"
params = {"dns": dns_query}
headers = {"accept": "application/dns-message"}
response = requests.get(url, params=params, headers=headers)
dns_response = dns.message.from_wire(response.content)
return str(dns_response)
if __name__ == "__main__":
print("JSON query result:")
print(query_dns_json())
print("\nWire Format query result:")
print(query_dns_wire())
Generating Base64-Encoded DNS Wire Format Data
# pip install dnspython
import base64
import dns.message
import dns.rdatatype
# Create a DNS query message
query = dns.message.make_query('example.com', dns.rdatatype.A)
# Convert message to Wire Format
wire_format = query.to_wire()
# Encode to base64
wire_format_base64 = base64.b64encode(wire_format).decode('utf-8')
# Print
print(wire_format_base64)