HomeSharpStack
network-security15 min

Understanding Firewalls and Network Boundaries

Understanding Firewalls and Network Boundaries

A firewall is your network's security guard. It sits at the boundary between your internal network and the outside world, examining every piece of data that tries to enter or leave. Think of it like a checkpoint at a border—some travelers are allowed through, others are turned away, and everything is logged for review.

In this guide, you'll learn how firewalls work, why they're essential, and how they fit into your overall security strategy alongside the zero-trust principles and incident response practices you're already familiar with.

What Is a Firewall?

A firewall is a network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. It's typically the first line of defense in any network security architecture.

Firewalls operate at different layers of the network:

  • Network Layer (Layer 3): Examines IP addresses and basic routing information
  • Transport Layer (Layer 4): Looks at ports and protocols like TCP and UDP
  • Application Layer (Layer 7): Inspects actual data content and application-level protocols

Most modern firewalls work at multiple layers simultaneously, giving you comprehensive protection.

How Firewalls Work: The Basic Process

Every packet of data traveling across your network encounters the firewall's decision-making process:

  1. Packet Arrives: Data enters the firewall from either inside or outside your network
  2. Rule Matching: The firewall compares the packet against its ruleset
  3. Decision: Based on the rules, the packet is either allowed (ALLOW), blocked (DENY), or logged for review (LOG)
  4. Action: The packet is forwarded, dropped, or quarantined accordingly

This happens millions of times per second in modern networks, all transparently to your users.

Firewall Rules: The Decision Engine

Firewall rules are the core of how a firewall makes decisions. Each rule typically contains:

  • Source: Where the traffic is coming from (IP address or range)
  • Destination: Where the traffic is going (IP address or range)
  • Protocol: What type of communication (TCP, UDP, ICMP, etc.)
  • Port: Which specific service port (80 for HTTP, 443 for HTTPS, 22 for SSH, etc.)
  • Action: What to do with matching traffic (Allow, Deny, or Log)

Here's a simple example of how firewall rules might be structured:

// Example firewall ruleset (conceptual representation)
const firewallRules = [
  {
    id: 1,
    source: "any",
    destination: "192.168.1.0/24",
    protocol: "TCP",
    port: 443,
    action: "ALLOW",
    description: "Allow HTTPS traffic to internal servers"
  },
  {
    id: 2,
    source: "any",
    destination: "192.168.1.0/24",
    protocol: "TCP",
    port: 22,
    action: "DENY",
    description: "Block SSH from external networks"
  },
  {
    id: 3,
    source: "10.0.0.0/8",
    destination: "any",
    protocol: "TCP",
    port: 53,
    action: "ALLOW",
    description: "Allow DNS queries from internal network"
  },
  {
    id: 4,
    source: "any",
    destination: "any",
    protocol: "any",
    port: "any",
    action: "DENY",
    description: "Default deny all (implicit rule)"
  }
];

// Function to evaluate a packet against rules
function evaluatePacket(packet) {
  for (let rule of firewallRules) {
    if (matchesRule(packet, rule)) {
      return rule.action; // Returns ALLOW, DENY, or LOG
    }
  }
  return "DENY"; // Default action if no rules match
}

function matchesRule(packet, rule) {
  return sourceMatches(packet.source, rule.source) &&
         destinationMatches(packet.destination, rule.destination) &&
         protocolMatches(packet.protocol, rule.protocol) &&
         portMatches(packet.port, rule.port);
}

The order of rules matters significantly. Firewalls evaluate rules from top to bottom and stop at the first match. This is why more specific rules should come before general ones.

Types of Firewalls

Stateless Firewalls examine each packet independently without considering previous packets. They're fast but less intelligent. A stateless firewall might allow incoming traffic on port 443 but wouldn't understand that this traffic is a response to an outgoing request.

Stateful Firewalls track the state of network connections. They remember that your computer requested data from a web server and automatically allow the response back in. This is much smarter and more secure. Most modern firewalls are stateful.

Here's a conceptual example of how a stateful firewall tracks connections:

// Stateful firewall connection tracking
const activeConnections = new Map();

function handleOutgoingPacket(packet) {
  // When traffic leaves, record the connection
  const connectionKey = `${packet.source}:${packet.sourcePort}-${packet.destination}:${packet.destPort}`;
  activeConnections.set(connectionKey, {
    established: true,
    timestamp: Date.now(),
    protocol: packet.protocol
  });
  return "ALLOW"; // Allow outgoing traffic
}

