pfctl / pf.conf (1/2)
pf (packet filter) is the OpenBSD firewall that also ships with macOS and FreeBSD; pfctl is its control tool that loads rules from pf.conf and shows rules, states and statistics. Its key difference from Linux firewalls: in pf the last matching rule wins, unless a rule says quick.
110.1 Why is it used?
- It is the built-in firewall on macOS (/sbin/pfctl), FreeBSD and OpenBSD: block, pass, NAT, tables and anchors.
- On macOS pf is disabled by default; sudo pfctl -e enables it.
- Tables hold fast IP lists you can change without reloading rules.
- pfctl does not exist on Linux (use nft) or Windows (netsh), so the lab does the same jobs with nft on Linux.
110.2 How it works: last match wins
1 block in all2 pass in proto tcp to port 223 block in quick from <blocklist>4 pass out all keep state- Step 1
1.SSH packet from office laptop 192.168.1.20 reaches pf
- Step 2
2.pf checks all rules top to bottom: rule 1 block, rule 2 pass
- Step 3
3.Last match (rule 2) wins -> PASS to the Mac server
- Step 4
4.Blocklist host 203.0.113.5 tries port 22
- Step 5
5.Rule 3 has quick -> BLOCK immediately, rule 4 never checked
- Step 1
1.Pass rules keep state by default
- Step 2
2.Server reply matches the state table, no rules checked
- Step 3
3.Laptop compromised: admin adds 192.168.1.20 to <blocklist>
- Step 4
4.Old SSH state still active -> next packet still passes
- Step 5
5.pfctl -k 192.168.1.20 kills its states -> next packet blocked by rule 3
110.3 General options and loading rules
| Option | Meaning |
|---|---|
| -e / -d | Enable / disable pf |
| -E / -X token | Enable with a reference token / release it |
| -v / -vv | Verbose, rule stats, warnings |
| -z | Clear per-rule statistics |
| -i en0 | Only one interface or group |
| -f file / -f - | Load rules from file / stdin |
| -n -f file / -nvf file | Parse only, no load / and print parsed rules |
| -D m=v | Define / override a macro |
| -A / -N / -O / -R | Load only queue / NAT / options / filter rules |
| -o none|basic|profile | Ruleset optimizer level |
| -x none|urgent|misc|loud | Kernel debug level |
110.4 Anchors (macOS workflow)
- An anchor is a named sub-ruleset inside the main ruleset.
- macOS keeps its own rules in com.apple, so put yours in a separate anchor like com.example.
- -a '*' -s rules prints main + all anchors; -a com.example -F rules flushes only that anchor.
A Mac is not available in this lab, so this is the documented example output.
echo "block drop in quick proto tcp \ from any to any port 23" \ | sudo pfctl -a com.example -f -sudo pfctl -a com.example -srsudo pfctl -t blocklist -T add 203.0.113.5sudo pfctl -k 203.0.113.5$ sudo pfctl -a com.example -srblock drop in quick proto tcp from any to any port = 23$ sudo pfctl -t blocklist -T add 203.0.113.51/1 addresses added.110.5 Flush, show and kill states
| Command | What it does |
|---|---|
| -F rules | nat | queue | Flush filter / NAT / queue rules |
| -F states | Clear the whole state table (every live connection record) |
| -F Tables | Sources | info | osfp | Tables / source tracking / stats / fingerprints |
| -F all | Everything above: careful on a remote Mac |
| -sr / -ss / -si | Loaded rules / state table / info with counters |
| -s Anchors | Tables | timeouts | memory | Interfaces | Other views |
| -v -s rules | Rules + evaluations, packets, bytes |
| -k host [-k host2] | Kill states from host (to host2) |
| -K host | Kill source tracking entries |
110.6 Tables with pfctl
-t T -T show / add / delete-T add -f file addresses from file-T replace / flush replace / clear all-T test IP does it match?-T kill / zero table delete / stats 0-T expire 86400 delete older than N s-Tl -f /etc/pf.conf table definitions only-v -T show per-address counters-v add/delete/test result letters: A added C changed D deleted M match X duplicate Y conflict Z stats zero-v -s Tables flags: c const p persist a active i inactive r referenced h hidden (same name in anchor)110.7 pf.conf: macros, tables and set options
ext_if = "en0"all_ifs = "{" $ext_if lo0 "}"include "/etc/pf.anchors/custom.conf"table <private> const { 10/8, 172.16/12, \ 192.168/16 }table <badhosts> persisttable <spam> persist file "/etc/spammers"table <local> { self }table <t> { 10.0.0.0/8, !10.1.0.0/16 }block on $ext_if from { <private>, \ <badhosts> } to any- A macro (ext_if = "en0") is used as $ext_if; include pulls in another file at that point.
- const: pfctl cannot change the table; persist: keep it even with no rule; self: all addresses of this host.
- !10.1.0.0/16 is a negated entry: an exception inside 10.0.0.0/8 that never matches.
| Set option | Meaning |
|---|---|
| set timeout tcp.established 86400 | How long an established TCP state is kept |
| set timeout udp.single / udp.multiple | Only source sent / both sides sent |
| set timeout adaptive.start 6000 / adaptive.end 12000 | Timeouts shrink as states grow |
| set limit states 20000 | Max states (also frags, src-nodes, table-entries) |
| set block-policy drop | return | Silent drop / send RST or ICMP unreachable |
| set optimization satellite | Longer timeouts for slow links |
| set skip on lo0 | Do not filter loopback |
| set require-order yes | no | Enforce statement order in pf.conf |
| set state-policy if-bound | floating | States tied to an interface or not |
110.8 Practical: the same jobs with nft on Linux
which pfctl || echo pfctl: not foundcat /etc/n110/fw.nftnft -c -f /etc/n110/bad.nftnft -c -f /etc/n110/fw.nft && echo syntax OKnft -f /etc/n110/fw.nftnft list chain inet n110 custom$ which pfctl || echo pfctl: not foundpfctl: not found$ cat /etc/n110/fw.nftdefine ext_if = "eth0"table inet n110 { set blocklist { type ipv4_addr } chain input { type filter hook input priority 0 iifname $ext_if ip saddr @blocklist counter drop jump custom } chain custom { tcp dport 23 counter drop }}$ nft -c -f /etc/n110/bad.nft/etc/n110/bad.nft:3:9-13: Error: syntax error, unexpected string tcp dprot 23 drop ^^^^^$ nft -c -f /etc/n110/fw.nft && echo syntax OKsyntax OK$ nft -f /etc/n110/fw.nft$ nft list chain inet n110 customtable inet n110 { chain custom { tcp dport 23 counter packets 0 bytes 0 drop }}- Step 1
1.n110-pc 192.168.130.10 and n110-bad 192.168.130.66 reach server 192.168.130.80:80
- Step 2
2.Add 192.168.130.66 to the blocklist set (like pfctl -T add)
- Step 3
3.Bad host's packet dropped by ip saddr @blocklist
- Step 4
4.PC's packet still reaches the server
- Step 5
5.Old SSH state of bad host deleted with conntrack -D (like pfctl -k)
nft add element inet n110 blocklist \ { 192.168.130.66 }nft list set inet n110 blocklistnft get element inet n110 blocklist \ { 192.168.130.10 }nc -zv -w 2 192.168.130.80 80 # badnc -zv -w 2 192.168.130.80 80 # pcconntrack -D -s 192.168.130.66conntrack -L -s 192.168.130.66$ nft list set inet n110 blocklisttable inet n110 { set blocklist { type ipv4_addr elements = { 192.168.130.66 } }}$ nft get element inet n110 blocklist { 192.168.130.10 }Error: Could not process rule: No such file or directoryget element inet n110 blocklist { 192.168.130.10 }^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^bad$ nc -zv -w 2 192.168.130.80 80nc: connect to 192.168.130.80 port 80 (tcp) timed out: Operation in progresspc$ nc -zv -w 2 192.168.130.80 80Connection to 192.168.130.80 80 port [tcp/http] succeeded!$ conntrack -D -s 192.168.130.66tcp 6 431982 ESTABLISHED src=192.168.130.66 dst=192.168.130.80 sport=53398 dport=22 src=192.168.130.80 dst=192.168.130.66 sport=22 dport=53398 [ASSURED] mark=0 use=1conntrack v1.4.8 (conntrack-tools): 1 flow entries have been deleted.$ conntrack -L -s 192.168.130.66conntrack v1.4.8 (conntrack-tools): 0 flow entries have been shown.nft list chain inet n110 inputcd /proc/sys/net/netfiltergrep . nf_conntrack_tcp_timeout_established \ nf_conntrack_udp_timeout nf_conntrack_maxnft flush set inet n110 blocklistnft delete table inet n110$ nft list chain inet n110 inputtable inet n110 { chain input { type filter hook input priority filter; policy accept; iifname "eth0" ip saddr @blocklist counter packets 2 bytes 120 drop jump custom }}$ grep . nf_conntrack_tcp_timeout_established \ nf_conntrack_udp_timeout nf_conntrack_maxnf_conntrack_tcp_timeout_established:432000nf_conntrack_udp_timeout:30nf_conntrack_max:262144110.9 Comparison: pfctl vs nft
| Job | pf / pfctl | Linux nft |
|---|---|---|
| Rule evaluation | Last match wins (quick = final at once) | First match wins |
| Syntax check | pfctl -nf pf.conf | nft -c -f file |
| Load rules | pfctl -f pf.conf | nft -f file |
| Macro | ext_if = "en0" | define ext_if = "eth0" |
| IP list | table <blocklist>, pfctl -T add / show / test | set blocklist, add element / list set / get element |
| Sub-ruleset | anchor (-a com.example) | custom chain + jump |
| Kill states | pfctl -k host | conntrack -D -s host |
| Timeouts / limits | set timeout / set limit | sysctl nf_conntrack_* (TCP established 432000 s) |
| Flush all | pfctl -F all | nft delete table / flush ruleset |
110.10 Common mistakes
- Assuming first match like iptables: block in all followed by pass in proto tcp to port 443 lets 443 PASS (last match wins, no quick).
- Expecting a new block rule to close live connections: old state still passes; run pfctl -k host.
- Editing com.apple rules instead of using your own anchor.
- Running pfctl -F all or -F states on a remote Mac: you can cut your own session.
110.11 Interview tips
- pf = last match wins, iptables/nft = first match wins; quick makes pf stop at that rule.
- Pass rules keep state by default; after blocking a host, kill its states with pfctl -k.
- On macOS: own anchor, check with pfctl -nf, verify with pfctl -sr.
- Tables change without a ruleset reload (pfctl -t T -T add).