🚀 南墙WAF实战部署指南 | 集成人机验证与防护规则
南墙WAF作为国产高性能Web应用防火墙,可无缝对接现有架构。以下是结合Cloudflare Turnstile的增强配置方案:
一、部署架构优化
流量路径:
用户 → Cloudflare CDN → 南墙WAF(前置) → OpenResty/NPM → Halo
二、核心配置步骤
1. 启用人机验证模块
# 进入南墙容器
docker exec -it nwaf bash
# 编辑防护配置文件
vi /usr/local/nwaf/conf/nwaf_rules/anti_cc.conf
# 开启验证码模式
anti_cc_enable on;
verify_method challenge; # 可选:challenge(基础验证) | turnstile(集成Cloudflare)
2. 对接Cloudflare Turnstile
# 在server块添加
set $turnstile_sitekey "YOUR_SITE_KEY";
set $turnstile_secret "YOUR_SECRET_KEY";
location /verify {
internal;
proxy_method POST;
proxy_pass https://challenges.cloudflare.com/turnstile/v0/siteverify;
proxy_set_header Content-Type "application/x-www-form-urlencoded";
proxy_set_header Host challenges.cloudflare.com;
proxy_pass_request_body on;
}
3. 定制防护规则
创建/usr/local/nwaf/conf/nwaf_rules/custom_verify.conf
:
location / {
access_by_lua_block {
-- 触发验证条件:QPS>50或异常UA
local qps = ngx.shared.status:get("cc_qps") or 0
if qps > 50 or ngx.var.http_user_agent == "badbot" then
local args = ngx.req.get_uri_args()
if not args.cf_verify_token then
ngx.header.Content-Type = "text/html"
ngx.say([[
<!DOCTYPE html>
<html>
<head><title>安全验证</title></head>
<body>
<div class="cf-turnstile" data-sitekey="$turnstile_sitekey"></div>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
</body>
</html>
]])
ngx.exit(200)
else
-- 验证token有效性
local res = ngx.location.capture("/verify", {
args = "secret="..ngx.var.turnstile_secret.."&response="..args.cf_verify_token
})
if res.status ~= 200 then
ngx.exit(403)
end
end
end
}
}
三、智能防护策略
1. 动态规则引擎
# 在nwaf_rules/anti_cc.conf中添加
dynamic_rule_engine on;
rule_check_interval 5s; # 每5秒更新规则
# 自动学习阈值
auto_learning on;
learning_period 3600; # 1小时学习期
2. CC攻击防护
http {
lua_shared_dict cc_cache 100m;
anti_cc {
enable on;
interval 60; # 统计周期(秒)
burst 100; # 允许突发请求数
ratio 0.5; # 触发验证的请求比例
duration 300; # 封禁时长(秒)
error_code 503; # 拦截响应码
}
}
四、日志分析与监控
1. 实时攻击仪表盘
# 安装ELK套件
docker run --name nwaf-elk -p 5601:5601 -p 9200:9200 -p 5044:5044 \
-v elk-data:/var/lib/elasticsearch sebp/elk:latest
# 配置南墙日志输出
log_format json_escape escape=json
'{'
'"time":"$time_iso8601",'
'"client":"$remote_addr",'
'"method":"$request_method",'
'"uri":"$request_uri",'
'"status":"$status",'
'"waf_action":"$waf_action"'
'}';
access_log syslog:server=elk-ip:5140 json_escape;
2. 关键指标监控
# Prometheus配置示例
- job_name: 'nwaf'
static_configs:
- targets: ['nwaf-ip:9145']
metrics_path: /metrics
五、性能调优
1. 连接池优化
events {
worker_connections 65535;
multi_accept on;
use epoll;
}
http {
lua_socket_pool_size 100;
lua_socket_keepalive_timeout 60s;
}
2. 缓存加速
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=waf_cache:10m inactive=60m;
location / {
proxy_cache waf_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_key "$scheme$request_method$host$request_uri";
}
📌 部署验证
- 模拟攻击测试:
# 触发CC防护
siege -c 100 -t 1M http://yourdomain.com/api/comments
- 验证Turnstile集成:
curl -X POST http://yourdomain.com/api/comments -d "cf_verify_token=INVALID_TOKEN"
# 应返回403状态码
💡 高级技巧
- 通过
waf_debug on;
开启调试模式分析拦截详情 - 使用
waf_exclude_rule_id
排除误报规则 - 结合GeoIP模块实现地域级访问控制