[Memo2011][ITPASS] ika 構築作業ログ 18 (fail2ban)
11/11/02 に fail2ban のインストールを関,大西で行った.(ロガー: 大西)
[ITPASSサーバ構築ドキュメント へ戻る]
fail2ban のインストール
以下のコマンドを実行し, fail2ban をインストールした.
$ sudo -s # apt-get install fail2ban
iptable -L でインストールされたことを確認
以下のように表示された.
# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination fail2ban-ssh tcp -- anywhere anywhere multiport dports ssh Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain fail2ban-ssh (1 references) target prot opt source destination RETURN all -- anywhere anywhere
ssh に関する設定
設定ファイルは, /etc/fail2ban/jail.conf である. 以下のコマンドで jail.conf の内容を確認した.
#less /etc/fail2ban/jail.conf
以下に, 内容の一部を記載する.
destemail = itpadmin@localhost [ssh] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 6
この部分を, 以下のように書き換えた.
destemail = itpadmin@localhost [ssh] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 4
jail.conf の内容にある文字列の意味は以下.
- ignoreip: 不正アクセスとして扱わないアクセス元を指定.
- bantime: 不正アクセスと認めたときに, アクセスを何秒遮断するか指定.
- findtime, maxretry: findtime 秒間のうちに maxretry 回失敗すると不正アクセスとみなされる.
- destemail: 不正アクセスが認められたときのメールの送り先.
- logpath: 監視をするログの場所を指定する.
fail2ban を起動する
以下のコマンドで fail2ban を起動した.
# /etc/init.d/fail2ban start
fail2ban が動いているか確認した.
# /etc/init.d/fail2ban status Status of authentication failure monitor:fail2ban is running.
ssh の動作テスト
関の計算機から ssh による接続を試みた.
$ ssh hoge@133.30.109.21 The authenticity of host '133.30.109.21 (133.30.109.21)' can't be established. RSA key fingerprint is bb:17:63:6f:69:b7:84:66:36:60:ff:23:e4:f5:6c:e2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '133.30.109.21' (RSA) to the list of known hosts. hoge@133.30.109.21's password: Permission denied, please try again. hoge@133.30.109.21's password: Permission denied, please try again. hoge@133.30.109.21's password: Permission denied (publickey,password). $ ssh hoge@133.30.109.21 hoge@133.30.109.21's password: Permission denied, please try again. hoge@133.30.109.21's password:
3 回失敗すると接続が切られ, 何も表示されなくなった. その後, 10 分間は同じ IP アドレスからは接続できなくなった. 以下のコマンドで, ssh が ban されたか確認した.
#iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination fail2ban-ssh tcp -- anywhere anywhere multiport dports ssh Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain fail2ban-ssh (1 references) target prot opt source destination DROP all -- 133.30.???.?? anywhere RETURN all -- anywhere anywhere
/var/log/fail2ban.log を確認すると, 以下の記述が書かれていた.
2011-11-02 15:05:53,893 fail2ban.actions: WARNING [ssh] Ban 133.30.***.** 2011-11-02 15:15:54,566 fail2ban.actions: WARNING [ssh] Unban 133.30.***.**
挙動がシステムログメールに記載されるように設定
スクリプトの設置
マニュアルに設置するよう書かれていたが, 前の作業[[Memo2011][ITPASS] ika 構築作業ログ 13 (ログ設定)]で設置が完了していたためこの作業は行わなかった.
残っている作業
11/11/03の tako のシステムログメールを確認する.
apache に関する設定
設定ファイルの書き換え
/etc/fail2ban/jail.conf の apache に関する設定を以下のように書き換えた.
変更前
[apache] enabled = false port = http,https filter = apache-auth logpath = /var/log/apache*/*error.log maxretry = 6 # default action is now multiport, so apache-multiport jail was left # for compatibility with previous (<0.7.6-2) releases [apache-multiport] enabled = false port = http,https filter = apache-auth logpath = /var/log/apache*/*error.log maxretry = 6
変更後
[apache] enabled = true port = http,https filter = apache-auth findtime= 60 logpath = /var/log/httpd-error.log maxretry = 30 bantime = 1200 # default action is now multiport, so apache-multiport jail was left # for compatibility with previous (<0.7.6-2) releases [apache-multiport] enabled = true port = http,https filter = apache-auth findtime = 10 logpath = /var/log/httpd-access.log maxretry = 30
failregex の追加
- /etc/fail2ban/filter.d/apache-auth.conf の failregex を以下のように編集する.
- failregex を設定すると, その設定に対応したログのメッセージをカウントし, findtime 以内に maxretry 回,
同じ IP アドレスからのメッセージがカウントされるとその IP アドレスからのアクセスが ban される.
- ignoreregex は failregex で設定したもののうち, カウントしたくないメッセージがある場合に設定する.
変更前
failregex = [[]client <HOST>[]] user .* authentication failure [[]client <HOST>[]] user .* not found [[]client <HOST>[]] user .* password mismatch # Option: ignoreregex # Notes.: regex to ignore. If this regex matches, the line is ignored. # Values: TEXT # ignoreregex =
変更後
failregex = [[]client <HOST>[]] user .* authentication failure [[]client <HOST>[]] user .* not found [[]client <HOST>[]] user .* password mismatch [[]client <HOST>[]] (13)Permission denied: [[]client <HOST>[]] File does not exist: ^<HOST> -.*GET.* # Option: ignoreregex # Notes.: regex to ignore. If this regex matches, the line is ignored. # Values: TEXT # ignoreregex = ^<HOST> -.*GET.*(jpg|jpeg|gif|png).*
- 一つの html ページに画像がたくさん貼付けてあるとそれだけで引っかかってしまう. そのため, png, jpg(jpeg), gif についても ignore に追加した.
動作テスト(未完了)
11/11/03 に作業を行う.
fail2ban.conf の中のログ確認
#less /etc/fail2ban.fail2ban.conf # Option: loglevel # Notes.: Set the log level output. # 1 = ERROR # 2 = WARN # 3 = INFO # 4 = DEBUG # Values: NUM Default: 3 # loglevel = 3
ログレベルが 3 になっていることを確認した.
Keyword(s):
References:[[ITPASS2011]2011年度サーバ構築ログ]