2020年8月22日土曜日

fail2ban on CentOS8

先日CentOS8化したメール中継サーバですが、firewalldとipsetをCentOS7の この記事 と同じようにpublic, domesticゾーンを利用するように設定していました。

sshとsmtp-submissionは国内IPのみ許可するように設定したのでそれほどでもないのですが、smtpはIP制限することもできず鬱陶しいアクセスが多く辟易してきたので対処することにしました。

CentOS6の時は、iptablesでルールを書いていたりしましたが今回はfail2banを利用します。
firewall-cmdとipsetを使用する場合は、以下のようになります。
# dnf install fail2ban

# cat << EOF > /etc/fail2ban/jail.local
[DEFAULT]
banaction = firewallcmd-ipset[actiontype=<multiport>]
banaction_allports = firewallcmd-ipset[actiontype=<allports>]

bantime = 604800
findtime = 86400
maxretry = 3

[sshd]
enabled = true

[postfix]
enabled = true
mode = aggressive
maxretry = 2
EOF
#

# cat << EOF > /etc/fail2ban/action.d/firewallcmd-common.local
[Init]
blocktype = DROP
EOF
#
これで、sshdとpostfixの不正アクセス対策が動作します。
動作状況は下記のようにfail2ban-clientコマンドで確認できます。
# fail2ban-client status
Status
|- Number of jail:	2
`- Jail list:	postfix, sshd
#
# fail2ban-client status postfix
Status for the jail: postfix
|- Filter
|  |- Currently failed:	5
|  |- Total failed:	90
|  `- Journal matches:	_SYSTEMD_UNIT=postfix.service
`- Actions
   |- Currently banned:	18
   |- Total banned:	31
   `- Banned IP list:	x.x.x.x y.y.y.y .......
#
nftablesやipsetの定義は下記のように、iptables、ipsetコマンドで確認できます。
# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 22 match-set f2b-sshd src
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 25,465,587 match-set f2b-postfix src

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
#
# ipset --list --name
f2b-postfix
DOMESTICv4
DOMESTICv6
f2b-sshd
#
# ipset --list f2b-postfix
Name: f2b-postfix
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 65536 timeout 600
Size in memory: 1944
References: 1
Number of entries: 18
Members:
x.x.x.x timeout 342227
y.y.y.y timeout 214654
~
#
f2b-xxxがfail2banで作成されたipsetです。
ipsetのDOMESTICv4は、firewalldのdomesticゾーンに紐づけています。
本サーバでは、IPv6のアドレスをアサインしていないのでDOMESTICv6は未使用です。

上記設定で暫く運用していたのですが、smtpポートアクセスで遮断できないものが多かったのでpostfix用フィルターを下記のように改造してみました。
# cd /etc/fail2ban/filter.d
# diff -uw postfix.conf.original  postfix.conf
--- postfix.conf.original	2020-04-16 21:41:47.000000000 +0900
+++ postfix.conf	2020-08-10 21:45:56.232418891 +0900
@@ -44,9 +44,13 @@
 mdre-extra = %(mdre-auth)s
             %(mdre-normal)s
 
-mdpr-aggressive = (?:%(mdpr-auth)s|%(mdpr-normal)s|%(mdpr-ddos)s)
+mdpr-ddos2 = disconnect from
+mdre-ddos2 = ^unknown\[<HOST>\].+commands=\d/\d
+
+mdpr-aggressive = (?:%(mdpr-auth)s|%(mdpr-normal)s|%(mdpr-ddos)s|%(mdpr-ddos2)s)
 mdre-aggressive = %(mdre-auth2)s
                   %(mdre-normal)s
+                  %(mdre-ddos2)s
 
 mdpr-errors = too many errors after \S+
 mdre-errors = ^from [^[]*\[<HOST>\]%(_port)s$
逆引きが出来ず、かつ必要なコマンドを発行していない接続は大体検知出来ています。
漏れはありますが9割以上遮断できているので一先ず良しとしています。

sshdのアクセス制限については、TCP Wrapperが利用できなくなり不便になりました。
ipsetとhosts.allowを併用して許可IPを絞り込んでいたのですが、良い代替方式が見つかりません。

2020年8月9日日曜日

MikroTik CRS326-24G-2S+IN購入

eurodkで購入。送料込みで$200以下、発送も速くてとても良い。

  • MikroTik CRS326-24G-2S+IN : 24 Gigabit ports, 2 SFP+

以前購入したMikroTik CRS309-1G-8S+INと筐体サイズが同じでいい感じです。
「server room power for your home!」とのこと。

postfixのメール中継設定

メール中継サーバで利用しているCentOS6のEOLが迫ってきたので、CentOS8に入れ替えました。 その際にpostfixのメール中継サーバ設定で少しはまったのでメモしておきます。

結論から言うと「パラメータのデフォルト値が変わっているのでマニュアルを確認しよう」です。

CentOS8ではpostfix 3.3.1がインストールされますが、compatibility_levelの設定値によりパラメータのデフォルト値が変わります。
postconfの出力からcompatibility_levelを抜き出すと、下記になります。
# postconf | grep compatibility_level
append_dot_mydomain = ${{$compatibility_level} < {1} ? {yes} : {no}}
compatibility_level = 2
mynetworks_style = ${{$compatibility_level} < {2} ? {subnet} : {host}}
relay_domains = ${{$compatibility_level} < {2} ? {$mydestination} : {}}
smtpd_relay_restrictions = ${{$compatibility_level} < {1} ? {} : {permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination}}
smtputf8_enable = ${{$compatibility_level} < {1} ? {no} : {yes}}
#
デフォルトのmain.cfでは、compatibility_levelが2に設定されています。
このためrelay_domainsのデフォルトが空となり、リレーが許可されません。
リレーを許可するには、明示的にrelay_domainsを設定する必要があります。
また、これらパラメータを参照しているパラメータも影響を受けるので注意が必要です。

今回のリプレースでは旧サーバの設定ファイルをコピーせず、デフォルトの設定ファイルから書き直しました。 main.cf内のコメントを読みながら設定していて「The default relay_domains value is $mydestination.」と記載があったため、旧バージョンと同じと考えてしまい、リレーできない原因が分からず暫くはまりました。

マニュアルにはバッチリ記載がありましたので、横着せずマニュアルを確認しようと言うことかと思います。 ちなみにcompatibility_levelのデフォルトは0なので、旧サーバの設定をコピーした場合は挙動が変わることはなかったようです。