DNS Tunneling
Data Exfil Theory
Domain Name System (DNS) is a protocol used to translate human-readable domain names into IP addresses, which are necessary for routing internet traffic. Because DNS traffic is typically allowed through firewalls, attackers can abuse it to tunnel data in and out of restricted networks. When a client requests the IP address for a domain like www.example.com, it typically contacts a recursive resolver. The resolver queries, typically via UDP port 53, a hierarchy of DNS servers in this order:
Root name server: Directs the resolver to the appropriate Top-Level Domain (TLD) name server based on the domain suffix (e.g.,
.com).Top Level Domain (TLD) name server: Refers the resolver to the authoritative name server for the specific domain.
Authoritative name server: Holds the actual DNS records and returns the requested IP address via an
Arecord.

In our scenario:
FELINEAUTHORITYis positoned in the WAN alongside our Kali host and acts as the authoritative name server for thefeline.corpzone.MULTISERVER03,CONFLUENCE01, and Kali can route to it, butPGDATABASE01andHRSHAREScannot.PGDATABASE01usesMULTISERVER03as its DNS resolver.

When PGDATABASE01 sends a DNS query for exfiltrated-data.feline.corp, it forwards the request to MULTISERVER03, which then forwards it to FELINEAUTHORITY since it’s authoritative for the zone. The dnsmasq service running on FELINEAUTHORITY responds to the query with NXDOMAIN (request failed) since it is not configured to respond for this specific URL (it does not have an A record for it).
In the above scenario, an arbitrary DNS query from an internal host with no other outbound connectivity (PGDATABASE01) has found its way to an external server we control (FELINEAUTHORITY). This makes DNS a potential covert channel for exfiltration. Data can be split into chunks, converted to a safe format like hexadecimal or Base64, and sent via the subdomain part of DNS queries (e.g.,chunk1 on chunk1.feline.corp). The authoritative server logs these and reconstructs the data.
Data Infil Theory
Conversely, arbitrary data can be infiltrated using DNS TXT records, a DNS record type designed to carry arbitrary string data. Configuring dnsmasq to serve TXT records allows a remote client to query for them using tools like nslookup, receiving back the configured string values. This can be expanded to deliver encoded files or commands into isolated networks.
This bidirectional data exchange capability via DNS underpins techniques like DNS tunneling, where tools (e.g., dnscat2) encode traffic into DNS queries and responses to bypass typical network restrictions.
dnscat2
dnscat2 is a tool for creating a covert command-and-control (C2) channel using DNS queries and responses. It can exfiltrate data by embedding it in DNS subdomain queries and receive data through DNS record types like TXT, CNAME, and MX.
A typical setup involves running a dnscat2 server on an authoritative DNS server for a chosen domain, and a dnscat2 client on a compromised host configured to query that domain. This forms a tunnel through standard DNS traffic, often overlooked by firewalls.
On the target host, the dnscat2 client binary is executed, pointing to the attacker's domain. The client and server then negotiate an encrypted session, displaying a unique authentication phrase on both ends to confirm connection integrity. This ensures no man-in-the-middle tampering has occurred during tunnel establishment.
On the server-side we can see that a new window is created.
Captured traffic shows a high volume of DNS queries and responses using CNAME, TXT, and MX record types, revealing the tunneling activity. Although encrypted, the sheer number of queries makes this technique noisy and detectable with proper monitoring tools. Once the tunnel is active, interaction with the remote client is handled through dnscat2’s command sessions.
The listen command sets up a local port on the attacker’s system, forwarding TCP traffic through the DNS tunnel to a specified remote host and port, similar to SSH's -L option.
For example, forwarding local port 4455 to port 445 (SMB) on an internal server allows the attacker to access SMB shares through the tunnel. Standard SMB commands, such as smbclient, can then connect through the forwarded port, albeit with slower response times due to the overhead of encapsulating TCP traffic inside DNS queries and responses transported over UDP.
Last updated
Was this helpful?