Fail2ban ログを集計して不正アクセスを防ぐ

Fail2banは、定期的にログを集計して不正なアクセスを検知し、アクセス元のIPアドレスをファイアウォールで一定時間ブロックしたりメールアラートを送信したりするパッケージです。

Fail2ban
公式サイト

インストール

Fail2banは標準リポジトリには含まれていないので、epel拡張リポジトリを使用します。

epelのリポジトリのインストール

# yum install epel-release

epelを利用して、Fail2banをインストール

# yum --enablerepo=epel install fail2ban

旧バージョンの脆弱性

0.8.11より前のバージョンには、サービス運用妨害 (DoS) の脆弱性が存在します。
https://jvn.jp/vu/JVNVU97877909/index.html
攻撃者のアクセス元ではない、IPアドレスをブロック対象としてしまうようです。

Fail2banのバージョン確認

バージョンの確認を行うには、fail2ban-clientを使用します。

# fail2ban-client version
0.9.7

設定ファイル

/etc/fail2ban/
├── action.d
│   ├── dummy.conf
│   ├── hostsdeny.conf
│   ├── iptables.conf
│   ├── mail-whois.conf
│   ├── mail.conf
│   └── shorewall.conf
├── fail2ban.conf
├── fail2ban.local (このファイルは設定変更時に作成する)
├── filter.d
│   ├── apache-auth.conf
│   ├── apache-noscript.conf
│   ├── couriersmtp.conf
│   ├── postfix.conf
│   ├── proftpd.conf
│   ├── qmail.conf
│   ├── sasl.conf
│   ├── sshd.conf
│   └── vsftpd.conf
├── jail.conf
└── jail.local (このファイルは設定変更時に作成する)

設定変更の注意点

Fail2banは fail2ban.confとjail.conf に設定が書かれていますが、この2つのファイルを変更してはいけません。
設定は fail2ban.localとjail.local を作成し、fail2ban.confとjail.conf の内容を上書きするかたちで設定を行うのが正しい操作です。

アップデートで fail2ban.confとjail.conf は変更されることがあるからとマニュアルに書かれています。

Fail2banのマニュアル 設定ファイル

Every .conf file can be overridden with a file named .local. The .conf file is read first, then .local, with later settings overriding earlier ones. Thus, a .local file doesn't have to include everything in the corresponding .conf file, only those settings that you wish to override. すべての.confファイルは、.localという名前のファイルでオーバーライドできます。 .confファイルが最初に読み込まれ、次に.localが読み込まれ、後の設定が前の設定を上書きします。したがって、.localファイルは、対応する.confファイルにすべてを含める必要はなく、上書きしたい設定のみを含めます。
Modifications should take place in the .local and not in the .conf. This avoids merging problem when upgrading. These files are well documented and detailed information should be available there. 変更は.confではなく.localで行う必要があります。これにより、アップグレード時のマージ問題が回避されます。これらのファイルは十分に文書化されており、詳細な情報がそこにあります。

ログファイル

かつては標準でSYSLOGに出力されていたものを、設定ファイルでfail2ban.logに変更しログローテートを設定する、というのが一般的でした。
現在は標準で /var/log/fail2ban.log に出力され、ログローテートも標準で設定が組み込まれています。
このためログファイルの出力先などは、特に変更する必要がなくなっています。

コマンド

サービス起動

CentOS7
systemctl start fail2ban.service

CentOS6以前
service fail2ban start

サービス停止

CentOS7
systemctl stop fail2ban.service

CentOS6以前
service fail2ban stop

サービス再起動

CentOS7
systemctl restart fail2ban.service

CentOS6以前
service fail2ban restart

サービス自動起動有効

CentOS7
systemctl enable fail2ban.service

CentOS6以前
chkconfig fail2ban on

サービス自動起動無効

CentOS7
systemctl disable fail2ban.service

CentOS6以前
chkconfig fail2ban off

サービスステータス表示

fail2ban全体の状況

# fail2ban-client status
Status
|- Number of jail:	1
`- Jail list:	sshd

ルールごとの状況

# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed:	0
|  |- Total failed:	4
|  `- Journal matches:	_SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned:	1
   |- Total banned:	1
   `- Banned IP list:	192.168.1.100

関連記事

スポンサーリンク

FlashPlayerバージョンを切り替える方法

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る