Linux:fail2banで防ぐ「DoS/DDoS攻撃」対策

不正アクセスを検知し一定時間、対象IPアドレスからのアクセスをブロックするソフトウェア「fail2ban」を用いた、DoS/DDoS攻撃対策を行います。通常 fail2banは、単体では使用せずファイアウォール(iptablesや firewalld)ともに使用します。(2017/12/23作成)

Firewall aitoff / Pixabay

1.fail2banの導入

0)環境

$ cat /etc/redhat-release
CentOS release 6.9 (Final)

1)インストール

fail2banのインストールには、Yumリポジトリ「EPEL」が必要になります。

$ rpm -qa | grep -i epel-release
epel-release-6-8.noarch

$ sudo yum install fail2ban

2)fail2banのlog設定

$ sudo mkdir /var/log/fail2ban

$ sudo vi /etc/fail2ban/fail2ban.conf

# logtarget = SYSLOG
logtarget = /var/log/fail2ban/fail2ban.log

$ sudo vi /etc/logrotate.d/fail2ban

/var/log/fail2ban/fail2ban.log {
    missingok
    notifempty
    weekly
    rotate 5
    compress
    dateext
    create 0644 root root
    postrotate
        /usr/bin/fail2ban-client set logtarget /var/log/fail2ban/fail2ban.log 2> /dev/null || true
    endscript
}

3)fail2banのloglevel設定

Ban(ブロックした)やUnBan(ブロックを解除した)の処理だけを記録するように fail2banの設定ファイルを編集します。

$ sudo vi /etc/fail2ban/fail2ban.conf

loglevel = INFO ⇒ NOTICE

注意) 後の解説にある fail2banの filter定義「recidive」を用いる場合、loglevelは「INFO」にする必要があります。

4)fail2banのファイアウォールversion

$ iptables -V
iptables v1.4.7

ファイアウォール(iptables)のバージョンが古いとうまく動作しないことがあるようなので、 /etc/fail2ban/action.d/iptables-common.conf も編集して lockingoptオプションを無効にします。

# Option:  lockingopt
# Notes.:  Option was introduced to iptables to prevent multiple instances from
#          running concurrently and causing irratic behavior.  -w was introduced
#          in iptables 1.4.20, so might be absent on older systems
#          See https://github.com/fail2ban/fail2ban/issues/1122
# Values:  STRING
lockingopt =
↓
#lockingopt =

# Option:  iptables
# Notes.:  Actual command to be executed, including common to all calls options
# Values:  STRING
iptables = iptables <lockingopt>
↓
iptables = iptables

なお iptables –helpコマンドを実行して「-w」オプションがあれば本作業は必要ありません。

5)fail2banのfilter定義

$ sudo vi /etc/fail2ban/filter.d/nginx-dos.conf

[Definition]
failregex = ^<HOST> -.*"(HEAD|GET|POST).*
ignoreregex = \.(?i)(jpe?g|gif|png|bmp|pdf|js|css|woff|eot|ttf|ico|txt|xml|swf|xlsx?|docx?|pptx?)

6)fail2banのJail(ban)定義

$ sudo vi /etc/fail2ban/jail.local
[nginx-dos]
enabled = true
port = http,https
filter = nginx-dos
logpath = /var/log/nginx/access.log
action = iptables-multiport[name=dos, port="http,https", protocol=tcp]
         sendmail-whois[name=dos, dest=root, sender=fail2ban]
maxretry = 12
findtime = 3
bantime = 7200

このJail(檻)設定内容をまとめると「 /etc/fail2ban/filter.d/nginx-dos.conf フィルターファイルのルールに、3秒間で12回一致したら、そのIPアドレスを1日ブロック」する。ブロック結果は root宛にメール配信する。

[recidive]
enabled  = true
filter   = recidive
logpath  = /var/log/fail2ban/fail2ban.log
action   = iptables-allports[name=recidive]
           sendmail-whois-lines[name=recidive, logpath=/var/log/fail2ban/fail2ban.log, dest=root, sender=fail2ban]
maxretry = 5     
findtime = 43200   ; 12 hours
bantime  = 604800  ; 1 week

このJail(檻)設定内容をまとめると「 /etc/fail2ban/filter.d/recidive.conf フィルターファイルのルールに、攻撃再犯者には厳しく…fail2banでしつこい攻撃者だけ長期Banする設定。ブロック結果は root宛にメール配信する。

7)起動/自動起動設定

$ sudo /etc/init.d/fail2ban start
$ sudo /sbin/chkconfig fail2ban on

2.fail2banの運用

1)ホワイトリスト設定

$ sudo vi /etc/fail2ban/jail.conf

ignoreip = 127.0.0.1/8 xxx.xxx.0.0/16

2)間違えてIPを Banしてしまった場合の対処

$ sudo fail2ban-client set {Jailルール名} unbanip {BANしたIPアドレス}

3)どれくらいIPが Banされているのかを確認

$ sudo fail2ban-client status {Jailルール名}
Status for the jail: {Jailルール名}
|- Filter
|  |- Currently failed: 1
|  |- Total failed:     184
|  `- File list:        /var/log/nginx/access.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   107.158.xxx.xxx

以上

About yoshimasa

埼玉県さいたま市在住、2男3女のパパです。Linux系の技術情報を中心にまとめています。1978年2月生まれ。