1 Userspace DNS tunnel with support for DoH and DoT
2 David Fifield <david@bamsoftware.com>
5 dnstt is a DNS tunnel with these features:
6 * Works over DNS over HTTPS (DoH) and DNS over TLS (DoT) as well as
8 * Embeds a sequencing and session protocol (KCP/smux), which means that
9 the client does not have to wait for a response before sending more
10 data, and any lost packets are automatically retransmitted.
11 * Encrypts the contents of the tunnel and authenticates the server by
14 It has these noteworthy limitations:
15 * Requires intermediary resolvers to support large responses (1232 bytes,
16 which is more than the mandated minimum of 512 bytes).
18 dnstt is an application-layer tunnel that runs in userspace. It doesn't
19 provide a TUN/TAP interface; it only hooks up a local TCP port with a
20 remote TCP port (like netcat or `ssh -L`) by way of a DNS resolver. It
21 does not itself provide a SOCKS or HTTP proxy interface, but you can get
22 the same effect by running a proxy on the tunnel server and having the
23 tunnel terminate at the proxy.
26 .------. .--------. .------.
27 |tunnel|-- DoH / DoT --|resolver|-- UDP DNS --|tunnel|
28 |client| '--------' |server|
35 Because the server side of the tunnel acts like an authoritative name
36 server, you need to own a domain name and set up a subdomain for the
37 tunnel. Let's say your domain name is example.com and your server's IP
38 addresses are 203.0.113.2 and 2001:db8::2. Go to your name registrar and
39 add three new records:
42 A tns.example.com points to 203.0.113.2
43 AAAA tns.example.com points to 2001:db8::2
44 NS t.example.com is managed by tns.example.com
47 The labels `tns` and `t` can be anything you want, but the `tns` label
48 should not be a subdomain of the `t` label (that space is reserved for
49 the contents of the tunnel), and the `t` label should be short (because
50 there is limited space available in a DNS message, and the domain name
51 takes up part of that space).
53 Now, when a recursive DNS resolver receives a query for a name like
54 aaaa.t.example.com, it will forward the query to the tunnel server at
55 203.0.113.2 or 2001:db8::2.
58 ## Tunnel server setup
66 First you need to generate the server keypair that will be used to
67 authenticate the server and encrypt the tunnel.
69 $ ./dnstt-server -gen-key -privkey-file server.key -pubkey-file server.pub
70 privkey written to server.key
71 pubkey written to server.pub
74 Run the server. You need to provide an address that will listen for UDP
75 DNS packets (`:5300`), the private key file (`server.key`), the root of
76 the DNS zone (`t.example.com`), and a TCP address to which incoming
77 tunnel stream will be forwarded (`127.0.0.1:8000`).
79 $ ./dnstt-server -udp :5300 -privkey-file server.key t.example.com 127.0.0.1:8000
82 The tunnel server needs to be able to receive packets on an external
83 port 53. You can have it listen on port 53 directly using `-udp :53`,
84 but that requires the program to run as root. It is better to run the
85 program as an ordinary user and have it listen on an unprivileged port
86 (`:5300` above), and port-forward port 53 to it. On Linux, use this
87 command to forward external port 53 to localhost port 5300:
89 # iptables -I INPUT -p udp --dport 5300 -j ACCEPT
90 # iptables -t nat -I PREROUTING -i eth0 -p udp --dport 53 -j REDIRECT --to-ports 5300
91 # ip6tables -I INPUT -p udp --dport 5300 -j ACCEPT
92 # ip6tables -t nat -I PREROUTING -i eth0 -p udp --dport 53 -j REDIRECT --to-ports 5300
95 You need to also run something for the tunnel server to connect to. It
96 can be a proxy server or anything else. For testing, you can use an
99 $ ncat -lkv 127.0.0.1 8000
103 ## Tunnel client setup
111 Copy the server.pub file from the server to the client. You don't need
112 server.key on the client; leave it on the server.
114 Choose a public DoH or DoT resolver. There is a list of DoH resolvers
116 * https://github.com/curl/curl/wiki/DNS-over-HTTPS#publicly-available-servers
118 And DoT resolvers here:
119 * https://dnsprivacy.org/wiki/display/DP/DNS+Privacy+Public+Resolvers#DNSPrivacyPublicResolvers-DNS-over-TLS%28DoT%29
120 * https://dnsencryption.info/imc19-doe.html
122 To run the tunnel client using DoH, you need to provide the URL of the
123 DoH resolver (`https://doh.example/dns-query`), the server's public key
124 files (`server.pub`), the root of the DNS zone (`t.example.com`), and
125 the local TCP port that will receive connections and forward them
126 through the tunnel (`127.0.0.1:7000`):
128 $ ./dnstt-client -doh https://doh.example/dns-query -pubkey-file server.pub t.example.com 127.0.0.1:7000
131 For DoT, it's the same, but use the `-dot` option instead:
133 $ ./dnstt-client -dot dot.example:853 -pubkey-file server.pub t.example.com 127.0.0.1:7000
136 Once the tunnel client is running, you can connect to the local end of
137 the tunnel, type something, and see it appear at the remote end.
139 $ ncat -v 127.0.0.1 7000
142 The client also has a plaintext UDP mode that can work through a
143 recursive resolver or directly to the tunnel server
144 (`-udp tns.example.com`), but it does not provide any covertness for the
145 tunnel and should only be used for testing.
148 ## How to make a proxy
150 You can make the tunnel into a general-purpose proxy by running a proxy
151 server and connecting the server end of the tunnel to it. For example,
152 Ncat has a built-in simple HTTP server:
154 $ ncat -lkv --proxy-type http 127.0.0.1 8000
155 $ ./dnstt-server -udp :5300 -privkey-file server.key t.example.com 127.0.0.1:8000
158 On the client, have the tunnel client listen on 127.0.0.1:7000, and configure
159 your applications to use http://127.0.0.1:7000/ as an HTTP proxy.
162 $ ./dnstt-client -doh https://doh.example/dns-query -pubkey-file server.pub t.example.com 127.0.0.1:7000
163 $ curl -x http://127.0.0.1:7000/ http://example.com/
169 Support for DoH and DoT is only to make it more difficult for a local
170 observer to see that a DNS tunnel is being used, not for the overall
171 security of the connection. There is a separate encryption layer inside
172 the tunnel that protects the contents of the tunnel from the resolver
175 The encryption of DoH or DoT prevents a network observer between the
176 tunnel client and the resolver from seeing the remote destination of the
177 tunnel. An observer can see that the tunnel client is connecting to a
178 resolver, but cannot see where the resolver is forwarding its queries.
179 An observer can probably infer, based on volume and other traffic
180 characteristics, that a tunnel is being used, though it cannot tell
181 where the remote end of the tunnel is, nor what the contents of the
182 tunnel are. If the tunnel client is not using DoH or DoT but instead UDP
183 (`-udp` option), then even an observer between the tunnel client and the
184 resolver can see that a tunnel is being used and where the remote end of
187 An observer between the resolver and the tunnel server (this includes
188 the resolver itself) can easily tell that a tunnel is being used and
189 where the remote end of the tunnel is, because there is no DoH or DoT
190 encryption at that point. This kind of observer still cannot read the
191 contents of the tunnel, because there is an additional layer of
192 end-to-end encryption between the tunnel client and the tunnel server.
194 An observer who watches what leaves the tunnel server will be able to
195 see anything that the tunnel server forwards to some other host (if the
196 tunnel server is acting as a proxy, for example), unless that data has
197 been separately encrypted before being sent through the tunnel.
200 ## Encryption and authentication
202 The tunnel uses a Noise protocol (https://noiseprotocol.org/noise.html)
203 for end-to-end security between the tunnel client and tunnel server.
204 This protocol is independent of the DoH or DoT encryption between the
205 tunnel client and resolver. The specific protocol is Noise_NK_25519_ChaChaPoly_BLAKE2s
206 (https://noiseprotocol.org/noise.html#protocol-names-and-modifiers).
207 The NK handshake pattern authenticates the server but not the client.
209 The Noise layer is sandwiched between two other protocol layers: KCP
210 (https://github.com/xtaci/kcp-go) which creates a reliable stream on top
211 of unreliable datagrams, and smux (https://github.com/xtaci/smux) which
212 provides stream multiplexing and session features. An observer (such as
213 the intermediary resolver) may read the headers of the KCP layer, but not
214 of the smux layer nor of the streams that are inside. The model is
215 similar to what you would get with TLS or SSH over TCP: an observer can
216 see TCP-level ACKs and sequence numbers, but cannot read the stream data
228 When you run `dnstt-server -gen-key`, you can save the private and
229 public keys to a file using the `-privkey-file` and `-pubkey-file`
230 options. You can then load the keys later using `-privkey-file` on the
231 server and `-pubkey-file` on the client. Alternatively, you can deal
232 with the keys as literal hexadecimal strings rather than files. If you
233 run `dnstt-server -gen-key` without the `-privkey-file` and
234 `-pubkey-file` options, it will display the keys rather than save them
235 to files. You can then use the keys with `-privkey` on the server and
236 `-pubkey` on the client.
238 $ ./dnstt-server -gen-key
239 privkey 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
240 pubkey 0000111122223333444455556666777788889999aaaabbbbccccddddeeeeffff
241 $ ./dnstt-server -udp :5300 -privkey 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef t.example.com 127.0.0.1:8000
242 $ ./dnstt-client -dot dot.example:853 -pubkey 0000111122223333444455556666777788889999aaaabbbbccccddddeeeeffff t.example.com 127.0.0.1:7000
244 If you run the server without `-privkey-file` or `-privkey`, it will
245 generate a temporary keypair and print the public key in the log. But
246 the key will be different the next time you restart the server, and you
247 will have to reconfigure clients.
252 In the client, the available space for user data per query depends on
253 the length of the domain name in use. Shorter domain names leave more
256 In the server, the available space for user data per response depends on
257 the maximum UDP payload size. The larger the UDP payload size, the more
258 space there is for user data. You want to use as large a UDP payload
259 size as possible, but not larger than what is supported by the resolver
260 you are using. Values above 1452 may cause IP fragmentation which can
261 reduce performance. You can control the maximum UDP payload size with
262 the `-mtu` option. The default is 1232 bytes; this ought to be supported
263 by most resolvers that understand EDNS(0) (RFC 6891). For maximum
264 compatibility, set the maximum to 512, but know that doing so will
265 reduce downstream bandwidth.
267 $ ./dnstt-client -mtu 512 -doh https://doh.example/dns-query -pubkey-file server.pub t.example.com 127.0.0.1:7000
270 The client and server emit an "effective MTU" log line when starting up
271 that shows how much space is available for user data in each query or
272 response. For the server, there may be more space available in some
273 responses and less in others (depending on the size of the corresponding
274 query); the logged value is the minimum that is guaranteed to be
275 supported in any response.