Nginx使用Naxsi搭建Web应用防火墙(WAF),防xss、防注入×××

Naxsi是一个开放源代码、高效、低维护规则的Nginx web应用防火墙(Web Application Firewall)模块。Naxsi的主要目标是加固web应用程序,以抵御SQL注入、跨站脚本、跨域伪造请求、本地和远程文件包含漏洞。
官网地址:https://github.com/nbs-system/naxsi

Naxsi 不要求任何特定的依赖,它需要的 libpcre ,libssl ,zlib ,gzip 这些 Nginx 已经集成了。

  • 下载Naxsi模块
[[email protected] soft]# wget https://github.com/nbs-system/naxsi/archive/master.zip
[[email protected] soft]# tar zxvf naxsi-master.zip
  • 重新编译Nginx添加Naxsi模块
[[email protected] ~]# cd /opt/openresty/nginx/sbin/   #进入当前运行的目录
[[email protected] sbin]# ./nginx -V                         #查看之前编译参数
nginx version: openresty/1.11.2.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../iconv-nginx-module-0.14 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --with-http_realip_module --with-pcre --with-http_ssl_module
[[email protected] sbin]# cd /opt/soft/openresty-1.11.2.5/   #进入源码目录
[[email protected] openresty-1.11.2.5]# ./configure --prefix=/opt/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../iconv-nginx-module-0.14 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --with-http_realip_module --with-pcre --with-http_ssl_module --add-module=/opt/soft/naxsi-master/naxsi_src/      #重新编译nginx,加入之前编译的参数,再加入naxsi模块,指定到naxsi的源码目录中的naxsi_src目录
......
./configure: error: no /opt/soft/openresty-1.11.2.5/../ngx_devel_kit-0.3.0/config was found       #报错,openresty的./nginx -V中模块路径../xxx的都是openresty默认添加的,这些路径是在根目录的build下,编译时候会自动加上
ERROR: failed to run command: sh ./configure --prefix=/opt/openresty/nginx/nginx \...
[[email protected] openresty-1.11.2.5]#
[[email protected] openresty-1.11.2.5]# ./configure --prefix=/opt/openresty/ --add-module=/opt/soft/naxsi-master/naxsi_src/ --with-luajit    #只需要加这几项
[[email protected] openresty-1.11.2.5]# gmake
[[email protected] openresty-1.11.2.5]# find . -type f -iname nginx
./build/nginx-1.11.2/objs/nginx
[[email protected] openresty-1.11.2.5]# cp /opt/openresty/nginx/sbin/nginx{,.20180803bak}   #这步很重要
[[email protected] openresty-1.11.2.5]# cp ./build/nginx-1.11.2/objs/nginx /opt/openresty/nginx/sbin/nginx
cp: overwrite ‘/opt/openresty/nginx/sbin/nginx’? y
cp: cannot create regular file ‘/opt/openresty/nginx/sbin/nginx’: Text file busy
[[email protected] openresty-1.11.2.5]# killall -9 nginx
[[email protected] openresty-1.11.2.5]# cp build/nginx-1.11.2/objs/nginx /opt/openresty/nginx/sbin/
cp: overwrite ‘/opt/openresty/nginx/sbin/nginx’? y
[[email protected] openresty-1.11.2.5]# cd !$
cd /opt/openresty/nginx/sbin/
[[email protected] sbin]# ./nginx -V
nginx version: openresty/1.11.2.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/openresty//nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --add-module=/opt/soft/naxsi-master/naxsi_src --with-http_ssl_module
[[email protected] sbin]#

复制Naxsi核心配置文件到nginx/conf下

[[email protected] build]# cp /opt/soft/naxsi-master/naxsi_config/naxsi_core.rules /opt/openresty/nginx/conf/

在nginx.conf配置文件中添加Naxsi核心配置文件

[[email protected] build]# cp /opt/openresty/nginx/conf/nginx.conf{,.20190804bak}
[[email protected] build]# vim /opt/openresty/nginx/conf/nginx.conf
......
http {
    include       mime.types;
    include       naxsi_core.rules;      #加载naxsi 核心规则文件
        default_type  application/octet-stream;
        ......
        }
......      

配置Naxsi规则,新建文件naxsi.rules

[[email protected] build]# vim /opt/openresty/nginx/conf/naxsi.rules
#LearningMode  启用学习模式,即拦截请求后不拒绝访问,只将触发规则的请求写入error_log选项定义的文件中。如果对规则产生的影响不太清楚,可以先设置为学习模式。启用学习模式不能起到拦截非法请求的防御的效果。建议先使用学习模式,规则测试完成后再启用拦截模式。这样可以避免出现对网站、服务器某些不可知的影响。
#SecRulesEnabled|SecRulesDisabled
SecRulesEnabled;      #启用Naxsi模块
DeniedUrl "/RequestDenied";     #拒绝的请求发送到内部URL
#check rules  设置各规则不同的触发阈值。 一旦该阈值触发,请求将被阻塞。
CheckRule "$SQL >= 8" BLOCK;
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 4" BLOCK;
CheckRule "$EVADE >= 4" BLOCK;
CheckRule "$XSS >= 8" BLOCK;
#naxsi log
error_log        logs/naxsi.log;    #nginx相对路径都是相对于根目录
[[email protected] build]# 

