关于openresty:Nginx-快速集成免费-WAF

7次阅读

共计 3133 个字符,预计需要花费 8 分钟才能阅读完成。

OpenResty 是一个基于 Nginx 和 LuaJIT 的全功能 Web 应用服务器,它提供了一种弱小而灵便的形式来构建和扩大 Web 应用服务器,同时放弃了 Nginx 的高性能和可靠性。OpenResty 是 APISIX、Kong、Ingress Nginx 等网关类产品的根底,因而 OpenResty 及其衍生产品非常适合作为 WAF 防护的对立入口。

本次应用的收费 WAF 次要用了雷池社区版,挂一个官网链接(感兴趣的敌人能够尝试一下):https://waf-ce.chaitin.cn/。

本文讲述了如何利用收费的长亭雷池 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 配置文件,指定 host 及 port,以下为批改后的配置文件

......

    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