function handleIncomingPacket(packet) {
  // For incoming traffic, check if it matches an established connection
  const reverseKey = `${packet.destination}:${packet.destPort}-${packet.source}:${packet.sourcePort}`;
  
  if (activeConnections.has(reverseKey)) {
    const connection = activeConnections.get(reverseKey);
    if (Date.now() - connection.timestamp < 3600000) { // 1 hour timeout
      return "ALLOW"; // Allow response to established connection
    }
  }
  
  // Check firewall rules for new incoming connections
  return evaluateAgainstRules(packet);
}

Firewalls and Zero-Trust Architecture

You're already familiar with zero-trust principles—never trust, always verify. Firewalls play a crucial role in zero-trust networks by enforcing strict boundaries and requiring explicit authorization for all traffic.

In a zero-trust model:

  • All traffic is considered untrusted by default
  • Every connection request must be authenticated and authorized
  • Firewalls enforce microsegmentation, creating smaller security zones within your network
  • Traffic between zones requires explicit firewall rules

This is different from traditional "perimeter security" where the firewall protects the outside boundary, but internal traffic is trusted. Zero-trust firewalls protect every boundary, including internal ones.

Firewalls in Incident Response

When a security incident occurs, firewalls become critical tools for investigation and containment. Your incident response team uses firewall logs to:

  • Trace Attack Paths: See exactly how an attacker entered and moved through your network
  • Identify Compromised Systems: Find which internal systems communicated with external threats
  • Contain Threats: Quickly block malicious IP addresses or domains at the firewall
  • Preserve Evidence: Firewall logs provide detailed records for forensic analysis

For example, if you detect suspicious outbound traffic to an unknown IP address, your firewall logs show you exactly when it started, how much data was transferred, and which internal system initiated it.

Common Firewall Configurations

Inbound Rules control traffic entering your network from the internet. A typical inbound rule might allow HTTPS (port 443) to your web servers but block everything else by default.

Outbound Rules control traffic leaving your network. Many organizations allow most outbound traffic but block specific dangerous destinations or protocols. Some restrict outbound traffic to only approved destinations (more restrictive, more secure).

DMZ (Demilitarized Zone) is a network segment between your internal network and the internet. Servers in the DMZ (like web servers) have different firewall rules than internal systems. They can receive traffic from the internet but have limited access to internal resources.

Here's a conceptual DMZ firewall configuration:

// DMZ firewall rules example
const dmzRules = [
  {
    name: "Allow external HTTPS to web servers",
    source: "0.0.0.0/0",
    destination: "192.168.2.0/24", // DMZ subnet
    protocol: "TCP",
    port: 443,
    action: "ALLOW"
  },
  {
    name: "Allow external HTTP to web servers",
    source: "0.0.0.0/0",
    destination: "192.168.2.0/24",
    protocol: "TCP",
    port: 80,
    action: "ALLOW"
  },
  {
    name: "Block DMZ to internal network",
    source: "192.168.2.0/24", // DMZ
    destination: "192.168.1.0/24", // Internal network
    protocol: "any",
    port: "any",
    action: "DENY"
  },
  {
    name: "Allow DMZ to database server (specific)",
    source: "192.168.2.10", // Specific web server
    destination: "192.168.1.50", // Database server
    protocol: "TCP",
    port: 3306, // MySQL
    action: "ALLOW"
  }
];

Firewall Limitations and Blind Spots

While firewalls are essential, they have limitations you should understand:

  • Encrypted Traffic: Firewalls can't inspect encrypted data (like HTTPS). They see the connection but not the content. This is actually good for privacy but means malware could hide inside encrypted traffic.
  • Internal Threats: Firewalls primarily protect network boundaries. They don't stop a compromised internal system from attacking other internal systems.
  • Application-Level Attacks: A firewall might allow HTTP traffic (port 80) but can't detect if that traffic contains a SQL injection attack.
  • Zero-Day Exploits: Firewalls block based on known threats. New, unknown vulnerabilities might slip through.

This is why firewalls work best as part of a layered security approach, combined with intrusion detection systems, endpoint protection, and the zero-trust principles you're learning.

Firewall Best Practices

Default Deny Principle: Configure your firewall to deny all traffic by default, then explicitly allow only what's necessary. This is more secure than allowing everything and trying to block bad traffic.

Regular Rule Audits: Review firewall rules periodically. Over time, rules accumulate and some become obsolete. Old rules create unnecessary security gaps.

Logging and Monitoring: Enable comprehensive logging on your firewall. Log both allowed and denied traffic. Use these logs for security monitoring and incident response.

