1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AlmaLinux8にfail2banをインストールする際のメモ

Posted at

fail2ban インストール方法

  • fial2ban はログファイルをスキャンし、悪意のある攻撃や兆候を元に、攻撃元の IP アドレスを自動で遮断(ban)するソフトウェアです。
  • オープンソース・プロジェクトとして開発されており、GNU GPL v2 ライセンスの下、誰でも自由に使うことができます。
[root@localhost ~]# dnf -y install fail2ban
  • インストール後、下記のバージョンが表記できれば問題なし。
[root@localhost ~]# fail2ban-server --version
Fail2Ban v0.11.2
[root@localhost ~]# fail2ban-client --version
Fail2Ban v0.11.2
[root@localhost ~]# fail2ban-regex --version
fail2ban-regex 0.11.2
[root@localhost ~]# fail2ban-python --version
Python 3.6.8

fail2ban の設定

  • jail.confjail.local にコピーし、設定を行う。
[root@localhost ~]# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
[root@localhost ~]# vim /etc/fail2ban/jail.local
[root@localhost ~]# diff /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

fail2ban の設定項目 - step.1 BANの設定

  • bantime にBANする時間を指定します。1w で一週間となります。単位なしだと秒単位となります。
  • findtime 内に maxretry 回の出現があるとBANします。
  • backend は一定期間おきにログを見に行く polling としておきます。
  • banaction にBANのアクションを設定します。
設定値 内容
banaction = iptables-multiport
banaction_allports = iptables-allports
iptableを使用時の設定
banaction = firewallcmd-ipset
banaction_allports = firewallcmd-allports
firewallを使用時の設定
# "bantime" is the number of seconds that a host is banned.
bantime  = 1w

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime  = 3m

# "maxretry" is the number of failures before a host get banned.
maxretry = 3


# "backend" specifies the backend used to get files modification.
# Available options are "pyinotify", "gamin", "polling", "systemd" and "auto".
# This option can be overridden in each jail as well.
#
# pyinotify: requires pyinotify (a file alteration monitor) to be installed.
#              If pyinotify is not installed, Fail2ban will use auto.
# gamin:     requires Gamin (a file alteration monitor) to be installed.
#              If Gamin is not installed, Fail2ban will use auto.
# polling:   uses a polling algorithm which does not require external libraries.
# systemd:   uses systemd python library to access the systemd journal.
#              Specifying "logpath" is not valid for this backend.
#              See "journalmatch" in the jails associated filter config
# auto:      will try to use the following backends, in order:
#              pyinotify, gamin, polling.
#
# Note: if systemd backend is chosen as the default but you enable a jail
#       for which logs are present only in its own log files, specify some other
#       backend for that jail (e.g. polling) and provide empty value for
#       journalmatch. See https://github.com/fail2ban/fail2ban/issues/959#issuecomment-74901200
#backend = auto
backend = polling


#
# Action shortcuts. To be used to define action parameter

# Default banning action (e.g. iptables, iptables-new,
# iptables-multiport, shorewall, etc) It is used to define
# action_* variables. Can be overridden globally or per
# section within jail.local file
#banaction = iptables-multiport
banaction = firewallcmd-ipset

#banaction_allports = iptables-allports
banaction_allports = firewallcmd-allports

fail2ban の設定項目 - step.2 メール通知

  • destemail にアラートメールの通知先を設定します。
  • sender にアラートメールの送信元を設定します。
  • mta にMTAの設定。(alternatives --display mta コマンドで確認)
  • action に通知内容を設定します。
    ※メール送信設定済みの前提です。
設定値 内容
%(action_)s BANのみ
%(action_m)s BANとメール通知
%(action_mw)s BANとメール通知(whois情報を含める)
%(action_mwl)s BANとメール通知(whois情報とサービスのログも含める)
# Destination email address used solely for the interpolations in
# jail.{conf,local,d/*} configuration files.
destemail = support@hogehoge.jp

# Sender email address used solely for some actions
sender = no-reply@hogehoge.jp

# E-mail action. Since 0.8.1 Fail2Ban uses sendmail MTA for the
# mailing. Change mta configuration parameter to mail if you want to
# revert to conventional 'mail'.
mta = sendmail

# Choose default action.  To change, just override value of 'action' with the
# interpolation to the chosen action shortcut (e.g.  action_mw, action_mwl, etc) in jail.local
# globally (section [DEFAULT]) or per specific section
action = %(action_mw)s

fail2ban の設定項目 - step.3 有効化したいJAILルールを指定

  • 有効化したいJAILルール箇所に enabled = true 行を新規追記する。
[apache-auth]
enabled  = true

[postfix]
enabled = true

[dovecot]
enabled = true

[postfix-sasl]
enabled  = true

fail2ban のコマンド

  • systemctlコマンド
  • fail2ban-client reload で設定を変更したときなどはクライアントリロードを行う。
  • fail2ban-client status JAILルール名 で各JAILルールごとの確認が行える。
  • fail2ban-client set JAILルール名 banip IPアドレス で手動BAN設定を行える。
  • fail2ban-client set JAILルール名 unbanip IPアドレス で手動BAN設定の解除を行える。
[root@localhost ~]# systemctl [start|stop|status] fail2ban
[root@localhost ~]# systemctl [enable|disable] fail2ban
[root@localhost ~]# fail2ban-client reload

[root@localhost ~]# fail2ban-client status postfix-sasl
Status for the jail: postfix-sasl
|- Filter
|  |- Currently failed: 16
|  |- Total failed:     505
|  `- Journal matches:  _SYSTEMD_UNIT=postfix.service
`- Actions
   |- Currently banned: 2
   |- Total banned:     2
   `- Banned IP list:   <BANされたIPアドレス>
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?