nginx + lua 构建网站防护waf

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx + lua 构建网站防护waf相关的知识,希望对你有一定的参考价值。

最近在帮朋友维护一个站点。这个站点是一个php网站。坑爹的是用IIS做代理。出了无数问题之后忍无可忍终于要我帮他切换到nginx上面,前期被不断的扫描和CC。最后找到了waf这样一个解决方案缓解一下。话不多说直接开始。


waf的作用:

防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
防止svn/备份之类文件泄漏
防止ApacheBench之类压力测试工具的攻击
屏蔽常见的扫描黑客工具,扫描器
屏蔽异常的网络请求
屏蔽图片附件类目录php执行权限
防止webshell上传

nginx 的话我选择春哥开源的:OpenResty一个伟大的项目。


好了步骤开始:


1、安装Luagit:

# wget http://luajit.org/download/LuaJIT-2.1.0-beta1.tar.gz

# tar -xvf LuaJIT-2.1.0-beta1.tar.gz

# cd LuaJIT-2.1.0-beta1

# make

# make install

#ln -sf luajit-2.1.0-beta1 /usr/local/bin/luajit


2、安装openresty:

 ./configure --prefix=/opt/openresty   --with-luajit     --without-http_redis2_module       --with-http_iconv_module

gmake

gmake install


3、测试openresty:

[[email protected] ngx_lua_waf]# cd /opt/openresty/nginx/conf/

[[email protected] conf]# cat nginx.conf

http {

    server { 

        listen 80;

        location / {

                 default_type text/html;

              content_by_lua_block {

                  ngx.say("HelloWorld")

                            }

                 }

     }

}

###

测试一下访问是否输出hello world,后面应该会有一些列的简介。

[[email protected] conf]# curl localhost

HelloWorld


4、下载开源项目:

[[email protected] nginx]# cd /opt/openresty/nginx/

[[email protected] nginx]# git clone https://github.com/loveshell/ngx_lua_waf.git


5、然后修改nginx添加配置,支持lua脚本地址,在http段位置:

lua_package_path "/opt/openresty/nginx/ngx_lua_waf/?.lua";  ###相关项目存放地址

lua_shared_dict limit 10m;                       ###存放limit表的大小

init_by_lua_file  /opt/openresty/nginx/ngx_lua_waf/init.lua; ###相应地址

access_by_lua_file /opt/openresty/nginx/ngx_lua_waf/waf.lua; ##相应地址


6、修改ngx_lua_waf相关配置:

[[email protected] ngx_lua_waf]# vim config.lua 

RulePath = "/opt/openresty/nginx/ngx_lua_waf/wafconf/"   ##指定相应位置

attacklog = "on"                            ##开启日志

logdir = "/opt/openresty/nginx/logs/hack/"           ##日志存放位置

UrlDeny="on"                              ##是否开启URL防护

Redirect="on"                             ##地址重定向

CookieMatch="on"                           ##cookie拦截

postMatch="on"                            ##post拦截

whiteModule="on"                           ##白名单

black_fileExt={"php","jsp"}                        

ipWhitelist={"127.0.0.1"}                    ##白名单IP

ipBlocklist={"1.0.0.1"}                     ##黑名单IP

CCDeny="on"                             ##开启CC防护        

CCrate="100/60"                          ##60秒内允许同一个IP访问100次


7、创建日志存放目录:

[[email protected] ngx_lua_waf]#mkdir /opt/openresty/nginx/logs/hack/

[[email protected] ngx_lua_waf]#chown -R nobody:nobody /opt/openresty/nginx/logs/hack/


8、启动nginx测试:

[[email protected] logs]# /opt/openresty/nginx/sbin/nginx 


9、网页访问一条测试:

技术分享


10、压力测试CC攻击:

把congfig.lua的频率改成如下:

CCDeny="on"

CCrate="50/60"

测试结果:

[[email protected] ngx_lua_waf]# ab -c 100 -n 100 http://192.168.63.242/index.heml

This is ApacheBench, Version 2.3

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/


Benchmarking 192.168.63.242 (be patient).....done



Server Software:        openresty/1.11.2.2

Server Hostname:        192.168.63.242

Server Port:            80


Document Path:          /index.heml

Document Length:        2078 bytes


Concurrency Level:      100

Time taken for tests:   0.052 seconds

Complete requests:      100

Failed requests:        49      ###因为做了现在,所以这么多是失败的。



到处已经构建成功了一套waf防御系统,非常感谢loveshell提供这么棒的waf开源项目,还有春哥的openresty.


后期起到更多的lua + nginx学习成果,第一篇完成,希望我们后期都能写出自己的waf防火墙。


http://www.roncoo.com/article/detail/126294


本文出自 “11949850” 博客,请务必保留此出处http://11959850.blog.51cto.com/11949850/1878797

以上是关于nginx + lua 构建网站防护waf的主要内容,如果未能解决你的问题,请参考以下文章

nginx+ngx_lua支持WAF防护功能

nginx安装ngx_lua_waf防护

安装nginx+ngx_lua支持WAF防护功能

让Nginx支持Lua导入过滤规则担任WAF角色

软WAF的另一种思路:基于Openresty+Naxsi的WAF实现

使用Nginx+Lua实现自定义WAF