LoginSignup
21
17

More than 5 years have passed since last update.

Nginx WAFモジュール(Naxsi)インストール

Posted at

ファイルのダウンロード・展開

naxsi-core

コマンド
cd /usr/local/src/
wget https://naxsi.googlecode.com/files/naxsi-core-0.49.tgz
tar zxvf naxsi-core-0.49.tgz

naxsi_util

コマンド
cd /usr/local/src
wget https://naxsi.googlecode.com/files/nx_util-1.1.tgz
tar zxvf nx_util-1.1.tgz cd nx_util-1.1

Nginxへモジュールの追加

コマンド
cd /uer/local/src/nginx-1.5.9/
./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module --add-module=../naxsi-core-0.49/naxsi_src/
make
maike install

準備

ディフォルトルールの追加

コマンド
cp -rp /usr/local/src/naxsi-core-0.49/naxsi_config/naxsi_core.rules /usr/local/nginx/conf/

nx_utilをコピー

コマンド
cp -rp /usr/local/src/nx_util-1.1/nx_util/ /usr/local/nginx/

nx_utilの設定

/usr/local/nginx/nx_util/nx_util.conf
[nx_util]
data_dir=/usr/local/nginx/nx_util/nx_datas
database_dir=
naxsi_core_rules=/usr/local/nginx/conf/naxsi_core.rules

Nginxの設定

学習モード

/usr/local/nginx/conf/nginx.conf
http {
     #Naxsiのディフォルトルール
     include        /usr/local/nginx/conf/naxsi_core.rules;

     server {
          listen       80;
          server_name  hogehoge.com;

          access_log   /var/log/nginx/hogehoge.com_access80.log main;
          error_log    /var/log/nginx/hogehoge.com_error80.log debug;
          #エラー出力を”debug”にしないと、ルールの生成が行われないので注意
          #error_log    /var/log/nginx/hogehoge.com_error80.log warn;

          location / {
               proxy_pass http://127.0.0.1:8080/;
               proxy_set_header Host hogehoge.com;

               #学習モードを有効にする
               LearningMode;
         #自動学習したルールは一旦読み込ませない(まだ何もない)
               #include /usr/local/nginx/conf/my.rules;

               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;
               ############
          }
          #リジェクトされた際のページ
          location /RequestDenied {
               return 500;
          }
     }

Nginxの再起動

コマンド
#設定ファイルの検証
/etc/init.d/nginx configtest
#再起動
/etc/init.d/nginx restart

動作確認 学習ルールの生成

ブラウザなどから該当のページヘ接続を行う
エラーログに下記のようなログが出ていれば設定完了

ログファイル
2014/02/01 15:58:42 [alert] 9671#0: *5 header already sent, client: 111.222.333.111, server: hogehoge.com, request: "GET /concrete/css/ccm.base.css?v=c83aebd08ed4da35e834b3353901f784 HTTP/1.1", upstream: "http://127.0.0.1:8080/concrete/css/ccm.base.css?v=c83aebd08ed4da35e834b3353901f784", host: "hogehoge.com", referrer: "http://hogehoge.com/"
2014/02/01 15:58:42 [alert] 9671#0: *6 header already sent, client: 111.222.333.111, server: hogehoge.com, request: "GET /packages/image_slider/blocks/image_slider/css/easyslider.css?v=c83aebd08ed4da35e834b3353901f784 HTTP/1.1", upstream: "http://127.0.0.1:8080/packages/image_slider/blocks/image_slider/css/easyslider.css?v=c83aebd08ed4da35e834b3353901f784", host: "hogehoge.com", referrer: "http://hogehoge.com/"
2014/02/01 15:58:42 [error] 9671#0: *11 NAXSI_FMT: ip=111.222.333.111&server=hogehoge.com&uri=/packages/image_slider/blocks/image_slider/css/gallery.css&learning=1&total_processed=4&total_blocked=4&zone0=HEADERS&id0=1005&var_name0=cookie&zone1=HEADERS&id1=1010&var_name1=cookie&zone2=HEADERS&id2=1011&var_name2=cookie&zone3=HEADERS&id3=1315&var_name3=cookie, client: 111.222.333.111, server: hogehoge.com, request: "GET /packages/image_slider/blocks/image_slider/css/gallery.css?v=c83aebd08ed4da35e834b3353901f784 HTTP/1.1", host: "hogehoge.com", referrer: "http://hogehoge.com/"

ログの出力後、想定される挙動をブラウザで行う

ルールの生成
python /usr/local/nginx/nx_util/nx_util.py -c /usr/local/nginx/nx_util/nx_util.conf -l /var/log/nginx/hogehoge.com_error80.log -o > /usr/local/nginx/conf/my.rules
レポートの生成
python /usr/local/nginx/nx_util/nx_util.py -c /usr/local/nginx/nx_util/nx_util.conf -H /home/hoge/public_html/report.html
最新のログの反映
python /usr/local/nginx/nx_util/nx_util.py -i -c /usr/local/nginx/nx_util/nx_util.conf -l /var/log/nginx/hogehoge.com_error80.log

本番設定

/usr/local/nginx/conf/nginx.conf
http {

     server {

          #error_log    /var/log/nginx/hogehoge.com_error80.log debug;
          error_log    /var/log/nginx/hogehoge_error80.log warn;

          location / {

               #LearningMode; #Enables learning mode
               #自動学習モードをOFF
               include /usr/local/nginx/conf/my.rules;
               #自動学習したルールを反映
          }
     }

Nginxの再起動

コマンド
#設定ファイルの検証
/etc/init.d/nginx configtest
#再起動
/etc/init.d/nginx restart
21
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
17