Networks, hosts & ports

Describe who and what precisely

Lesson 3 of 20 · 12 min · Module 1: How a firewall thinks

Most firewall mistakes are rules that are slightly too wide: a whole network where one host was meant, or every port where one was needed.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Choose between a host, a network, and any
  • Read the size of a network from its prefix length
  • Limit a rule to the ports a service uses

The idea

An address in a rule can be a single host (10.0.1.50), a network in CIDR notation (10.0.1.0/24), or any. A port can be one number, a list (80,443), or a range (1024-65535). Choose the smallest description that still covers the traffic you need.

allow tcp from 192.168.1.10 to 192.168.20.0/24 port 3389
allow udp from 192.168.1.0/24 to 192.168.20.5 port 123

01Read the prefix length

In 10.0.1.0/24 the /24 says the first 24 bits are fixed, which leaves 8 bits for hosts: 256 addresses, 10.0.1.0 to 10.0.1.255. A /16 fixes two numbers and covers 65,536 addresses. A single host is a /32, and you can write it without the suffix.

02Match the protocol to the service

A service is a protocol and a port together. Web traffic is tcp 80 (HTTP) and tcp 443 (HTTPS). SSH is tcp 22. DNS queries normally use udp 53. A rule for udp 53 does not allow tcp 53; to a firewall they are different traffic. Give each need its own rule, so each one is easy to read, to log, and to remove later.

03Named objects on real devices

Real firewalls let you name hosts, networks and services once and reuse them in rules, like lan and web in this lab. Here you write the addresses out to practise reading them.

# nftables
define admin = 10.0.1.50
define servers = 10.0.2.0/24
nft add rule inet filter forward ip saddr $admin ip daddr $servers tcp dport 22 accept

# FortiGate address object
config firewall address
    edit "ADMIN_PC"
        set subnet 10.0.1.50 255.255.255.255
    next
end

# Huawei USG address set
ip address-set admin_pc type object
 address 0 10.0.1.50 mask 32

Your turn

Write three rules. The administrator's PC 10.0.1.50 may use SSH (tcp 22) to every server in 10.0.2.0/24. The office network 10.0.1.0/24 may reach the web server 10.0.2.10 on tcp 80 and 443. The office network may send DNS queries (udp 53) to the DNS server 10.0.2.53. Nothing else is allowed.

What the checks look for

  • Only the administrator has SSH to the servers
  • The office reaches the website on 80 and 443 only
  • DNS is allowed as udp 53 to the DNS server only
  • Other networks get nothing
Try it in the editor →

Take this with you

Smallest source, smallest destination, exact protocol and port. A precise rule is also a readable one.

Go a little further

Narrow the SSH rule further so the administrator can reach only the database server 10.0.2.20, and watch the check for the web server change.

Learn Firewall Foundations for free

20 short lessons with real code and instant checks.

See the whole course →