Tunneling Through Port 53: Why Port-Based Firewalls Never Worked

The WireGuard post got me thinking about something from my travelling days. A network engineer used to carry a console cable everywhere, because you never knew when a box on a customer site would need talking to in person. That meant a lot of hotels, and a lot of hotel Wi-Fi sitting behind a paywall. You joined the network, a browser popped up, and a card form wanted money for the privilege of reading your email.

Somewhere in the engineering community it became common practice to notice that these networks let DNS through before you had paid. If you had a server somewhere on the internet listening on port 53, in my case a small EC2 instance and at one point a Raspberry Pi with a public address, a VPN to that server went straight out. I think it was OpenVPN back then, but the principle works with any VPN. I will leave it to you to guess whether I ever did this myself.

The story is a nice excuse for a lesson that still matters. It was never really about hotels. It is about what a firewall rule says, what the person who wrote it meant, and the gap between the two.

Why it worked

A captive portal has a problem to solve. An unauthenticated guest has to be able to reach the login page, and to do that their device needs to resolve a name. So the portal allows DNS for everyone, and the simplest way to write that rule is:

allow udp/53 and tcp/53, from guests, to anywhere

That rule says nothing about DNS. It says port 53. The firewall never checks that what crosses the port is a DNS query. It trusts the port number as a label, and a label is something the sender gets to choose.

There are two variants of the trick, and it matters which one you are facing.

Variant 1: VPN on port 53Variant 2: real DNS tunneling
What goes on the wireVPN datagrams on UDP/53Data encoded inside genuine DNS queries and responses
NeedsRaw UDP/53 to an arbitrary hostOnly a working recursive resolver
SpeedFast enough for email and browsingSlow
Stopped byForcing guests to the local resolverInspecting the DNS itself

Most paywalls had the sloppy rule above, so variant 1 worked. Variant 2 is what you need when the network forces all DNS through its own resolver. I covered the tools for that, iodine and dnscat2, in DNS Tunneling and Covert C2. This post is about variant 1, and about the moment variant 1 stops working.

The lab

I rebuilt the whole scenario on my Debian 13 test VM, using WireGuard instead of OpenVPN because it is simpler to read and I wrote about it last time. The principle is identical. Everything below is real output from that lab.

Three roles on one machine:

RoleWhereAddress
Guest laptopnetwork namespace guest10.50.0.2/24
Hotel gatewaythe VM itself, forwarding and running nftables10.50.0.1 and 203.0.113.1
My server in the cloudnetwork namespace cloud203.0.113.10/24

203.0.113.0/24 is a documentation range, which keeps the lab off any real address.

sudo ip netns add guest
sudo ip netns add cloud

sudo ip link add vg type veth peer name g0 netns guest
sudo ip addr add 10.50.0.1/24 dev vg && sudo ip link set vg up
sudo ip -n guest addr add 10.50.0.2/24 dev g0
sudo ip -n guest link set g0 up
sudo ip -n guest route add default via 10.50.0.1

sudo ip link add vc type veth peer name c0 netns cloud
sudo ip addr add 203.0.113.1/24 dev vc && sudo ip link set vc up
sudo ip -n cloud addr add 203.0.113.10/24 dev c0
sudo ip -n cloud link set c0 up
sudo ip -n cloud route add default via 203.0.113.1

sudo sysctl -w net.ipv4.ip_forward=1

The paywall

This ruleset is the “unpaid guest” state. Everything is dropped except DNS, and the packets that do get out are NATed like any hotel network would do:

table inet hotel {
  chain forward {
    type filter hook forward priority 0; policy drop;
    ct state established,related accept
    iifname "vg" udp dport 53 accept
    iifname "vg" tcp dport 53 accept
    counter comment "everything else dropped"
  }
}
table ip hotelnat {
  chain post {
    type nat hook postrouting priority 100;
    oifname "vc" masquerade
  }
}

First, what the guest can and cannot do:

