Chatting Over DNS

Glyphack ·

16 min read Original article ↗

It’s always fun to see how a tool can be used to do something that is not its primary purpose. Earlier this year I was playing around with the idea of developing an app that uses DNS for transferring data. I know what you’re thinking, “Isn’t DNS for figuring out the IP address of a domain?” Yeah, exactly that.

DNS is one of the parts of the internet that works even in the most restricted networks. That’s why there are many VPNs that use DNS for transferring data. Usually firewalls that block internet access allow DNS queries but then block the connection when you try to connect to an external IP address.

But the downside of this approach is that it’s really slow. DNS messages are capped at 512 bytes. Normal web apps are not designed for this constraint; each request contains a few kilobytes of data. So one HTTP request is split into multiple DNS messages, and requires multiple round trips to complete one action on the app. Plus, the firewall might detect malicious DNS activity and drop your messages.

Here’s a comparison of sending {"to":"bob","content":"hi"} to a web app over HTTPS through a DNS tunnel, versus sending it with Igloo:

Chatting_Over_DNS-igloo-vs-dns-tunneling.svg

Most of this extra overhead is for TLS, HTTP headers, application authentication, and cookies.

My goal was to make an app for communication under such constraints1. Igloo has basic features of sending messages, files, and voice, plus encryption and authentication. All over DNS!

In the rest of this post I’ll walk you through how it works.

If you are not familiar with how DNS works, check out this guide from Julia Evans.

The application uses a client-server architecture with clients on Windows, Mac, Linux, and Android.

Finding the Server #

We first need to make a connection to the server from our client. One solution is to use the IP address. This way the client can make the DNS query directly to the server. For example:

dig @[Your IP Address] example.com

IP addresses are not something that you can rely on all the time. Also, if the user is in a restricted environment, the firewall will block connection to external IP addresses. So even the DNS message cannot reach us. In this case the user can only read the internal DNS server. So we need to use the internal DNS server, which user has access to, and send the message to our own server. With a DNS server you can usually ask it to reach to other DNS servers to answer a query. This happens when you query it for a name that it does not know about.

The solution that other DNS tunneling apps use is to become the authoritative name server for a domain. When you buy a domain name, you define where the DNS server that knows the IP address of your domain is. The name server becomes authoritative for that domain. Every other DNS server has to ask your server for this domain. So any request asking for your domain ends up at your server.

In this method the user makes a request to the internal DNS. When the internal DNS does not know the address, it has to reach out to other DNS servers and ultimately reaches our server. This is how the user can reach our server without having the direct IP address.

Here’s an example: you own glyphack.com, and in the domain settings you enter your server IP address as the name server. Now a user queries glyphack.com from a public DNS server. If this domain is new, the resolver does not know about it, so it queries other DNS servers. This continues until one DNS server knows your name servers, and it sends the query to your name server. And in this case the name server is our own server. So we get all the DNS requests for queries that end with glyphack.com, like example.glyphack.com.

How the user reaches our server through DNS

Sending Data #

Now that we can reach the server, the next challenge is how we encode the data as the DNS request.

In a DNS request you query for a name. For example, in msg.glyphack.com we have one name and three labels: glyphack, com, and msg. The first two labels are the domain name and cannot be used to store custom data, but extra labels can be used. For example, part1.part2.glyphack.com is still a valid query.

We cannot simply put the binary data in the label data because the name section only allows for letters and numbers. One solution for converting binary into characters is base64, but it uses characters like + and / that are not allowed in DNS. Also, there’s no difference between the characters A and a in the label, and you cannot simply rely on the fact that the bytes arrive exactly as is since a resolver can also flip the case of characters2. The encoding that is suitable is either base32 or base36. Since base36 has a few more characters and takes a bit less space than base32, I chose it over base32.

We also need to convert our program data structures into binary. I used bincode for this.

Let’s see an example of a send-message request in the app:

02 Request type for sending a message

03 62 6f 62 Recipient name (length prefixed)

02 68 69 Message content (length prefixed)

Calculating the base36, we have 13oe9bapr621. Then the client sends this request as 13oe9bapr621.glyphack.com.

What’s the size limit in this approach?

Each label is limited to 63 bytes of data. We’re not limited to only one label; however, in total the name should be below 255 bytes.

To calculate how much space we have, we need to take these into account:

  • Each label has a length byte in front.
  • The domain itself is 12 characters (14 bytes).

63 + 1 + 63 + 1 + 63 + 1 + 48 + 1 + 14

Excluding the length prefixes and domain, we have:

63 + 63 + 63 + 48 = 237

And base36 uses ~ 1.55 characters per byte:

237 / 1.55 = 153

That’s how much information we can send in each request.

You see how buying a shorter domain name can leave more space for the application bytes. But this is still big enough to have a functional and secure application.

Receiving Data #

