Chaitin WAF
chaitin-waf 插件在将流量代理到上游之前,把请求信息发送到 SafeLine 检测服务。SafeLine 可识别 SQL 注入、跨站脚本、命令注入和路径遍历等 Web 威胁。
插件可以阻断检测到的威胁,也可以在不中断流量的情况下进行监控。路由级模式会覆盖插件元数据中配置的模式。
WAF 检测失败时的流量处理
默认情况下,WAF 无法执行检测时,插件会拒绝流量。将 allow_degradation 设为 true 可让流量继续通过。监控模式下,SafeLine 请求失败时仍会放行:
| 失败情况 | 禁用降级 | 启用降级 |
|---|---|---|
| 缺少插件元数据,或无法选出 SafeLine 检测器 | 拒绝请求 | 跳过 WAF 检测并放行 |
| 阻断模式下的 SafeLine 请求失败 | 拒绝请求 | 请求失败或超时后放行 |
| 监控模式下的 SafeLine 请求失败 | 放行 | 放行 |
SafeLine 成功响应时,仍由已配置的模式决定是阻断检测到的威胁,还是仅记录威胁。路由或服务可以覆盖共享降级设置,启用或禁用降级。有关可用设置和超时,请参阅 Chaitin WAF 配置。
APISIX 3.20.0 支持在 WAF 检测不可用时配置放行。API7 企业版 3.9.23 和 3.10.10 未提供此选项。
响应头
启用响应头后,插件可以添加以下字段:
| 响应头 | 说明 |
|---|---|
X-APISIX-CHAITIN-WAF | 处理结果。yes:SafeLine 已检查请求。no:匹配规则跳过了检查。off:插件模式禁用了检查。unhealthy:没有可用的检测器。err:插件处理失败。waf-err:与检测器通信失败。timeout:检测器请求超时。 |
X-APISIX-CHAITIN-WAF-TIME | SafeLine 请求的往返时间,单位为毫秒。 |
X-APISIX-CHAITIN-WAF-STATUS | SafeLine 检测器报告的状态。 |
X-APISIX-CHAITIN-WAF-ACTION | SafeLine 的判定:pass 或 reject。 |
X-APISIX-CHAITIN-WAF-ERROR | 包含错误消息的调试响应头。 |
X-APISIX-CHAITIN-WAF-SERVER | 标识所选检测器的调试响应头。 |
append_waf_resp_header 控制标准响应头。只有启用标准响应头后,append_waf_debug_header 才会添加错误和检测器响应头。调试响应头可能暴露检测器详情和错误消息,请仅在排查问题时启用。
示例
以下示例配置 SafeLine,并使用该插件阻断或监控恶意请求。
设置 SafeLine
以下示例使用网关通过 T1K 协议、TCP 端口 8000 访问的 SafeLine 检测器。请按照官方部署指南安装 SafeLine。
在 SafeLine 安装目录中,使用以下设置更新现有的检测器配置:
bind_addr: 0.0.0.0
listen_port: 8000
保存配置后重启检测器:
docker restart safeline-detector
对于在同一主机上的 Docker 中运行的网关,将其连接到 SafeLine 的安装网络,并使用检测器容器名称:
export GATEWAY_CONTAINER=replace-with-gateway-container-name
docker network connect safeline-ce "$GATEWAY_CONTAINER"
export WAF_HOST=safeline-detector
对于其他部署方式,仅向网关网络开放 TCP 端口 8000,并将 WAF_HOST 设为可访问的 DNS 名称或 IP 地址:
export WAF_HOST=replace-with-safeline-detector-host
配置检测器地址
检测器节点列表是所有 chaitin-waf 实例共享的插件元数据。
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/plugin_metadata/chaitin-waf" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d @- <<EOF
{
"nodes": [
{
"host": "$WAF_HOST",
"port": 8000
}
]
}
EOF
插件元数据是全局集合,不能使用标签选择器隔离。在修改此条目之前,先导出完整集合:
adc dump -o adc-metadata.yaml --with-id \
--include-resource-type plugin_metadata
添加或更新 chaitin-waf 条目,同时保留 adc-metadata.yaml 中的所有其他条目:
plugin_metadata:
# 保留导出文件中的所有其他插件元数据条目。
chaitin-waf:
nodes:
- host: "${WAF_HOST}"
port: 8000
预览完整的元数据变更,确认其中没有意外的更新或删除:
adc diff -f adc-metadata.yaml \
--include-resource-type plugin_metadata
同步已审查的插件元数据集合:
adc sync -f adc-metadata.yaml \
--include-resource-type plugin_metadata
在部署所用的完整 GatewayProxy 清单中的 spec.pluginMetadata 下添加以下条目:
chaitin-waf:
nodes:
- host: replace-with-safeline-detector-host
port: 8000
通过部署常用的 Kubernetes 或 GitOps 流程应用更新后的完整清单。
当插件元数据和路由都未设置 mode 时,当前网关版本使用 monitor。
配置检测器地址后,以下示例会在一条路由上阻断威胁、在不阻断的情况下监控威胁,并在单独的路由上覆盖元数据级模式。
阻断恶意请求
以下示例演示如何配置一条路由,阻断 SafeLine 检测到的威胁。
配置路由:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes/chaitin-waf-block" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"uri": "/anything/*",
"plugins": {
"chaitin-waf": {
"mode": "block",
"append_waf_resp_header": true,
"append_waf_debug_header": true
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: chaitin-waf-block
labels:
docs-example: chaitin-waf-block
routes:
- name: chaitin-waf-block
uris:
- /anything/*
plugins:
chaitin-waf:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览此示例负责的变更,确认差异中没有意外的更新或删除:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-block
同步已审查的服务配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-block
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: chaitin-waf-block
spec:
plugins:
- name: chaitin-waf
config:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: chaitin-waf-block
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: PathPrefix
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: chaitin-waf-block
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: chaitin-waf-block
spec:
ingressClassName: apisix
http:
- name: chaitin-waf-block
match:
paths:
- /anything/*
upstreams:
- name: httpbin-external-domain
plugins:
- name: chaitin-waf
enable: true
config:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
应用配置:
kubectl apply -f chaitin-waf-ic.yaml
mode: block 会拒绝检测到的威胁。响应头会显示 SafeLine 的判定,并在此诊断示例中显示所选的检测器。
发送普通请求:
curl -i "http://127.0.0.1:9080/anything/get"
响应应为 200 OK,并包含 X-APISIX-CHAITIN-WAF-ACTION: pass。
发送包含 SQL 注入的请求:
curl -i "http://127.0.0.1:9080/anything/get?id=1%20union%20select%201,2,3"
响应应包含类似以下的字段:
HTTP/1.1 403 Forbidden
X-APISIX-CHAITIN-WAF-ACTION: reject
X-APISIX-CHAITIN-WAF-STATUS: 403
X-APISIX-CHAITIN-WAF: yes
{"code": 403, "success":false, "message": "blocked by Chaitin SafeLine Web Application Firewall", "event_id": "..."}
监控威胁而不阻断
以下示例演示如何在不中断流量的情况下监控检测到的威胁。
将共享元数据模式设为 monitor,同时保留之前配置的检测器节点:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/plugin_metadata/chaitin-waf" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d @- <<EOF
{
"nodes": [
{
"host": "$WAF_HOST",
"port": 8000
}
],
"mode": "monitor"
}
EOF
在之前导出的完整 adc-metadata.yaml 中,向现有条目添加 mode: monitor:
plugin_metadata:
# 保留导出文件中的所有其他插件元数据条目。
chaitin-waf:
nodes:
- host: "${WAF_HOST}"
port: 8000
mode: monitor
预览完整的元数据变更,确认其中没有意外的更新或删除:
adc diff -f adc-metadata.yaml \
--include-resource-type plugin_metadata
同步已审查的插件元数据集合:
adc sync -f adc-metadata.yaml \
--include-resource-type plugin_metadata
在完整 GatewayProxy 清单中的 spec.pluginMetadata 下,向 chaitin-waf 条目添加 mode: monitor:
chaitin-waf:
nodes:
- host: replace-with-safeline-detector-host
port: 8000
mode: monitor
通过部署常用的 Kubernetes 或 GitOps 流程应用更新后的完整清单。
创建一条继承元数据模式的路由:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes/chaitin-waf-monitor" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"uri": "/monitor/*",
"plugins": {
"chaitin-waf": {
"append_waf_resp_header": true,
"append_waf_debug_header": true
},
"proxy-rewrite": {
"regex_uri": ["^/monitor/(.*)", "/anything/$1"]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: chaitin-waf-monitor
labels:
docs-example: chaitin-waf-monitor
routes:
- name: chaitin-waf-monitor
uris:
- /monitor/*
plugins:
chaitin-waf:
append_waf_resp_header: true
append_waf_debug_header: true
proxy-rewrite:
regex_uri:
- ^/monitor/(.*)
- /anything/$1
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览此示例负责的变更,确认差异中没有意外的更新或删除:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-monitor
同步已审查的服务配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-monitor
复用上一示例中的 httpbin-external-domain 上游,创建一条 chaitin-waf 配置中省略 mode 的路 由:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: chaitin-waf-monitor
spec:
plugins:
- name: chaitin-waf
config:
append_waf_resp_header: true
append_waf_debug_header: true
- name: proxy-rewrite
config:
regex_uri:
- ^/monitor/(.*)
- /anything/$1
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: chaitin-waf-monitor
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: PathPrefix
value: /monitor
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: chaitin-waf-monitor
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: chaitin-waf-monitor
spec:
ingressClassName: apisix
http:
- name: chaitin-waf-monitor
match:
paths:
- /monitor/*
upstreams:
- name: httpbin-external-domain
plugins:
- name: chaitin-waf
enable: true
config:
append_waf_resp_header: true
append_waf_debug_header: true
- name: proxy-rewrite
enable: true
config:
regex_uri:
- ^/monitor/(.*)
- /anything/$1
应用路由配置:
kubectl apply -f chaitin-waf-monitor-ic.yaml
通过监控路由发送 SQL 注入请求:
curl -i "http://127.0.0.1:9080/monitor/get?id=1%20union%20select%201,2,3"
请求应到达上游并返回 200 OK。响应头仍应包含 X-APISIX-CHAITIN-WAF-ACTION: reject 和 X-APISIX-CHAITIN-WAF-STATUS: 403,而网关错误日志会记录 chaitin-waf monitor mode: request would have been rejected。
在路由上覆盖监控模式
以下示例演示路由级配置如何在一条路由上覆盖共享监控模式。
在监控路由上设置 mode: block,以覆盖元数据级模式:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes/chaitin-waf-monitor" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"plugins": {
"chaitin-waf": {
"mode": "block",
"append_waf_resp_header": true,
"append_waf_debug_header": true
}
}
}'
在路由的插件配置中添加 mode: block:
services:
- name: chaitin-waf-monitor
labels:
docs-example: chaitin-waf-monitor
routes:
- name: chaitin-waf-monitor
uris:
- /monitor/*
plugins:
chaitin-waf:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
proxy-rewrite:
regex_uri:
- ^/monitor/(.*)
- /anything/$1
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
预览更新:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-monitor
同步已审查的服务配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-monitor
更新所用 API 的路由资源:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: chaitin-waf-monitor
spec:
plugins:
- name: chaitin-waf
config:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
- name: proxy-rewrite
config:
regex_uri:
- ^/monitor/(.*)
- /anything/$1
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: chaitin-waf-monitor
spec:
ingressClassName: apisix
http:
- name: chaitin-waf-monitor
match:
paths:
- /monitor/*
upstreams:
- name: httpbin-external-domain
plugins:
- name: chaitin-waf
enable: true
config:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
- name: proxy-rewrite
enable: true
config:
regex_uri:
- ^/monitor/(.*)
- /anything/$1
应用完整的更新后资源:
kubectl apply -f chaitin-waf-monitor-ic.yaml
再次发送 SQL 注入请求。此时路由应返回 403 Forbidden,表明路由配置的优先级高于插件元数据。
向 WAF 上报响应
网关向客户端发送响应后,SafeLine 可以通过响应上报获取响应状态、响应头和有大小限制的响应体样本。上报不会阻断或修改发送给客户端的响应。
在前面创建的阻断路由上启用响应上报:
- Admin API
- ADC
- Ingress Controller
更新路由的插件配置:
curl "http://127.0.0.1:9180/apisix/admin/routes/chaitin-waf-block" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"plugins": {
"chaitin-waf": {
"mode": "block",
"append_waf_resp_header": true,
"append_waf_debug_header": true,
"config": {
"log_resp": true,
"resp_body_size": 4
}
}
}
}'
在阻断示例的完整 adc.yaml 中,将 config 添加到现有的 chaitin-waf 插件配置:
plugins:
chaitin-waf:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
config:
log_resp: true
resp_body_size: 4
预览完整服务配置,确认没有意外变更:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-block
同步已审查的配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-block
将 config 添加到 chaitin-waf-ic.yaml 中现有的 chaitin-waf 条目:
- name: chaitin-waf
config:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
config:
log_resp: true
resp_body_size: 4
如果使用 ApisixRoute,请在插件条目中保留 enable: true。应用更新后的完整资源:
kubectl apply -f chaitin-waf-ic.yaml
通过该路由发送一个正常请求:
curl -i "http://127.0.0.1:9080/anything/get"
客户端应正常收到上游响应,其中包含类似以下的响应头:
HTTP/1.1 200 OK
X-APISIX-CHAITIN-WAF: yes
X-APISIX-CHAITIN-WAF-ACTION: pass
网关向客户端发送响应后才进行上报。若要在网关日志中验证上报,请在生效的 config.yaml 中将 nginx_config.error_log_level 设置为 debug,重载网关并再次发送请求。此设置控制 NGINX 错误日志的级别,与 debug.yaml 中的 APISIX 调试模式是两项独 立的设置。
网关日志应包含以下内容:
lua-resty-t1k: reported response
验证完成后恢复原有错误日志级别,以免产生不必要的调试日志。
响应上报默认禁用。本例启用上报并保留默认的 4 KiB 响应体限制。将响应体限制设为 0 可仅上报状态和响应头。内置忽略列表或 extra_ignored_content_types 所列的响应内容类型完全不会上报。
上报以异步方式发送,但在响应期间采集样本仍会占用网关内存。提高限制时请考虑并发响应,避免不必要地发送敏感响应数据,并在网关日志中监控响应上报错误。
响应上报自 API7 企业版 3.9.20、3.10.7 和 APISIX 3.19.0 起引入。
检查压缩的请求体
本例以原来的阻断路由为基础。若要检查使用 gzip 或 deflate 压缩的请求体,请在该路由上同时启用 request-decompress 和 chaitin-waf。如需继续上报响应,请保留上一个示例的 config 配置块。
- Admin API
- ADC
- Ingress Controller
更新路由上的插件:
curl "http://127.0.0.1:9180/apisix/admin/routes/chaitin-waf-block" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"plugins": {
"request-decompress": {},
"chaitin-waf": {
"mode": "block",
"append_waf_resp_header": true,
"append_waf_debug_header": true
}
}
}'
在阻断示例的完整 adc.yaml 中,添加与 chaitin-waf 并列的 request-decompress:
plugins:
request-decompress: {}
chaitin-waf:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
预览完整服务配置,确认没有意外变更:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-block
同步已审查的配置:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=chaitin-waf-block
在 chaitin-waf-ic.yaml 中现有的 chaitin-waf 条目前添加 Request Decompress:
- name: request-decompress
config: {}
- name: chaitin-waf
config:
mode: block
append_waf_resp_header: true
append_waf_debug_header: true
如果使用 ApisixRoute,请在两个插件条目中均保留 enable: true。应用更新后的完整资源:
kubectl apply -f chaitin-waf-ic.yaml
发送一个经过 gzip 压缩的 JSON 请求。如果没有 gzip 命令,请先安装。
printf '%s' '{"message":"hello from compressed request"}' | gzip | \
curl -i "http://127.0.0.1:9080/anything/post" -X POST \
-H "Content-Type: application/json" \
-H "Content-Encoding: gzip" \
--data-binary @-
响应应为 200 OK 并包含 X-APISIX-CHAITIN-WAF-ACTION: pass,同时表明上游收到了已解压的 JSON:
{
"json": {
"message": "hello from compressed request"
}
}
Request Decompress 在 rewrite 阶段处理请求体,先于 chaitin-waf 在 access 阶段的检测。请按 SafeLine 需要检查的最大请求设置 Request Decompress 的请求体大小上限。Request Decompress 自 API7 企业版 3.10.9 和 APISIX 3.20.0 起引入。