How Firewalls Work: Your Network's First Line of Defense

How Firewalls Work: Your Network's First Line of Defense

Firewalls filter network traffic, blocking malicious connections while allowing legitimate ones. Learn how different firewall types work and how to configure yours properly.

Passwordly Team
9 min read

What Is a Firewall

A firewall is a security system that monitors and controls incoming and outgoing network traffic based on predetermined rules. It acts as a gatekeeper between your device (or network) and the internet, deciding which connections are allowed and which are blocked.

The concept is straightforward: every piece of data traveling across a network is contained in packets. Each packet has a source address, destination address, port number, and protocol. A firewall examines these attributes and compares them against its rules to decide whether to allow, block, or flag the traffic.

Think of it as border control for your network. Just as customs officials check travelers' documents and cargo, a firewall inspects network packets against a set of rules. Legitimate traffic passes through; suspicious or unauthorized traffic is stopped.

Firewalls have been a fundamental component of network security since the late 1980s. While they've evolved significantly โ€” from simple packet filters to intelligent, application-aware systems โ€” the core principle remains: control what enters and exits your network.

Every internet-connected device operates behind at least one firewall, whether you know it or not. Your router has a firewall. Your operating system has a firewall. If you use a corporate network, there are enterprise-grade firewalls protecting the perimeter. Understanding how they work helps you make better decisions about your security configuration.

Types of Firewalls

Firewalls exist in several forms, each with different capabilities:

Packet-filtering firewalls: The simplest type. They examine individual packets in isolation and make allow/block decisions based on source IP, destination IP, port number, and protocol. Fast and efficient but can be fooled because they don't understand the context of connections.

Stateful inspection firewalls: Track the state of active connections and make decisions based on the context of the traffic. If you initiate a connection to a web server, the firewall remembers this and allows the response traffic. Unsolicited incoming traffic is blocked. This is what most modern firewalls use.

Application-layer firewalls (proxy firewalls): Operate at the application level and understand specific protocols (HTTP, FTP, DNS). They can inspect the actual content of traffic and make more sophisticated decisions. A web application firewall (WAF) can block SQL injection attempts, for example.

Next-generation firewalls (NGFW): Combine stateful inspection with deep packet inspection, intrusion prevention, application awareness, and threat intelligence. They identify applications regardless of port (detecting Skype on port 80, for example) and integrate malware detection.

Host-based firewalls: Software firewalls running on individual devices (Windows Defender Firewall, macOS Application Firewall, iptables on Linux). They protect the specific device they're installed on.

Network-based firewalls: Hardware or virtual appliances that protect entire network segments. Your home router's firewall protects your entire home network. Enterprise firewalls protect corporate networks.

How Packet Filtering Works

Packet filtering is the foundation of all firewalls. Understanding it clarifies how more advanced firewalls build on this concept.

Every network packet contains header information:

  • Source IP address: Where the packet came from
  • Destination IP address: Where the packet is going
  • Source port: The sending application's port number
  • Destination port: The receiving application's port (e.g., port 80 for HTTP, 443 for HTTPS, 22 for SSH)
  • Protocol: TCP, UDP, ICMP, etc.

A packet-filtering firewall applies rules against these headers. Rules are typically processed in order, with the first matching rule applied:

Example rules (simplified):

  1. Allow TCP traffic from any source to port 443 (HTTPS) โ€” Allow web browsing
  2. Allow TCP traffic from any source to port 80 (HTTP) โ€” Allow unencrypted web
  3. Block TCP traffic from any source to port 22 (SSH) โ€” Block SSH access from the internet
  4. Allow TCP traffic from 192.168.1.0/24 to any port โ€” Allow all traffic from the local network
  5. Block all other traffic โ€” Default deny

The "default deny" principle: Good firewall configurations end with a rule that blocks everything not explicitly allowed. This ensures that if a new service or protocol appears, it's blocked until a rule is created to allow it. This is far more secure than the alternative ("default allow"), which permits everything unless specifically blocked.

