User Agents

Web browsers and everyday web tech, explained

DNS Tunneling: How It Works and Why It's Slow

· 5 min read · How the Web Works

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

DNS tunneling is a technique that encodes data inside DNS queries and responses, allowing information to pass through networks that restrict direct internet access. It is often used by attackers to exfiltrate data or by users to bypass captive portals. This guide explains the mechanics of DNS tunneling and why it is inherently slow, so you can understand both its capabilities and its limitations.

What You'll Need

  • A basic understanding of DNS (Domain Name System) and how domain names resolve to IP addresses.
  • Familiarity with command-line tools (like dig or nslookup) for testing DNS queries.
  • Optional: A domain name you control and a server to act as the DNS tunnel endpoint (for experimentation).

Step-by-Step

  1. Understand the DNS query structure. A DNS query typically asks for the IP address of a hostname, like example.com. The hostname can include subdomains, such as data.example.com. Each label (the part between dots) can be up to 63 characters, and the full hostname can be up to 253 characters. This space is what tunneling exploits.
  2. Encode your data into the hostname. To send data, you convert it into a format that fits in a hostname. For example, you might take a piece of text, encode it as hexadecimal or base64, and then place it as a subdomain. So hello becomes 68656c6c6f.example.com (hex) or aGVsbG8=.example.com (base64). The receiving DNS server sees this query and extracts the encoded data from the hostname.
  3. Set up a DNS server you control. You need a server that can receive DNS queries for your domain and log or process the hostnames. This can be a custom script or a tool like dnscat2 or iodine. The server will parse the incoming queries, decode the data, and respond appropriately.
  4. Send the DNS query. On the client side, you send a DNS query for the encoded hostname to your server. This is typically done using standard DNS resolution tools or a tunneling client. The query travels to your DNS server, which logs the hostname.
  5. Receive data on the server. Your server extracts the encoded data from the hostname and decodes it. This gives you the original data that was sent. For example, if the hostname was 68656c6c6f.example.com, the server decodes it to get hello.
  6. Send data back to the client. The server can also send data back to the client by encoding it in the DNS response. The response typically contains an IP address or a CNAME record, but you can put data in the response fields, such as the answer or additional sections. The client decodes this to receive the data.
  7. Maintain a session. For continuous communication, you need to keep the DNS queries flowing. Each query can carry a small amount of data (typically 50-200 bytes after overhead), so you must send many queries to transfer larger amounts of data. This is why tunneling is slow.

Common Mistakes

  • Ignoring DNS query size limits. Each label is limited to 63 characters, and the full hostname to 253. Trying to send more data in a single query will cause the query to be rejected or truncated.
  • Using characters not allowed in hostnames. Hostnames are limited to letters, digits, hyphens, and periods. Using other characters (like underscores or special symbols) will break the query.
  • Not handling DNS caching. DNS resolvers and servers cache responses. If you send the same query repeatedly, you may get a cached response instead of a fresh one, breaking the tunnel.
  • Forgetting about DNS timeouts. DNS queries have short timeouts (often 5 seconds). If your server doesn't respond quickly, the client may retry or give up.

Tips

  • Use a dedicated domain. Avoid using a domain that is actively used for other purposes, as the DNS traffic may be monitored or filtered.
  • Compress data before encoding. Compressing your data reduces the number of DNS queries needed, speeding up the transfer.
  • Use a tool designed for tunneling. Tools like iodine or dnscat2 handle the encoding, session management, and error correction for you, making the process more reliable.
  • Test with small data first. Start with a few bytes to ensure the tunnel works before sending larger payloads.

Troubleshooting

  • Queries are not reaching your server. Check that your domain's NS records point to your server and that your server is listening on port 53 (UDP and TCP). Use dig yourdomain.com to see if the query resolves.
  • Data appears garbled. Ensure you are using the same encoding on both ends. Also check for case sensitivity (hostnames are case-insensitive, but your encoding might not be).
  • Transfer is extremely slow. This is normal. DNS tunneling is slow by design. But if it's slower than expected, check for network latency and DNS caching. You can try increasing the query rate or using a closer DNS server.
  • Queries are being blocked. Some networks block DNS queries to non-standard servers. Try using a well-known DNS server (like 8.8.8.8) as a relay, or use a different port (though port 53 is standard).

Frequently Asked Questions

  • Why is DNS tunneling so slow? Because each DNS query can only carry a small amount of data (typically 50-200 bytes), and you need to send many queries to transfer larger amounts. Additionally, DNS queries are subject to network latency, and the protocol is not designed for high-throughput data transfer.
  • Is DNS tunneling legal? It depends on your jurisdiction and intent. Using it to bypass network restrictions may violate terms of service or laws. It is often associated with malicious activity, so it's important to use it only for legitimate purposes, such as testing your own network's security.
  • Can DNS tunneling be detected? Yes. Network administrators can detect unusual DNS traffic patterns, such as high volumes of queries to a single domain or queries with long, random-looking subdomains. There are also security tools that inspect DNS payloads for encoded data.
  • What is the maximum data rate of DNS tunneling? It varies, but typically you can expect a few kilobytes per second at best. The rate depends on network latency, the number of queries you can send per second, and the size of each query.
  • Do I need a domain to use DNS tunneling? Yes, you need a domain you control, because you need to direct DNS queries to your server. You can use a free subdomain service, but it's less reliable.
  • Can I use DNS tunneling to browse the internet? Technically yes, but it would be extremely slow and impractical for most web browsing. It's better suited for sending small amounts of data, like command-and-control traffic.