使用Nginx+lua搭建waf防火墙

9/24/2022 Nginx

# 安装部署

部署步骤:

  1. 安装OpenResty
  2. 下载waf的lua脚本
  3. OpenResty中引入lua脚本
  4. 配置waf规则:/usr/local/openresty/nginx/conf/waf/rule-config

# OpenResty安装

OpenResty (opens new window) 是一个基于 Nginx (opens new window) 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

来源:https://openresty.org/cn/linux-packages.html

wget https://openresty.org/package/centos/openresty.repo
mv openresty.repo /etc/yum.repos.d/
yum install -y openresty

# WAF部署

下载waf脚本

wget https://github.com/zhanguangcheng/nginx-lua-waf/archive/refs/heads/master.zip
unzip master.zip
cp -r ./nginx-lua-waf-master/waf /usr/local/openresty/nginx/conf/

在OpenResty配置文件中引入脚本

vim /usr/local/openresty/nginx/conf/nginx.conf

http {
    lua_shared_dict limit 50m;
    lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua;;";
    init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
    access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";
    header_filter_by_lua_file "/usr/local/openresty/nginx/conf/waf/header_filter.lua";

	server {

	}
}

启动

ln -s /usr/local/openresty/lualib/resty/ /usr/local/openresty/nginx/conf/waf/resty

# 检查配置语法
openresty -t

systemctl start openresty
systemctl enable openresty

# 测试

  • 访问非法路径,使用浏览器访问:http://192.168.1.217/.svn/entries
  • 在url参数、POST参数、Cookie中包含非法字符串时
  • 上传木马文件,如xxx.php
  • 访问频率过快
  • ...

都将会触发防火墙,或者直接返回403无权限访问

e98e427428b3484e80ba44f541bbf2e0.png

# 更多

zhanguangcheng/nginx-lua-waf (opens new window)

🕑 最后更新时间: 2022-09-24 23:24