SkillPilotAI

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

Output
1 block in all2 pass in proto tcp to port 223 block in quick from <blocklist>4 pass out all keep state
pf evaluating the four rules
  1. 1.SSH packet from office laptop 192.168.1.20 reaches pf

  2. 2.pf checks all rules top to bottom: rule 1 block, rule 2 pass

  3. 3.Last match (rule 2) wins -> PASS to the Mac server

  4. 4.Blocklist host 203.0.113.5 tries port 22

  5. 5.Rule 3 has quick -> BLOCK immediately, rule 4 never checked

keep state and pfctl -k
  1. 1.Pass rules keep state by default

  2. 2.Server reply matches the state table, no rules checked

  3. 3.Laptop compromised: admin adds 192.168.1.20 to <blocklist>

  4. 4.Old SSH state still active -> next packet still passes

  5. 5.pfctl -k 192.168.1.20 kills its states -> next packet blocked by rule 3

110.3 General options and loading rules

OptionMeaning
-e / -dEnable / disable pf
-E / -X tokenEnable with a reference token / release it
-v / -vvVerbose, rule stats, warnings
-zClear per-rule statistics
-i en0Only one interface or group
-f file / -f -Load rules from file / stdin
-n -f file / -nvf fileParse only, no load / and print parsed rules
-D m=vDefine / override a macro
-A / -N / -O / -RLoad only queue / NAT / options / filter rules
-o none|basic|profileRuleset optimizer level
-x none|urgent|misc|loudKernel 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.

Terminal
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
Output
$ 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

CommandWhat it does
-F rules | nat | queueFlush filter / NAT / queue rules
-F statesClear the whole state table (every live connection record)
-F Tables | Sources | info | osfpTables / source tracking / stats / fingerprints
-F allEverything above: careful on a remote Mac
-sr / -ss / -siLoaded rules / state table / info with counters
-s Anchors | Tables | timeouts | memory | InterfacesOther views
-v -s rulesRules + evaluations, packets, bytes
-k host [-k host2]Kill states from host (to host2)
-K hostKill source tracking entries

110.6 Tables with pfctl

Output
-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

Output
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 optionMeaning
set timeout tcp.established 86400How long an established TCP state is kept
set timeout udp.single / udp.multipleOnly source sent / both sides sent
set timeout adaptive.start 6000 / adaptive.end 12000Timeouts shrink as states grow
set limit states 20000Max states (also frags, src-nodes, table-entries)
set block-policy drop | returnSilent drop / send RST or ICMP unreachable
set optimization satelliteLonger timeouts for slow links
set skip on lo0Do not filter loopback
set require-order yes | noEnforce statement order in pf.conf
set state-policy if-bound | floatingStates tied to an interface or not

110.8 Practical: the same jobs with nft on Linux

Terminal
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
Output
$ 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  }}
Lab: blocklist set = pf table
  1. 1.n110-pc 192.168.130.10 and n110-bad 192.168.130.66 reach server 192.168.130.80:80

  2. 2.Add 192.168.130.66 to the blocklist set (like pfctl -T add)

  3. 3.Bad host's packet dropped by ip saddr @blocklist

  4. 4.PC's packet still reaches the server

  5. 5.Old SSH state of bad host deleted with conntrack -D (like pfctl -k)

Terminal
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
Output
$ 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.
Terminal
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
Output
$ 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:262144

110.9 Comparison: pfctl vs nft

Jobpf / pfctlLinux nft
Rule evaluationLast match wins (quick = final at once)First match wins
Syntax checkpfctl -nf pf.confnft -c -f file
Load rulespfctl -f pf.confnft -f file
Macroext_if = "en0"define ext_if = "eth0"
IP listtable <blocklist>, pfctl -T add / show / testset blocklist, add element / list set / get element
Sub-rulesetanchor (-a com.example)custom chain + jump
Kill statespfctl -k hostconntrack -D -s host
Timeouts / limitsset timeout / set limitsysctl nf_conntrack_* (TCP established 432000 s)
Flush allpfctl -F allnft 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).