虚拟主机添加支持Naxsi防×××

[[email protected] build]# vim /opt/openresty/nginx/conf/nginx.conf
......
http {
        ......
                server {
                            ......
                                        location / {
                                                        ......
                                    include naxsi.rules;
                                                        ......
                                                       }
                                        ......
                                        error_page   500 502 503 504  /50x.html;
                    location = /50x.html {
                                    root   html;
                     }
                    location /RequestDenied {       #定义naxsi.rules中DeniedUrl返回的代码
                                 return 403;
                    }
                    error_page  403  /403.html;
                    location = /403.html {
                                     root   html;
                    }
                                        ......
                   }
                ......
       }
......
[email protected] build]# vim /opt/openresty/nginx/html/403.html
<html>
  <head>
    <title>Error 403 Request Denied</title>
  </head>
  <body>
    <h2>Error 403 Request Denied</h2>
    For some reasons,you request has been denied.
  </body>
<</html>
[[email protected] build]# /opt/openresty/nginx/sbin/nginx -t -c /opt/openresty/nginx/conf/nginx.conf
nginx: the configuration file /opt/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/openresty/nginx/conf/nginx.conf test is successful
[[email protected] build]# cd /opt/openresty/nginx/sbin/
[[email protected] sbin]# ./nginx -s reload

测试Naxsi
在一台测试机上执行:curl http://192.168.100.127/?a=%3C

[[email protected] ~]# curl http://192.168.100.127/?a=%3C
<html>
  <head>
    <title>Error 403 Request Denied</title>
  </head>
  <body>
    <h2>Error 403 Request Denied</h2>
    For some reasons,you request has been denied.
  </body>
<</html>
[[email protected] ~]# 

nginx服务器日志

[[email protected] sbin]# tail -500f /opt/openresty/nginx/logs/naxsi.log
2018/08/04 21:53:03 [error] 4824#0: *7 NAXSI_FMT: ip=10.10.13.100&server=192.168.100.127&uri=/&learning=0&vers=0.56&total_processed=3&total_blocked=1&block=1&cscore0=$XSS&score0=8&zone0=ARGS&id0=1302&var_name0=a, client: 10.10.13.100, server: localhost, request: "GET /?a=%3C HTTP/1.1", host: "192.168.100.127"

如果错误日志出现NAXSI_FMT的信息说明成功。

还可以添加白名单naxsi_BasicRule.conf

[[email protected] sbin]# vim /opt/openresty/nginx/conf/naxsi_BasicRule.conf
BasicRule wl:0 "mz:$ARGS_VAR:script";
BasicRule wl:0 "mz:$ARGS_VAR:id";
[[email protected] sbin]# ./nginx -s reload

表示xss×××正常是被拦截的,若被添加白名单,则不被拦截:此处是Get 参数名若为id 或者script,则不被拦截。

测试Naxsi

正常输入http://192.168.100.127 可以正常访问

在上面的地址后加上正常参数,例如:http://192.168.100.127/?id=1 或者 http://192.168.100.127/?id=1%20AND%201=1 返回的页面和上面一样,因为配置到白名单了

如果访问http://192.168.100.127/?name=40//and//1=1 会提示Error 403 Request Denied,含有条件注入被拦截了

访问http://192.168.100.127/?name=%28%29 会提示Error 403 Request Denied,含有特殊字符

白名单规则语法:
BasicRule wl:ID [negative] [mz:[$URL:target_url]|[match_zone]|[$ARGS_VAR:varname]|[$BODY_VARS:varname]|[$HEADERS_VAR:varname]|[NAME]]

wl:ID (White List ID) 哪些拦截规则会进入白名单
wl:0:把所有拦截规则加入白名单
wl:42:把ID为42的拦截规则加入白名单
wl:42,41,43:把ID为42, 41和43的拦截规则加入白名单
wl:-42:把所有拦截规则加入白名单,除了ID为42的拦截规则

mz:(Match Zones)
ARGS: GET的整个参数,如: foo=bar&in=%20
$ARGS_VAR: GET参数的参数名, 如:foo=bar&in=%20中的foo和in
$ARGS_VAR_X: 正则匹配的GET参数的参数名
HEADERS: 整个HTTP协议头
$HEADERS_VAR: HTTP协议头的名字
$HEADERS_VAR_X: 正则匹配的HTTP协议头的名字
BODY: POST的整个参数内容
$BODY_VAR: POST参数的参数名
$BODY_VAR_X: 正则匹配的POST参数的参数名
URL: URL(?前的)
URL_X: 正则匹配的URL(?前的)
FILE_EXT: 文件名 (POST上传文件时上传的文件名)

Naxsi 社区提供了一些常用的白名单规则,例如 wordpress 。可以在 https://github.com/nbs-system/naxsi-rules 下载白名单规则。

原文地址:http://blog.51cto.com/ityunwei2017/2154753

时间: 2024-08-16 06:14:28

