差分

移動先: 案内検索

SIP-Fail2ban

4,931 バイト除去, 2020年8月7日 (金) 19:40
CentOS 8
:http://www.fail2ban.org/
:http://sourceforge.net/projects/fail2ban/
iptablesおよびpythonを必要とします。最新のFail2banではAsteriskにも対応しています。
 
==CentOS==
CentOSではeaplからyumでインストールすることが可能です。CentOSではepelからyumでインストールすることが可能です。
yum install -y epel-release
yum --enablerepo=epel -y install fail2ban
fail2banを起動すると監視が始まります。
==動作条件==
pythonとiptablesが必要。yum install python iptablesなどで入れておいて下さい。
==インストール==
まずSFからfail2banをダウンロードし、展開します。
tar jxvf fail2ban-0.8.4.tar.bz2
展開したディレクトリでインストールを実行します。
cd fail2ban-0.8.4
python ./setup.py install
スタートアップ・スクリプトをコピーしておきます(CentOSなどRedHat系の場合の例)。
cp files/redhat-initd /etc/init.d/fail2ban
==設定=====Asteriskのログフォーマットを変更する起動の確認===Fail2banはそのままではAsteriskのログの日付を認識できないため、Asteriskのログフォーマットを変更します。<br>/etc/asterisk/logger.confを編集し、日付のフォーマット変更を行います。<br>[general]セクションにあるiptables -Lでiptablesの状態を確認するとfail2banでbanされたIPアドレス等が確認できます。 dateformat=%F %Tのコメントを外すか、もしこのエントリがなければ記述します。設定を変更したら、Asteriskを再起動するか、loggerモジュールのリロードを行って、変更を有効にします。これによりAsteriskのログの日付形式が以下のように変わりますので、確認してください。 [2010Chain f2b-12asterisk-30 09:25:25] NOTICE[17537] chan_sip.c:.....===Asterisk用の定義ファイルを作る===/etc/fail2ban/filter.d ディレクトリに asterisk.conf という名前で以下のようなファイルを作ります。ここで指定したメッセージがBAN基準として使われるメッセージとなります。<br><br>Asterisk tcp (1.8系の場合references) # Fail2Ban configuration filetarget prot opt source destination #REJECT all #-- # $Revision: 250 $ # [INCLUDES] # Read common prefixes46.17.42. If any customizations available 180 anywhere reject-with icmp-port- read them fromunreachable # common.localRETURN all #before = common.conf-- anywhere anywhere
[Definition] #_daemon = Chain f2b-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 "<HOST>" can # be used for standard IP/hostname matching and is only an alias for # (?:::f{4,6}:)?(?P<host>\S+) # Values: TEXT # failregex = Registration from '.*' failed for '<HOST>(:[0-9]{1,5})?' - Wrong password Registration from '.*' failed for '<HOST>udp (:[0-9]{1,5})?' - No matching peer found Registration from '.*' failed for '<HOST>(:[0-9]{1,5})?' - Username/auth name mismatch Registration from '.*' failed for '<HOST>(:[0-9]{1,5})?' - Device does not match ACL Registration from '.*' failed for '<HOST>(:[0-9]{1,5}references)?' - Peer is not supposed to register Registration from '.*' failed for '<HOST>(:[0-9]{1,5})?' - Not a local domain target prot opt source destination # Option: REJECT all ignoreregex # Notes.: regex to ignore. If this regex matches, the line is ignored. # Values: TEXT # ignoreregex =Asterisk 1.6とそれ以前の場合 # Fail2Ban configuration file # # # $Revision: 250 $ # [INCLUDES] # Read common prefixes. If any customizations available -- read them from # common46.local #before = common17.conf [Definition] #_daemon = asterisk # Option: failregex # Notes42.: regex to match the password failures messages in the logfile. The # host must be matched by a group named "host". The tag "<HOST>" can # be used for standard IP/hostname matching and is only an alias for # (?:::f{4,6}:)?(?P<host>\S+) # Values: TEXT # failregex = Registration from '.*' failed for '<HOST>' - Wrong password Registration from '.*' failed for '<HOST>' - No matching peer found Registration from '.*' failed for '<HOST>' - Username/auth name mismatch Registration from '.*' failed for '<HOST>' - Device does not match ACL Registration from '.*' failed for '<HOST>' - Peer is not supposed to register Registration from '.*' failed for '<HOST>' 180 anywhere reject- Not a local domain # Option: ignoreregex # Notes.: regex to ignore. If this regex matches, the line is ignored. # Values: TEXT # ignoreregex =Asterisk 1.8とそれ以前ではログのホスト部分にポート番号を含む、含まないの違いがあるためfailregexの記述を変える必要がありますので注意してください。この部分に合致するメッセージが、ログファイルに現れたならばBAN基準になりますので注意して記述します。これ意外にも、引っかけたいメッセージがある場合にはそれも記述するとよいでしょう。<br> ===BANのアクションを作成する===ここではUDPの5060ポート、つまりSIPだけをBAN対象としたいためアクションをSIP用に作成します。 /etc/fail2ban/action.d で以下のようにしてアクションを作成します。<br>まず cp iptables-allports.conf iptables-sip.confを行って、全ポート用のアクションをコピーします。次に iptables-sip.conf を編集し、以下のようにBANとUNBANのエントリを修正します。 # Option: actionban # Notes.: command executed when banning an IP. Take care that the # command is executed with Fail2Ban user rights. # Tags: <ip> IP address # <failures> number of failures # <time> unix timestamp of the ban time # Values: CMD # actionban = iptables -I fail2ban-<name> 1 -s <ip> -p udp --dport 5060 -j DROP # Option: actionunban # Notes.: command executed when unbanning an IP. Take care that the # command is executed with Fail2Ban user rights. # Tags: <ip> IP address # <failures> number of failures # <time> unix timestamp of the ban time # Values: CMD # actionunban = iptables -D fail2ban-<name> -s <ip> -p udp --dport 5060 -j DROP-p udp と --dport 5060 を actionban と actionunban に追記します。 ===fail2banの設定ファイルを修正===/etc/fail2ban にある jail.conf ファイルの最後に以下を追加します。 [asterisk-iptables] enabled = true filter = asterisk action = iptablesicmp-sip[name=ASTERISK, protocol=all] sendmailport-whois[name=ASTERISK, dest=root, sender=fail2ban@example.net] logpath = /var/log/asterisk/messagesunreachable maxretry = 5 findtime = 600 bantime = 604800*actionBAN処理のアクションを定義します。この例ではiptables-sipを実行します。その後、sendmail-whois で BANしたIPアドレスのwhois情報を dest= で指定された宛先に送ります。このとき使用されるメールのFrom:はfail2ban@exampleになりますので、適切なものに書き換えます。<br>アクションの所で iptables-allports を指定するとSIPだけでなく、すべてのポートからの接続を蹴るように iptables に設定されます。『怪しい攻撃元』をブロックするという意味では、こちらのアクションの方がより安全と言えます。*logpathAsteriskのログファイルへのパスを記述します。*maxretry何回以上失敗したらBANするかの指定です。*findtimeこの時間内にmaxretryで指定した回数以上失敗するとBANします。上の例では600秒(10分)の間に、5回以上の失敗があった場合にはBANされます。*bantimeここで指定された期間がBAN期間になります。指定は秒数です。上の例では 60x60x24x7=604800、つまり1週間になります。 ==fail2banを起動する== /etc/init.d/fail2ban start起動したら期待の動作をするかどうかを、よく確認してください。試しに故意に間違えたパスワードで5回以上ログインをしてみるなどです。<br>起動に問題がなければ、fail2banが自動起動されるように登録しておけば良いでしょう。 chkconfig --add fail2ban===起動の確認===iptables -L -v で確認すると 61638 8222K fail2ban-ASTERISK all -- any any anywhere anywhere Chain fail2ban-ASTERISK (1 references) pkts bytes target prot opt in out source destination 61627 8216K RETURN all -- any any anywhere anywhereのようなエントリがあるはずです。<br>BANされるとメールが送られ 11 6424 DROP udp -- any any xxx.xxx.xxx.xx anywhere udp dpt:5060のようなDROPのエントリが追加されているはずです。デフォルトではban時間は1時間です。
==INVITEによるBrute force攻撃への対策==
フィルタ追加後、fail2banを再起動し設定完了です。
==securityログの設定==/etc/asterisk/logger.confに [logfiles] security => securityを追加(コメント解除)して、/var/log/asterisk/securityにできるログをfail2banに監視させる方法もあります。 ==ステータス確認==fail2ban-client コマンドで問い合わせるのがいいです。 # コマンド一覧 fail2ban-client # 有効なjail一覧 fail2ban-client status # jail名"asterisk"のステータス fail2ban-client status asterisk # 手動でban/unban fail2ban-client set asterisk banip 11.22.33.44 fail2ban-client set asterisk unbanip 11.22.33.44banされてることになっていても、actionが正しく書けていないと実際にはfirewalldやiptablesなどに反映されず、攻撃されっぱなしもあり得るので、挙動確認が大事です。<br>fail2ban-server コマンドは直接叩いてはいけないようです。<br>または、ログファイル /var/log/fail2ban.log を見てもいいです。 == CentOS 8 == Asterisk16, CentOS 8でも同様にepelからdnfでインストールすることが可能です。<BR>CentOS 8では、iptablesではなく、nftables及びfirewalldが使用されていますが、問題なく対応しています。 dnf install -y epel-release dnf --enablerepo=epel -y install fail2ban fail2ban-systemd Asteriskのlogger.confの変更、/etc/fail2ban/jail.localの作成は上記、[[#CentOS|以前のCentOS]]と同様にしてください。<BR>しかし、2020年8月7日時点では、ここでfail2banを起動しても 2020-08-07 16:39:54,981 fail2ban.transmitter [58697]: WARNING Command ['server-stream', [['set', 'syslogsocket', 'auto'] (略) Received ValueError('Action firewallcmd-rich-rules already exists',) 2020-08-07 16:39:54,981 fail2ban [Category58697]: ERROR NOK:セキュリティ('Action firewallcmd-rich-rules already exists',) というエラーが出て動きません。さしあたりの回避策として/etc/fail2ban/jail.d/00-firewalld.confを次のように変更します。 (略) [DEFAULT] banaction = firewallcmd-ipset banaction_allports = firewallcmd-ipset #banaction = firewallcmd-rich-rules[actiontype=<multiport>] #banaction_allports = firewallcmd-rich-rules[actiontype=<allports>] これでfail2banを起動すると監視が始まります。 systemctl enable fail2ban systemctl start fail2ban 稼働状態の確認としては、上記fail2ban-clientコマンドまたは、次のコマンドが使用できます。 ipset --list Asterisk 16及び[[Asterisk pjsip|pjsip]]を使用していると、Asteriskにパッチを当てる等しなくても[[#INVITEによるBrute force攻撃への対策|INVITEによるBrute force攻撃]]も防御できるようです。

案内メニュー

VoIp-Info.jp

HP Directplus -HP公式オンラインストア-