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

Leader_roncoo 2016-12-02 02:40:30
最近在帮朋友维护一个站点。这个站点是一个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防火墙。

原文详情地址:http://blog.csdn.net/leader_roncoo/article/details/53433006
...全文
716 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

19,613

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