Introduction

Network traffic represents one of the most overlooked vectors for privacy leakage on macOS. Applications constantly communicate with external servers, often transmitting telemetry data, usage analytics, and identifying information. Configuring the macOS firewall properly provides granular control over which connections your system allows, blocks, or logs.

This guide covers three layers of firewall configuration on macOS: the built-in Application Firewall (ALF), the packet-filtering pf firewall, and advanced rules for blocking known trackers. These tools work together to create defense-in-depth for your network privacy.

Understanding macOS Firewall Options

macOS includes two native firewall technologies. The Application Firewall operates at the application level, controlling which apps can accept incoming connections. The pf firewall (derived from OpenBSD) operates at the packet level, providing network-level filtering capabilities.

The Application Firewall is user-friendly and sufficient for basic blocking of unsolicited inbound connections. However, for privacy-focused users wanting to block outbound tracking domains or create sophisticated rules, pf offers far greater flexibility.

Configuring the Application Firewall

Enabling via System Settings

The simplest approach uses System Settings. Navigate to System Settings > Network > Firewall and enable it. This blocks all incoming connections to applications that haven’t explicitly been granted permission.

Enabling via Command Line

For automation and scripting, use the ` Firewall` command:

# Check firewall status
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate

# Enable firewall
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate on

# Disable firewall
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off

Managing App Rules

The Application Firewall maintains a list of signed applications. When an unsigned app attempts to listen for connections, macOS prompts you. For developers managing multiple unsigned applications or development tools, command-line management proves essential:

# List all rules
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --listapps

# Add an application to the firewall
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --addapp /path/to/Application.app

# Remove an application
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --removeapp /path/to/Application.app

# Block an app that was previously allowed
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --blockapp /path/to/Application.app

Advanced Packet Filtering with pf

The pf firewall provides powerful network-level filtering. Unlike the Application Firewall, pf can filter both inbound and outbound traffic, block specific domains, and perform network address translation.

Basic pf Configuration

Create a custom anchor file for your rules:

sudo mkdir -p /etc/pf.anchors/my.custom.rules

Edit the anchor file with your rules:

sudo nano /etc/pf.anchors/my.custom.rules

Blocking Tracking Domains

For privacy, blocking known tracker domains reduces telemetry and advertising tracking. Create a blocklist:

# Block common tracker domains
block drop in quick on en0 proto { tcp, udp } from any to { ads.example.com, tracker.example.com, analytics.example.com }
block drop out quick on en0 proto { tcp, udp } from any to { ads.example.com, tracker.example.com, analytics.example.com }

The quick keyword causes pf to drop matching packets immediately without evaluating subsequent rules.

Loading pf Rules

After creating your anchor file, load the rules:

# Test the rules file for syntax errors
sudo pfctl -vnf /etc/pf.anchors/my.custom.rules

# Load the rules
sudo pfctl -f /etc/pf.anchors.my.custom.rules

# Enable pf if not already running
sudo pfctl -e

Creating an Outbound Blocking Ruleset

For comprehensive privacy, control outbound connections as well. This prevents applications from transmitting data without your knowledge:

# Default deny outbound
block out all

# Allow essential services
pass out quick on en0 proto { tcp, udp } from any to any port { 53, 80, 443 } keep state

# Allow specific applications (example for curl)
pass out quick on en0 proto { tcp } from any to any port 443 user 501 keep state

This configuration blocks all outbound traffic except DNS, HTTP, and HTTPS, plus any additional rules you explicitly define.

Automating Firewall Management

Create a launch daemon to load your rules at boot:

sudo nano /Library/LaunchDaemons/com.custom.pf.plist

Add the following content:

<?xml version="1.0" TESTING"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.custom.pf</string>
    <key>ProgramArguments</key>
    <array>
        <string>/sbin/pfctl</string>
        <string>-f</string>
        <string>/etc/pf.anchors/my.custom.rules</string>
        <string>-e</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Load the daemon:

sudo launchctl load /Library/LaunchDaemons/com.custom.pf.plist

Monitoring Firewall Activity

Verify your rules are working by monitoring connection attempts:

# View active pf rules
sudo pfctl -s rules

# Show packet statistics
sudo pfctl -s all

# Monitor connections in real time (requires enabling logging)
sudo tcpdump -i en0 -n

Practical Privacy Rule Set

This example combines the techniques above into a practical privacy-focused ruleset:

# /etc/pf.anchors/privacy.rules

# Block known tracking domains
block drop out quick on en0 proto { tcp, udp } from any to { 
    doubleclick.net, 
    googlesyndication.com,
    googleadservices.com,
    facebook.com/tr,
    pixel.facebook.com
}

# Allow essential network services
pass out quick on en0 proto { tcp, udp } from any to any port 53 keep state
pass out quick on en0 proto tcp from any to any port { 80, 443 } keep state

# Block all other outbound (optional - use with caution)
# block out quick on en0 all

Common Pitfalls

When configuring macOS firewall for privacy, avoid these mistakes:

  1. Blocking Apple services: Some Apple features require specific ports. Blocking them causes functionality issues with iCloud, FaceTime, and Messages.

  2. Overly broad rules: Blocking all traffic makes your system unusable. Start restrictive and add exceptions as needed.

  3. Forgetting anchor files: pf rules require proper anchoring. Without loading your custom rules, they remain inactive.

  4. Not testing rules: Always test new rules in a controlled environment before deploying to production systems.

Conclusion

Proper firewall configuration provides essential network privacy control on macOS. The Application Firewall handles basic inbound protection, while pf enables sophisticated outbound filtering and tracker blocking. Combined with regular monitoring and automated rule loading, these tools create a robust network privacy layer.

Remember that firewall configuration requires balance between security and usability. Start with conservative rules, test thoroughly, and adjust based on your specific use case and threat model.

Built by theluckystrike — More at zovo.one