Limitations of pure packet filtering:

  • Can't detect malicious content inside allowed packets
  • Doesn't understand the state of connections (can be tricked by spoofed packets)
  • Can't identify applications using non-standard ports
  • Rules based solely on IP/port/protocol are easy to circumvent

Stateful Inspection

Stateful inspection (also called stateful packet inspection or dynamic packet filtering) adds connection tracking to packet filtering โ€” and it's the standard for modern firewalls.

Instead of examining each packet in isolation, a stateful firewall maintains a state table โ€” a record of all active connections. When you open a connection to a web server:

  1. Your device sends a SYN packet (TCP connection request) to port 443
  2. The firewall records this in the state table: "Device 192.168.1.10 initiated a connection to 203.0.113.5:443"
  3. The web server responds with SYN-ACK
  4. The firewall checks the state table โ€” this response matches an active outbound connection โ€” so it's allowed
  5. Data flows back and forth as part of this established connection
  6. When the connection closes, the entry is removed from the state table

Why this matters:

An attacker can't send unsolicited packets into your network pretending to be a response to something you requested. If there's no matching entry in the state table, the packet is dropped. This prevents a wide range of attacks:

  • IP spoofing attacks โ€” packets with forged source addresses are blocked if they don't match an active connection
  • Unsolicited inbound connections โ€” no one can reach your devices from the internet unless you initiated the connection (or a specific rule allows it)
  • Port scanning โ€” the firewall drops probes because no active connection exists

Stateful inspection is what your home router and operating system firewalls use. It's extremely effective for consumer use cases and is part of why modern home networks are significantly safer than they were in the early 2000s.

Application-Layer Firewalls

Application-layer firewalls go beyond ports and IPs โ€” they understand the actual application protocols being used and can inspect content.

Web Application Firewalls (WAF):

A WAF sits in front of web applications and analyzes HTTP/HTTPS traffic for malicious patterns:

  • SQL injection attempts: Blocks requests containing SQL code in form fields or URL parameters
  • Cross-site scripting (XSS): Blocks requests containing JavaScript injection patterns
  • Path traversal: Blocks attempts to access files outside the intended directory
  • Rate limiting: Blocks excessive requests from a single source (brute force, DDoS)
  • Bot detection: Identifies and blocks automated scraping or attack tools

WAFs are essential for protecting web applications and APIs. Major cloud providers (AWS WAF, Cloudflare WAF, Azure WAF) offer WAFs as managed services.

Deep Packet Inspection (DPI):

Some firewalls and network devices can inspect the payload (content) of packets, not just the headers. This allows them to:

  • Identify applications regardless of port (detecting BitTorrent on port 80)
  • Block specific content types (executable downloads, specific file types)
  • Detect malware signatures within network traffic
  • Enforce acceptable use policies (blocking certain categories of websites)

Note: DPI raises privacy concerns when used by ISPs or governments to monitor content. In a security context, it's valuable for detecting threats; in a surveillance context, it's an invasion of privacy. End-to-end encryption (HTTPS, VPNs) prevents DPI from reading encrypted content.

Built-In OS Firewalls

Every major operating system includes a built-in firewall. Here's how to ensure yours is configured correctly:

Windows Defender Firewall:

  • Enabled by default on all Windows installations
  • To verify: Settings โ†’ Privacy & Security โ†’ Windows Security โ†’ Firewall & network protection
  • Provides three profiles: Domain (corporate network), Private (home network), and Public (untrusted networks)
  • Public profile should be the most restrictive โ€” blocks all inbound connections except permitted apps
  • You can configure per-application rules: Allow or block specific programs from accessing the network
  • For advanced configuration: Windows Defender Firewall with Advanced Security (type "wf.msc" in Run dialog)

