1
0

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.

fail2banで総当たり攻撃を防ぐ

Last updated at Posted at 2022-10-03

お知らせ

こんにちは、愛知工業大学システム工学研究会です。
この記事は、2018-11-20に書かれた記事です。
間違った内容や古い内容が紛れていますが、過去の記事を残す一環として記事を書きます。
よろしくおねがいします。

fail2banを設定して一定回数以上のログイン試行をしたipアドレスをbanするようにしました。

対象読者

  • webサーバーでBasic認証をしている人
  • 同じipからの攻撃が多い人

使用している環境

  • ubuntu 18.04.1
  • nginx 1.14.1

fail2banについて

fail2banを使うと一定回数以上ログインに失敗したipアドレスからの
アクセスをbanすることができるので、パスワードの総当たり攻撃を防ぐことができます。
今回はnginxに設定しました。

fail2banの設定

インストール

$sudo aptitude install fail2ban

でインストールできます。

設定ファイルの編集

設定ファイルは
/etc/fail2ban/jail.conf
になります。330行目あたりにnginxの設定がありますのでそちらを編集しました。

[nginx-http-auth] port = http,https enabled = true filter = nginx-http-auth logpath = /var/log/nginx/*error*.log maxretry = 1 bantime = -1 action = iptables-multiport[name=404, port="http,https", protocol=tcp]

logpathの方で監視するlogファイルを指定して、maxretryで何回ログインを失敗したらbanするか決めることができます。bantimeはbanしたipアドレスをいつまでbanし続けるか指定するものになり、-1とすると永遠にbanされた状態となります。actionではbanされた際に行う処理を書くことができます。sendmailコマンドでメールを送ることができる環境であれば、

action = iptables-multiport[name=404, port="http,https", protocol=tcp] sendmail-whois[name=nginx, dest=example@gmail.com, sender=example@gmail.com, sendername="Fail2Ban"]

とすることでbanしたことをメールで受け取れるようになります。

起動

$sudo systemctl start fail2ban

で起動できます。

確認

$sudo fail2ban-client status nginx-http-auth

でbanされたipを確認することができ、

$sudo fail2ban-client set nginx-http-auth unbanip ban解除するipアドレス

とすることでbanを解除することができます。

image.png

ちなみにメールの設定をするとこのようなメールが送信されます。

まとめ

今回はnginxでの設定をしましたが、sshなど他のサービスでも使うことができるので便利だと思います。
読んでいただきありがとうございました。

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?