ping to the cloud server:      100% packet loss
tcp/443 to the cloud server:   times out
tcp/53 to the cloud server:    reaches the host

The third line returned “Connection refused”, which sounds like a failure but means the host answered. Nothing was listening yet, and the packet got through the firewall. That is the whole weakness in one line.

The tunnel

The server end is WireGuard in the cloud namespace and the client end is WireGuard in the guest namespace, with the keys generated as in the previous post. The only thing that changes between the failing and working runs is the port:

# server, in the "cloud" namespace
sudo ip -n cloud link add wg0 type wireguard
sudo ip -n cloud addr add 10.99.0.1/24 dev wg0
sudo ip netns exec cloud wg set wg0 listen-port 53 private-key srv.key \
  peer $(cat cli.pub) allowed-ips 10.99.0.2/32
sudo ip -n cloud link set wg0 up

# client, in the "guest" namespace
sudo ip -n guest link add wg0 type wireguard
sudo ip -n guest addr add 10.99.0.2/24 dev wg0
sudo ip netns exec guest wg set wg0 private-key cli.key \
  peer $(cat srv.pub) endpoint 203.0.113.10:53 \
  allowed-ips 10.99.0.0/24 persistent-keepalive 1
sudo ip -n guest link set wg0 up

With the default port 51820 the handshake never completes:

$ sudo ip netns exec guest ping -c3 -W2 10.99.0.1
3 packets transmitted, 0 received, 100% packet loss

$ sudo ip netns exec guest wg show wg0
  endpoint: 203.0.113.10:51820
  transfer: 0 B received, 148 B sent

148 bytes is the WireGuard handshake initiation, sent and never answered. The drop counter in the ruleset is counting those packets.

Change the port to 53 and the picture is different:

$ sudo ip netns exec guest ping -c3 -W2 10.99.0.1
3 packets transmitted, 3 received, 0% packet loss

$ sudo ip netns exec guest wg show wg0
  endpoint: 203.0.113.10:53
  latest handshake: 2 seconds ago

The tunnel is up. To prove it carries real traffic, I started a web server on the tunnel address in the cloud namespace on TCP 8443, a port the firewall would normally block:

via the tunnel:         HTTP 200
directly, no tunnel:    blocked (timeout)

The firewall’s own counters tell the same story. With the drop counter reset, five pings and a curl through the tunnel dropped 0 packets. A single direct attempt at TCP 8443 dropped 4 packets, 240 bytes. The paywall blocks the service and passes the tunnel carrying it, because the tunnel’s outer packets look like the one thing it was told to allow.

What the firewall could have seen

A stateful port-based firewall sees UDP, destination port 53, and moves on. A device that looks at the payload sees something different. This is a capture on the guest side while the tunnel carried traffic:

10.50.0.2.36666 > 203.0.113.10.53: 1024 [35535a] [7858q] [1792n] Type0 (Class 184)? . [|domain]
10.50.0.2.36666 > 203.0.113.10.53: 1024 [35535a] [7858q] [2048n] Type0 (Class 195)? . Type22830 (Class 19510)? <BAD PTR> [|domain]
203.0.113.10.53 > 10.50.0.2.36666: 1024 [15507a] [10083q] [768n] Type0 (Class 143)? . Type44850 (Class 27492)? . [|domain]

tcpdump tries to decode port 53 as DNS and produces nonsense: record counts in the thousands, types and classes that do not exist, <BAD PTR> and a decoder that gives up with [|domain]. Real DNS never looks like this. The sizes are familiar too. A 156 byte IP packet is a 128 byte UDP payload, the same WireGuard data packet size as in the previous post, and the 60 byte packets are 32 byte keepalives.

Anything doing even basic protocol validation on port 53 would have flagged this on the first packet.

Forcing DNS through the hotel’s own resolver

The proper fix for variant 1 is to stop treating “port 53” as “may talk to anyone” and make it mean “may talk to our resolver”. In nftables that is a redirect, with dnsmasq standing in for the hotel’s resolver on the gateway:

