LoginSignup
64
64

More than 5 years have passed since last update.

CentOS7での fail2banのインストールと設定方法(with firewalld)

Last updated at Posted at 2015-12-15

fail2banとは、SSH等の不正アクセス(ログイン失敗)を検知し、一定時間そのIPアドレスからのアクセスをブロックするソフトである。
通常、fail2banは単体では使用せず、ファイアウォールソフト(iptablesやfirewalld)ともに使用する。この記事では、firewalldを使用する。
また、先述したしたようにSSH以外の不正アクセス対策に使用することもできるが、今回の記事ではSSHの設定に限定した。

※この記事は、2015年11月現在の最新版のfail2ban、CentOS(7.1.1503)にもとづいて記述されている。VPSホスティングサービス等によっては初期インストール状態が違うため、下記の手順があてはまらない場合があるかもしれないことをご承知おき願いたい。

epel-releaseリポジトリをインストールしていない場合、先にインストールする

# yum install epel-release

インストール

# yum install fail2ban fail2ban-systemd firewalld

設定

設定ファイル "jail.conf" をコピーする

# cp -p /etc/fail2ban/jail.conf /etc/fail2ban/jail.d/jail.local

コピーした設定ファイル "jail.local" を編集する(vi などで)

# vi /etc/fail2ban/jail.d/jail.local

 編集例

[sshd]                              <-コメントを削除
enabled = true                      <-コメントを削除

#banaction = iptables-multiport     <-コメント化(存在すれば)
banaction = firewallcmd-ipset       <-追加
  • その他、bantime, maxretry などを必要に応じて設定する。
  • bantimeを変更する場合、/etc/fail2ban/action.d/firewallcmd-ipset.conf ファイルのbantimeも変更する。

OS再起動時にサービスを自動的に起動にする

# systemctl enable fail2ban
# systemctl enable firewalld

サービスを起動する

# systemctl start firewalld
# systemctl start fail2ban

fail2banのstatusを確認する

# systemctl status fail2ban

出力例

 fail2ban.service - Fail2Ban Service
    Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; disabled)
    Active: active (running) since Sun 2015-11-29 05:28:54 UTC; 14s ago
      Docs: man:fail2ban(1)
   Process: 2318 ExecStart=/usr/bin/fail2ban-client -x start (code=exited, status=0/SUCCESS)
  Main PID: 2321 (fail2ban-server)
    CGroup: /system.slice/fail2ban.service
            └─2321 /usr/bin/python2 -s /usr/bin/fail2ban-server -s /var/run/fail2ban/fail2ban.sock -p /var/run/fail2ban/fail2ban.pid -x -b

 Nov 29 05:28:54 vultr.guest systemd[1]: Starting Fail2Ban Service...
 Nov 29 05:28:54 vultr.guest fail2ban-client[2318]: 2015-11-29 05:28:54,559 fail2ban.server         [2319]: INFO    Starting Fail2ban v0.9.3
 Nov 29 05:28:54 vultr.guest fail2ban-client[2318]: 2015-11-29 05:28:54,559 fail2ban.server         [2319]: INFO    Starting in daemon mode
 Nov 29 05:28:54 vultr.guest systemd[1]: Started Fail2Ban Service.

banされたIPアドレスの確認(1)

# fail2ban-client status sshd

出力例

Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     23
|  `- File list:        /var/log/secure
`- Actions
   |- Currently banned: 2
   |- Total banned:     3
   `- Banned IP list:   299.152.299.137 299.299.15.198

banされたIPアドレスの確認(2) ※稼働確認にも使用できる

# ipset --list

出力例

Name: fail2ban-sshd
Type: hash:ip
Revision: 1
Header: family inet hashsize 1024 maxelem 65536 timeout 600000
Size in memory: 21840
References: 1
Members:
299.152.299.137 timeout 473548
  • 全く何も表示されない場合、実質稼働していないので設定等を見直す。

fail2banのログの場所

/var/log/fail2ban.log

64
64
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
64
64