socketfilterfw
socketfilterfw controls the macOS Application Firewall (System Settings > Network > Firewall) from the command line. It is a per-app firewall for incoming connections only, and knowing where it stops and pf starts is a common interview point.
112.1 What it does and where it runs
- Decides which app may accept incoming connections; it looks at the app, not the port.
- macOS only, not in the PATH: use the full path, and sudo for changes.
- Does not filter outgoing traffic, ports or IPs: that is pfctl/pf.conf. Linux and Windows do not have this tool.
/usr/libexec/ApplicationFirewall/socketfilterfw --option [on|off] [app]112.2 How an incoming connection is decided
- Step 1
1.Office laptop 192.168.1.20 connects to MacBook :8080
- Step 2
2.pf (pfctl) checks IP / port rules
- Step 3
3.Application Firewall (socketfilterfw) checks the app
- Step 4
4.MyServer.app is allowed in the app list
- Step 5
5.Connection reaches MyServer on the MacBook
| Check (in order) | Result |
|---|---|
| 1. Block all on? | Drop every new incoming connection |
| 2. App in the app list? | Apply its allow / block rule |
| 3. Signed app? | Auto-allow (if allow signed is on) |
| 4. No match | Prompt: Allow / Deny, answer saved in the list |
112.3 Global firewall state
--getglobalstate is the firewall on?--setglobalstate on firewall on--setglobalstate off firewall off--getblockall is block-all on?--setblockall on block all incoming--setblockall off block-all off- setblockall on blocks all incoming except essential services: good on public Wi-Fi.
- getglobalstate State = 0 off, 1 on, 2 block all.
112.4 Signed software
- Signed = the app carries its developer's digital signature verified by Apple.
- setallowsigned covers built-in signed software; setallowsignedapp covers downloaded signed apps.
- Both off = a prompt for every app: stricter setup.
--getallowsigned show status--setallowsigned on built-in: allow--setallowsigned off built-in: ask--setallowsignedapp on downloaded: allow--setallowsignedapp off downloaded: ask112.5 Stealth mode
--getstealthmode is stealth on?--setstealthmode on no reply to ping/probes--setstealthmode off normal repliesD=/usr/libexec/ApplicationFirewallFW=$D/socketfilterfwsudo $FW --getglobalstatesudo $FW --setstealthmode on$ sudo $FW --getglobalstateFirewall is enabled. (State = 1)$ sudo $FW --setstealthmode onStealth mode enabled- Step 1
1.Scanner laptop 192.168.1.66 on cafe Wi-Fi pings the MacBook
- Step 2
2.Application Firewall (stealth on) silently drops the ping
- Step 3
3.Scanner tries closed port 9090 (SYN)
- Step 4
4.No RST is sent back
- Step 5
5.Scanner times out: the Mac looks absent from the network
112.6 Linux lab: stealth and block all with nft
- No Mac in the lab: container 192.168.132.10 plays the Mac (MyServer on 8080), scanner is 192.168.132.20.
- Scan only your own lab network or networks where you have permission.
Baseline, firewall off: ping replies, 8080 open, 9090 refused (the host sends a TCP RST).
ping -c 2 -W 1 192.168.132.10nc -zv -w 2 192.168.132.10 8080nc -zv -w 2 192.168.132.10 9090nmap -Pn -p 8080,9090 192.168.132.1064 bytes from 192.168.132.10: icmp_seq=1 ttl=64 time=0.480 ms2 packets transmitted, 2 received, 0% packet loss, time 1027msConnection to 192.168.132.10 8080 port [tcp/http-alt] succeeded!nc: connect to 192.168.132.10 port 9090 (tcp) failed: Connection refusedPORT STATE SERVICE8080/tcp open http-proxy9090/tcp closed zeus-adminStealth-like rule set: drop echo-request, accept 8080 (the allowed app), drop the SYN of every other new connection without a reply.
nft add table inet alfnft add chain inet alf in '{ type filter hook input priority 0; }'nft add rule inet alf in icmp type echo-request counter dropnft add rule inet alf in tcp dport 8080 counter acceptnft add rule inet alf in tcp flags '& (syn|ack) == syn' counter drop[scan]$ ping -c 2 -W 1 192.168.132.102 packets transmitted, 0 received, 100% packet loss, time 1020ms[scan]$ nc -zv -w 2 192.168.132.10 8080Connection to 192.168.132.10 8080 port [tcp/http-alt] succeeded![scan]$ nc -zv -w 2 192.168.132.10 9090nc: connect to 192.168.132.10 port 9090 (tcp) timed out: Operation in progressreal 0m 2.00s[scan]$ nmap -Pn -p 8080,9090 192.168.132.10PORT STATE SERVICE8080/tcp open http-proxy9090/tcp filtered zeus-admin[mac]$ nft list chain inet alf in icmp type echo-request counter packets 2 bytes 168 drop tcp dport 8080 counter packets 6 bytes 288 accept tcp flags syn / syn,ack counter packets 4 bytes 208 drop| Port 9090 test | Firewall off | Stealth on |
|---|---|---|
| nc | Connection refused (RST) | timed out after 2 s |
| nmap state | closed | filtered |
| ping | Replies | 100% packet loss |
Block all: remove the 8080 accept rule. Now even the allowed app is unreachable, but outgoing still works because the reply to an outgoing connection is a SYN-ACK, not a new SYN.
nft flush chain inet alf innft add rule inet alf in icmp type echo-request counter dropnft add rule inet alf in tcp flags '& (syn|ack) == syn' counter dropcurl -sI https://example.com | head -1[scan]$ nmap -Pn -p 8080,9090 192.168.132.10PORT STATE SERVICE8080/tcp filtered http-proxy9090/tcp filtered zeus-admin[mac]$ curl -sI https://example.com | head -1HTTP/2 200112.7 Logging
--getloggingmode is logging on?--setloggingmode on|off logging on / off--getloggingopt detail level--setloggingopt throttled fewer repeats--setloggingopt brief short messages--setloggingopt detail full detaillog stream --predicate 'subsystem == "com.apple.alf"'- throttled cuts repeated messages; detail is best while troubleshooting.
112.8 Application rules
--listapps apps + allow/block--add <path> add app to list--remove <path> remove from list--blockapp <path> block incoming--unblockapp <path> allow incoming--getappblocked <path> is it blocked?sudo $FW --add /Applications/MyServer.appsudo $FW --unblockapp /Applications/MyServer.appsudo $FW --listapps1 : /Applications/MyServer.app ( Allow incoming connections )- Recommended pattern: run --unblockapp right after --add so the decision is clearly allow.
112.9 Help
socketfilterfw -h # all supported optionssudo pkill -HUP socketfilterfw # reload, apply changes now112.10 Comparison: Application Firewall vs pf
| Point | Application Firewall | pf |
|---|---|---|
| Filters by | App | IP, port, protocol |
| Direction | Incoming only | In and out |
| Tool | socketfilterfw / System Settings | pfctl + pf.conf |
| Order | Second | First |
112.11 Common mistakes
- Believing firewall on = all traffic filtered: after block all, curl still got HTTP/2 200.
- Typing socketfilterfw without the full path (it is not in the PATH) or without sudo.
- Expecting it to block an IP or port: use pf.conf rules for that.
- Forgetting pkill -HUP socketfilterfw when a change does not seem to apply.
112.12 Interview tips
- Application Firewall = per-app, incoming only; pf = packet filter on IP/port/protocol, both directions.
- An incoming packet passes pf first, then the Application Firewall; servers often use both.
- Public Wi-Fi: --setblockall on + --setstealthmode on.
- Stealth = no reply to ping or closed-port probes, so nmap shows filtered instead of closed.