Linux下用nginx和naxsi搭建web防火墙,nginxnaxsi


Linux下用nginx和naxsi搭建web防火墙

  • Naxsi

Naxsi是一个开源,高性能,地位会规则,nginx的web应用程序防火墙模块,著名的web服务器和反向代理,它的目标是帮助人们保护web应用程序,对跨站脚本,SQL注入,跨站请求伪造,本地和远程文件包含漏洞。

这次就使用naxsi模块来完成WAF构建。

  • 下载Naxsi以及编译

下载:master naxsi-master.zip

编译:./configure –prefix=$TOOLDIR/openresty/openRestyDir  

--with-http_stub_status_module \

--add-module=$TOOLDIR/openResty/nginx-http-concat-master \

--add-module=$TOOLDIR/openResty/naxsi-master/naxsi_src \

--with-http_realip_module \

--with-http_ssl_module \

--with-luajit

 

gmake

gmake install

 

   ps: 这些模块其实在安装openresty的时候已经添加并且编译了,所以这一步可以省略。

 

  • 定义一个虚拟主机的安全策略

cd /open/thirdparty/openrestyDir/nginx/conf/naxsi_conf

vi mysite.rules

mysite.rules内容如下:

//开启自学习模式

#LearningMode;

//开启naxsi 

SecRulesEnabled; 

//关闭规则

#SecRulesDisabled;

//定义请求被拒的位置,nginx.conf中有相关配置

DeniedUrl "/RequestDenied";

//确定naxsi何时采取行动

# check rules  

CheckRule "$XSS >= 4" BLOCK;

CheckRule "$TRAVERSAL >= 4" BLOCK;

CheckRule "$EVADE >= 8" BLOCK;

CheckRule "$UPLOAD >= 8" BLOCK;

CheckRule "$RFI >= 8" BLOCK;

CheckRule "$SQL >= 8" BLOCK

将mysite.rules文件在nginx.conf中配置,如图:

且配置被拒请求的位置:返回403状态码

location /RequestDenied {

        return 403;

    }

 

四、添加naxsi核心配置naxsi_core.rules

在nginx.conf中添加naxsi的核心配置: 

  • 白名单规则配置

这是这次用到的白名单规则,我们可以根据自身的要求添加白名单规则,然后将规则include到server中的location中。

总结:

在查阅了一些资料后,我对naxsi的理解就是,帮助我们抵挡一些不合法的请求或者攻击,可以帮我们过滤某些请求。而这其中最重要的就是mysite.rules、naxsi_white.rules文件,他们都属于防火墙中的规则,mysite.rules文件是对不合法请求的处理,而naxsi_white.rules则是规定什么url合法的(当然不限于url,还有参数,get和post等),哪些是不合法的。这些文件都在nginx.conf文件中include,这样在nginx启动的时候,就会加载到这些配置文件了,也会对请求做出处理。

步骤如下:

解析http请求:协议解析模块,也就是白名单规则。

匹配规则:规则检测模块,匹配规则库

防御动作:return 403等

日志记录:将过滤日志记录下来

     

    

 

 

相关内容

    暂无相关文章