Principle of Least Privilege: Each system should only have access to the specific ports and protocols it needs. A web server doesn't need SSH access from the internet; a database server doesn't need to initiate outbound connections.

Segmentation: Use firewalls to create network segments (like DMZ, internal network, guest network). Different segments have different security levels and different rules.

Practical Example: Protecting a Web Application

Let's say you're protecting a simple web application with a frontend and backend database:

// Web application firewall rules
const webAppRules = [
  // External users to web server
  {
    name: "Allow HTTPS to web server",
    source: "0.0.0.0/0",
    destination: "203.0.113.10", // Web server public IP
    protocol: "TCP",
    port: 443,
    action: "ALLOW"
  },
  // Web server to database (internal only)
  {
    name: "Allow web server to database",
    source: "203.0.113.10",
    destination: "192.168.1.50", // Database server
    protocol: "TCP",
    port: 5432, // PostgreSQL
    action: "ALLOW"
  },
  // Block direct database access from internet
  {
    name: "Block external database access",
    source: "0.0.0.0/0",
    destination: "192.168.1.50",
    protocol: "TCP",
    port: 5432,
    action: "DENY"
  },
  // Block web server from initiating outbound connections
  {
    name: "Block web server outbound",
    source: "203.0.113.10",
    destination: "0.0.0.0/0",
    protocol: "any",
    port: "any",
    action: "DENY"
  },
  // Default deny
  {
    name: "Default deny all",
    source: "any",
    destination: "any",
    protocol: "any",
    port: "any",
    action: "DENY"
  }
];

This configuration ensures that:

  • Users can access the web application securely (HTTPS)
  • The web server can communicate with the database
  • The database is not directly accessible from the internet
  • The web server can't be used to attack external systems

Monitoring Firewall Activity

A firewall generates logs for every decision it makes. These logs are invaluable for security:

// Example firewall log entry
const firewallLogEntry = {
  timestamp: "2024-01-15T14:32:45Z",
  sourceIP: "203.0.113.100",
  sourcePort: 54321,
  destinationIP: "192.168.1.50",
  destinationPort: 5432,
  protocol: "TCP",
  action: "DENY",
  ruleID: 5,
  ruleName: "Block external database access",
  bytesTransferred: 0,
  packetCount: 1
};

// Security team analyzes logs to detect patterns
function analyzeFirewallLogs(logs) {
  const suspiciousPatterns = [];
  
  // Look for repeated denied connections (potential attack)
  const deniedBySource = {};
  logs.forEach(log => {
    if (log.action === "DENY") {
      deniedBySource[log.sourceIP] = (deniedBySource[log.sourceIP] || 0) + 1;
    }
  });
  
  // Flag IPs with many denied attempts
  Object.entries(deniedBySource).forEach(([ip, count]) => {
    if (count > 100) {
      suspiciousPatterns.push({
        type: "Potential port scan",
        sourceIP: ip,
        deniedAttempts: count
      });
    }
  });
  
  return suspiciousPatterns;
}

Firewalls in Cloud Security

In cloud environments, firewalls work similarly but with some differences. Cloud providers offer security groups or network ACLs that function like firewalls. You define rules for which traffic can enter and leave your cloud resources.

The principles remain the same: default deny, explicit allow, principle of least privilege, and regular audits. Whether your firewall is a physical device in your data center or a virtual security group in the cloud, the security concepts are identical.

Summary

Firewalls are your network's first line of defense. They monitor all traffic entering and leaving your network, making allow/deny decisions based on predetermined rules. By understanding how firewalls work, configuring them properly, and monitoring their logs, you create a strong security foundation.

Remember that firewalls are most effective as part of a comprehensive security strategy that includes zero-trust principles, incident response procedures, and other security layers. No single tool provides complete protection, but a well-configured firewall significantly reduces your attack surface and helps you detect and respond to security incidents.

Key Takeaways

  • Firewalls are network security systems that monitor and control traffic based on rules, acting as the first line of defense by examining source, destination, protocol, and port information to make allow/deny decisions
  • Stateful firewalls track active connections and automatically allow response traffic, while the default-deny principle ensures only explicitly authorized traffic passes through, following the principle of least privilege
  • Firewalls work best as part of layered security including zero-trust architecture and incident response procedures, with firewall logs serving as critical evidence for detecting attacks and investigating security incidents

Enjoyed this reading?

SharpStack delivers personalized tech readings every day, calibrated to your skill level. 5 minutes a day to stay sharp.

“Stay sharp. At your pace. Everyday.”