对自己网站:Nginx+Lua实现的WAF

Web应用防护系统(也称为:网站应用级入侵防御系统。英文:Web Application Firewall,简称: WAF)。利用国际上公认的一种说法:Web应用防火墙是通过执行一系列针对HTTP/HTTPS的安全策略来专门为Web应用提供保护的一款产品。

需求产生 由于原生态的Nginx的一些安全防护功能有限.

于是自己就在GitHub找到对应得教程

  • 功能列表:
  1. 支持IP白名单和黑名单功能,直接将黑名单的IP访问拒绝。
  2. 支持URL白名单,将不需要过滤的URL进行定义。
  3. 支持User-Agent的过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
  4. 支持CC攻击防护,单个URL指定时间的访问次数,超过设定值,直接返回403。
  5. 支持Cookie过滤,匹配自定义规则中的条目,然后进行处理(返回403)。
  6. 支持URL过滤,匹配自定义规则中的条目,如果用户请求的URL包含这些,返回403。
  7. 支持URL参数过滤,原理同上。
  8. 支持日志记录,将所有拒绝的操作,记录到日志中去。
  9. 日志记录为JSON格式,便于日志分析,例如使用ELKStack进行攻击日志收集、存储、搜索和展示。

Nginx + Lua部署

 
  1. [root@nginx-lua ~]# cd /usr/local/src
  2. [root@nginx-lua src]# wget 'http://nginx.org/download/nginx-1.12.1.tar.gz'
  3. [root@nginx-lua src]# wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz

下载当前最新的luajit和ngx_devel_kit (NDK)

 
  1. [root@nginx-lua src]# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
  2. [root@nginx-lua src]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
  3. [root@nginx-lua src]# wget wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.10.10.zip

创建Nginx运行的普通用户

 
  1. [root@nginx-lua src]# useradd -s /sbin/nologin -M www

解压NDK和lua-nginx-module,安装LuaJIT Luajit是Lua即时编译器。

 
  1. [root@openstack-compute-node5 src]# tar zxvf v0.3.0.tar.gz 解压后为ngx_devel_kit-0.3.0
  2. [root@openstack-compute-node5 src]# unzip -q v0.10.10.zip解压后为lua-nginx-module-0.10.10
  3. [root@webs-ebt src]# tar zxvf LuaJIT-2.0.5.tar.gz
  4. [root@webs-ebt src]# cd LuaJIT-2.0.5
  5. [root@webs-ebt LuaJIT-2.0.5]# make && make install

安装Nginx并加载模块

 
  1. [root@webs-ebt src]# tar zxf nginx-1.12.1.tar.gz
  2. [root@webs-ebt src]# tar zxvf pcre-8.41.tar.gz
  3. [root@webs-ebt src]# cd nginx-1.12.1
  4. [root@webs-ebt nginx-1.12.1]# export LUAJIT_LIB=/usr/local/lib
  5. [root@webs-ebt nginx-1.12.1]# export LUAJIT_INC=/usr/local/include/luajit-2.0
  6. [root@webs-ebt nginx-1.12.1]#./configure --user=www --group=www --prefix=/usr/local/nginx-1.12.1/ --with-pcre=/usr/local/src/pcre-8.41 --with-http_stub_status_module --with-http_sub_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --add-module=../ngx_devel_kit-0.3.0/ --add-module=../lua-nginx-module-0.10.10/
  7. [root@webs-ebt nginx-1.12.1]# make -j2 && make install
  8. [root@webs-ebt nginx-1.12.1]# ln -s /usr/local/nginx-1.12.1 /usr/local/nginx
  9. [root@webs-ebt nginx-1.12.1]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

安装完毕后,修改nginx.conf,增加第一个配置。

 
  1.  
  2. location /hello {
  3. default_type 'text/plain';
  4. content_by_lua 'ngx.say("hello,lua")';
  5. }
  6.  
  7. [root@webs-ebt src]# /usr/local/nginx/sbin/nginx -t
  8. [root@webs-ebt src]# /usr/local/nginx/sbin/nginx -t

然后访问http://xxx.xxx.xxx.xxx/hello 如果出现hello,lua。表示安装完成,然后就可以。

OpenResty部署

 
  1. 安装依赖包
  2. # yum install -y readline-devel pcre-devel openssl-devel
  3. # cd /usr/local/src
  4. 下载并编译安装openresty
  5. # wget "https://openresty.org/download/openresty-1.11.2.5.tar.gz"
  6. # tar zxf openresty-1.11.2.5.tar.gz
  7. # cd openresty-1.11.2.5
  8. # ./configure --prefix=/usr/local/openresty-1.11.2.5 \
  9. --with-luajit --with-http_stub_status_module \
  10. --with-pcre=/usr/local/src/pcre-8.41 --with-pcre-jit
  11. # gmake && gmake install
  12. # ln -s /usr/local/openresty-1.11.2.5 /usr/local/openresty
  13.  
  14. 测试openresty安装
  15. # vim /usr/local/openresty/nginx/conf/nginx.conf
  16. server {
  17. location /hello {
  18. default_type text/html;
  19. content_by_lua_block {
  20. ngx.say("HelloWorld")
  21. }
  22. }
  23. }
  24. [root@webs-ebt src]# /usr/local/openresty-1.11.2.5/nginx/sbin/nginx -t
  25. nginx: the configuration file /usr/local/openresty-1.11.2.5/nginx/conf/nginx.conf syntax is ok
  26. nginx: configuration file /usr/local/openresty-1.11.2.5/nginx/conf/nginx.conf test is successful
  27. # /usr/local/openresty/nginx/sbin/nginx
  28. Hello World
  29. # curl http://192.168.199.33/hello
  30. HelloWorld

最后WAF部署

 
  1. #git clone https://github.com/unixhot/waf.git
  2. #cp -a ./waf/waf /usr/local/openresty/nginx/conf/
  3.  
  4. 修改Nginx的配置文件,加入以下配置。注意路径,同时WAF日志默认存放在/tmp/日期_waf.log
  5. #WAF
  6. lua_shared_dict limit 50m;
  7. lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
  8. init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
  9. access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";
  10.  
  11. [root@openstack-compute-node5 ~]# /usr/local/openresty/nginx/sbin/nginx –t
  12. [root@openstack-compute-node5 ~]# /usr/local/openresty/nginx/sbin/nginx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值