Other DNS tunneling programs place the response into the answer section of the DNS response. This part is normally filled with IP address or TXT records. We can use the answer section to include our custom bytes.

  • In A and AAAA records we can directly write bytes.
  • TXT contains text. So we have to convert the bytes into characters, but with fewer constraints than in the request.

Imagine a request where the client sends information by looking up AAAA records for 13oe9bapr621.glyphack.com. The server here responds with:

13oe9bapr621.glyphack.com. 300 IN AAAA 0:1301:11a5:2748:99ad:38ab:3f70:e77e
13oe9bapr621.glyphack.com. 300 IN AAAA 188:5711:a5df:1400::

As you can see, the full response can be split between multiple AAAA records.

The response size is only limited to the 512 bytes. That is the maximum DNS response size.

Application Request Response Life Cycle #

The application defines request and response types, and works with these types.

The DNS server handles unwrapping the application-defined request data from the DNS request format, and wrapping the responses in the DNS response format.

For example, these are the types for sending a message:

#[derive(Encode, Decode)]
pub enum RequestBody {
    SendTextMessage(SendTextMessageRequest),
    // Rest of the request types ...
}


#[derive(Encode, Decode)]
pub enum Response {
    SendTextMessage(SendMessageResponse),
    // Rest of the types ...
}

#[derive(Encode, Decode)]
pub struct SendTextMessageRequest {
    pub to: UserName,
    pub content: String,
}

#[derive(Encode, Decode)]
pub struct SendMessageResponse {}

pub type UserName = String;

I’m using the hickory DNS library in both client and server to work with DNS request/response. I wanted to keep the final application small, and using a library didn’t sound like a good choice. But the hickory crate is split in such a good way that you can grab only the parts you need. In this case I’m only using the message creation and parsing code.

I structured the code in a way that the application logic can mostly ignore the decoding and encoding of the request. This is not universal since the application has to be mindful of the size limit, but other than that it just works on request/response types.

The following code shows the flow from receiving the request to creating the response:

fn handle(dns_message: &Message) -> Option<Message> {
    // The question, for example `13oe9bapr621.glyphack.com`.
    let name = dns_message.queries.first()?.name();
    let data_labels = name.num_labels().checked_sub(2)?;
    let mut encoded = String::new();
    for label in name.iter().take(data_labels as usize) {
        encoded.push_str(std::str::from_utf8(label).ok()?);
    }

    // base36 text -> bytes -> request.
    let bytes = base36_decode(&encoded).ok()?;
    let (body, _) =
        bincode::decode_from_slice::<RequestBody, _>(&bytes, config::standard()).ok()?;

    let response = match body {
        RequestBody::SendTextMessage(msg) => {
            println!("message for {}: {}", msg.to, msg.content);
            Response::SendTextMessage(SendMessageResponse {})
        }
        _ => return None,
    };

    // Response -> bytes -> base36 text in a TXT record.
    let bytes = encode_to_binary(response);
    let b36_bytes = vec![base36_encode(&bytes)];
    let mut reply = Message::response(dns_message.metadata.id, dns_message.metadata.op_code);
    reply.metadata.authoritative = true;
    reply.metadata.response_code = ResponseCode::NoError;
    reply.add_answer(Record::from_rdata(
        name.clone(),
        300,
        RData::TXT(rdata::TXT::new(b36_bytes)),
    ));
    Some(reply)
}

Security with Noise #

Up until this point we can use DNS to carry requests and responses. But anyone can see the communication. An attacker can corrupt or alter messages.

The same problem exists in the browsers, and the solution is TLS. In communication protocols like this we can use the Noise Protocol. This is what dnstt does.

You can use Noise to implement:

  • Key exchange between the client and server to establish a connection.
  • Integrity check for a message to detect request tampering.
  • Message encryption.
  • Detection of replay attacks. Someone on the network cannot resend an already processed message and change something on the server.
  • Server verification. The client checks the identity of the server when connecting. So a third party cannot impersonate the actual server.

I used the package mcginty/snow, which implements this protocol.

The request that holds the Noise payload is defined as:

pub struct Request {
    /// 0 = handshake (payload is the 32-byte Noise `e`).
    /// non-zero = transport message (payload is the AEAD ciphertext).
    pub session_id: SessionId,
    pub nonce: u64,
    pub payload: Vec<u8>,
}

All clients start with a session ID and nonce set to 0. This helps with distinguishing handshake from non-handshake requests since the code for handling them is different. Once the key is exchanged, the client receives a session ID. This will be used later for decrypting messages and authenticating the user.

During the handshake, the client and server exchange ephemeral keys that are used in the rest of the communication to encrypt data. In contrast to using a static key for messages, this method gives forward secrecy.

I chose the NK handshake pattern because the client has pre-knowledge of the server’s static public key. The server’s public key is pinned in the client binary.

When the client and server are done with the handshake, the server stores the derived keys along with a non-constant session ID for the client. The session ID is something that the client has to send in every request. We use this to detect who the user is and to decrypt the message.

