Nginx 快速集成免费 WAF

ge031310月前 ⋅ 852 阅读

OpenResty 是一个基于 Nginx 和 LuaJIT 的全功能 Web 应用服务器,它提供了一种强大而灵活的方式来构建和扩展 Web 应用服务器,同时保持了 Nginx 的高性能和可靠性。OpenResty 是 APISIX、Kong、Ingress Nginx 等网关类产品的基础,因此 OpenResty 及其衍生产品非常适合作为 WAF 防护的统一入口。

本次使用的免费WAF主要用了雷池社区版,挂一个官网链接(感兴趣的朋友可以尝试一下):https://waf-ce.chaitin.cn/

图1.png

本文讲述了如何利用免费的长亭雷池 WAF 社区版,通过 lua-resty-t1k 插件为 OpenResty 增加安全防护,实现转发与检测服务分离的安全架构。

基础版

基础版以 OpenResty 与长亭雷池 WAF 社区版安装在同一台 Host 为例。

第一步 – 安装雷池

长亭雷池 WAF 社区版有多种安装方式,具体安装方式可以参考长亭雷池 WAF 社区版官网文档:https://waf-ce.chaitin.cn/posts/guide_install

如果之前已经安装了长亭雷池 WAF 社区版,确保版本 >= 2.0.0。长亭雷池 WAF 社区版管理页面左下角显示了当前版本。

第二步 – 配置 OpenResty

我们以 OpenResty 官方镜像 alpine-fat 为例,介绍如何开启长亭雷池 WAF 防护:

进入长亭雷池 WAF 社区版安装目录,确认当前目录下存在 resources/detecotr 目录,启动 OpenResty。

 docker run -d --name openresty -v $(pwd)/resources/detector:/opt/detector openresty/openresty:alpine-fat

进入 OpenResty 容器,使用 luarocks 安装 lua-resty-t1k 插件:

docker exec -it openresty bash
luarocks install lua-resty-t1k

修改 /etc/nginx/conf.d/default.conf 路径下的 OpenResty 配置,增加 /t 路径测试 WAF 防护。以下为完整配置,可以直接替换 /etc/nginx/conf.d/default.conf 文件:

server {
    listen       80;
    server_name  localhost;

    location /t {
        access_by_lua_block {
            local t1k = require "resty.t1k"

            local t = {
                mode = "block",
                host = "unix:/opt/detector/snserver.sock",
            }

            local ok, err, result = t1k.do_access(t, true)
            if not ok then
                ngx.log(ngx.ERR, err)
            end
        }

        header_filter_by_lua_block {
            local t1k = require "resty.t1k"
            t1k.do_header_filter()
        }

        content_by_lua_block {
            ngx.say("passed")
        }
    }

    location / {
        root   /usr/local/openresty/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }
}

验证并重载 OpenResty 配置:

openresty -t && openresty -s reload

第三步 – 验证

访问 /t/shell.php 验证防护效果:

# curl http://127.0.0.1/t/shell.php
{"code": 403, "success":false, "message": "blocked by Chaitin SafeLine Web Application Firewall", "event_id": "2005f374e2c44757a449b1071f284e3b"}

同时在长亭雷池 WAF 社区版能够看到对应拦截日志。

正常访问 /t 不会触发拦截:

# curl http://127.0.0.1/t/
passed

进阶版

实际生产环境中,长亭雷池 WAF 社区版可能与 OpenResty 不在同一个 Host,或者有多个 OpenResty 分布在不同 Host。这时就需要将检测服务映射到 Host 指定端口供其它 Host 通过网络访问。

定位到长亭雷池 WAF 社区版的安装目录,修改 compose.yaml,在 detector 部分增加端口映射,修改结果:

......
  detector:
    container_name: safeline-detector
    restart: always
    image: chaitin/safeline-detector:${IMAGE_TAG}
    volumes:
    - ${SAFELINE_DIR}/resources/detector:/resources/detector
    - ${SAFELINE_DIR}/logs/detector:/logs/detector
    - /etc/localtime:/etc/localtime:ro
    environment:
    - LOG_DIR=/logs/detector
    networks:
      safeline-ce:
        ipv4_address: ${SUBNET_PREFIX}.5
    cap_drop:
    - net_raw
    ports: ["8000:8000"] # 新增行
  mario:
    container_name: safeline-mario
    restart: always
.....

修改 resources/detector/snserver.yml 的检测服务配置文件,修改结果:

fusion_sofile: ./config/libfusion2.so
ip_location_db: ./GeoLite2-City.mmdb
# bind_addr: unix:///resources/detector/snserver.sock # 注释行
bind_addr: 0.0.0.0 # 新增行
listen_port: 8000 # 新增行
......

执行

docker compose up -d

使得配置修改生效,使用 nc 命令验证检测服务端口可达

# nc -zv ${长亭雷池 WAF 社区版 Host IP} 8000
Connection to ${长亭雷池 WAF 社区版 Host IP} 8000 port [tcp/*] succeeded!

修改 OpenResty 配置文件,指定 hostport,以下为修改后的配置文件

......

    location /t {
        access_by_lua_block {
            local t1k = require "resty.t1k"

            local t = {
                mode = "block",
                host = "长亭雷池 WAF 社区版 Host IP",
                port = 8000,
            }

            local ok, err, result = t1k.do_access(t, true)
            if not ok then
                ngx.log(ngx.ERR, err)
            end
        }

        header_filter_by_lua_block {
            local t1k = require "resty.t1k"
            t1k.do_header_filter()
        }

        content_by_lua_block {
            ngx.say("passed")
        }
    }

......

验证并重载 OpenResty 配置

openresty -t && openresty -s reload

之后就可以验证安全防护能力。

Enjoy a more secure OpenResty!

全部评论: 0

    相关推荐