レンタルサーバ(CentOS7)を不正アクセスからサーバを守るfail2ban
不正なアクセスは後をたちませんねぇ。firewalldで不要なポートは閉じていても
踏み台を探すことに必死な方たちからちょくちょくご挨拶を頂きます。
fail2banなるものがあるそうなのでこれを使って不正な、不要なアクセスをシャットアウトしたいと思います。
■ インストール
# yum install fail2ban fail2ban-systemd
■ インストールしたfail2banを有効にします
# systemctl enable fail2ban
# systemctl start fail2ban
/etc/fail2ban/jail.d/local.confに下記を書き込んで、優先的に設定していきます。
[DEFAULT]
banaction = firewallcmd-ipset
backend = systemd
[sshd]
enabled = true
※ 閾値を変更する場合など、/etc/fail2ban/jail.conf の設定を元に local.confを更新すれよさそうです。
■ fail2banの動作確認
□現在稼働しているJailの確認
ひな形:fail2ban-client status
# fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd
□banされているIPアドレス確認
ひな形:fail2ban-client status [Jail名] ※[Jail名]は定義したルール名がは
いります
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 34
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 0
|- Total banned: 2
`- Banned IP list:
まだbanされていない状態です。
テスト用のサーバから故意に不正アクセスしてみます。
ssh test@222.123.13.xxx
(あくまでもテストです。不正アクセスはやめましょう。。。testユーザさんも
いません。)
なんどSSHで不正アクセスしても弾かれませんでした。
なぜだろう?
よくよく考えたらポート番号を変更していることを思い出しました><
vim /etc/fail2ban/jail.d/local.conf
[DEFAULT]
banaction = firewallcmd-ipset
backend = systemd
[sshd]
enabled = true
posrt = xxxx
として、再度検証してみます。
□banされているIPアドレス確認
ひな形:fail2ban-client status [Jail名] ※[Jail名]は定義したルール名がは
いります
# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 31
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 1
|- Total banned: 1
`- Banned IP list: 222.123.13.xxx
無事?banされました^^;
次に解除していかないと。。。
□banされたIPを解除
ひな形:fail2ban-client set [Jail名] unbanip [IPアドレス]
# fail2ban-client set sshd unbanip 222.123.13.xxx
222.123.13.xxx
□解除されたか確認
# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 34
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 0
|- Total banned: 2
`- Banned IP list:
無事解除できたみたいです。