fail2banを使ったsip総当たりアタックの防御方法 | 雲の上はいつも青空

雲の上はいつも青空

不思議な経歴をもつエンジニア!?の徒然なブログです。
お仕事関係の話が多いと思いますが、コメントとかもらえると中の人はとても喜びます(^O^)/

最近Asteriskを動かしているとsipポートへの総当たり攻撃がすごいので、しばらくはルータで弾いていたのですが、そうすると任意のグローバル側から自分のAsteriskへ接続が出来ないのでとても不便でした。
※iPhoneのVPN(PPTP)で接続出来るようにしていたのですが、WiFiや3Gの接続が切れる度にVPNも切れてしまい、自動では復旧しないのでちょっと使い辛かったです。

AsteriskのMLで高橋さん(Asterisk徹底活用ガイドの著者)よりfail2banという素晴らしいアプリケーションを教えていただき早速設定して使ってみました。
ほぼ1ヶ月稼動させてみました結果、効果が素晴らしいのでご紹介します。

fail2banの動作原理は極めてシンプルです。
・指定されたログファイルを監視する。
・ログファイルに決められた単語が出現するとカウントする。
・カウント数が指定された閾値を超えると規定のアクションを呼ぶ。
・アクションではiptablesに指示を出し攻撃元からの通信をブロックする。
・ブロック有効期間が経過したらブロックを解除する。

詳しい設定方法はこちらをご覧下さい。
ここではDebianで設定する方法を書いてみます。

環境は、以下の通りです。
debian GNU linux 6.0 squeeze
kernel 2.6.32-5-686
asterisk 1.6.2.9-2

設定は以下のように行います。
1.fail2banのインストール
2.fail2banが使うastersik用のログフィルターを作成
3.Asteriskのログフォーマットを変更
4.jail.confにastersik用のアクションを追加


それではインストール&設定作業を開始します。

1.fail2banのインストール
# aptitude install fail2ban

これで必要なモジュールは全てインストールされます。


2.fail2banが使うastersik用のログフィルターを作成
下記の内容を、/etc/fail2ban/filter.d/asterisk.conf として作成します。

# Fail2Ban configuration file
#
#
# $Revision: 250 $
#

[INCLUDES]

# Read common prefixes. If any customizations available -- read them from
# common.local
#before = common.conf


[Definition]

#_daemon = asterisk

# Option: failregex
# Notes.: regex to match the password failures messages in the logfile. The
# host must be matched by a group named "host". The tag "" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P\S+)
# Values: TEXT
#

failregex = Registration from '.*' failed for '' - Wrong password
Registration from '.*' failed for '' - No matching peer found
Registration from '.*' failed for '' - Username/auth name mismatch
Registration from '.*' failed for '' - Device does not match ACL
Registration from '.*' failed for '' - Peer is not supposed to register
Registration from '.*' failed for '' - Not a local domain

# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =

3.Asteriskのログフォーマットを変更
/etc/astersik/logger.confを開き、ファイルの先頭にあるgeneralのdateformatのコメントアウトを外します。

[general]
; Customize the display of debug message time stamps
; this example is the ISO 8601 date format (yyyy-mm-dd HH:MM:SS)
; see strftime(3) Linux manual for format specifiers
dateformat=%F %T

logger.confを保存し、Asteriskを再起動します。


4.jail.confにastersik用のアクションを追加
/etc/fail2ban/jail.conf の一番最後に下記のアクションを追加してください。

[asterisk-iptables]

enabled = true
filter = asterisk
action = iptables-allports[name=ASTERISK, protocol=all]
sendmail-whois[name=ASTERISK, dest=root, sender=fail2ban@example.net]
logpath = /var/log/asterisk/messages
maxretry = 5
findtime = 600
bantime = 259200

この設定により、/var/log/asterisk/messagesを監視し、10分間に5回以上フィルターに引っかかるような単語が出現(つまりアタックされている)すると、その接続元からのすべての通信(allports)を3日間(259,200秒)ブロックするという動作を行います。
※senderはfail2banが動作したというお知らせメールのfrom行に書かれます。

全ての設定が終わりましたら、fail2banを再起動します。
# /etc/init.d/fail2ban stop
# /etc/init.d/fail2ban start

これで、root宛へメールが飛んでいれば起動しています。
わざとパスワードを間違えてsipへレジストしてみる等、動作確認はしっかり行って下さい。

また、Jail.confには最初からいろいろなアクションが書かれていますが、ssh以外はfalseになっています(つまり監視していない)。

このfail2banは色々と応用が利くのでもう少し触ってみることにします。
最後になりましたが、素晴らしいソフトを教えて頂き本当にありがとうございます>高橋さん