Skip to content

运行时封禁名单示例 ​

源代码位于 examples/waf-ban/。

封禁是运维态而非配置:通过管理后台或 /api/v1/waf/bans 接口创建,保存在后台数据库, 不会写入 ingress.yaml。

配置(启用后台与受信代理) ​

yaml
# Runtime ban list example.
#
# Bans are NOT configured here. They are created from the Admin Console
# (WAF -> 封禁名单) or via POST /api/v1/waf/bans, stored in the admin database,
# and injected into the running ingress as an immutable snapshot. Publishing a
# ban never reloads this file, so caches and rate-limit counters are untouched.
#
# This file only enables the pieces the ban list depends on:
#   - admin console (where bans are managed)
#   - waf.trusted_proxies (so the client IP behind a load balancer is real;
#     banning a proxy address is rejected by the API on purpose)
version: v1
port: 8080

admin:
  enabled: true
  port: 9080
  database:
    driver: sqlite
    dsn: file:./admin.db?cache=shared&_fk=1
  web:
    dev_proxy: true

waf:
  # The ban list works even with waf.enabled: false — it is deliberately
  # independent from the WAF switch so an emergency "turn WAF off" cannot
  # silently drop active bans. Enabling WAF here just adds signature rules.
  enabled: true
  # Only trust X-Forwarded-For from the proxy tier. Without this, the client IP
  # is the raw TCP peer and banning it would hit the load balancer instead.
  trusted_proxies:
    - 10.0.0.0/8
    - 172.16.0.0/12

rules:
  - host: api.example.com
    backend:
      service:
        name: api
        port: 8080
        protocol: http

创建、试算与解封 ​

bash
# 封禁前试算影响面
curl -s -X POST http://127.0.0.1:9080/api/v1/waf/bans/preview \
  -H 'Content-Type: application/json' \
  -d '{"kind":"ip","value":"203.0.113.42"}'

# 封禁 IP 24 小时
curl -s -X POST http://127.0.0.1:9080/api/v1/waf/bans \
  -H 'Content-Type: application/json' \
  -d '{"kind":"ip","value":"203.0.113.42","ttl_seconds":86400,"reason":"端口扫描"}'

# 按 UA 封禁(Go 正则语法)
curl -s -X POST http://127.0.0.1:9080/api/v1/waf/bans \
  -H 'Content-Type: application/json' \
  -d '{"kind":"ua_regex","value":"(?i)(nikto|sqlmap)","ttl_seconds":604800}'

# 解封(立即恢复访问)
curl -s -X DELETE http://127.0.0.1:9080/api/v1/waf/bans/1

校验 ​

bash
ingress validate -c examples/waf-ban/ingress.yaml

语义、安全护栏与限制见 WAF 指南。

Released under the MIT License.