Stateful and stateless filtering
A stateless filter judges each packet alone. To let a server download updates, it needs one rule for the outgoing request and another for the replies, and the reply rule has to be broad because the client port is random. An attacker who sends packets that look like replies will match it too.
A stateful filter remembers connections. Linux does this with connection tracking (conntrack), which gives
every packet a state. NEW starts a connection, ESTABLISHED belongs to one, and
RELATED is connected to one, such as an ICMP error. A single rule then covers every reply:
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT UFW adds this for you. With raw iptables you have to add it, and default deny without it breaks DNS lookups, package updates and every other outbound connection. The linter warns about that combination.
A Cisco IOS access list is stateless. The established keyword only checks whether a TCP packet has the ACK or RST
flag, which a sender can forge, and it does nothing for UDP. Use a zone-based firewall or a dedicated firewall where real
connection tracking is needed.
Rule order in UFW and iptables
Both tools read rules from the top and stop at the first match. That makes order part of the policy. To allow one network and block everyone else, the allow must come first:
1. allow tcp from 10.20.0.0/16 to any port 5432
2. deny tcp from any to any port 5432 Reversed, rule 1 blocks everyone and rule 2 is never reached.
| Task | UFW | iptables |
|---|---|---|
| List with numbers | ufw status numbered | iptables -L INPUT -n --line-numbers |
| Add at the end | ufw allow ... | iptables -A INPUT ... |
| Insert at the top | ufw insert 1 ... | iptables -I INPUT 1 ... |
| Delete by number | ufw delete 3 | iptables -D INPUT 3 |
| Default deny | ufw default deny incoming | iptables -P INPUT DROP |
UFW is a front end. It writes iptables or nftables rules and adds sensible ones of its own, including replies, loopback and some ICMP. Avoid mixing hand-written iptables commands with UFW on one host, because the result is hard to reason about. Docker is a frequent surprise. It publishes container ports with its own rules, and traffic to them can bypass UFW entirely.
Rate limits also differ. ufw limit has a fixed threshold and blocks an address that opens six or more connections
within 30 seconds. The iptables export builds the same behavior with the recent module.
Cisco extended ACLs, wildcard masks and the implicit deny
An extended access list matches protocol, source, destination and port. Addresses use a wildcard mask, where
a 0 bit must match and a 1 bit is ignored. It is the inverse of the subnet mask, so a /24 becomes 0.0.0.255 and a
/22 becomes 0.0.3.255. Single addresses are written with host, and everything with any.
ip access-list extended EDGE-FILTER-IN
remark SSH from admin network
permit tcp 198.51.100.0 0.0.0.255 any eq 22
permit tcp any any range 8000 8080
deny ip any any log
Every list ends with an invisible deny ip any any. This implicit deny has two consequences.
A list that contains only deny lines blocks all traffic, so a blocklist needs a final permit ip any any, which
the export adds. The implicit rule also keeps no counters and logs nothing, so the export writes it out with
log when you choose default deny.
Direction is relative to the interface. in filters packets that arrive at the interface, and out
filters packets that leave through it. A list on a router applies to traffic passing through, while UFW and iptables INPUT
rules protect the host itself. Remarks are limited to 100 characters, and the export shortens longer comments.
Applying rules over SSH without locking yourself out
- Have another way in. Confirm that a console, hypervisor or cloud serial connection works before you start.
- Save the current rules. Use
iptables-save > before.rules, or copy the files in/etc/ufw. - Schedule a rollback. Arrange for the old rules to return in a few minutes unless you cancel. On Debian and
Ubuntu,
iptables-applydoes this. Anywhere else, useat:
echo 'iptables-restore < /root/before.rules' | at now + 5 minutes - Allow SSH first, from the address you are connected from, and set default deny last. The export uses this order.
- Test with a second session. Your existing one survives because it is already established, so it proves nothing.
- Cancel the rollback with
atrm, and then save the rules so they survive a reboot.
On Cisco IOS, reload in 10 restarts the router with its saved configuration unless you run
reload cancel. Do not save until you have tested. Build a new named list and switch the interface to it instead
of editing the live one line by line.
What the linter checks
- Exposed services. SSH, Telnet, SMB, MySQL, RDP, PostgreSQL, Redis and MongoDB open to any address. Ranges and all-port rules that include them count.
- Telnet. Flagged even from a trusted network, because it sends passwords in the clear.
- Lockout. Default deny with no rule that allows SSH, including when an earlier deny hides the allow.
- Missing replies. Default deny without connection tracking.
- Shadowed rules. A rule that can never match because an earlier one covers it, with extra weight when the actions are opposite.
The linter sees only this ruleset. It does not know your existing rules, cloud security groups or network address translation. A rate limit on SSH slows guessing but still leaves the service exposed, so it is flagged as well. Prefer a VPN or a management network, with keys instead of passwords.
Comments are reduced to printable ASCII on one line and quoted for the shell, so a pasted comment cannot run a command. Addresses, ports and names are validated the same way. Presets use documentation addresses, which you need to replace with your own.
Why is rate limiting missing from the Cisco output?
An IOS access list can only permit or deny a packet. It has no counter per source. The tool exports the rule as a permit and adds a remark, so you know that the limit is missing. Use policing on an interface, or control plane policing to protect the router itself.