Source port 443: when your ZBF self zone talks to strangers

Lesedauer: 15 Minuten

Many Cisco admins build a Zone-Based Firewall policy for the router itself, run a quick port scan, see SSH and SNMP filtered from the internet and move on. While reviewing IOS-XE edge routers, we came across a very common pattern that turns this assumption upside down: management services that look filtered are reachable from anywhere on the internet. All it takes is a different source port.

The short version

  • Traffic from the internet to the router itself is handled by the outside to self zone-pair.
  • Replies to connections the router opens itself (Smart Licensing over HTTPS, DNS lookups) are often let in with pass and an ACL that matches the source port of the server, like permit tcp any eq 443 any.
  • pass is stateless. The source port is chosen by whoever sends the packet. A SYN from source port 443 to port 22 matches that „reply“ rule and reaches SSH.
  • Source port 53 works the same way: against SSH through the TCP DNS rule, and against SNMP and every other UDP service on the router through the UDP DNS rule.
  • The fix: let real replies in statefully, or at least match the destination port range and pin the source where you can. In both cases, give every listening service its own access control.

Why the router needs reply rules at all

With ZBF, the router itself lives in the system-defined self zone. Once you configure an outside to self policy, everything not explicitly matched lands in class-default and gets dropped. That is exactly what you want.

But the router is also a client. It resolves names, and current IOS-XE releases report license usage to Cisco over HTTPS (Smart Licensing). The answers come back from the internet and have to get through the same outside to self policy. As long as nothing tracks the sessions the router opens itself, those answers need explicit rules. A lot of configurations, including many templates and forum examples, handle this with pass and a set of „reply“ ACLs. A typical policy looks like this:

ip access-list extended ACL-OUT-SELF-WEB
 10 permit tcp any eq www any
 20 permit tcp any eq 443 any
ip access-list extended ACL-OUT-SELF-DNS
 10 permit udp any eq domain any
 20 permit tcp any eq domain any
ip access-list extended ACL-OUT-SELF-ADMIN
 10 permit tcp host 192.0.2.10 any eq 22
!
class-map type inspect match-any CM-OUT-SELF-WEB
 match access-group name ACL-OUT-SELF-WEB
class-map type inspect match-any CM-OUT-SELF-DNS
 match access-group name ACL-OUT-SELF-DNS
class-map type inspect match-any CM-OUT-SELF-ADMIN
 match access-group name ACL-OUT-SELF-ADMIN
!
policy-map type inspect PM-OUT-TO-SELF
 class type inspect CM-OUT-SELF-ADMIN
  pass
 class type inspect CM-OUT-SELF-DNS
  pass
 class type inspect CM-OUT-SELF-WEB
  pass
 class class-default
  drop log
!
zone-pair security ZP-OUT-TO-SELF source outside destination self
 service-policy type inspect PM-OUT-TO-SELF

It reads well: SSH only from the admin host, plus replies from web and DNS servers. A normal port scan from the internet shows SSH and Telnet as filtered. Ticket closed.

The catch: pass does not know what a reply is

inspect keeps a session table and only lets in packets that belong to a connection someone actually opened. pass does nothing of the sort. It works like a plain ACL entry: if the packet matches, it goes through. And permit tcp any eq 443 any matches every TCP packet with source port 443, whether the router asked for it or not.

Think of a doorman who only checks the return address on the envelope. The return address is written by the sender.

Here is what happens when an attacker sends a SYN from source port 443 to port 22:

  1. The packet hits PM-OUT-TO-SELF. CM-OUT-SELF-ADMIN does not match, the source is not the admin host.
  2. CM-OUT-SELF-WEB matches, because the source port is 443. Action: pass.
  3. SSH on the router answers with a SYN-ACK. The self to outside direction is usually wide open: if no zone-pair is configured for it, traffic from the router is not filtered at all. The answer goes out.
  4. Every following packet from the attacker also uses source port 443 and matches again. The TCP session is complete, and the SSH server is ready for a login.

Class order doesn’t help, by the way. Classes are evaluated top-down and the first match wins. A packet that doesn’t match the admin class simply falls through to the next class, and any class with pass that matches lets it in. Only class-default drops.

