Stateless 和 Stateful规则

核心区别

特性 Stateless(无状态) Stateful(有状态)
检查方式 逐包检查,每个包独立 基于连接上下文检查
是否跟踪连接 ❌ 不跟踪 ✅ 跟踪流量流
考虑方向 ❌ 不考虑 ✅ 区分入站/出站
处理速度 ⚡ 快 🐢 较慢(可能延迟包)
规则复杂度 简单(5-tuple) 复杂(Suricata)
日志能力 ❌ 无 ✅ 支持流量和告警日志
规则语言 标准网络属性 Suricata 规则
默认行为 无默认 默认允许通过
类似于 VPC NACL VPC Security Group

The figure shows a firewall stateless engine, which inspects packets against stateless rules, and a firewall stateful engine, which inspects traffic flows against stateful rules. The stateless engine sits above and to the left of the stateful engine. One vertical grey arrow points down to the stateless engine from above. The stateless engine and the stateful engine each have a vertical green arrow labeled “pass” that comes from the bottom of the engine, joins the green arrow from the other engine and points down to the bottom of the figure. From the right side of the stateless engine, a grey arrow labeled “forward to stateful” points out and down to the stateful engine. Each engine also has a horizontal red arrow labeled “drop” that points right from the right side of the engine to a large red X. The stateful engine also has arrows indicating an alert that’s sent with drop and an optional alert that’s sent with pass.

Stateless(无状态)—— 逐包独立检查:

包1 ──► [检查] ──► Pass/Drop
包2 ──► [检查] ──► Pass/Drop    每个包独立判断
包3 ──► [检查] ──► Pass/Drop    不知道包之间的关系

Stateful(有状态)—— 基于连接上下文:

┌─────────────────────────────────────┐
│          Connection Context         │
│  (跟踪这是同一个 TCP 连接的包)        │
├─────────────────────────────────────┤
│ 包1 (SYN)      ──┐                  │
│ 包2 (SYN-ACK)  ──┼──► 整体判断       │
│ 包3 (ACK)      ──┤                  │
│ 包4 (DATA)     ──┘                  │
└─────────────────────────────────────┘

默认行为:

引擎 默认
Stateless 无默认,必须显式配置 Default Action
Stateful 默认允许通过(与 Security Group 相反!)

何时用哪个:

场景 推荐
快速黑名单/白名单 Stateless
简单 IP/端口过滤 Stateless
域名过滤 Stateful
深度包检测 Stateful
IDS/IPS 规则 Stateful
需要日志 Stateful
高性能优先 Stateless

最佳实践是组合使用它们:

流量进入
    │
    ▼
┌─────────────────────┐
│  Stateless Rules    │  ← 第一道:快速过滤明显的黑名单
│  - 已知恶意 IP       │
│  - 不需要的端口       │
└──────────┬──────────┘
           │ Forward
           ▼
┌─────────────────────┐
│  Stateful Rules     │  ← 第二道:深度检测剩余流量
│  - 域名过滤          │
│  - IPS 规则         │
│  - 内容检测          │
└──────────┬──────────┘
           │
           ▼
       目标资源

Stateless 快但简单(逐包、无上下文、像 NACL),Stateful 慢但强大(跟踪连接、支持 Suricata、像 SG 但默认允许)。实际使用中组合起来:Stateless 快速过滤 + Stateful 深度检测。