お外でひかり電話 第10章  “Mercy,mercy,mercy?” セキュリティfail2banの導入

fail2banの導入

今回導入するのは
「fail2ban」
という不正アクセスをログの記述から検索して、相手のIPアドレスからの通信を受け付けないようにするフリーの防御サービス(ソフト)。

詳しくはこちらのサイト。
日本語でのasterisk研究最高峰のサイトです。
asteriskに関する基本的情報は全て手に入る。

でも最新バージョンへの対応を随時しているわけではないから、そこは僕らの出番だよ。

# apt-get install fail2ban

設定ファイル3つ

/etc/fail2ban/jail.local (基本はjail.confファイルなのだがこのjail.localに書いた内容が最優先される。無ければ自分で作成する。どのログファイルを監視するのか。また、下記2つのフィルターとアクションとを結びつける設定ファイルの役割)
/etc/fail2ban/action.d/iptables-sip.conf(自分で作成。フィルター条件に合致したものを具体的にどうするのかを設定)
/etc/fail2ban/filter.d/asterisk.conf(ログファイルにどんな文言が出現したら反応するのかを設定。このファイルには後で修正を加えるのでその時に紹介します。)

# nano /etc/fail2ban/jail.local

[DEFAULT]
ignoreip = 127.0.0.1/8 192.168.0.0/24 XXX.XXX.XXX.XXX;固定IPを取っている場合はXXX~の部分を置き換えておくこと。
ignorecommand =
# “bantime” is the number of seconds that a host is banned.
bantime = 604800
findtime = 600
maxretry = 2# “backend” specifies the backend used to get files modification.
backend = auto
usedns = warn
destmail = sample@ZZZZ.ZZZ ;送信したいメールアドレス
sendername = Fail2Ban
sender = fail2ban@localhost# ACTIONS
mta = exim
protocol = tcp
chain = INPUT
action_ = %(banaction)s[name=%(__name__)s, port=”%(port)s”, protocol=”%(protocol)s”, cha$# ban & send an e-mail with whois report to the destemail.
action_mw = %(banaction)s[name=%(__name__)s, port=”%(port)s”, protocol=”%(protocol)s”, c$
%(mta)s-whois[name=%(__name__)s, dest=”%(destemail)s”, protocol=”%(protoco$# ban & send an e-mail with whois report and relevant log lines
# to the destemail.
action_mwl = %(banaction)s[name=%(__name__)s, port=”%(port)s”, protocol=”%(protocol)s”, $
%(mta)s-whois-lines[name=%(__name__)s, dest=”%(destemail)s”, logpath=%(lo$# 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$
# globally (section [DEFAULT]) or per specific section
action = %(action_)s#
# JAILS
#
[ssh]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 6[asterisk]
enabled = true
port = 5060,5061
filter = asterisk
action = iptables-asterisk[name=ASTERISK, protocol=all]
sendmail[name=ASTERISK, dest=sample@ZZZZ.ZZZ, sender=root] ;ここも送信したいメールアドレスを記入
logpath = /var/log/asterisk/messages ;監視するべきログファイルの場所
maxretry = 2
findtime = 1200
bantime = -1

asteriskのフィルターに関して

時間のカウントは全て秒単位。

「20分間(findtime = 1200秒)の間に2回(maxretry = 2回)のいたずらがあったら永久にbanする」という意味。

bantime = -1はまさに「過去の時間」でありまた「おとといおいで」=「二度と来ないで」を表していることに。
ちなみに1週間だけのbanだったら

bantime = 604800(秒)
と設定したら良いのだけど。
Mercy,mercy・・・mercy?
僕が使っているのはただ電話のためだけのサーバー。
そしてスマホ何台かだけが接続されるサーバー。
だから、まったくの他人にアクセスされる必要は無いので永久banで良いのです。
はじめはbantimeを1週間にしていたんだけど、1週間から10日くらいしたら再び不正ログインしようとする痕跡が残ってる。

banアクションの設定ファイルを作る

新規ファイル作成の手法で設定ファイルを作成。

# nano /etc/fail2ban/action.d/iptables-sip.conf

# Fail2Ban configuration file
[INCLUDES]
before = iptables-blocktype.conf
[Definition]# Option: actionstart
actionstart = iptables -N fail2ban-
iptables -A fail2ban- -j RETURN
iptables -I -p -j fail2ban-# Option: actionstop
actionstop = iptables -D -p -j fail2ban-
iptables -F fail2ban-
iptables -X fail2ban-# Option: actioncheck
actioncheck = iptables -n -L | grep -q ‘fail2ban-[ \t]’# Option: actionban
actionban = iptables -I fail2ban- 1 -s -p udp –dport 5060 -j DROP# Option: actionunban
actionunban = iptables -D fail2ban- -s -p udp –dport 5060 -j DROP[Init]# Default name of the chain
name = default# Option: protocol
protocol = tcp# Option: chain
chain = INPUT

 

fail2banを起動。

# /etc/init.d/fail2ban start

自動起動設定

# update-rc.d fail2ban defaults

Follow me!

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください