为了更好的防护系统,降低系统被攻击的可能性,在无第三方商业版web应用防火墙的前提下,针对nginx添加lua实现WAF防火墙。
nginx | luajit | ngx_devel_kit | lua-nginx-module | 备注 |
---|---|---|---|---|
V1.16.0 | V2.2.1 | v0.3.1 | v0.10.14rc5 | 建议lua-nginx-module使用这个版本v0.10.14rc5 |
注意:这里需要特别强调一下,lua-nginx-module版本V0.10.20、V0.10.19 、V0.10.15均进行了尝试,均报如下错误,这里换成了v0.10.14rc5版本,则正常。
完整工具下载
#1、安装依赖包
yum install wget vim dos2unix lrzsz unzip zip -y
yum -y install pcre-devel
yum -y install openssl openssl-devel
yum install gcc* -y
#2、编译安装
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar axf nginx-1.16.0.tar.gz
cd nginx-1.16.0
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module --with-http_v2_module \
--with-http_stub_status_module --with-pcre --with-stream
make && make install
LuaJIT的意思是Lua Just-In-Time,是即时的Lua代码解释器。必须去github下载否则运行是会出现报错,项目地址:https://github.com/openresty/luajit2
#1、下载软件包
wget https://github.com/openresty/luajit2/archive/refs/heads/v2.1-agentzh.zip
unzip v2.1-agentzh.zip
cd luajit2-2.1-agentzh
make install PREFIX=/usr/local/luajit
#2、添加环境变量
vim /etc/profile
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1
source /etc/profile
#3、加载lua库到ld.so.conf文件
echo "/usr/local/luajit/lib/" >> /etc/ld.so.conf
#4、执行ldconfig让动态函式库加载到缓存中
ldconfig
wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
tar axf v0.3.1.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.14rc5.zip
unzip v0.10.14rc5.zip
#1、备份(必须)
cp -r /usr/local/nginx /usr/local/nginx_`date +%F`
#2、重新编译安装
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module --with-http_v2_module \
--with-http_stub_status_module --with-pcre --with-stream \
--add-module=/data/pkgs/lua-nginx-module-0.10.14rc5 \
--add-module=/data/pkgs/ngx_devel_kit-0.3.1
make && make install
#1、调用lua测试,编辑Nginx.conf 添加/lua
[root@localhost nginx-1.16.0]# vim /usr/local/nginx/conf/nginx.conf
location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("Hi,欢迎来到英雄联盟")';
}
[root@localhost nginx-1.16.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx-1.16.0]# /usr/local/nginx/sbin/nginx
项目地址:https://github.com/loveshell/ngx_lua_waf
#1、下载waf模块
wget https://github.com/loveshell/ngx_lua_waf/archive/refs/heads/master.zip
unzip master.zip
mkdir /usr/local/nginx/conf/waf
mv ngx_lua_waf-master/* /usr/local/nginx/conf/waf
vim /usr/local/nginx/conf/nginx.conf
lua_shared_dict limit 50m;
lua_package_path "/usr/local/nginx/conf/waf/?.lua";
init_by_lua_file "/usr/local/nginx/conf/waf/init.lua";
access_by_lua_file "/usr/local/nginx/conf/waf/waf.lua";
说明:过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割。
浏览器访问:http://192.168.1.212/lua?id=1 or 1=1
1、默认规则没有这点的防护,访问结果如下图所示:
2、配置规则
在args规则中添加比如\sor\s+,然后/usr/local/nginx/sbin/nginx -s reload
vim /usr/local/nginx/conf/waf/wafconf/args
\sor\s+
/usr/local/nginx/sbin/nginx -s reload
yum install
ab -n1000 -c 100 http://192.168.1.212/lua
1、不添加waf规则的时候,对网站进行ab压力测试,再次访问是没有问题的
2、开启ccdeny防护规则
vim /usr/local/nginx/conf/waf/config.lua
CCDeny="on" #开启ccdeny的功能
CCrate="100/60" #限制每分钟100次访问
如下图所示:
3、通过ab工具对网站进行压力测试,再次访问就变成503了
如下图所示:
更多详细内容请参考:Linux运维实战总结