Last-modified: 2016-08-29 (月) 01:12:50
fail2ban/Debianへのインストール

概要

fail2banで不正アクセスからサーバーを守ります。
今回はデフォルト設定(SSH)の他に、nginxのbasic認証に数回失敗すると弾くようにしてみます。

手順

  1. セットアップします。
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
    
     
     
     
    -
    !
     
    -
    !
    -
    !
     
    -
    !
    
    apt-get update
    apt-get install fail2ban
     
    # /etc/fail2ban/fail2ban.confを編集
    vi /etc/fail2ban/fail2ban.conf
    logtarget = /var/log/fail2ban/log
    # ここまで(/etc/fail2ban/fail2ban.conf)
     
    # ログの出力先を変えたので、logrotateの設定も更新(省略)
    mkdir /var/log/fail2ban
     
    # 一応デフォルト起動になっているか確認
    l /etc/rc2.d/
  2. 設定ファイルを作成します。
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
    
     
     
    -
    !
     
     
    -
    !
    -
    !
    -
    !
    -
    !
    -
    !
    -
    !
     
     
     
     
     
     
     
     
    -
    !
     
     
     
     
     
     
    -
    
    cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
     
    # /etc/fail2ban/jail.localを編集
    vi /etc/fail2ban/jail.local
     
    [DEFAULT]
    # 対象外IP(半角スペース区切りで複数指定可能)
    ignoreip = 127.0.0.1/8 <…(必要ならば追加する)>
    # 復帰までの時間(秒)
    bantime  = 1800
    # 許容する不正アクセス数
    maxretry = 3
    # 検知時のメール通知先
    destemail = [email protected]
    # MAT
    mat=mail
    # デフォルトアクション
    action = %(action_mw)s
     
    [ssh]
    enabled  = true
    port     = ssh
    filter   = sshd
    logpath  = /var/log/auth/log
    maxretry = 5
     
    # nginx 認証失敗をチェック
    [nginx-auth]
    enabled  = true
    filter   = nginx-auth
    logpath  = /opt/nginx/log/*/error.log
    port     = http,https
    bantime  = 900
    maxretry = 5
    # ここまで(/etc/fail2ban/jail.local)
    
  3. フィルタ(/etc/fail2ban/filter.d/nginx-auth.conf)を作成します。
    basic認証は、認証が必要な場合に401を返し、成功すると200、失敗するとまた401を返すため
    アクセスログから401を探すルールにすると、複数のタブで認証サイトを一気に開いただけで引っかかる可能性があります。
    そのため、エラーログから認証に失敗した場合のエラーメッセージを元にしたルールを作成しました。
    (nginxのバージョンやロケールによってメッセージは変わる可能性があります)
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
    
    -
    !
    -
    !
    
    # 期待するエラー(ユーザー名が不一致)
    2014/10/15 21:02:02 [error] 1234#0: *123 user "<ユーザー名>" was not found in "<basic認証ファイルパス>", client: 123.123.234.234, ・・・
    # 期待するエラー(パスワードが不一致)
    2014/10/15 21:34:09 [error] 1234#0: *123 user "<ユーザー名>": password mismatch, client: 123.123.234.234, ・・・
    
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
    
    -
    |
    |
    |
    |
    |
    !
     
     
    -
    |
    !
     
     
     
     
    -
    |
    |
    !
     
    -
    |
    |
    |
    !
    
    # Fail2Ban configuration file
    #
    # Author: paburica
    #
    # $Revision$
    #
     
    [INCLUDES]
     
    # Read common prefixes. If any customizations available -- read them from
    # common.local
    before = common.conf
     
     
    [Definition]
     
    # Option:  failregex
    # Values:  TEXT
    #
    failregex = ^.*\[error\].*, client: <HOST>,.*$
     
    # Option:  ignoreregex
    # Notes.:  regex to ignore. If this regex matches, the line is ignored.
    # Values:  TEXT
    #
    ignoreregex =
  4. 正規表現を確認します。
    Everything is expanded.Everything is shortened.
      1
    
     
    
    fail2ban-regex <対象ログパス> /etc/fail2ban/filter.d/nginx-auth.conf
  5. 反映します。
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
    
    -
    !
     
    -
    !
    
    # 再起動
    service fail2ban restart
     
    # ログ出力先を変えたので削除
    rm /var/log/fail2ban.log
  6. iptablesで動作確認をします。
    (画像は、iptablesで不正パケットをdrop前にlog出力しているので、それをトリガーにフィルタを作成してみた時のものです。
    こちらのほうが見栄えが良いので貼っています。実際はnginxのbasic認証をわざと失敗して、dropチェインに追加されることを確認します)
    Everything is expanded.Everything is shortened.
      1
    
     
    
    iptables -L -v
    WS000000.PNG
  7. メールでも動作確認をします。
    (画像は、同じくiptablesのログを使ったテストフィルタの結果です
    実際はnginxのbasic認証をわざと失敗して、メール通知されることを確認します)
    WS000001.PNG

検証時の環境