ブルートフォースアタック対策

facebookアプリに登録していたサーバーから、
迷惑メールが送信されているとの指摘(※提供されたメールヘッダ)があり、調査。
結果的にはブルートフォースアタックで、
postmasterのパスワードが破られた事によるもの。
SSH,FTPS,POP3(dovecot)それぞれに対策を実施。

Q.考えられる対策

A1.sshdに対する連続アクセスを遮断

A2.sshdに対する閾値を超えるログイン失敗の場合遮断

        • >DenyHostsを導入

A3.海外のIPを遮断


今回はA1で対策を施しました。

IPtablesの設定を追加して特定回数

#接続制限フラグ立て
iptables -N SSHAttacker
iptables -A SSHAttacker -m recent --set --name attacker -j LOG --log-level warn --log-prefix 'SSHAttaker:'
iptables -A SSHAttacker -j DROP

#接続制限されている場合は10分間接続拒否
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --rcheck --seconds 600 --name attacker -j DROP

#60秒以内で5回以上の接続を制限
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 5 --rttl --name SSH -j SSHAttacker

# 外部からのTCP110番ポート(POP3)へのアクセスを制限つきで許可
iptables -A INPUT -p tcp -m state --syn --state NEW --dport 110 -m limit --limit 1/second --limit-burst 1 -j ACCEPT
iptables -A INPUT -p tcp -m state --syn --state NEW --dport 110 -j DROP

#設定を保存
iptables-save
/etc/rc.d/init.d/iptables restart

#設定を確認
iptables -L

rootログインを無効化

#useradd hogehoge
#passwd hogehoge
#usermod -G wheel hogehoge

不正アクセスログの確認

//ログイン履歴の確認
# lastlog
lp                                         **Never logged in**
sync                                       **Never logged in**
shutdown                                   **Never logged in**
postmaster       pts/0    183.63.220.2     Fri May 18 02:32:37 +0900 2012

//特定のIPのログイン試行回数
# grep 'xxx.xxx.xxx.xxx(不正アクセスをしてきたIP)' /var/log/secure.1 | grep 'user' | wc -l

//ログの監視
# tail -f /var/log/maillog
# tail -f /var/log/secure

//メールスプール確認(未配信のメール)
#mailq
※/var/spool/postfix/active 配下に保存

//キューID
-------------------------------------------------------------------------
52A3A357328*    1525 Sun May 13 10:28:20 info@aplusresults1.com
                                         eric.johnson@schenkerusa.com
                                         eric.schaudt@comcast.net
                                         eric.w.downey@comcast.net
EEF08357387*    1525 Sun May 13 10:39:18 info@aplusresults1.com
                                         hhwoo@generalbank.com
                                         hib@port.rotterdam.nl
                                         hicorp00@bellsouth.net
-------------------------------------------------------------------------
//未配信に溜まったメールの内容
//Postfixの場合
///var/spool/postfix/deferred/キューIDの最初の1文字/キューID
//この内容はテキスト形式ではないので、
//postcatで確認する
#postcat -q キューID

以下参考

sshブルートフォースアタック防ぎたい。対応策発見
http://d.hatena.ne.jp/editnuki/20110829/1314579180
・New netfilter matches
http://www.netfilter.org/documentation/HOWTO/netfilter-extensions-HOWTO-3.html#ss3.16
・DenyHosts で ssh ブルートフォースアタック対策
http://www.maruko2.com/mw/DenyHosts_%E3%81%A7_ssh_%E3%83%96%E3%83%AB%E3%83%BC%E3%83%88%E3%83%95%E3%82%A9%E3%83%BC%E3%82%B9%E3%82%A2%E3%82%BF%E3%83%83%E3%82%AF%E5%AF%BE%E7%AD%96
POP3への辞書攻撃と我が対策
http://memorandum.yamasnet.com/archives/Post-368.html
・IDS(侵入検知システム)
AIDE(Advanced Intrusion Detection Environment)をインストールする
http://dev.classmethod.jp/cloud/amazon-ec2-ids-aide/
chkrootkit
chkrootkit | grep INFECTED
http://pocketstudio.jp/linux/?chkrootkit
セキュアなSSHサーバーを構築する
http://itpro.nikkeibp.co.jp/article/COLUMN/20070611/274357/
・提供されたメールヘッダ

---------------------------------------------------------------------
From: 	- Mon May 14 20:27:32 2012
X-UIDL: 	GmailIdxxxxxxxxxxxx
X-Mozilla-Status: 	0001
X-Mozilla-Status2: 	00000000
[送信先]
Delivered-To: 	*****************@gmail.com

[経由したサーバーのIPアドレス]
Received: 	by 10.216.86.67 with SMTP id v45csp131221wee; Sun, 13 May 
2012 22:27:22 -0700 (PDT)

Received: 	by 10.68.200.202 with SMTP id 
ju10mr8900357pbc.54.1336973241457; Sun, 13 May 2012 22:27:21 -0700 (PDT)
Return-Path: 	<info@fedex.com>

[送信したパソコン名またはホスト名など ]
Received: 	from xxxxxxxx.xxxxx.com (xxxxxxx.xxxxx.ne.jp. 
[IPxxxxxxxxxx]) by mx.google.com with ESMTP id 
gi5si130055pbc.56.2012.05.13.22.26.50; Sun, 13 May 2012 22:27:21 -0700 
(PDT)

--------------------------------------------
-->SPF「Sender Policy Framework」
メールの送信元アドレスの詐称を防ぐための、いわゆる送信ドメイン認証技術
Pass・・正当なメール
Fail・・不当なメール
neutral・・SPF 指定がないものとして扱われる
--------------------------------------------
Received-SPF: 	neutral (google.com: IPxxxxxxxxxx is neither permitted 
nor denied by best guess record for domain of info@fedex.com) 
client-ip=IPxxxxxxxxxx;
Authentication-Results: 	mx.google.com; spf=neutral (google.com: 
IPxxxxxxxxxx is neither permitted nor denied by best guess record for 
domain of info@fedex.com) smtp.mail=info@fedex.com

[送信したパソコン名またはホスト名など ]
Received: 	from User (unknown [41.85.161.2]) by 
self.server.com (Postfix) with ESMTP id 1F90718F47C; Mon, 14 
May 2012 14:22:13 +0900 (JST)


Reply-To: 	<parceldelivery_fdex@mail.com>
From: 	FedEx CustomerCare Unit<info@fedex.com>
Subject: 	Your Parcel/Delivery Details..........
Date: 	Mon, 14 May 2012 06:22:32 +0100
MIME-Version: 	1.0
Content-Type: 	text/plain; charset="Windows-1251"
Content-Transfer-Encoding: 	7bit
X-Priority: 	3
X-MSMail-Priority: 	Normal
[送信者が使用したメールソフト名]
X-Mailer: 	Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: 	Produced By Microsoft MimeOLE V6.00.2600.0000
Message-Id: 	<20120514052214.1F90718F47C@self.server.com>
To: 	undisclosed-recipients:;
-------------------------------------------------------------------------