Sub topic 5
What a firewall does and does not do
This chapter explains the concept behind the macOS application firewall: what it filters, what it ignores, and why it matters even on a home network. The chapters that follow cover turning it on and tuning it.
Prerequisites. You should be comfortable with basic macOS navigation, including System Settings. You should also know how to run a command in Terminal and how sudo works. You do not need networking expertise. The few terms you need are defined below.
The threat: unwanted incoming connections
Your Mac talks to other computers over a network. Most of that traffic is your own doing. You open a web page, your Mac asks a server for it, and the server replies. Your Mac started that conversation, so it is an outgoing connection.
Some software works the other way. It waits for other computers to contact it. Examples include file sharing, screen sharing, Remote Login (SSH), a local web server you run for development, and a music app that streams to other devices. These programs open a listening port. A port is a numbered doorway on your Mac, and a listening port is one that accepts connections from outside. A connection that someone else starts toward your Mac is an incoming connection.
Every listening port is a small piece of attack surface. If the program behind it has a bug, or is configured with a weak password, anyone who can reach the port can try to exploit it. This risk is highest on networks you do not control, such as cafés, hotels, airports, and shared offices. On those networks, strangers sit on the same local network as your Mac.
A firewall reduces this risk. It sits between the network and your programs and decides which incoming connections are allowed to reach them.
The last two nodes in the diagram show outgoing traffic. The macOS application firewall does not check that path. This is the most important limit to remember, and a later section covers it.
What the macOS firewall does
The macOS firewall is an application firewall. It makes decisions per application, not per port number. This is different from the traditional approach, where an administrator writes rules such as "block TCP port 22."
Here is how it works at the level you need:
- A program tries to accept incoming connections.
- The firewall checks whether that program is allowed to do so.
- If the program is allowed, the connection goes through. If it is blocked, the connection is refused.
- If the program has no rule yet, macOS may show a dialog asking whether to allow incoming connections for it.
The firewall identifies programs by their code signature. A code signature is a cryptographic stamp that shows who built the program and that it has not been altered. Apple-signed software and software from the App Store can be allowed automatically, depending on your settings. The next chapters cover those settings.
A per-app model has a practical benefit. You do not need to know which port a program uses. You decide whether the program itself may receive incoming connections. It also has a cost, which is that you get less detailed control than a port-based tool would give you.
Why it is off by default
On a fresh macOS install, the application firewall is usually off. This is less alarming than it sounds, for two reasons.
- Most services that listen on the network are switched off by default. File Sharing, Remote Login, and Screen Sharing all require you to enable them.
- Home routers typically use network address translation (NAT), which hides the devices behind them. A computer on the internet cannot start a connection to your Mac directly, because the router does not know which device should receive it.
So a default Mac at home is not wide open. But "not wide open" is not the same as "protected." The risks start when you turn on a sharing service, install software that listens on a port, or join a network without a router's protection in between.
What the firewall does not do
A firewall is easy to overestimate. Keep these limits in mind.
It does not filter outgoing connections
The macOS application firewall controls incoming connections only. If malware is already running on your Mac, it can usually contact its operator over an outgoing connection, and the built-in firewall will not stop it. Tools that filter outgoing traffic exist as third-party products, but they are outside this tutorial.
It does not remove malware
A firewall does not scan files or detect malicious code. If you open a malicious attachment or install a trojan, the firewall has no say in that. Other macOS protections, such as Gatekeeper and XProtect, address that problem.
It does not protect services you have deliberately allowed
If you allow an app to accept incoming connections, the firewall lets those connections through. A vulnerable app that you allowed is still vulnerable. The firewall only decides whether connections reach it.
It does not protect against phishing or weak passwords
If an attacker tricks you into typing your password on a fake website, no network filter can help. The same is true if you use a guessable password on an exposed service such as Remote Login.
It does not help if someone has your Mac
The firewall addresses network exposure. It does nothing against someone who steals the machine and reads the disk. That is the job of FileVault, covered in earlier chapters. This is why the plan treats the firewall, FileVault, and Find My as complementary rather than interchangeable.
Why enable it anyway
Given those limits, a reasonable question is whether the firewall is worth the effort. For most people it is, for these reasons:
- It costs almost nothing. There is no noticeable performance cost and little maintenance.
- It protects you on hostile networks. At a café or hotel, you cannot rely on a router to shield you.
- It catches mistakes. If you start a development server bound to all network interfaces, or install an app that quietly opens a port, the firewall's prompt tells you it happened.
- It supports defense in depth. Security works best as overlapping layers. One layer failing should not mean total exposure.
Trade-offs
Enabling the firewall has some friction:
- You will see "Do you want the application to accept incoming network connections?" prompts, mainly for developer tools and networked apps.
- If you click Deny by mistake, a legitimate app may stop working until you correct the rule.
- Stricter options, covered in a later chapter, can break features you rely on, such as AirDrop-related discovery or file sharing.
These are small costs. They also make your Mac's behavior more visible, which is useful on its own.
A first look from Terminal
You can read the firewall's state without changing anything. The command below reports whether the application firewall is enabled.
/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstateA typical result looks like this when the firewall is off:
Firewall is disabled. (State = 0)When it is on, the output reads Firewall is enabled. (State = 1). A state of 2 means the "block all incoming connections" option is active. Reading the state does not require sudo. Changing it does, and the next chapter walks through that.
You can also list the programs that have rules.
/usr/libexec/ApplicationFirewall/socketfilterfw --listappsIf the firewall has never been used, the list may be empty or short. As you allow or deny apps, entries appear with their status.
Seeing what is listening
The firewall matters most when something is listening. You can check which programs currently accept incoming connections with lsof. The command lists processes that have a TCP port in the LISTEN state.
sudo lsof -iTCP -sTCP:LISTEN -n -PThe flags mean the following:
-iTCPlimits the output to TCP network connections.-sTCP:LISTENkeeps only sockets waiting for incoming connections.-nskips hostname lookups.-Pshows port numbers instead of service names.
Each line shows a command name, a process ID, and an address. Pay attention to the address column. An entry bound to 127.0.0.1 or localhost accepts connections only from your own Mac. An entry bound to * accepts connections from the network, and this is the kind of listener a firewall is meant to control.
On a clean Mac with sharing turned off, this list is often short. If you see an unfamiliar process listening on *, find out what it is before you decide whether to allow or remove it.
How the pieces fit
The three features in this tutorial answer three different questions.
| Feature | Question it answers | Threat addressed |
|---|---|---|
| FileVault | Can someone read my data if they have the disk? | Theft or loss, data at rest |
| Firewall | Can someone on the network reach my apps? | Unwanted incoming connections |
| Find My | Can I locate, lock, or erase a missing Mac? | Loss or theft, device recovery |
The firewall is the only one of the three that deals with the network. It does not help after a theft, and FileVault and Find My do not help against a remote connection attempt. Together they cover different parts of the same problem.
Common misunderstandings
- "My Mac has a firewall, so I do not need to update it." Updates fix the bugs that attackers exploit. The firewall does not fix those bugs.
- "Turning on the firewall will make my Mac invisible." That is the job of stealth mode, a separate option covered in a later chapter. Even then, you are not invisible on a network.
- "A home router is the same as a Mac firewall." A router protects the whole network from the internet. It does not protect your Mac from other devices on the same network, such as a compromised laptop or a guest's phone.
- "If an app prompts me, I should always click Allow." Allow only when you recognize the app and expect it to accept connections. If you did not expect the prompt, deny it and investigate.
Exercise
Your Mac is on the café Wi-Fi and the application firewall is enabled. Name one threat the firewall helps with and one it does not. Explain each briefly.
Show answer
The firewall helps against unwanted incoming connections from other devices on the café network, such as someone trying to reach a sharing service or a development server on your Mac. It does not help against malware that is already on your Mac and makes outgoing connections, because the macOS application firewall filters incoming connections only. It also does not help if someone steals the Mac and reads the disk, which FileVault addresses.
Exercise
You run /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate and it prints "Firewall is disabled. (State = 0)". Then you run sudo lsof -iTCP -sTCP:LISTEN -n -P and see an unfamiliar process listening on *. What does each result tell you, and what should you do next?
Show answer
The first result tells you the application firewall is off, so no per-app filtering of incoming connections is happening. The second tells you a program is accepting connections from the network, since * means all interfaces. Because the firewall is off, nothing stands between the network and that program. Find out what the process is by checking its command name and process ID, and decide whether you need it. If you do not, quit or uninstall it. In either case, consider enabling the firewall so the program's incoming connections are filtered.