The DNS rules are the twin of the problem. permit tcp any eq domain any opens SSH and Telnet to source port 53 just like the web rule does for 443. permit udp any eq domain any lets every UDP packet with source port 53 reach any UDP service on the router, SNMP included. And „public“ is still a very common community string for SNMP v1/v2c. On a router like that, one packet with source port 53 is enough to read its configuration data.

What we measured

Scan results from a router with this pattern, before and after the fix:

TargetSource portBeforeAfter
TCP 22, 23443openfiltered
TCP 22, 2353openfiltered
UDP 161randomopen|filteredopen|filtered
UDP 16153openopen|filtered
nmap results from an external host without NAT, against a router with Telnet enabled on the vty lines

The SNMP rows are the interesting ones. Without a fixed source port SNMP looks exactly like a filtered service. With source port 53 the router answers.

One caveat: nmap’s SNMP probe uses the community string „public“. A router that is reachable through source port 53 but uses a different community will not answer the probe and still shows open|filtered. A clean SNMP result alone does not prove that the service is unreachable.

How to check your own routers

1. Read the config

Find the zone-pair from outside to self, list its classes, and look at the ACLs behind every class that uses pass. Any entry that only checks the source port (any eq 443 any, any eq www any, any eq domain any) is a candidate. Then check which services the router actually listens on, including high ports:

show zone security
show policy-map type inspect zone-pair ZP-OUT-TO-SELF | include Class-map
show ip access-lists ACL-OUT-SELF-WEB
show ip access-lists ACL-OUT-SELF-DNS
show control-plane host open-ports

2. Test from the outside

Use a host with a public IP and no NAT in front of it. A NAT device may rewrite your source port, and the test will look clean even though it isn’t. Run every scan once without and once with a fixed source port, and compare the results.

ip=203.0.113.10

# baseline
nmap -sS -Pn -p 22,23 $ip
nmap -sU -Pn -p 161 $ip

# same scans with a fixed source port
nmap -sS -Pn -g 443 -p 22,23 $ip
nmap -sS -Pn -g 53 -p 22,23 $ip
nmap -sU -Pn -g 53 -p 161 $ip

# proof: full TCP session from source port 443, shows the SSH banner
ncat -p 443 $ip 22

If the baseline says filtered and the variant with the fixed source port says open, or ncat returns an SSH banner, you have found it.

The fix

If you work remotely, arm a rollback before you touch the zone policy. With an archive configured, IOS-XE can revert the change on its own unless you confirm it:

archive
 path flash:archive
!
configure terminal revert timer 20
! ... apply the change, test access ...
configure confirm
write memory

Without an archive, reload in 20 and reload cancel do the same job, with a reboot as the fallback.

Option 1: Let real replies in statefully

The reply rules only exist because nothing tracks the sessions the router opens itself. ZBF can inspect traffic from the self zone for basic protocols such as TCP, UDP and ICMP. With a self to outside zone-pair using inspect, the replies to the router’s own connections are let back in through the session table, and the source port trick no longer works for them:

class-map type inspect match-any CM-SELF-OUT
 match protocol tcp
 match protocol udp
 match protocol icmp
!
policy-map type inspect PM-SELF-TO-OUT
 class type inspect CM-SELF-OUT
  inspect
 class class-default
  pass
!
zone-pair security ZP-SELF-TO-OUT source self destination outside
 service-policy type inspect PM-SELF-TO-OUT

class-default uses pass here on purpose, so that router traffic other than TCP, UDP and ICMP (ESP for IPsec, for example) keeps working. Once you have confirmed that DNS, NTP and Smart Licensing still work, remove the web and DNS reply classes from PM-OUT-TO-SELF. Self-zone inspection has platform and release specific limitations, so test this on your device before relying on it. If it doesn’t fit your setup, use option 2.

Option 2: Tighten the reply ACLs

A real reply to a TCP or DNS connection the router opened goes to the high port the router picked as its source, never to 22, 23 or 161. So match the destination port range as well. Wherever the remote side is known, pin it. DNS resolvers and NTP servers are easy. Cloud services behind CDNs are not, so they keep any as source.

