37
回編集
差分
→CentOS 8
:http://www.fail2ban.org/
:http://sourceforge.net/projects/fail2ban/
==設定CentOS==CentOSではepelからyumでインストールすることが可能です。 yum install -y epel-release yum --enablerepo===Asteriskのログフォーマットを変更する===epel -y install fail2banFail2banはそのままではAsteriskのログの日付を認識できないため、Asteriskのログフォーマットを変更します。yumでインストールした最新のFail2BanはAsteriskのログフォーマットを変更し、ローカル設定ファイルを作成するだけで使用することができます。<brBR>/etc/asterisk/loggerまずAsteriskのlogger.confを編集し、日付のフォーマット変更を行います。<br>confを修正し [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
[INCLUDESasterisk] enabled = truefail2banを起動すると監視が始まります。 ===起動の確認===iptables -Lでiptablesの状態を確認するとfail2banでbanされたIPアドレス等が確認できます。 Chain f2b-asterisk-tcp (1 references) target prot opt source destination REJECT all -- 46.17.42.180 anywhere reject-with icmp-port-unreachable RETURN all -- anywhere anywhere
こちらは Asterisk-1.8.23.0 用です。Asterisk-11.5.1 でもほぼ同じコードが使えます。
--- asterisk-1.8.23.0/channels/chan_sip.c.orig 2013-08-02 11:41:03.233638321 +0900
+++ asterisk-1.8.23.0/channels/chan_sip.c 2013-12-06 14:51:08.698990909 +0900
@@ -22673,7 +22673,7 @@
return 0;
}
if (res < 0) { /* Something failed in authentication */
- ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
+ ast_log(LOG_NOTICE, "Failed to authenticate device %s (%s)\n", get_header(req, "From"), ast_sockaddr_stringify(addr));
transmit_response(p, "403 Forbidden", req);
sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
return 0;
@@ -23334,7 +23334,7 @@
goto request_invite_cleanup;
}
if (res < 0) { /* Something failed in authentication */
- ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
+ ast_log(LOG_NOTICE, "Failed to authenticate device %s (%s)\n", get_header(req, "From"), ast_sockaddr_stringify(addr));
transmit_response_reliable(p, "403 Forbidden", req);
p->invitestate = INV_COMPLETED;
sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
@@ -25164,7 +25164,7 @@
p->lastinvite = seqno;
return 0;
} else if (auth_result < 0) {
- ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
+ ast_log(LOG_NOTICE, "Failed to authenticate device %s (%s)\n", get_header(req, "From"), ast_sockaddr_stringify(addr));
transmit_response(p, "403 Forbidden", req);
sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
ast_string_field_set(p, theirtag, NULL);
@@ -25384,7 +25384,7 @@
if (res == AUTH_CHALLENGE_SENT) /* authpeer = NULL here */
return 0;
if (res != AUTH_SUCCESSFUL) {
- ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", get_header(req, "From"));
+ ast_log(LOG_NOTICE, "Failed to authenticate device %s (%s)\n", get_header(req, "From"), ast_sockaddr_stringify(addr));
transmit_response(p, "403 Forbidden", req);
pvt_set_needdestroy(p, "authentication failed"); Asterisk 11.23.1用パッチです。 [Definition]--- channels/chan_sip.c.orig 2016-09-09 01:28:35.000000000 +0900 +++ channels/chan_sip.c 2016-10-28 23:26:38.985774935 +0900 @@ -18751,7 +18751,7 @@ static void receive_message(struct sip_p return; } if (res < 0) { /* Something failed in authentication */ - ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", sip_get_header(req, "From")); + ast_log(LOG_NOTICE, "Failed to authenticate device %s (%s)\n", sip_get_header(req, "From"), ast_sockaddr_stringify(addr)); transmit_response(p, "403 Forbidden", req); sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT); return; @@ -24963,7 +24963,7 @@ static int handle_request_options(struct return 0; } if (res < 0) { /* Something failed in authentication */ - ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", sip_get_header(req, "From")); + ast_log(LOG_NOTICE, "Failed to authenticate device %s (%s)\n", sip_get_header(req, "From"), ast_sockaddr_stringify(addr)); transmit_response(p, "403 Forbidden", req); sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT); return 0; @@ -25798,7 +25798,7 @@ static int handle_request_invite(struct goto request_invite_cleanup; } if (res < 0) { /* Something failed in authentication */ - ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", sip_get_header(req, "From")); + ast_log(LOG_NOTICE, "Failed to authenticate device %s (%s)\n", sip_get_header(req, "From"), ast_sockaddr_stringify(addr)); transmit_response_reliable(p, "403 Forbidden", req); p->invitestate = INV_COMPLETED; sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT); @@ -27788,7 +27788,7 @@ static int handle_request_publish(struct p->lastinvite = seqno; return 0; } else if (auth_result < 0) { - ast_log(LOG_NOTICE, "Failed to authenticate device %s\n", sip_get_header(req, "From")); + ast_log(LOG_NOTICE, "Failed to authenticate device %s (%s)\n", sip_get_header(req, "From"), ast_sockaddr_stringify(addr)); transmit_response(p, "403 Forbidden", req); sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT); ast_string_field_set(p, theirtag, NULL); @@ -28003,7 +28003,7 @@ static int handle_request_subscribe(stru if (res == AUTH_CHALLENGE_SENT) /* authpeer = NULL here */ return 0; if (res != AUTH_SUCCESSFUL) { - ast_log(LOG_NOTICE, "Failed to authenticate device %s for SUBSCRIBE\n", sip_get_header(req, "From")); + ast_log(LOG_NOTICE, "Failed to authenticate device %s (%s) for SUBSCRIBE\n", sip_get_header(req, "From"), ast_sockaddr_stringify(addr)); transmit_response(p, "403 Forbidden", req);
pvt_set_needdestroy(p, "authentication failed"); パッチを当てて、Asteriskをコンパイルし直し、再起動します。 すると、先ほどの攻撃時のログは以下のように出力されるようになります。 #_daemon Failed to authenticate user "Anonymous" <sip:anonymous@192.168.1.2>;tag=as105e401c (123.45.67.89:5060) ログの( )内に攻撃元の実IPアドレスが表示されるようになり、これを元にfail2banで攻撃を検知することができます。 ===fail2banへ設定を追加=== 修正したログに合わせたフィルタ設定をfail2banに追加します。 /etc/fail2ban/filter.d/asterisk.conf の failregex の項目に以下を追加します。 NOTICE.* .*: Failed to authenticate user .* \(<HOST>:.*\) フィルタ追加後、fail2banを再起動し設定完了です。 ==securityログの設定==/etc/asterisk/logger.confに [logfiles] security = > securityを追加(コメント解除)して、/var/log/asterisk/securityにできるログをfail2banに監視させる方法もあります。 ==ステータス確認==fail2ban-client コマンドで問い合わせるのがいいです。 # コマンド一覧 fail2ban-client
# 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有効なjail一覧 #fail2ban-client status
# Option: ignoreregex手動でban/unban # Notesfail2ban-client set asterisk banip 11.: regex to ignore22. If this regex matches, the line is ignored33.44 # Values: TEXTfail2ban-client set asterisk unbanip 11.22.33.44 #banされてることになっていても、actionが正しく書けていないと実際にはfirewalldやiptablesなどに反映されず、攻撃されっぱなしもあり得るので、挙動確認が大事です。<br> ignoreregex =fail2ban-server コマンドは直接叩いてはいけないようです。<br>Asterisk 1または、ログファイル /var/log/fail2ban.6とそれ以前の場合log を見てもいいです。 # Fail2Ban configuration file #== CentOS 8 == # # $Revision: 250 $Asterisk16, CentOS 8でも同様にepelからdnfでインストールすることが可能です。<BR> #CentOS 8では、iptablesではなく、nftables及びfirewalldが使用されていますが、問題なく対応しています。 dnf install -y epel-release [INCLUDES] # Read common prefixes. If any customizations available dnf -- read them from # common.local #before enablerepo= common.conf [Definition]epel -y install fail2ban fail2ban-systemd #_daemon = asterisk # Option: failregex # NotesAsteriskのlogger.: regex to match the password failures messages in the logfileconfの変更、/etc/fail2ban/jail. The localの作成は上記、[[# host must be matched by a group named "host". The tag "CentOS|以前のCentOS]]と同様にしてください。<HOSTBR>" can # be used for standard IP/hostname matching and is only an alias forしかし、2020年8月7日時点では、ここでfail2banを起動しても # (?:2020-08-07 16:39:f{454,6}981 fail2ban.transmitter [58697]:)?(?P<host>\S+) # Values: TEXT # failregex = Registration from WARNING Command ['.*server-stream' failed for , [['<HOST>set' - Wrong password Registration from , '.*syslogsocket' failed for , '<HOST>auto' - No matching peer found] (略) Registration from Received ValueError('.*Action firewallcmd-rich-rules already exists' failed for ,) 2020-08-07 16:39:54,981 fail2ban [58697]: ERROR NOK: ('<HOST>Action firewallcmd-rich-rules already exists' - Username/auth name mismatch,) Registration from '.*' failed for '<HOST>' - Device does not match ACL Registration from 'というエラーが出て動きません。さしあたりの回避策として/etc/fail2ban/jail.*' failed for '<HOST>' d/00- Peer is not supposed to register Registration from 'firewalld.*' failed for '<HOST>' - Not a local domainconfを次のように変更します。 (略) # Option: [DEFAULT] ignoreregexbanaction = firewallcmd-ipset # Notes.: regex to ignore. If this regex matches, the line is ignored.banaction_allports = firewallcmd-ipset # Values: TEXTbanaction = firewallcmd-rich-rules[actiontype=<multiport>] # ignoreregex banaction_allports = firewallcmd-rich-rules[actiontype=Asterisk 1.8とそれ以前ではログのホスト部分にポート番号を含む、含まないの違いがあるためfailregexの記述を変える必要がありますので注意してください。この部分に合致するメッセージが、ログファイルに現れたならばBAN基準になりますので注意して記述します。これ意外にも、引っかけたいメッセージがある場合にはそれも記述するとよいでしょう。<brallports>]