table ip hotelnat {
  chain pre {
    type nat hook prerouting priority -100;
    iifname "vg" udp dport 53 counter redirect to :53
    iifname "vg" tcp dport 53 counter redirect to :53
  }
  ...
}

Now a DNS query the guest addresses to the cloud server is answered by the hotel instead:

$ sudo ip netns exec guest dig +short @203.0.113.10 portal.hotel.test
10.50.0.1

The guest asked 203.0.113.10 and got an answer from 10.50.0.1. The same WireGuard-on-53 config now fails again:

$ sudo ip netns exec guest wg show wg0
  endpoint: 203.0.113.10:53
  transfer: 0 B received, 148 B sent

The handshake packets are redirected to the resolver, which does not recognise them as DNS. dnsmasq’s log has the details:

config error is REFUSED (EDE: invalid data)

It answers REFUSED to the junk. The tunnel is dead, and a DNS-aware network has stopped it with one rule.

That does not close the door entirely. If the network lets guests resolve arbitrary names through its resolver, then DNS tunneling (variant 2) still works, because the data is now carried inside perfectly valid queries. It is slower and noisier, and the detection side, query entropy, label lengths and volume per domain, is in the DNS tunneling post.

The same idea on other ports

Port 53 is the historical example because captive portals had to leave it open. The principle works anywhere a port is allowed because it must be. Almost every network allows outbound HTTPS, so a tunnel listening on TCP 443 looks, to a port-based rule, like a web connection. Chisel in the Pivoting and Tunneling series is exactly this, an HTTP tunnel built to blend in.

The common thread is that allowing a port tells you nothing about the protocol using it. Each time, the only thing that stops it is looking at what is actually on the wire.

What actually stops it

In rough order of effort:

  1. Make “DNS allowed” mean “our resolver”. Redirect or block port 53 to anywhere else, as in the lab. This ends variant 1 for the price of two rules.
  2. Do not exempt anything pre-authentication beyond what is needed. The portal needs DNS and a route to itself. It does not need a route to the whole internet.
  3. Identify protocols by behaviour, not port. Application control and DNS filtering on a firewall classify traffic by what it does, so a VPN on port 53 or a tunnel on 443 gets labelled for what it is. I covered the operator side of guest networks in why DPI breaks guest Wi-Fi.
  4. Watch for the shape of a tunnel. Sustained volume, long labels and high entropy in DNS, or one internal host with a long-lived flow to a single external port. The egress side of this is in Pivoting and Tunneling Part 6.
  5. Default deny, outbound as well. A guest or server network that can only reach named destinations on named ports has nothing to tunnel to.

Three things that went wrong in my lab

These are worth knowing if you build something similar.

  1. WireGuard creates its UDP socket in the namespace where the interface is created, not where you move it. My first attempt did ip link add wg0 type wireguard and then ip link set wg0 netns guest. The tunnel came up, but the encapsulated packets were sent from the gateway’s own namespace, so they bypassed the firewall entirely. The capture showed the source as the gateway, 203.0.113.1, which gave it away. The fix is to create the interface inside the namespace, as in the commands above.
  2. nft reset counters only resets named counter objects. The counter in my rule is anonymous, so I was reading a running total and thought packets were being dropped while the tunnel worked. nft reset rules is the command that clears them, and the 0 versus 4 packet result above comes from that.
  3. A “blocked” result means nothing until you confirm the ruleset loaded. One of my rulesets failed on a syntax error, and the test I ran afterwards happily passed because there was no firewall at all. Check with nft list ruleset before you believe a negative result.

Verdict

The hotel trick worked because the rule said port 53 and the designer meant DNS. That gap is as old as port-based filtering, and it does not close by choosing a different port. It closes when the firewall checks the protocol, or when the rule says who a port may talk to and not just which number it uses.

WireGuard, OpenVPN and every other tunnel are just the demonstration. The lesson for anyone writing egress rules is simpler: an allowed port is a hole, so decide what you are willing to have go through it.