NTP is the exception to the high port rule: IOS uses port 123 on both ends, so the NTP rule matches eq ntp as destination instead of gt 1023. Note that the original policy above had no NTP rule at all, so NTP replies were silently dropped by class-default. If the router syncs its clock over the internet, it needs this class.

The new entries are added before the old ones are removed, so real replies are never dropped during the change. The resolver and NTP addresses are examples (Google Public DNS, PTB). Use the servers configured on your router.

ip access-list extended ACL-OUT-SELF-WEB
 11 permit tcp any eq www any gt 1023
 21 permit tcp any eq 443 any gt 1023
 no 10
 no 20
!
ip access-list extended ACL-OUT-SELF-DNS
 11 permit udp host 8.8.8.8 eq domain any gt 1023
 12 permit udp host 8.8.4.4 eq domain any gt 1023
 21 permit tcp host 8.8.8.8 eq domain any gt 1023
 22 permit tcp host 8.8.4.4 eq domain any gt 1023
 no 10
 no 20
!
ip access-list extended ACL-OUT-SELF-NTP
 10 permit udp host 192.53.103.103 eq ntp any eq ntp
 20 permit udp host 192.53.103.104 eq ntp any eq ntp
 30 permit udp host 192.53.103.108 eq ntp any eq ntp
!
class-map type inspect match-any CM-OUT-SELF-NTP
 match access-group name ACL-OUT-SELF-NTP
!
policy-map type inspect PM-OUT-TO-SELF
 class type inspect CM-OUT-SELF-NTP
  pass

In both cases: protect every service on its own

gt 1023 keeps the well-known services out. Anything the router runs on a high port is still reachable with a crafted packet from source port 443. And a zone policy is one template change away from being wrong again. So every management service needs its own protection, independent of the zone policy:

ip access-list standard ACL-MGMT-LINES
 10 permit 192.0.2.0 0.0.0.255
 20 permit host 198.51.100.20
 90 deny any log
!
! adjust the range to the vty lines of your device
line vty 0 15
 access-class ACL-MGMT-LINES in
 transport input ssh
 exec-timeout 15 0
!
! SNMP: never "public", always with an ACL
ip access-list standard 99
 10 permit 192.0.2.50
snmp-server community UseALongRandomStringHere RO 99

Better still, replace SNMP v1/v2c with SNMPv3 using authentication and encryption (authPriv).

Verify

Run the scans again. Everything with -g 443 and -g 53 should now look exactly like the baseline, and ncat -p 443 should no longer return a banner. Then check that the router still does its own work: DNS, NTP, and the counters on the classes that carry the replies:

ping www.cisco.com repeat 3
show ntp associations
show policy-map type inspect zone-pair | include Zone-pair|Class-map|packets

The ping is only a DNS test: the name has to resolve. Whether the echo replies come back depends on your policy. With option 2 they are dropped by class-default, which is fine. The NTP servers should reach 377 after a while, and the classes carrying replies (the inspect class in ZP-SELF-TO-OUT for option 1, the web, DNS and NTP classes in ZP-OUT-TO-SELF for option 2) should keep counting packets.

Checklist

  • No pass rule in outside to self that matches on the source port alone.
  • Replies to the router’s own connections are either inspected statefully or matched on the destination port range and, where possible, a fixed source host.
  • vty lines have an access-class and accept SSH only. SNMP has an ACL and a community string that isn’t „public“, or better, uses SNMPv3.
  • show control-plane host open-ports shows no service you didn’t expect.
  • If the outside interface runs IPv6, the same checks are done for the IPv6 policy and ACLs.
  • External scans run with and without a fixed source port, from a host without NAT.
  • The check is repeated after IOS-XE upgrades and template changes. Patterns like this come back through copy and paste.

Closing thoughts

None of this is a bug in IOS-XE. ZBF does exactly what the configuration says. The trouble is that „reply traffic“ sounds stateful while pass is not, and a single nmap flag is enough to show the difference.

If you run Cisco routers with ZBF at the edge, the check takes about ten minutes per device and is well worth it.