您现在的位置是:首页 > 文章详情

nginx + lua 构建网站防护waf(一)

日期:2018-11-29点击:504

最近几天比较忙,帮朋友维护一个站点。这个站点是一个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:

[root@www ngx_lua_waf]# cd /opt/openresty/nginx/conf/

[root@www conf]# cat nginx.conf

http {

    server { 

        listen 80;

        location / {

                 default_type text/html;

              content_by_lua_block {

                  ngx.say("HelloWorld")

                            }

                 }

     }

}

###

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

[root@www conf]# curl localhost

HelloWorld


4、下载开源项目:

[root@www nginx]# cd /opt/openresty/nginx/

[root@www 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相关配置:

[root@www 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、创建日志存放目录:

[root@www ngx_lua_waf]#mkdir /opt/openresty/nginx/logs/hack/

[root@www ngx_lua_waf]#chown -R nobody:nobody /opt/openresty/nginx/logs/hack/


8、启动nginx测试:

[root@www logs]# /opt/openresty/nginx/sbin/nginx 


9、网页访问一条测试:

face/6HHjsZ4KasDNjpi7sj5TtEEWwesApXbD.png


10、压力测试CC攻击:

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

CCDeny="on"

CCrate="50/60"

测试结果:

[root@www 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防火墙。

原文链接:https://blog.roncoo.com/article/126294
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章