LoginSignup
5
14

More than 5 years have passed since last update.

SSHブルートフォース攻撃を『Fail2ban』で防いでみました

Posted at

SSHブルートフォース攻撃とは、SSHに対する総当たり攻撃のことです。
複雑なパスワードを設定しても、時間はかかりますが、いずれログインに成功します。
その為、パスワード認証の場合、対策が必要です。

■ Fail2Banとは

Fail2banは、ログファイルを監視し、悪意のある兆候を示すIPアドレスからの接続をブロックします。
Fail2Banは、sshdやApache HTTP Serverなどの不正アクセス対策に使用することができます。

■ 検証環境

CentOS 7.3
Fail2ban 0.9.6

■ 攻撃ログの確認

SSHブルートフォース攻撃のログは、通常のSSHログインと同様、/var/log/secure に出力されます。

$ grep "Failed password" /var/log/secure
May 20 15:31:11 localhost sshd[6358]: Failed password for invalid user testuser from ***.***.***.*** port 64168 ssh2
May 20 15:31:11 localhost sshd[6357]: Failed password for invalid user testuser from ***.***.***.*** port 64167 ssh2
May 20 15:31:11 localhost sshd[6355]: Failed password for invalid user testuser from ***.***.***.*** port 64165 ssh2
May 20 15:31:12 localhost sshd[6356]: Failed password for invalid user testuser from ***.***.***.*** port 64166 ssh2
May 20 15:31:12 localhost sshd[6354]: Failed password for invalid user testuser from ***.***.***.*** port 64164 ssh2
May 20 15:31:13 localhost sshd[6358]: Failed password for invalid user testuser from ***.***.***.*** port 64168 ssh2
May 20 15:31:13 localhost sshd[6357]: Failed password for invalid user testuser from ***.***.***.*** port 64167 ssh2
May 20 15:31:13 localhost sshd[6355]: Failed password for invalid user testuser from ***.***.***.*** port 64165 ssh2
May 20 15:31:13 localhost sshd[6356]: Failed password for invalid user testuser from ***.***.***.*** port 64166 ssh2
May 20 15:31:13 localhost sshd[6354]: Failed password for invalid user testuser from ***.***.***.*** port 64164 ssh2

■ Fail2Banを導入・起動

◆インストール
$ yum install epel-release
$ yum install fail2ban
◆設定ファイル
$ cp -p /etc/fail2ban/jail.conf /etc/fail2ban/jail.d/jail.local
$ cp -p /etc/fail2ban/jail.d/jail.local /etc/fail2ban/jail.d/jail.local.org
$ vi /etc/fail2ban/jail.d/jail.local
~ jail.localを編集 ~
$ diff /etc/fail2ban/jail.d/jail.local /etc/fail2ban/jail.d/jail.local.org
23,24c23,24
< [sshd]
< enabled = true
> # [sshd]
> # enabled = true
160,161c160,161
< banaction = firewallcmd-ipset
< banaction_allports = firewallcmd-allports
> banaction = iptables-multiport
> banaction_allports = iptables-allports
◆Fail2Banを起動
$ systemctl start fail2ban

■ 動作確認

◆Fail2banでブロックしたIPアドレスを表示
$ ipset list fail2ban-sshd
Name: fail2ban-sshd
Type: hash:ip
Revision: 1
Header: family inet hashsize 1024 maxelem 65536 timeout 600
Size in memory: 16592
References: 1
Members:
***.***.***.*** timeout 590

アクセス制限時間の初期値は、600秒です。
アクセス制限時間は、bantimeで設定しています。

$ cat /etc/fail2ban/jail.d/jail.local
~ 省略 ~
bantime  = 600
~ 省略 ~
5
14
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
5
14