fail2ban

Server/CentOS 2014. 5. 29. 13:01

# secure log
May 28 11:18:28 oops4u saslauthd[31802]: pam_unix(smtp:auth): check pass; user unknown
May 28 11:18:28 oops4u saslauthd[31802]: pam_unix(smtp:auth): authentication failure; logname= uid=0 euid=0 tty= ruser= rhost=
May 28 11:18:28 oops4u saslauthd[31802]: pam_succeed_if(smtp:auth): error retrieving information about user library

 

# message log
May 28 13:03:11 oops4u saslauthd[31804]: do_auth         : auth failure: [user=sale] [service=smtp] [realm=] [mech=pam] [reason=PAM auth error]
May 28 13:03:16 oops4u saslauthd[31802]: do_auth         : auth failure: [user=sale] [service=smtp] [realm=] [mech=pam] [reason=PAM auth error]
May 28 13:03:21 oops4u saslauthd[31801]: do_auth         : auth failure: [user=sale] [service=smtp] [realm=] [mech=pam] [reason=PAM auth error]

 

# mail log
May 28 12:04:50 oops4u sendmail[4749]: s4P34kTF004749: wvps46-163-109-6.dedicated.hosteurope.de [46.163.109.6] did not issue MAIL/EXPN/VRFY/ETRN during connection to MTA
May 28 12:05:34 oops4u sendmail[4827]: s4P35UXD004827: www.igbce-og-agv.de [46.163.109.83] did not issue MAIL/EXPN/VRFY/ETRN during connection to MTA
May 28 12:06:14 oops4u sendmail[4836]: s4P36B47004836: wvps46-163-109-86.dedicated.hosteurope.de [46.163.109.86] did not issue MAIL/EXPN/VRFY/ETRN during connection to MTA

 

정말 지긋지긋하게 보아온 로그.
맘고생했던 기간에 비해 아주 간단하게 해결됐습니다.

 

 

fail2ban

 

username/password 무차별 침입 시도(Brute-force, dictionary attack)를 방지하기 위한 툴.
log 파일에서 흔히 볼 수 있는 패턴들에 대하여 방화벽에 새로운 규칙(ip 차단 등)을 추가할 수 있습니다.
상세한 설치 메뉴얼은 fail2ban 홈페이지를 참조합니다.
site : http://www.fail2ban.org/

 

 

설치

 

# wget https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.8.13
# tar zxvf 0.8.13
# cd fail2ban-0.8.13
# ./setup.py install

 

 

자신의 배포판에 맞는 스크립트 복사 & 모드(755) 확인 & 부팅시 자동 실행 설정

 

# cd files
# cp redhat-initd /etc/init.d/fail2ban
# chkconfig --add fail2ban

 

 

logrotate 추가

 

# vi /etc/logrotate.d/fail2ban
/var/log/fail2ban.log {
     weekly
     rotate 4
     missingok
     compress
     postrotate
       /usr/local/bin/fail2ban-client set logtarget /var/log/fail2ban.log >/dev/null
     endscript
}

 

 

설정 (Sendmail)

 

# cd /etc/fail2ban
# vi filter.d/sendmail.conf

 

# Fail2Ban configuration file
#
# Source: http://www.the-art-of-web.com/system/fail2ban-sendmail/
# Contibutors: Gutza, the SASL regex
#
# $Revision: 0 $
#

 

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

 

failregex = \[<HOST>\] .*to MTA
            \[<HOST>\] \(may be forged\)
            \[<HOST>\], reject.*\.\.\. Relaying denied
            (User unknown)\n* \[<HOST>\]
            badlogin: .* \[<HOST>\] plaintext .* SASL

 

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

 

# vi jail.conf

 

[sendmail]
enabled  = true
filter   = sendmail
action   = iptables-multiport[name=sendmail, port="pop3,imap,smtp,pop3s,imaps,smtps", protocol=tcp]
           sendmail-whois[name=sendmail, dest=you@example.com]
logpath  = /var/log/maillog

 

dest 부분에는 내용을 전달받을 내 이메일 주소를 입력합니다.

 

이 같은 설정 방식으로 다른 서비스들도(ssh, smtp...) 필요하다면 fail2ban 홈페이지를 참조하여 추가합니다.
jail.conf 파일에서 bantime도 설정합니다. (기본 10분 차단으로 되어 있습니다.)
차단된 로그는 /var/log/fail2ban.log 나 전달된 메일에서 확인할 수 있습니다.

 

 

데몬 시작

 

# /etc/init.d/fail2ban start

 

 

 


WRITTEN BY
손가락귀신
정신 못차리면, 벌 받는다.

,