Introduction
A university server is like a building with 65535 numbered doors. Behind door 443 the website answers, behind door 22 the administrators log in, and behind door 3306 sits the database. A firewall is the guard at the front who decides, for every visitor, which door they may knock on.
This lesson explains ports, how a firewall reads its rules, and the one design choice that matters most: what happens to traffic that no rule mentions.
How it works
A computer runs many network services at once. The port number (0 to 65535) in each packet says which service it is for. Ports 0 to 1023 are the well-known ones (22 SSH, 53 DNS, 80 HTTP, 443 HTTPS), 1024 to 49151 are registered for particular programs (3306 MySQL, 3389 Remote Desktop), and 49152 to 65535 are picked freely for the client's side of a connection. A service is listening when it waits on a port for new connections.
A packet filter firewall reads each packet's protocol, destination port and source address, and compares them with an ordered list of rules:
- Rules are checked from the top.
- The first rule that matches decides: allow or deny. Later rules are not looked at.
- If no rule matches, the default policy decides.
Two habits follow. Put specific rules before general ones, or the general rule hides them. And make the default policy deny: then anything you forgot to think about is blocked, not let in. This is the "default deny" principle, and it turns mistakes into closed doors instead of open ones.
Try it below. Send the test packet from 10.1.2.3, then from 203.0.113.7, and watch which rule decides. Then move the last rule to the top and see the warning appear: a rule that can never be reached.
A plain packet filter judges each packet alone. A stateful firewall also remembers connections: when a computer inside opens a connection, the firewall lets that connection's replies back in, and drops unrequested packets from outside, even on the same port.
// A stateful firewall remembers connections that started inside, and lets only their replies back in.
const open = new Set();
function outgoing(to) {
open.add(to);
return "out to " + to + ": allowed, remembered";
}
function incoming(from) {
return "in from " + from + ": " + (open.has(from) ? "allowed (reply to our request)" : "dropped (nobody asked)");
}
console.log(outgoing("192.0.2.10:443"));
console.log(incoming("192.0.2.10:443"));
console.log(incoming("198.51.100.66:443"));Example
A small version of a university server's rule list. Students may reach the website, only the administrators' network may use SSH, and everything else is denied.
// A firewall checks rules from the top; the first rule that matches decides.
const rules = [
{ action: "allow", port: 443, from: "anyone" }, // the website, for everyone
{ action: "allow", port: 22, from: "admins" }, // SSH, only from the admins' network
{ action: "deny", port: "any", from: "anyone" }, // everything else
];
function decide(packet) {
for (let k = 0; k < rules.length; k++) {
const r = rules[k];
const portOk = r.port === "any" || r.port === packet.port;
const fromOk = r.from === "anyone" || r.from === packet.from;
if (portOk && fromOk) return r.action + " (rule " + (k + 1) + ")";
}
return "deny (default)";
}
const packets = [
{ port: 443, from: "student" },
{ port: 22, from: "admins" },
{ port: 22, from: "student" },
{ port: 3306, from: "student" },
];
for (const p of packets) console.log("port " + String(p.port).padEnd(5) + " from " + p.from.padEnd(8) + " -> " + decide(p));Line by line
rulesis checked in order. Rule 3 matches every port and every source, so it is the catch-all at the bottom.decidewalks the rules. A rule matches when its port isanyor equal to the packet's port, and its source isanyoneor equal to the packet's source.- A student on port 443 matches rule 1 at once: allowed.
- The administrators on port 22 pass rule 1 (wrong port) and match rule 2: allowed.
- A student on port 22 fails rule 1 and rule 2 (wrong source), so the catch-all denies it. Port 3306 is denied the same way.
Expected result
port 443 from student -> allow (rule 1)
port 22 from admins -> allow (rule 2)
port 22 from student -> deny (rule 3)
port 3306 from student -> deny (rule 3)
In the real world
- Home routers, operating systems (Windows Defender Firewall, Linux nftables) and cloud providers' "security groups" all use rule lists like this one.
- Cloud security groups usually allow only what you list and drop the rest: default deny is built in.
- A web application firewall looks deeper, at HTTP requests themselves, to block attacks such as SQL injection; it adds to a network firewall but does not replace it.
- Leaving a database or remote-desktop port open to the internet is one of the most common ways servers get attacked, which is why scanners look for exactly those ports.
Common mistakes
- A broad rule above a specific one. "Deny all" at the top blocks everything below it.
- A default policy of allow. Every forgotten service is then open.
- Opening a port "just for testing" and never closing it.
- Forgetting outbound rules. Malware inside a network often calls out; limiting outbound traffic slows it down.
Best practices
- Start from default deny and allow only what each service needs.
- Restrict administration ports (SSH, Remote Desktop) to known networks or a VPN.
- Review the rules regularly and remove any you cannot explain.
- Log denied packets, so scans and attacks are noticed.
Summary
A port number says which service a packet is for, and a firewall decides which ports may be reached, by whom. Rules are read from the top, the first match decides, and a default policy handles everything else; it should be deny. Specific rules go above general ones, or they are never reached. Stateful firewalls also remember outgoing connections and let only their replies back in.
Quick check
Key takeaways
- Rules are read from the top, and the first one that matches decides.
- Make the default policy deny, so forgotten cases are blocked.
- Put specific rules above general ones, or they are never reached.
- Keep administration ports closed to the internet and review rules regularly.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.