いわマン備忘録

備忘録とちょっとした設定メモや小ネタをUPしたいと思っております。

saslアタック対策

前回がPostfixだったので、その流れでセキュリティ対策かなと。Postfixを動かすとsaslとsmtpのアタックが多い上ウザイのでfail2banで対策します。

fail2banはlogなどで吐かれたwarningやerrorを拾って動的にiptablesにルールを追加してくれるソフトウェア。Postfix以外でも色々なフィルタールールが用意されてます。編集は必要ですが。

今回はPostfix対策用に使います。

###
### インストール
###

yum -y install --enablerepo=epel fail2ban


# confの編集
vi /etc/fail2ban/fail2ban.conf

logtarget = /var/log/fail2ban.log

###
### 拒否設定の編集
###

vi /etc/fail2ban/jail.conf

# 除外するIP
ignoreip = 127.0.0.1/8 123.456.789.0/32 456.789.123.0/32

↑みたいな感じでIPを追加

# 検知した場合にシャットする時間
bantime = 300

# 検知する間隔
findtime = 300

# findtime, maxretryの役割
findtime秒間のうちにfilterで指定されたパターンにマッチするログが
maxretry回出現すれば不正アクセスとみなす

maxretry = 3

# ログの監視方法の指定

一定間隔ごとにlogを監視するpolling、gaminというファイルの変化を通知するデーモンを使う方法、自動化のautoの3種類。通常はautoで良いが、稀に誤動作の可能性あり。
個人的には「gamin」が良いと判断してます。
pyinotifyはファイル改ざん監視をする為のソフトでインストールされている場合はそれを使うことも可能。

また、監視するポート毎の設定で監視する方法を指定することも可能。

backend = auto

# ホスト名があった場合、DNS検索が実行され警告としてログに書き出す設定
# yes,no,warnがある
usedns = warn

###
### 監視する項目の個別設定
###

デフォルトではsshのみONになってますが、sshは別途対策を行っているのでfail2banではOFFにしておきます。

# postfix-sasl用の設定編集
# 複数ポートを指定する場合は「iptables-multiport」
[postfix-sasl]
enabled = true
filter = postfix-sasl
backend = gamin

# 通常
action = iptables[name=postfix-sasl, port="smtp", protocol=tcp]
sendmail-whois[name=postfix-sasl, dest=root, sender=fail2ban]
logpath = /var/log/maillog

# 複数ポート指定
action = iptables-multiport[name=postfix-sasl, port="smtp,smtps", protocol=tcp]
sendmail-whois[name=postfix-sasl, dest=root, sender=fail2ban]
logpath = /var/log/maillog

# Postfix Iptables Filter
[postfix-iptables]

enabled = true
filter = postfix
backend = gamin
action = iptables[name=postfix, port="smtp", protocol=tcp]
sendmail-whois[name=postfix, dest=root, sender=fail2ban]
logpath = /var/log/maillog

検知した際にroot宛にメールが送られる設定です。環境に応じて変更してください。

###
### Postfix-iptables
###

vi /etc/fail2ban/filter.d/postfix.conf

logに吐かれる内容を正規表現でパターンマッチさせます。下記はその例で、よく攻撃を受けるパターンに対応していると思います。これも環境に応じて変更してください。

failregex = ^%(__prefix_line)sNOQUEUE: reject: RCPT from \S+\[<HOST>\]: 554 5\.7\.1 .*$
^%(__prefix_line)sNOQUEUE: reject: RCPT from \S+\[<HOST>\]: 450 4\.7\.1 .*$
^%(__prefix_line)sNOQUEUE: reject: VRFY from \S+\[<HOST>\]: 550 5\.1\.1 .*$

###
### Postfix-sasl-iptables
###

vi /etc/fail2ban/filter.d/postfix-sasl.conf

[Definition]

_daemon = postfix/smtpd

#failregex = ^%(__prefix_line)swarning: [-._\w]+\[<HOST>\]: SASL (?:LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5) authentication failed(: [ A-Za-z0-9+/]*={0,2})?\s*$

# フィルタールール
failregex = (?i): warning: [-._\w]+\[<HOST>\]: SASL (?:LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5) authentication failed: \w

# 正規表現を無視する記述
ignoreregex =

※saslのルールには記述がないのでlogにエラーが出てしまう。追記しておく。

# logローテート変更
vi /etc/logrotate.d/fail2ban

# 追加
daily
rotate 15

# コメントアウト
size 30k

※ローテート設定において、fail2banのバージョンによっては多少違いがあります。あくまで参考までに。

その他、fail2ban自身を守ることも可能なので、必要に応じて設定しても良いかもしれません。ただ、全ポート監視になる為注意は必要です。

またiptablesをrestartした場合は、fail2banもrestartしないとフィルタールールが適用されませんのでご注意を。

# fail2ban自身の保護 全ポート監視だから危険
# 参考:http://whyscream.net/wiki/index.php/Fail2ban_monitoring_Fail2ban

# フィルタールール作成
# 実際には使用しないから1つ上の階層に作成
vi /etc/fail2ban/fail2ban.conf.filter

###
### Step1
###

# Fail2Ban configuration file
#
# Author: Tom Hendrikx
#
# $Revision$
#

[Definition]

# 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
#

# Count all bans in the logfile
failregex = fail2ban.actions: WARNING \[(.*)\] Ban <HOST>

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

# Ignore our own bans, to keep our counts exact.
# In your config, name your jail 'fail2ban', or change this line!
ignoreregex = fail2ban.actions: WARNING \[fail2ban\] Ban <HOST>


###
### Step2
###

[fail2ban]
enabled = true
filter = fail2ban
action = iptables-allports[name=fail2ban]
sendmail-whois[name=fail2ban]
logpath = /var/log/fail2ban.log
# findtime: 1 week
# findtime = 604800
findtime = 600
# bantime: 1 week
# bantime = 604800
bantime = 300


# fail2banの設定確認
fail2ban-client -d


# サービス起動
service fail2ban start
chkconfig fail2ban on