There’s a small catch with replay attacks. It is common for messages to be lost between the client and the server. From this section I understood that the client can send the nonce along with the request so the server can update its internal counter and keep processing messages. I implemented this, but while testing I noticed another kind of failure. Sometimes the response is lost when it is sent back from the server to the client. In this case the client retries, and two things can happen:

  • Some intermediate DNS resolver has the response in cache and returns it.
  • The same request gets to the server again with a nonce that was already used.

The first case is fine, but in the second case I added caching to the server for the last few messages of each session. So if a client retries the last message, the server does not process it. Instead, it returns the response from cache. I can get rid of this cache, but for that I have to make endpoints idempotent. My preference is to remove messages once the user reads them. In this case I cannot call this endpoint twice safely.

DNS request size is already limited, so we have to be cautious of the size. The extra information added to each message for security is 27 bytes in each request and 18 bytes in each response. But luckily we still have 126 more bytes in each request to implement application features.

Igloo stack

Authentication #

For authentication, applications usually use some token like JWT. But whatever we do, we cannot fit another ~150 bytes in DNS messages.

What we need is a way to know the user is who he says he is. And we already have a session ID that each connection gets after the handshake. It is a nice strategy to save space by using the ID to decrypt the message and authenticate the user. If someone tries to impersonate another user, sending the user session ID is not enough. He also needs the ephemeral secret key to encrypt the message. Otherwise, the server fails during decryption of the message.

So this is the authentication process: the server keeps the sessions that are not authenticated in a separate list. Once the client authenticates with username and password, the server moves that session ID to a new list where authenticated sessions live along with the user identity. From that point on, the server can determine the username associated with any message that comes in by looking up the session ID in the list.

Keeping unauthenticated sessions in a separate list is a good idea because we can clean them up shortly if they don’t authenticate, and keep resources free for other users to connect.

This is interesting because it shows how having control over the full stack makes it possible to simplify things. The authentication is happening at different levels multiple times (what’s the encryption key for this connection, who’s the user, etc.). There might be one answer to all of them, but the layers of abstraction hide it.

Sending Files and Voice #

After all of this we get a secure working chat app. But the DNS payload is very small. What are the limits? Well, you can send a short message to anyone. But for anything bigger, the user has to send sentences one by one.

Also, we cannot send files in this way. A normal file is at least a few kilobytes. If we find a way to transfer files, then the user can send a long message as a file.

We don’t need to fit a large payload into one DNS message. We can send multiple DNS messages that contain the information for one application message instead. This way we can even have file and voice messages in the application.

The client asks the server to allocate a buffer with the size of the file for it to write to. The server returns an ID, and the client uses that to upload the large message chunk by chunk.

For files, zstd compression works well. The client compresses the file before sending and decompresses after receiving the file. The server does not do anything other than taking the file from one user and sending it to another one.

Each file stays on the server. When the recipient client is online, it receives a message with a file ID. The client starts the download by sending the file ID to the server, then receives the file data part by part.

Here the client includes the offset in each request so the server does not need to store the state of the download. So if a message is lost, the client can request it again.

(There’s a strange-looking process where the server has to receive the file and decrypt it to store it, then re-encrypt it with the final user’s key so the client can open the file after the download.)

Voice works like files, except that text compression does not work well on it. And one second of voice is about 8 KB. The solution here is to use something like Opus encoding. It can drop the size to ~ 1 KB/sec, which makes voice usable on slow connections. I didn’t do this in the end because I didn’t have the time to figure out compiling the library on different platforms and making sure it works.

Finding DNS servers #

As I said one of the uses of this chat application is when access to the internet is behind a firewall and you cannot communicate with others. In that case, you probably cannot use DNS servers like 1.1.1.1 or 8.8.8.8 because they might be blocked in your network.

First option you have is the DNS server of the ISP. This is what you use normally if you don’t set any servers. Sometimes your ISP DNS server might block your traffic or even not have access to internet as well.

The second option is to find all the DNS servers in the network using masscan. This tool can find anything that responds on the DNS UPD port. Once you gather them you can try them one by one by trying to send a handshake message to Igloo server.

For this case the Igloo client application has a list of DNS servers. It probes them to see which one can reach Igloo, and uses the one that works.


What’s next? #

As you can see, even with the limited space on DNS, a lot of stuff can be done.

I was in a rush when building this application because I wanted to use it with a few people. So naturally I skipped some features I didn’t need myself.

These are cool things that I want to see happening:

  • End-to-end encryption between the clients so the server cannot see the message.
  • Group chats.
  • Keeping the message history on the client side in a safe way.
  • Parallel routing of DNS requests to increase bandwidth.
  • Opus codec (this binding looks good) for voice encoding/decoding.
  • In-app updates so the user can keep using the app through DNS.

I hope you never get stuck in a place where you cannot communicate with others. But if you do use Igloo to find a way out collectively.


Thanks to Kevin for reading drafts of this post.