Understanding the Mac OS X Server Firewall
Though Mac OS X’s Application Firewall provides a simplified interface for handling firewall rules based on services and applications, Mac OS X Server administrators require more granular access controls than the Application Firewall provides. Mac OS X Server v10.6 contains a host-based firewall service based on ipfw software that was developed as part of the FreeBSD project. This traditional stateful packet firewall provides stateless or stateful packet inspection.
Stateful packet inspection tracks the state of network connections traveling across it. A stateless firewall looks at packets as individual events. A stateful firewall, on the other hand, can track multipacket communication sessions and more intelligently accept or deny traffic. For example, a stateful firewall can remember that a protected client initiated a request to download data from an Internet server and allow data back in for that connection.
Mac OS X Server can do both stateless and stateful processing. To use stateful processing, Mac OS X Server adds the keep-state keyword to rules. Since the release of Mac OS X Server version 10.5 Leopard, an Adaptive Firewall has been present in OS X Server. The Adaptive Firewall will monitor firewall activity and block an IP address that has excessive failed login attempts.
While documentation may make this seem like a second firewall, the Adaptive Firewall is really a monitor that dynamically creates and disables rules in the ipfw firewall as needed. The Adaptive Firewall is currently called into action following ten failed login attempts. Such behavior blocks the requesting IP address for 15 minutes, which makes brute-force password attacks virtually impossible. No administrator needs to take action to enable the Adaptive Firewall, as it is enabled by default.
Accessing the Firewall Setup
Some servers will have access to several networks such as an internal network, the Internet, and a management network. To avoid having to configure rules manually for each system on a specific network, you can create an address group to handle network addresses. The Address Groups tab in the Settings pane enables you to group addresses logically and create address ranges to which you can then apply rules. An address group can be a single address, such as 192.168.3.1, or a range of addresses, such as 192.168.3.0–192.168.3.255. You can set up a range of addresses using subnet mask notation (192.168.3.0: 255.255.255.0) or Classless Interdomain Routing (CIDR) notation (192.168.3.0/24).
- From the Dock, open Server Admin.
- Authenticate, if necessary, and then click the name of the server you want to configure.
- In the Toolbar, click the Settings button, and then select the Firewall checkbox, if necessary.
- At the lower right of the Server Admin window, click Save.
- In the leftmost pane, click the Firewall service.
- Click the Address Groups tab (as seen in the figure above).
- Click the Add (+) button located at the bottom of the Server Admin window.
- In the proceeding dialog box, enter the network address or range (using subnet notation), and then click OK to add the group.
You will now be able to assign rules to this specific address group.
- Click the pencil button if or when you need to edit existing groups.
Configuring Service Firewall Rules
The Services tab defines the rules for predetermined services for a given address group selected in the pop-up menu.
By default, all traffic is allowed out, but only Apple administrative ports and established traffic are allowed in. Established traffic has validly been sent out and is receiving a reply. Established traffic may receive reply traffic on ports that are closed, but because the firewall software tracks established traffic, use of the ports is allowed for established traffic streams.
The Services tab contains a long list of predefined services, any of which may be activated for a given address group. If the service or port range needed has not been predefined, it is easy to add a custom service. To add a custom service like Jabber for the iChat service, do the following:
- Click the Advanced tab.
- Click the Add (+) button to display a dialog box in which you can define a custom service to add to the list.
- Provide a name for the service you are configuring. In this exercise, enter Jabber.
- Enter the port numbers and, from the Protocol pop-up menu, choose a transport type (such as TCP for Jabber).
If you are not sure which port to add for a given service, you can get that information from several places. You can find well-known services in the /etc/services file on your local computer.
- In Applications > Utilities, open the Terminal application.
- Use the grep command to filter out terms in the services file, which contains a list of known services and their associated ports and
$ grep -i jabber-server /etc/services jabber-server 5269/udp # Jabber Server Connection jabber-server 5269/tcp # Jabber Server Connection
- Click OK to save your rule.
- To view the rules you are currently enforcing with your firewall, in the Toolbar, click the Active Rules button.
This displays the currently active rules for all rule sets and address groups.
Notice that each rule includes a priority number, the number of packets and bytes affected by the rule, and a description of the rule.
As mentioned earlier in “Understanding the Mac OS X v10.6 Server Firewall,” the firewall service in Mac OS X Server is built on top of ipfw, a kernel-based application. This means that the actual code for the ipfw service(s) is built into the kernel stack, making it extremely fast and secure from tampering. You can control ipfw via the same-named command-line program.
Verifying current firewall rules is simply a matter of asking ipfw to list them. This list corresponds to the list in the Active Rules pane in Server Admin.
- In Applications > Utilities, open the Terminal application.
- Execute the ipfw command using the list argument:
# ipfw list 01000 allow ip from any to any via lo0 01010 deny ip from any to 127.0.0.0/8 01020 deny ip from 18.104.22.168/4 to any in 01030 deny tcp from any to 22.214.171.124/4 in 12300 allow tcp from any to any established 12301 allow tcp from any to any out 12302 allow tcp from any to any dst-port 22 12302 allow udp from any to any dst-port 22 12303 allow udp from any to any out keep-state 12304 allow tcp from any to any dst-port 53 out keep-state 12304 allow udp from any to any dst-port 53 out keep-state 12305 allow udp from any to any in frag 12306 allow tcp from any to any dst-port 311 12307 allow tcp from any to any dst-port 625 12308 allow udp from any to any dst-port 626 12309 allow icmp from any to any icmptypes 8 12310 allow icmp from any to any icmptypes 0 12311 allow igmp from any to any 65534 deny ip from any to any 65535 allow ip from any to any
Dissecting this output, let’s take ssh tcp as an example:
12302 allow tcp from any to any dst-port 22
The first position is the rule number; the second is the action (allow or deny); the third position represents protocol type, TCP in our example. Fourth is the from-to combination explaining the allowed or denied path of traffic. In this case you are allowing TCP from any network to any network (equivalent to allow all), and the final two positions describe the type of rule, in this case a port-based rule, and the port number. So reading this across, you are allowing TCP from any network to any network on port 22.
Using Stealth Mode
In the Server Admin Settings pane, under the Advanced tab, you can set stealth options and create custom rule sets for the firewall service. Stealth options drop denied packets rather than sending the requesting computer an error message.
Once you have completed setup and testing, you should enable the Stealth Mode option for both TCP and User Datagram Protocol (UDP), which makes the job for attackers much more difficult as probing attacks and open port discovery are thwarted.
- In the Stealth Mode section of the Advanced tab, select the “Enable For TCP” checkbox.
- Select the “Enable for UDP” checkbox.
- From Applications > Utilities, open the terminal and type the following command to ping your system:
You will receive the following timeout message to indicate that your system is not responding to the ping request.
Request timeout for icmp_seq
Understanding Firewall Rule Priority
The rules in the Firewall Settings Services pane operate with the rules shown in the Advanced tab. Usually, the broad rules in the Advanced tab block (or open) access for all ports. These broad, lower-priority (higher-numbered) rules apply after the rules in the Services pane.
The rules created in the Services pane open access to specific services. Higher in priority, Services rules take precedence over those created in the Advanced tab. For most normal uses, using the Advanced tab to open access to designated services is sufficient.
If you create multiple rules in the Advanced tab, the rule number determines the precedence for a rule. This number corresponds to the order of the rule in the Advanced pane. You can reorder rules in the Advanced pane by dragging them up or down in the list. If necessary, you can add more rules using the Advanced pane.
Although Server Admin treats the firewall as a service, it does not implement the firewall by a running process. Implementation is simply a set of behaviors in the kernel, controlled by the ipfw and sysctl tools. To start and stop the firewall, Server Admin sets a switch using the sysctl tool. Use the sysctl tool to enable the firewall as follows:
$ sysctl -w net.inet.ip.fw.enable=1
You can also disable the firewall by changing the setting to 0:
$ sysctl -w net.inet.ip.fw.enable=0
A successful command will respond with the change made represented after the arrow.
$ net.inet.ip.fw.enabled: 1 -> 0
Regardless of this setting, the rules loaded in the firewall remain. But they are ignored when the firewall is disabled.
You can also use the ipfw command-line program to manipulate firewall rules. This is practical when you’re working remotely or over ssh, or when a scripted solution is needed.
As an example rule, imagine that an Xserve with multiple network interfaces—physical or virtual—is running AFP (Apple Filing Protocol) for file access. The security team decides that AFP should be available only on the subnets that it is serving. AFP does not have the control to specify which interface it binds to. However, you can use the built-in firewall, ipfw, to block AFP on the unwanted ports. For example, to block AFP on the en0 interface, you can use the following command to add the appropriate rule:
ipfw add deny dst-port 548 via en0
The keywords to ipfw are as follows:
add—Denotes adding a rule
deny—Indicates what type of rule
dst-port—Denotes which port the rule affects and is specified by number or service name
via—Applies rules to packets arriving via the specified interface or IP address
If a rule number is not specified, ipfw will assign a default number to the added rule. You may want to specify this rule number yourself because the firewall evaluates rules in a sequential order. When a default rule number is assigned, it will be done in such a way that the rule becomes the last rule, prior to the default rule. You can add an equivalent AFP-blocking rule using this command:
ipfw add 6000 deny dst-port afpovertcp via 10.10.15.68
However, doing so specifies the rule number (6000). The command lists the destination port by service name (afpovertcp) and gives an IP address rather than an interface name.
If a rule is incorrect or no longer needed, you can remove it with the del (delete) command by executing the following:
ipfw del 6000
Using Firewall Log Files
The firewall sends log messages to /var/log/ipfw.log. For example, if the AFP service was blocked and a subsequent user request was made to the AFP service from a computer with the IP address of 10.1.17.2, the log might read:
Oct 17 09:41:17 mainserver servermgrd: servermgr_ipfilter:ipfw config:Notice:Flushed IPv6 rules Oct 17 09:41:19 mainserver servermgrd: servermgr_ipfilter:ipfw config:Notice:Enabled firewall Oct 17 09:41:24 mainserver ipfw: 1040 Deny TCP 10.1.17.2:49232 10.1.0.200:548 in via en0 Oct 17 09:41:59 mainserver ipfw: 1040 Deny TCP 10.1.17.2:49232 10.1.0.200:548 in via en0 Oct 17 09:42:31 mainserver ipfw: 1040 Deny TCP 10.1.17.2:49232 10.1.0.200:548 in via en0 Oct 17 09:43:31 mainserver ipfw: 100 Accept TCP 10.1.0.1:721 192.168.12.12:515 in via en0
Each log entry follows a similar form:
- Time of entry—In this example, it is Oct 17....
- Host name—In this example, it is mainserver.
- Process name and ID—In this example, it is ipfw.
- Log message—The first two lines in this sample simply state that the firewall is flushing, then rereading its configuration, then starting and enabling.
For the firewall itself, each message follows this pattern:
- Matching rule number—Why the firewall took this behavior
- Action—The action taken (Deny, Accept, and so on)
- Protocol—Which protocol this affected (TCP, UDP, and so on)
- Source—The source IP address of packet
- Destination—The destination IP address of packet
- Interface—The network interface on which this packet appeared; in this example, it is in via en0, but it could also be lo0 (loopback), en1, or some other interface.
You can fine-tune logging from the Server Admin graphical user interface (Settings > Logging) or by using the serveradmin command-line utility. For example, to log all allowed packets, you can make the following serveradmin call:
serveradmin settings ipfilter:logAllAllowed = yes
Using Firewall Configuration Files
In Mac OS X Server, the firewall is a service that administrators can configure. In contrast, Mac OS X does not support the firewall directly. Its ipfw-based firewall has no graphical user interface and you must manipulate it via the command line. However, Mac OS X does contain the Application Firewall, which you can configure in Security preferences.
Mac OS X Server uses several files for its ipfw-based firewall. The following configuration files are stored in /etc/ipfilter:
-r--r--r--@ 1 root wheel 281 Oct 17 12:56 ip6fw.conf.apple -r--r--r-- 1 root wheel 0 Oct 17 12:33 ip6fwstate-on -rw-r--r-- 1 root wheel 41219 Oct 17 12:56 ip_address_groups.plist -r--r--r-- 1 root wheel 38243 Sep 23 2009 ip_address_groups.plist.default -rw-r--r-- 1 root wheel 1874 Sep 23 2009 ipfw.conf -r--r--r--@ 1 root wheel 1353 Oct 17 12:56 ipfw.conf.apple -r--r--r-- 1 root wheel 1874 Sep 23 2009 ipfw.conf.default -r--r--r-- 1 root wheel 0 Oct 17 12:33 ipfwstate-on -r--r--r-- 1 root wheel 632 Sep 23 2009 standard_services.plist.default
Server Admin writes to these configuration files. Note that:
- IPv6 and IPv4 rules are kept in separate files.
- Files with a name ending in “state-on” exist only if the service is running. They are flags used at boot time to indicate if the firewall should be enabled.
- Plist files contain information presented in Server Admin. They are well commented, and you can customize services and addresses seen in Server Admin. For example, if you have a custom application that communicates with other servers on a particular port, you can add an entry to this file so that this service appears in the list of ports that may be selected via a checkbox.
- The .apple files are edited by servermgrd. Changes made in these files may be overwritten by changes in Server Admin and may render Server Admin unable to manage the
firewall service. You can make changes in other configuration files, such as ipfw.conf. Rules added to ipfw.conf will be loaded
into ipfw at start time. The ipfw.conf file lists rules in the same format as rules added with the ipfw command line, minus the ipfw command itself. For example:
add 03000 allow tcp from any to any http
When added to ipfw.conf, rule 3000 is appended to the rule list, allowing HTTP on any interface. In addition, two premigration files may exist if the server was upgraded to version 10.6 from an earlier version.
As mentioned in the section “Using Firewall Log Files,” the Mac OS X Server ipfw logs messages to /var/log/ipfw.log. The Mac OS X Server Adaptive Firewall is configured by and uses several files:
- /etc/af.plist lists Adaptive Firewall preferences.
- /var/db/af/whitelist contains addresses that will not be blocked.
- /var/db/af/blacklist contains addresses that will always be blocked.
- /System/Library/LaunchDaemons/com.apple.afctl.plist contains the launchd plist.
Do not manually edit the whitelist and blacklist files. Rather, you should use the command-line utility afctl to modify these files. The Apple Event Monitoring daemon, emond, performs the actual monitoring and spurs the Adaptive Firewall into action. While emond is an off-limits subsystem, the man page states that “emond accepts events from various services, runs them through a simple rules engine, and takes action.” One of its rules is /etc/emond.d/rules/AdaptiveFirewall.plist.
This rule is activated after ten failed login attempts and blocks the offending host attempting the login for a period of 15 minutes.
The Mac OS X Application Firewall also contains configuration files that affect its behavior. The Application Firewall is configured in Security preferences.
The Application Firewall can also be configured using the command line. The socketfilterfw program, which resides in /usr/libexec/ApplicationFirewall, can query and configure the Application Firewall. There is no man page for socketilterfw, but a usage statement can be printed when using the -h switch.
By default, the Application Firewall is set to allow all incoming connections. The Application Firewall offers two choices for any given application: “Allow incoming connections” and “Block incoming connections.”
The Application Firewall logs its activity at /var/log/alf.log and is enabled by default when the Application Firewall is active.
A sample log snippet follows:
Sep 29 16:16:00 dhcp-172-26-94-100 Firewall: Deny Microsoft Word data in from 192.168.92.234:52684 uid = 0 proto=17
You can set the Application Firewall state from the command line using the defaults command to alter preferences:
defaults write /Library/Preferences/com.apple.alf globalstate -int 1
In this command, the integer passed is one of the following:
1—On for specific services
2—On for essential services
The main preference file for the Application Firewall is located in /Library/Preferences/com.apple.alf.plist.
The executable files for the Application Firewall are located in /usr/libexec/ApplicationFirewall.