您的位置:首页 > 运维架构 > Nginx

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

2016-12-02 11:19 453 查看
最近在帮朋友维护一个站点。这个站点是一个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_modulegmakegmake install
3、测试openresty:[root@www ngx_lua_waf]# cd /opt/openresty/nginx/conf/[root@www conf]# cat nginx.confhttp { server { listen 80; location / { default_type text/html; content_by_lua_block { ngx.say("HelloWorld") } } }}###测试一下访问是否输出hello world,后面应该会有一些列的简介。[root@www conf]# curl localhostHelloWorld
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"} ##白名单IPipBlocklist={"1.0.0.1"} ##黑名单IPCCDeny="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、网页访问一条测试:


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.hemlThis is ApacheBench, Version 2.3Copyright 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.2Server Hostname: 192.168.63.242Server Port: 80
Document Path: /index.hemlDocument Length: 2078 bytes
Concurrency Level: 100Time taken for tests: 0.052 secondsComplete requests: 100Failed requests: 49 ###因为做了现在,所以这么多是失败的。

到处已经构建成功了一套waf防御系统,非常感谢loveshell提供这么棒的waf开源项目,还有春哥的openresty.
后期起到更多的lua + nginx学习成果,第一篇完成,希望我们后期都能写出自己的waf防火墙。
http://www.roncoo.com/article/detail/126294
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  waf nginx 运维技术