構築ネタ(FreeBSD) fail2banで遊ぶ on FreeBSD
CentOSでも使っていたfail2banをFreeBSDでも使おうと思ったメモ。
例によって ports からインストール。portinstall はカテゴリから指定しないと入らない。
# portinstall security/py-fail2ban Please do not forget to update your configuration files. They are in /usr/local/etc/fail2ban/.
ということで、設定ファイルを修正しないといけないのはCentOSの時と同じ。jail.conf には ipfw を使うひな形が用意されているが、あいにくうちはパケットフィルタリングに pf を使っているのでこれは利用できない。幸い action.d/ を覗くと pf.conf があるので pf 環境でも問題ない。デフォルトのパケットフィルタに pf を指定。せっかくなので、デフォルトで用意されている apache 関連のフィルタを全部使ってみる。logpath は適宜変更すること。
# vi jail.conf
[/usr/local/etc/fail2ban/jail.conf]
[DEFAULT]
ignoreip = 127.0.0.1 自ホストのIPアドレス
bantime = 600
findtime = 600
maxretry = 3
backend = auto
action = pf
[apache-badbots]
enabled = true
filter = apache-badbots
action = pf
sendmail-whois[name=BadBots, dest=root]
logpath = /home/*/logs/access_log
bantime = 172800
maxretry = 1
[apache-auth]
enabled = true
filter = apache-auth
action = pf
sendmail-whois[name=Auth, dest=root]
logpath = /home/*/logs/error_log
bantime = 172800
maxretry = 3
[apache-nohome]
enabled = true
filter = apache-nohome
action = pf
sendmail-whois[name=Nohome, dest=root]
logpath = /home/*/logs/error_log
bantime = 172800
maxretry = 3
[apache-noscript]
enabled = true
filter = apache-noscript
action = pf
sendmail-whois[name=Noscript, dest=root]
logpath = /home/*/logs/error_log
bantime = 172800
maxretry = 3
[apache-overflows]
enabled = true
filter = apache-overflows
action = pf
sendmail-whois[name=Overflows, dest=root]
logpath = /home/*/logs/error_log
bantime = 172800
maxretry = 3
rc.conf に fail2ban を追加。
# vi /etc/rc.conf [/etc/rc.conf] (追加部分のみ) fail2ban_enable="YES"
■ ログのローテーション設定
/etc/newsyslog.conf に fail2ban のエントリを追加。ローテーションのタイミングはお好みで。
# vi /etc/newsyslog.conf [/etc/newsyslog.conf] 53a54 > /var/log/fail2ban.log 644 7 * $W0D0 Z
■ pf 側の設定
前述した action.d/pf.conf の記述を確認すると、
actionban = /sbin/pfctl -t fail2ban -T add <ip>/32 actionunban = /sbin/pfctl -t fail2ban -T delete <ip>/32
と極めて単純な指定がされている。コメント行を除けばこの2行でことが足りるくらい。これに対応させるため、/etc/pf.conf で "fail2ban" というテーブルを定義しておけばよい。/etc/pf.conf のなるべく上の方にこれを追加する。
[/etc/pf.conf] # fail2ban table table <fail2ban> persist block quick on $ex_if from any to <fail2ban> block quick on $ex_if from <fail2ban> to any
pf.conf を変更したら、これを反映させるために pf を再起動する。
# pfctl -f /etc/pf.conf
しかる後に、fail2ban を起動する。
# sh /usr/local/etc/rc.d/fail2ban start 2011-10-22 12:05:29,577 fail2ban.server : INFO Starting Fail2ban v0.8.4 2011-10-22 12:05:29,577 fail2ban.server : INFO Starting in daemon mode
■運用
拒絶されているIPアドレスの確認
# pfctl -t fail2ban -T show No ALTQ support in kernel ALTQ related functions disabled (この下に拒絶されているIPアドレスのリストが表示される。)
誤動作した場合のfail2banの解除
# pfctl -t fail2ban -T delete IPアドレス
■通知
fail2ban が発動すると、メールで通知される。
<実例> Hi, The IP 184.105.140.157 has just been banned by Fail2Ban after 4 attempts against Auth. Here are more information about 184.105.140.157: # # Query terms are ambiguous. The query is assumed to be: # "n 184.105.140.157" # # Use "?" to get help. # # # The following results may also be obtained via: # http://whois.arin.net/rest/nets;q=184.105.140.157?showDetails=true&showARIN=false&ext=netref2 # NetRange: 184.104.0.0 - 184.105.255.255 CIDR: 184.104.0.0/15 OriginAS: AS6939 NetName: HURRICANE-11 NetHandle: NET-184-104-0-0-1 Parent: NET-184-0-0-0-0 NetType: Direct Allocation RegDate: 2010-05-10 Updated: 2010-06-03 Ref: http://whois.arin.net/rest/net/NET-184-104-0-0-1 OrgName: Hurricane Electric, Inc. OrgId: HURC Address: 760 Mission Court City: Fremont StateProv: CA PostalCode: 94539 Country: US RegDate: Updated: 2011-04-13 Ref: http://whois.arin.net/rest/org/HURC ReferralServer: rwhois://rwhois.he.net:4321 OrgAbuseHandle: ABUSE1036-ARIN OrgAbuseName: Abuse Department OrgAbusePhone: +1-510-580-4100 OrgAbuseEmail: abuse@he.net OrgAbuseRef: http://whois.arin.net/rest/poc/ABUSE1036-ARIN OrgTechHandle: ZH17-ARIN OrgTechName: Hurricane Electric OrgTechPhone: +1-510-580-4100 OrgTechEmail: hostmaster@he.net OrgTechRef: http://whois.arin.net/rest/poc/ZH17-ARIN RAbuseHandle: ABUSE1036-ARIN RAbuseName: Abuse Department RAbusePhone: +1-510-580-4100 RAbuseEmail: abuse@he.net RAbuseRef: http://whois.arin.net/rest/poc/ABUSE1036-ARIN RNOCHandle: ZH17-ARIN RNOCName: Hurricane Electric RNOCPhone: +1-510-580-4100 RNOCEmail: hostmaster@he.net RNOCRef: http://whois.arin.net/rest/poc/ZH17-ARIN RTechHandle: ZH17-ARIN RTechName: Hurricane Electric RTechPhone: +1-510-580-4100 RTechEmail: hostmaster@he.net RTechRef: http://whois.arin.net/rest/poc/ZH17-ARIN # # ARIN WHOIS data and services are subject to the Terms of Use # available at: https://www.arin.net/whois_tou.html # Regards, Fail2Ban
とりあえずこんな感じ。