Get started with the Mac OSX firewall

Get started with the Mac OSX firewall

Fri 02 May 2014

In this post I will give you the details you need to setup an effective firewall on your Mac. OS X comes with two types of firewalls, where one works at the application layer, and the other at the network layer. I give a brief introduction to both variants.

Introduction to ALF

Application Level Firewall (ALF) allows you to block incoming connections to your computer. I recommend that you turn on the firewall, which can be done via the graphical configuration tool found under:

"System Preferences" -> "Security and Privacy" -> "Firewall".

OS X Application Level Firewall Settings OSX Application Level Firewall Settings

You can also turn on ALF via the command line:

# defaults write /Library/Preferences/ globalstate -int 1

# launchctl load /System/Library/LaunchDaemons/

# launchctl load /System/Library/LaunchDaemons/

optionally disable ALF via the command line:

# defaults write /Library/Preferences/ globalstate -int 0

# launchctl unload /System/Library/LaunchDaemons/

# launchctl unload /System/Library/LaunchDaemons/

You can also edit most settings in ALF from the command line:

# cd /usr/libexec/ApplicationFirewall

# ./socketfilterfw --help

You can read the current ALF configuration using defaults command:

# defaults read /Library/Preferences/

You can restore the default ALF configuration:

# cp /usr/libexec/ApplicationFirewall/ /Library/Preferences/

Introduksjon til PacketFilter

Packet Filter (PF) is the firewall originating from the operatingsystem OpenBSD. PF was launched on December 1, 2001, and is used by OpenBSD, FreeBSD, NetBSD, MacOSX + Apple iOS (FreeBSD), BlackBerry (NetBSD) and more.

  • Starting with Mac OS X 10.7 Lion, Apple has replaced ipfw to pf
  • Mac OS X version of pf is ported from FreeBSD, using pre-OpenBSD 4.6 syntax

To get started with pf we need two things:

  • pf configuration file(s).
  • one launchd entry for pf so that it starts up when booting the machine.

By default, we find an empty configuration file in / etc / pf.anchors /, which gives us two possibilities:

  • Use the existing empty configuration file
  • Ignore the existing configuration file and create a new

The disadvantage of the first option is that you will lose your configuration if Apple see fit to do anything with this file during a system update.

pfctl also have some issues with loading the rules from the default configuration file.

I decide to go for option 2 here since my experience with updates from Apple are varying.

Get started with PF

To get started we need the following four files:

  • pf configuration file that is read at startup
  • one anchorfile containing rules and options that we load into pf at startup
  • one macrofile containing variables that we can use in our rules
  • one launchdaemon which ensures that pf gets started when starting the machine

We create the files with the following content:


anchor “"load anchor “" from "/etc/pf.anchors/"

include "/etc/pf.anchors/"

# Don't filter on the loopback interface
set skip on lo0

set ruleset-optimization basic

# Return blocked packet so that the other entity can free up its resources without waiting for timeouts
set block-policy return

# Scrub incoming packets, with no-df to prevent nfs from malfunctioning
scrub in all no-df

# Antispoof, with logging
antispoof log for $ext_if
antispoof log for $int_if

# Block by default, without log
block in

# Allow outgoing, without log
pass out

# Allow DHCP, without log
pass in inet proto udp from any port 67 to any port 68

# Allow ICMP, and log it
pass in log inet proto icmp

# Allow all from trusted servers, and log it
pass in log from $trusted_servers


icmp_types = "{ echoreq, unreach }"
ext_if = "{ en0 }"
int_if = "{ lo0 }"
trusted_servers = "{, }"


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple Computer/DTD PLIST 1.0//EN" "">
<plist version="1.0">
<string>FreeBSD Packet Filter (pf) daemon</string>

This is a simple setup that blocks most incoming traffic except DHCP and ICMP. We also opens for trusted IP addresses so that we can reach the machine via ssh.

If you want to learn PF, there are links to articles and books that you can look into at the bottom of this post.

Some nifty commands

pfctl -sa , give full status information
pfctl -v -n -f <path to configuration file> , parse configuration file and any anchor files referenced

Turn on logging in PF

To log what happens we can use tcpdump to listen on the interface that pf logs.

First we create the interface:

# ifconfig pflog0 create

, then we can listen on it:

# tcpdump -n -e -ttt -i pflog0

More resources