macOS Application Firewall:

  • Go to System Settings โ†’ Network โ†’ Firewall โ†’ Turn on
  • By default, it blocks incoming connections to programs not listed as allowed
  • "Stealth Mode" makes your Mac invisible to network scans (doesn't respond to ping or port probes)
  • For more control, the pf (packet filter) firewall is available via Terminal for advanced users

Linux (iptables/nftables/ufw):

  • iptables is the traditional Linux firewall (being replaced by nftables)
  • ufw (Uncomplicated Firewall) provides a simpler interface on Ubuntu and Debian-based distributions
  • firewalld is the default on Fedora, RHEL, and CentOS with zone-based configuration
  • Linux firewalls are the most flexible but require more configuration knowledge

Key settings for all OS firewalls:

  • Ensure the firewall is enabled (it should be by default, but verify)
  • Block all inbound connections by default (allow only specific services that need to be accessible)
  • Review the list of allowed applications periodically โ€” remove programs you've uninstalled or no longer use
  • Use the most restrictive profile when on untrusted networks (Public in Windows)

Your Router's Firewall

Your home router is the first line of defense for your entire network. It functions as a network firewall using a feature called NAT (Network Address Translation) combined with stateful packet inspection.

How your router protects you:

NAT assigns private IP addresses to your devices (192.168.x.x) and translates them to a single public IP address. Devices on the internet can only see your router's public IP โ€” they cannot directly reach devices behind it unless the router explicitly forwards the traffic. This provides a natural firewall effect:

  • Inbound connections from the internet are blocked by default (no matching NAT entry)
  • Only traffic that your devices initiated (and thus have NAT entries) is forwarded inward
  • Port scanning from the internet reaches only the router, not your devices

Router firewall configuration tips:

  • Change the default admin password. Default router passwords are well-known and published online. Use a strong, unique password.
  • Disable remote management. Unless you specifically need to manage your router from outside your home network, turn off remote administration access.
  • Disable UPnP (Universal Plug and Play). UPnP allows devices to automatically open ports on your router without your knowledge. While convenient for gaming and smart devices, it's a significant security risk. Disable it and manually forward only the ports you need.
  • Keep firmware updated. Router vulnerabilities are discovered regularly. Enable automatic updates if available, or check for updates monthly.
  • Disable WPS (WiFi Protected Setup). WPS has known vulnerabilities that allow attackers to guess your WiFi password. Disable it and use WPA3 (or WPA2) with a strong passphrase.
  • Use a separate network for IoT devices. Many routers support guest networks โ€” put smart home devices on a separate network from your computers and phones to limit the damage if an IoT device is compromised.

Firewall Best Practices

Default deny. Start by blocking everything, then create specific rules for traffic you want to allow. This is the fundamental principle of tight firewall configuration.

Layer your firewalls. Use both your router's network firewall and your operating system's host firewall. They protect against different threats โ€” the router protects against external traffic; the OS firewall protects against traffic from other devices on your local network (important in shared networks).

Minimize open ports. Every open port is a potential entry point. Only allow inbound connections for services you're actively running and need to be accessible. Regularly audit your open ports.

Review firewall rules periodically. Firewall configurations accumulate rules over time. Old rules for services you no longer use create unnecessary exposure. Review and clean up rules quarterly.

Monitor firewall logs. Firewall logs reveal blocked connection attempts, port scans, and other potentially suspicious activity. While most home users don't actively monitor logs, checking them occasionally can reveal patterns.

Don't disable the firewall. It's tempting to disable the firewall when troubleshooting connectivity issues. If you must, re-enable it immediately after resolving the issue. Leaving a firewall disabled "temporarily" often becomes permanent.

Combine with other security measures. A firewall is one layer of defense. Pair it with strong passwords from our password generator, up-to-date software, antivirus protection, and safe browsing habits for comprehensive security.


Firewalls are the silent guardians of your network โ€” quietly inspecting billions of packets and quietly blocking the vast majority of threats before they reach you. While they work mostly in the background, understanding how they function helps you configure them properly and recognize when they need attention. A well-configured firewall is your network's first and most reliable line of defense.

Related Articles

Continue exploring related topics