Configuring Firewall Rules
How to turn a list of requirements into firewall rules, put them in the right order, and test what they allow.
1From requirements to rules
A firewall is configured from requirements: statements of what type of traffic, from which sources or to which destinations, should be allowed or denied. For example:
- “Anyone on the internet may load our website over HTTPS.”
- “Only the IT admin subnet may log in to the web server remotely.”
- “Block everything from a range of addresses that attacked us last week.”
- “Nothing else gets in.”
Each requirement becomes one or more rules. Rules can allow or deny inbound or outbound traffic based on source or destination port or IP address, service, protocol, or application.
2Writing a rule
Rules follow a consistent pattern: action, direction, protocol, port, source. Two examples:
| Rule | What it does |
|---|---|
ALLOW inbound TCP port 22 from ALL | Allows all inbound TCP traffic with destination port 22, the port for SSH (secure remote login), from any address |
DENY inbound TCP port 80 from 192.168.1.0/24 | Denies inbound TCP traffic to port 80 (unencrypted web) from addresses 192.168.1.0 to 192.168.1.255 |
Translate requirements carefully: “only the admin subnet” means the source must be that subnet's range, not ALL; “nothing else” means a final deny rule.
3Ordering rules
Rules are applied in order, and the first matching rule decides. So changing the order of a set of rules can change which traffic is allowed or denied. When ordering, think about precedence: which rule should win when two could match the same packet?
A reliable pattern:
- Specific exceptions first (allow the admin subnet's SSH; deny a known-bad range).
- Then broader allows (allow HTTPS from everyone).
- Finally, a deny-all rule that catches everything not allowed above.
4Testing the rule set
Before going live, walk sample traffic through the rules and confirm each result matches the requirements. Here is a rule set for a web server segment:
Rule 1: ALLOW inbound TCP port 22 from 10.10.9.0/24 Rule 2: DENY inbound TCP ALL from 198.51.100.0/24 Rule 3: ALLOW inbound TCP port 443 from ALL Rule 4: DENY inbound ALL ALL from ALL
| Test traffic | Packet | Result | Matches requirement? |
|---|---|---|---|
| Visitor loads the site | inbound TCP port 443 from 203.0.113.40 | ALLOW (Rule 3) | Yes |
| Admin logs in from 10.10.9.12 | inbound TCP port 22 from 10.10.9.12 | ALLOW (Rule 1) | Yes |
| Login attempt from the internet | inbound TCP port 22 from 203.0.113.40 | DENY (Rule 4) | Yes |
| Blocked range visits the site | inbound TCP port 443 from 198.51.100.3 | DENY (Rule 2) | Yes |
| Unencrypted web request (port 80) | inbound TCP port 80 from 203.0.113.40 | DENY (Rule 4) | Yes |
Write one rule per requirement, put specific rules before broad ones and a deny-all last, then test sample traffic against the requirements before trusting the configuration.
5Worked examples
The web server segment
Requirements: (1) anyone may reach the website over HTTPS (TCP 443); (2) only the admin subnet 10.10.9.0/24 may use SSH (TCP 22); (3) block all traffic from 198.51.100.0/24, including website visits; (4) deny everything else. Write the rules in order.
- 1Requirement 2 is a specific allow:
ALLOW inbound TCP port 22 from 10.10.9.0/24. - 2Requirement 3 must beat the HTTPS allow, so it goes before it:
DENY inbound TCP ALL from 198.51.100.0/24. - 3Requirement 1:
ALLOW inbound TCP port 443 from ALL. - 4Requirement 4 last:
DENY inbound ALL ALL from ALL.
Two rules, two orders
Rules: Rule 1: ALLOW inbound TCP port 22 from ALL; Rule 2: DENY inbound TCP ALL from ALL. What happens to (a) an SSH packet, (b) a web request on port 80? Then reverse the rules.
- 1(a) SSH on port 22 matches Rule 1: allowed.
- 2(b) Port 80 does not match Rule 1; it matches Rule 2: denied.
- 3Reversed: the deny-all TCP rule comes first and matches every TCP packet, including SSH. Both are denied, and the SSH rule is never reached.
Why is the blocked range getting in?
An administrator writes the rules in this order: allow TCP 443 from ALL; deny TCP from 198.51.100.0/24; allow TCP 22 from 10.10.9.0/24; deny all. Logs show 198.51.100.3 still loading the website. Why, and how is it fixed?
- 1A request from
198.51.100.3to port 443 matches the first rule (allow 443 from ALL). - 2First match wins, so the deny rule for that range is never reached for website traffic.
- 3Fix: move the deny for
198.51.100.0/24above the HTTPS allow.
Stopping data leaving
Requirement: computers on the records segment must not send data to the internet, except to the backup service at 203.0.113.50 on TCP 443. Which direction are these rules, and in what order?
- 1These control traffic leaving the segment: outbound rules. For outbound traffic, the address that matters is the destination.
- 2First, the specific exception: allow outbound TCP 443 to
203.0.113.50. - 3Then deny all other outbound traffic to the internet.
6Common mistakes
A deny-all at the top blocks everything, because it matches every packet before any allow rule is reached.
“Only the admin subnet may log in” needs the admin subnet's range as the source. from ALL opens SSH to the whole internet.
Requirements often limit what may leave a segment too. Outbound rules stop data theft and stop infected machines contacting attackers.
Walk sample packets through the rules. A rule set can look right and still let through the very traffic a requirement forbids.
“Rule 2, the deny for 198.51.100.0/24, must come before Rule 3, which allows HTTPS from all addresses. Otherwise a website request from that range would match the HTTPS allow first and be permitted, because the firewall applies the first matching rule.”
Quote the rules, explain what each matches, and use first-match order to justify the arrangement.