Yado_tech

旅館+ITとはなんぞ

さくらのVPSにAsteriskを設定し、内線通話するまで

f:id:devilmakelie:20180109191549p:plain

自分用に

まずはOSを選択

自分はUbuntu 16.04 LTSを選びました。Asteriskが簡単にインストールできるため。
ただしスタートアップスクリプトufwは使わないほうがいいと思います。fail2banとコンフリクトしそうな気がします。

アップデートとaptitudeのインストール

sudo apt update
sudo apt upgrade
sudo apt-get install aptitude

fail2banのインストール

sudo aptitude install fail2ban

設定ファイルを書く

最初にfail2banはjail.confファイルを直接編集することは推奨されていないため、jail.localファイルを編集するようにする。

cd /etc/fail2ban
sudo cp jail.conf jail.local
sudo nano jail.local

jail.localに以下の文を追記する

[asterisk-iptables]と[blacklist]を設定してやる

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

[blacklist]
enabled = true
logpath  = /var/log/fail2ban.*
filter = blacklist
banaction = blacklist
bantime  = 31536000   ; 1 year
findtime = 31536000   ; 1 year
maxretry = 10

blacklistの作成

// /etc/fail2ban/filter.d/blacklist.conf の作成
$ cd /etc/fail2ban/filter.d
$ sudo wget https://raw.githubusercontent.com/mitchellkrogza/Fail2Ban-Blacklist-JAIL-for-Repeat-Offenders-with-Perma-Extended-Banning/master/filter.d/blacklist.conf -O blacklist.conf

// /etc/fail2ban/action.d/blacklist.conf の作成
$ cd /etc/fail2ban/action.d
$ sudo wget https://raw.githubusercontent.com/mitchellkrogza/Fail2Ban-Blacklist-JAIL-for-Repeat-Offenders-with-Perma-Extended-Banning/master/action.d/blacklist.conf -O blacklist.conf

/etc/fail2ban/filter.dasterisk.confファイルを編集する
まずはファイルのバックアップ

cd /etc/fail2ban/filter.d
mv asterisk.conf asterisk.conf.old

nano 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 "<HOST>" can
#          be used for standard IP/hostname matching and is only an alias for
#          (?:::f{4,6}:)?(?P<host>\S+)
# Values:  TEXT
#

failregex = SECURITY.* SecurityEvent="FailedACL".*RemoteAddress=".+?/.+?/<HOST>/.+?".*
            SECURITY.* SecurityEvent="InvalidAccountID".*RemoteAddress=".+?/.+?/<HOST>/.+?".*
            SECURITY.* SecurityEvent="ChallengeResponseFailed".*RemoteAddress=".+?/.+?/<HOST>/.+?".*
            SECURITY.* SecurityEvent="InvalidPassword".*RemoteAddress=".+?/.+?/<HOST>/.+?".*

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

fail2banの再起動 sudo service fail2ban restart起動する場合はsudo service fail2ban start

iptablesの設定

/etc/iptables/iptables.rulesを設定する

まずはバックアップ

cd /etc/iptables
sudo mv iptables.rules iptables.rules.old

んでsudo nano iptables.rulesで以下を記述する port:5060 UDP:10000~20000だと乗っ取りが怖いので多少変更してやる

具体的にはport:15060 UDP:20000~30000

設定がうまくいかなかったため、そのままでいくことにした。

*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p udp -m udp --dport 5060 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 5060 -j ACCEPT
-A INPUT -p udp -m udp --dport 10000:20000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

sudo iptables-restore > /etc/iptables/iptables.rulesiptablesを再起動する。

sudoが絶対必要なので忘れない!!

asteriskのインストール

sudo aptitude install asterisk

sip.conf, extensions.confのバックアップを取った後、適当に書き換える

cd /etc/asterisk
sudo mv sip.conf sip.conf.old
sudo mv extensions.conf extensions.conf.old

sip.conf

[general]
context=default
port=5060
bindaddr=0.0.0.0
language=ja
allow = ulaw
allow = alaw
allow = gsm
allow = ilbc
dtmfmode = inband

[201]
type=friend
defaultuser=201
secret=pass
host=dynamic
canreinvite=no
nat = force_rport,comedia

[202]
type=friend
defaultuser=202
secret=pass
host=dynamic
canreinvite=no
nat = force_rport,comedia

extensions.conf

[default]
;
; By default we include the demo.  In a production system, you
; probably don't want to have the demo there.
;
;include => demo
exten => 201,1,Dial(SIP/201,30,r)
exten => 201,2,Hangup()

exten => 202,1,Dial(SIP/202,30,r)
exten => 202,2,Hangup()

exten => 203,1,Answer()
exten => 203,2,Playback(hello-world)
exten => 203,3,Hangup()

以上でつながるはず。

出典

Fail2Banでアクセス制限

Fail2Ban (with iptables) And Asterisk - VoIP-Info