Sub topic 16
Ports in firewall rules
This chapter assumes you know what a port is, how TCP and UDP differ, and the default ports of common services. It shows how firewalls use ports to decide which traffic to allow.
What a rule matches
A packet filter looks at a few fields in each packet header. A typical rule matches on:
- the source and destination IP address
- the transport protocol (TCP or UDP)
- the destination port, and sometimes the source port
- the direction (inbound or outbound)
Because the protocol is part of the match, "allow 53" is incomplete. DNS uses UDP 53 for most queries and TCP 53 for large responses and zone transfers, so you must say which one you mean.
Default deny and least privilege
The safest design is default deny. The firewall drops everything unless a rule allows it. You then add one rule for each service the host must offer. Each rule should be as narrow as you can make it: the specific port, the specific protocol, and the specific source networks that need access.
Use this reasoning for any server:
- List the services the host must offer to others.
- Look up the port and protocol for each.
- Allow only those, from only the sources that need them.
- Leave everything else blocked.
Stateful filtering and ephemeral ports
A client connects from a random ephemeral port to a fixed server port. The reply travels back to that ephemeral port. You do not need to open the whole ephemeral range for this. A stateful firewall tracks each connection and automatically allows the replies to traffic it already permitted. Your rules then only describe who may start a connection.
Example: a web and SSH server
This nftables ruleset applies the default-deny approach. It allows web traffic from anywhere and SSH only from an admin network.
sudo nft -f - <<'EOF'
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
iif "lo" accept
tcp dport { 80, 443 } accept
ip saddr 192.0.2.0/24 tcp dport 22 accept
}
}
EOFThe policy drop line sets default deny. The ct state established,related line is the stateful part. The last rule limits SSH to one source network.
Common mistakes
- Opening too much. A rule such as "allow all TCP" or a wide port range defeats the filter.
- Forgetting UDP. Services such as DNS, NTP and DHCP need UDP rules.
- Trusting the port number. A firewall that matches only on port can be bypassed by running another protocol on an allowed port, and a service moved to a non-standard port needs its rule changed.
- Not testing. Check from outside with
nc -vz host port, and confirm listeners on the host withss -tulnp.
Exercise
A new mail server must accept SMTP from the internet, and administrators on 198.51.100.0/24 must reach it over SSH. Which inbound rules do you write under default deny?
Show answer
Allow TCP destination port 25 from any source, and allow TCP destination port 22 only from 198.51.100.0/24. Also allow established and related traffic so replies pass. Everything else stays dropped.