Nginx使用Naxsi搭建Web应用防火墙(WAF),防xss、防注入×××的相关文章

NGINX安全防护之基于ngx_lua的web应用防火墙

NGINX安全防护 ngx_lua_waf安装说明文档 作者github地址: https://github.com/loveshell/ngx_lua_waf 转自作者说明文档: ngx_lua_waf是我一个基于ngx_lua的web应用防火墙. 代码很简单,开发初衷主要是使用简单,高性能和轻量级. 现在开源出来.其中包含我们的过滤规则.如果大家有什么建议和想fa,欢迎和我一起完善.   用途: 用于过滤post,get,cookie方式常见的web攻击 防止sql注入,本地包含,部分溢出,

技术分享:杂谈如何绕过WAF(Web应用防火墙)(转)

0×01开场白 这个议题呢,主要是教大家一个思路,而不是把现成准备好的代码放给大家. 可能在大家眼中WAF(Web应用防火墙)就是"不要脸"的代名词.如果没有他,我们的"世界"可能会更加美好.但是事与愿违.没有它,你让各大网站怎么活.但是呢,我是站在你们的这一边的,所以,今天我们就来谈谈如何绕过WAF吧.之所以叫做"杂谈",是因为我在本次演讲里,会涉及到webkit.nginx&apache等.下面正式开始:) 0×02直视WAF: 作为

使用Haproxy及Nginx搭建Web群集

什么是HAProxy? HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理,可以运行于大部分主流的Linux操作系统上.本次实验使用三台服务器搭建Web群集,Haproxy作为调度服务器,两台Nginx服务器作为节点服务器. 实验环境 主机 系统 IP地址 主要软件 Haproxy服务器 CentOS-7-x86_64 192.168.100.100 haproxy Nginx服务器1 CentOS-7-x86_64 192

CentOS7 上部署Haproxy及Nginx 搭建Web群集

HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理,可以运行于大部分主流的Linux操作系统上.本次实验使用三台服务器搭建Web群集,Haproxy作为调度服务器,两台Nginx服务器作为节点服务器. 实验环境 主机 | 系统 | IP地址 | 只要软件 主机 系统 IP地址 主要软件 haproxy服务器 CentOS-7-x86_64 192.168.100.101 haproxy Nginx 服务器 CentOS-7-

Linux网络服务--LAMP+Nginx+Squid搭建web集群

一.         项目名称         LAMP+Nginx+Squid搭建web集群环境 二.         项目拓扑 三.         项目描述 3.1 项目环境 某部队为了满足信息化政治工作建设需要,用以丰富官兵日常生活内容,活化教育形式,更好的建设部队人文环境,准备架设部队内部的网站服务器,并申请使用军内公网IP:1.1.1.1 基于以上情况为该部提出以下解决方案.   3.2 实施方案 3.2.1  服务器操作系统与软件版本选择 操作系统选择RedHat企业版5.10:

如何搭建web服务器 使用Nginx搭建反向代理服务器

转载   如何搭建web服务器 使用Nginx搭建反向代理服务器 :   http://blog.csdn.net/w13770269691/article/details/6977727 引言:最近公司有台服务器遭受DDOS攻击,流量在70M以上,由于服务器硬件配置较高所以不需要DDOS硬件防火 墙.但我们要知道,IDC机房是肯定不允许这种流量一直处于这么高的,因为没法具体知道后面陆续攻击的流量会有多大,如果流量过大就会导致整个IDC网络 瘫痪.我们都知道北方的数据中心和南方的数据中心从带宽出

python3.x +django + nginx + uwsgi 搭建web服务

最近一直在用django开发自己的网站.在开发和线上环境的对接过程中遇到了许多的坑.所以想以一个老鸟的经历来写一下怎么 搭建web服务 一.python3.x .django .nginx .uwsgi 的介绍: 哈哈自己baidu吧 二.安装python3环境 ----

WAF Web应用防火墙

WAF   Web应用防火墙 Web应用防火墙是通过执行一系列针对HTTP/HTTPS的安全策略来专门为Web应用提供保护的一款产品. 与传统防火墙不同,WAF工作在应用层,因此对Web应用防护具有先天的技术优势.基于对Web应用业务和逻辑的深刻理解,WAF对来自Web应用程序客户端的各类请求进行内容检测和验证,确保其安全性与合法性,对非法的请求予以实时阻断,从而对各类网站站点进行有效防护. F5的BIG-IP应用安全管理器(ASM)是一个先进的Web应用防火墙,可显著减少和控制数据.知识产权和

轻松使用Nginx搭建web服务器

如果读者以前做过web开发的话,就应该知道如何去搭建一个web服务器来跑你的web站点,在windows下你可能会选择去用IIS,十分的快捷,在linux下,你可能首先会想到apache,"一哥"( W3Techs网站数据的排名)啦 今天小编要介绍后起之秀nginx来实现web服务器.小编在前面的博客中也详细的聊过apache的使用,如今又来说nginx的使用,读者可能以后搭建web服务器的话就不知道选用哪一种,O(∩_∩)O~,别急,小编这就来给读者分析一下Nginx和Apache区