PF 防火墙绕过与高级规则完全指南
一、PF 防火墙概述
PF(Packet Filter)是 OpenBSD 自 3.0 版本(2001)开始默认安装的防火墙,源自 Daniel Hartmeier 的设计。PF 后来被移植到 FreeBSD(2003)和 macOS(10.7 Lion),成为 BSD 系统的事实标准。
1.1 PF 历史
| 时间 | 操作系统 | 重大变化 |
|---|---|---|
| 2001 | OpenBSD 3.0 | PF 取代 IPFilter |
| 2003 | FreeBSD 5.x | PF 移植到 FreeBSD |
| 2004 | OpenBSD 3.5 | 引入 ALTQ |
| 2005 | OpenBSD 3.7 | 表(table)功能 |
| 2006 | OpenBSD 4.0 | 引入锚点(anchor) |
| 2007 | OpenBSD 4.1 | scrub 增强 |
| 2010 | OpenBSD 4.7 | CARP 集成 |
| 2011 | OpenBSD 4.9 | authpf 增强 |
| 2015 | OpenBSD 5.7 | 表内容动态加载 |
| 2017 | OpenBSD 6.1 | 引入 match |
| 2018 | OpenBSD 6.4 | RDR 优化 |
| 2019 | OpenBSD 6.5 | 性能优化 |
| 2020 | OpenBSD 6.7 | UDP 性能改进 |
| 2021 | OpenBSD 6.9 | TCP 优化 |
| 2022 | OpenBSD 7.1 | 性能进一步优化 |
| 2023 | OpenBSD 7.3 | 引入更多 match |
| 2024 | OpenBSD 7.5 | 持续改进 |
1.2 PF 与其他防火墙对比
| 特性 | PF | iptables | nftables | Cisco ACL |
|---|---|---|---|---|
| 状态化 | ✓ | 部分 | ✓ | ✗ |
| 性能 | 高 | 中 | 高 | 高 |
| NAT | ✓ | ✓ | ✓ | ✗ |
| 流量整形 | ✓(ALTQ) | ✗ | 有限 | ✗ |
| 表 | ✓ | ipset | ✓ | object-group |
| 锚点 | ✓ | 自定义链 | ✓ | - |
| 实时加载 | ✓ | 部分 | ✓ | ✗ |
| 规则语法 | 简洁 | 复杂 | 中等 | 中等 |
| IPv6 | ✓ | ✓ | ✓ | ✓ |
二、PF 基础配置
2.1 /etc/pf.conf 基本结构
PF
# /etc/pf.conf
# 宏定义
ext_if = "em0"
int_if = "em1"
tcp_services = "{ ssh, http, https }"
# 表
table <badhosts> persist
table <whitelist> persist file "/etc/whitelist"
table <rfc6890> { 0.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 240.0.0.0/4 }
# 选项
set block-policy drop
set skip on lo0
set loginterface egress
set optimization normal
set ruleset-optimization basic
set timeout { tcp.first 60, tcp.opening 60, tcp.established 3600 }
# 规范化
scrub in all
# NAT/重定向
nat pass on $ext_if from 192.168.1.0/24 to any -> ($ext_if)
rdr pass on $ext_if proto tcp to port 80 -> 192.168.1.10 port 80
# 过滤规则
block all
pass out keep state
pass in on $ext_if proto tcp to port ssh
pass in on $int_if from 192.168.1.0/24 to any
2.2 PF 命令
SH
# 检查规则语法
pfctl -nf /etc/pf.conf
# 加载规则
pfctl -f /etc/pf.conf
# 启用 PF
pfctl -e
# 禁用 PF
pfctl -d
# 查看规则
pfctl -s rules
pfctl -sr
# 查看状态
pfctl -s state
pfctl -ss
# 查看表
pfctl -t <tablename> -T show
pfctl -t <tablename> -T add 192.168.1.100
pfctl -t <tablename> -T delete 192.168.1.100
# 清空表
pfctl -t <tablename> -T flush
# 查看 NAT
pfctl -sn
pfctl -s nat
# 查看锚点
pfctl -s Anchors
pfctl -a <anchor> -s rules
# 加载锚点规则
pfctl -a <anchor> -f /etc/pf.anchor.conf
# 显示统计
pfctl -s info
pfctl -si
# 重置统计
pfctl -z
# 调试
pfctl -x debug
三、PF 高级特性
3.1 表(Table)
PF
# 静态表
table <ssh_allow> { 192.168.1.0/24, 10.0.0.0/8 }
# 动态表
table <badhosts> persist
# 文件加载表
table <rfc1918> persist file "/etc/rfc1918"
# 文件内容:
# 10.0.0.0/8
# 172.16.0.0/12
# 192.168.0.0/16
# 使用表
block in quick on egress from <badhosts> to any
pass in on egress from <ssh_allow> to any port ssh
3.2 锚点(Anchor)
PF
# 主配置
anchor "external/*"
# 外部规则文件
# /etc/pf.anchors/external/ssh
pass in quick on egress proto tcp to port ssh \
keep state \
(max-src-conn 3, max-src-conn-rate 5/30, overload <ssh_overload> flush global)
# 加载
# pfctl -a "external/ssh" -f /etc/pf.anchors/external/ssh
3.3 限速
PF
# 限制 SSH 连接
pass in on egress proto tcp to port ssh \
keep state \
(max-src-conn 3, \
max-src-conn-rate 5/30, \
overload <ssh_overload> flush global)
# 限制所有流量
pass in on egress \
keep state \
(max-src-states 100, \
max-src-conn 50, \
max-src-nodes 200, \
overload <overload> flush global)
3.4 状态过滤
PF
# keep state
pass in on egress proto tcp to port 80 keep state
# modulate state(TCP 序列号调制)
pass in on egress proto tcp to port 80 modulate state
# synproxy state(SYN 代理防御 SYN flood)
pass in on egress proto tcp to port 80 synproxy state
3.5 NAT 与重定向
PF
# 出站 NAT
nat pass on egress from 192.168.1.0/24 to any -> (egress)
# 入站重定向
rdr pass on egress proto tcp to port 80 -> 192.168.1.10 port 80
rdr pass on egress proto tcp to port 443 -> 192.168.1.10 port 443
# 端口范围
rdr pass on egress proto tcp to port 8000:8999 -> 192.168.1.10
# binat(双向 NAT)
binat on egress from 192.168.1.10 to any -> 1.2.3.4
3.6 流量整形(ALTQ)
PF
# 启用 ALTQ
altq on em0 cbq bandwidth 100Mb queue { std, http, ssh }
queue std bandwidth 50% cbq(default)
queue http bandwidth 40% priority 3 cbq(red)
queue ssh bandwidth 10% priority 7
# 应用到规则
pass out on em0 proto tcp to port 80 queue http
pass out on em0 proto tcp to port 22 queue ssh
3.7 日志
PF
# 启用日志
pass in log (all) on egress proto tcp to port 22
# 高级日志
pass in log (matches, to pflog0) on egress proto tcp to port 80
# 查看日志
tcpdump -n -e -ttt -r /var/log/pflog
3.8 CARP(冗余)
PF
# 主备同步
# 主节点
carp demote 0 # 不降级
# 同步规则
# /etc/pfsync.conf
pfsync syncpeer 192.168.1.1 syncif em1
3.9 authpf(Shell 认证)
SH
# /etc/pf.conf
anchor "authpf/*"
# /etc/authpf/authpf.rules
ssh_allow = "{ 192.168.1.0/24 }"
pass in quick on egress proto tcp from $user_ip to port ssh
3.10 高级匹配
PF
# 协议
pass in proto tcp
# 端口
pass in proto tcp to port 80
# 地址
pass in from 192.168.1.0/24
# 方向
pass in
pass out
# 接口
pass in on em0
# TOS/DSCP
pass in tos lowdelay
# ICMP 类型
pass in inet proto icmp to port unreachable
# TCP 标志
pass in proto tcp flags S/SA
# 最大 MTU
pass in max-pkt-rate 1000/1
四、PF 攻击面与绕过技术
4.1 已知 PF 漏洞
| CVE | 描述 | 影响版本 |
|---|---|---|
| CVE-2007-2833 | TCP 重组缺陷 | OpenBSD 4.0 |
| CVE-2008-1923 | IPv6 缺陷 | OpenBSD 4.2 |
| CVE-2012-1576 | ICMPv6 处理缺陷 | OpenBSD 5.0 |
| CVE-2015-1411 | PF IP 选项处理 | OpenBSD 5.7 |
| CVE-2018-19797 | PF 状态表缺陷 | OpenBSD 6.4 |
| CVE-2019-19612 | NAT64 转换缺陷 | OpenBSD 6.5 |
| CVE-2020-26116 | ICMPv6 错误 | OpenBSD 6.7 |
| CVE-2021-29662 | PF TCP 重组 | OpenBSD 6.8 |
| CVE-2022-0449 | PF TCP 重组 | OpenBSD 7.0 |
| CVE-2023-0547 | PF mbuf 处理 | OpenBSD 7.2 |
4.2 规则绕过技术
4.2.1 IP 分片攻击
PF
# 错误配置示例
# 仅检查第一个分片
pass in on egress proto tcp to port 80
# 攻击:发送分片包绕过端口检查
SH
# 攻击示例
# 1. 第一个分片是允许的端口
# 2. 第二个分片包含实际攻击载荷
# 3. PF 仅检查第一个分片
# 4. 内核重组
防御:
PF
# 启用分片重组
scrub in all fragment reassemble
4.2.2 TCP 标志操纵
PF
# 仅检查 SYN
pass in on egress proto tcp to port 80 flags S/SA
# 攻击:使用 FIN 标志发起扫描,PF 不阻止 FIN 扫描
SH
# 攻击:FIN 扫描
nmap -sF target.com
# PF 仅阻止 SYN
# FIN 扫描可以绕过
4.2.3 协议混淆
SH
# 攻击:使用 ICMP 隧道
icmpsh
# 数据在 ICMP 载荷中
# PF 不检查 ICMP 载荷
4.2.4 端口跳跃
SH
# 攻击:使用 53 端口
nc -p 53 target.com 80
# 源端口 53 看起来像 DNS
4.2.5 IP 欺骗
SH
# 攻击:使用可信 IP
hping3 -a 192.168.1.1 target.com
# 源 IP 是可信地址
# 绕过基于源 IP 的规则
4.2.6 IPv6 绕过
SH
# 攻击:使用 IPv6
nmap -6 target.com
# 如果规则仅针对 IPv4,IPv6 流量可以绕过
4.2.7 状态表耗尽
SH
# 攻击:消耗 PF 状态表
# 发送大量 SYN 包
hping3 --flood -S target.com
# PF 状态表耗尽
# 后续流量被丢弃或绕过
4.2.8 时间窗口攻击
SH
# 攻击:在 PF 规则重新加载时
# 通过 cron 定时规则切换
# 攻击窗口期内允许恶意流量
4.3 应用程序层绕过
4.3.1 HTTP 隧道
SH
# 攻击:HTTP CONNECT 代理
curl --proxytunnel -x proxy:8080 target.com:443
# 通过 HTTP 代理隧道
# PF 仅看到 HTTP 80 端口
4.3.2 DNS 隧道
SH
# 攻击:DNS 隧道
iodine
dnscat2
# 数据编码在 DNS 查询
# PF 不检查 DNS 载荷
4.3.3 ICMP 隧道
SH
# 攻击:ICMP 隧道
ptunnel
icmptunnel
# 数据封装在 ICMP 载荷
# PF 默认允许 ICMP
4.3.4 HTTPS 流量混淆
SH
# 攻击:CDN 域名
curl -H "Host: target.com" cdn.example.com
# 通过 CDN 中转
# 真实目标隐藏在 Host 头
4.4 内核漏洞
4.4.1 PF 内核模块漏洞
C
// 1. 触发 PF 内部缺陷
// 2. 缓冲区溢出
// 3. 整数溢出
// 4. 状态表损坏
// 5. 任意代码执行
4.4.2 ICMPv6 漏洞
C
// 利用 ICMPv6 错误处理
// 1. 构造恶意 ICMPv6 消息
// 2. 触发 PF 内部错误
// 3. 内核代码执行
4.5 NAT 绕过
4.5.1 NAT 探测
SH
# 1. 通过不同源 IP 探测
# 2. 时间差分析
# 3. TTL 分析
# 4. TCP 序列号分析
4.5.2 NAT 穿越
SH
# 1. STUN 协议
# 2. TURN 协议
# 3. ICE 协议
# 4. 各种 P2P 穿透技术
五、PF 防御最佳实践
5.1 默认拒绝
PF
# 默认拒绝
block all
# 显式允许
pass out keep state
pass in on egress proto tcp to port 22 keep state
pass in on egress proto tcp to port 80 keep state
pass in on egress proto tcp to port 443 keep state
5.2 规范化
PF
# 强制分片重组
scrub in all fragment reassemble
# 最小 MTU
scrub in all min-ttl 64
# MSS 设置
scrub in all max-mss 1440
# 验证 TTL
scrub in all set-tos lowdelay
5.3 防欺骗
PF
# 防 IP 欺骗
antispoof quick for { egress, lo0 }
# 阻止 bogon
table <bogon> persist file "/etc/bogon"
block in quick on egress from <bogon> to any
5.4 限速
PF
# 限速 SSH
pass in on egress proto tcp to port 22 \
keep state \
(max-src-conn 3, \
max-src-conn-rate 5/30, \
overload <ssh_overload> flush global)
# 限制连接数
pass in on egress \
keep state \
(max-src-states 200, \
max-src-conn 50, \
max-src-nodes 200, \
overload <overload> flush global)
# 限制包速率
pass in on egress \
keep state \
(max-pkt-rate 1000/1)
5.5 SYN 代理
PF
# SYN 代理防御 SYN flood
pass in on egress proto tcp to port 80 synproxy state
pass in on egress proto tcp to port 443 synproxy state
pass in on egress proto tcp to port 22 synproxy state
5.6 日志与监控
PF
# 启用日志
block in log (all) all
pass in log (all) on egress proto tcp to port 22
# 查看日志
tcpdump -n -e -ttt -r /var/log/pflog
# 实时监控
tcpdump -n -e -ttt -i pflog0
# 统计
pfctl -s info
5.7 表管理
SH
# 动态黑名单
# /etc/pf.blocked.conf
# 加载动态表
table <ssh_bruteforce> persist
# 自动化脚本
# /usr/local/bin/block_ssh.sh
#!/bin/sh
# 解析 auth.log 找出 SSH 暴力破解
# 添加到表
pfctl -t ssh_bruteforce -T add <ip>
5.8 监控规则
SH
# 1. 监控 PF 状态
pfctl -s info
# 2. 监控表
pfctl -t <table> -T show
# 3. 监控状态表大小
pfctl -s state | wc -l
# 4. 监控匹配
pfctl -s info -v
# 5. pflog
tcpdump -n -e -ttt -i pflog0
5.9 规则审计
SH
# 1. 检查规则语法
pfctl -nf /etc/pf.conf
# 2. 显示当前规则
pfctl -s rules
# 3. 显示 NAT
pfctl -s nat
# 4. 模拟测试
# 使用 vps 或测试环境
5.10 高级 PF 配置示例
PF
# /etc/pf.conf - 完整示例
# 宏
ext_if = "em0"
int_if = "em1"
lan_net = "192.168.1.0/24"
dmz_net = "10.0.1.0/24"
services_tcp = "{ 22, 80, 443, 25, 110, 143, 993, 995, 587 }"
services_udp = "{ 53, 123 }"
# 表
table <rfc1918> persist { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 }
table <bogons> persist file "/etc/bogons"
table <ssh_bruteforce> persist
table <badhosts> persist
table <whitelist> persist file "/etc/whitelist"
# 全局选项
set block-policy drop
set skip on lo0
set loginterface egress
set optimization aggressive
set ruleset-optimization none
set timeout { tcp.first 30, tcp.opening 60, tcp.established 1800, tcp.closing 60, tcp.finwait 30, tcp.closed 30, tcp.tsdiff 10 }
set limit { states 200000, src-nodes 50000, frags 50000, tables 5000, table-entries 1000000 }
set require-order yes
set fingerprints "/etc/pf.os"
# 规范化
scrub in all fragment reassemble min-ttl 64 max-ttl 255
# 防 IP 欺骗
antispoof quick for { lo0, $int_if, egress }
# 阻止 bogon
block in quick on egress from <bogons> to any
block out quick on egress from any to <bogons>
# 默认拒绝
block all
# 出站
pass out keep state
# 限制 ICMP
pass out inet proto icmp icmp-type { echoreq, unreach, timex }
pass in on egress inet proto icmp icmp-type { unreach, timex }
# ICMPv6 必要
pass in on egress inet6 proto ipv6-icmp
pass in on egress inet6 proto icmp6
# LAN 出站
pass in on $int_if from $lan_net to any keep state
# DMZ 服务
pass in on egress proto tcp to port $services_tcp \
keep state \
(max-src-conn 100, \
max-src-states 200, \
overload <badhosts> flush global)
# UDP 服务
pass in on egress proto udp to port $services_udp keep state
# 主动模式 FTP
pass in on egress proto tcp to port ftp keep state
pass in on egress proto tcp to port 49152:65535 keep state
# 阻断 SSH 暴力破解
pass in on egress proto tcp to port ssh \
keep state \
(max-src-conn 3, \
max-src-conn-rate 5/30, \
overload <ssh_bruteforce> flush global)
# 阻止黑名单
block in quick on egress from <badhosts> to any
block in quick on egress from <ssh_bruteforce> to any
六、应急响应
6.1 发现被绕过
SH
# 1. 收集 PF 状态
pfctl -s info > /tmp/pf_info.txt
pfctl -s state > /tmp/pf_state.txt
pfctl -s rules > /tmp/pf_rules.txt
pfctl -s nat > /tmp/pf_nat.txt
pfctl -s Anchors > /tmp/pf_anchors.txt
# 2. 查看 pflog
tcpdump -n -e -ttt -r /var/log/pflog > /tmp/pflog.txt
# 3. 检查表
for table in $(pfctl -s Tables); do
echo "=== $table ==="
pfctl -t "$table" -T show
done
# 4. 网络抓包
tcpdump -i pflog0 -w /tmp/capture.pcap
# 5. 检查主机
netstat -an
lsof -i -P -n
6.2 应急封锁
SH
# 1. 紧急封锁 IP
pfctl -t badhosts -T add 192.168.1.100
# 2. 临时规则
echo 'block in quick on egress from 192.168.1.100 to any' | pfctl -f -
# 3. 备份当前规则
pfctl -s rules > /tmp/pf_rules_backup.txt
# 4. 加载紧急规则
pfctl -f /etc/pf.emergency.conf
# 5. 隔离网络
ifconfig em0 down
6.3 取证
SH
# 1. 收集所有日志
cp /var/log/pflog /tmp/
cp /var/log/messages /tmp/
cp /var/log/authlog /tmp/
cp /var/log/daemon /tmp/
# 2. 收集系统状态
ps auxww > /tmp/processes.txt
netstat -anv > /tmp/network.txt
lsof > /tmp/open_files.txt
# 3. 收集配置
cp /etc/pf.conf /tmp/
cp -R /etc/pf.anchors/ /tmp/ 2>/dev/null
七、PF 在云时代的发展
7.1 容器化 PF
SH
# 在容器中运行 PF
# macvlan + 容器网络
# VNET + Jail
# VETH + Linux 容器
7.2 集群化 PF
SH
# CARP 集群
# pfsync 同步
# ECMP 负载均衡
7.3 与现代技术集成
SH
# 1. 与 SIEM 集成
# pflog -> syslog -> SIEM
# 2. 与云集成
# AWS Security Group + PF
# Azure NSG + PF
# 3. 与自动化集成
# Ansible 部署 PF
# Terraform 部署
总结
PF 防火墙是 BSD 系统的核心安全组件,提供了强大的状态过滤、流量整形、NAT 等功能。攻击者通过 IP 分片、协议混淆、隧道技术、内核漏洞等手段尝试绕过 PF。完整防御需要:
- 默认拒绝:最小开放原则
- 规范化:分片重组、TOS 设置
- 防欺骗:antispoof、bogon 列表
- 限速:限制连接数、连接速率
- SYN 代理:防御 SYN flood
- 持续监控:pflog、状态表
- 及时更新:应用安全补丁
- 规则审计:定期审查规则
- 纵深防御:多层安全机制
- 应急响应:建立快速响应流程
随着网络攻击持续演化,PF 也在不断增强(match、table、锚点等),但攻击者会持续寻找新的绕过技术。保持警惕、定期更新、严格审计是维护 PF 安全的核心。