posted by 은이종 2013. 4. 4. 18:08

0. 개요

python 기반의 툴로

log파일을 읽어 일정시간 차단을 어플로, ssh ftp 등 무작위 로그인을 통한 brute force attack에 대응하기 위한 좋은 어플이다.

iptables , tcpwrapper 등에 해당 host 를 직접 등록하여, 직관적인 차단이 가능하다.

 

1 .설치 과정

공홈 : http://www.fail2ban.org

CentOS 에 yum으로 설치 하려면

vi /etc/yum.repos.d/CentOS-Base.repo

에 아래 글 추가

[dag]
name=Dag RPM Repostory for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1
gpgkey=http://dag.wieers.com/packages/RPM-GPG-KEY.dag.txt

[kbs-CentOS-Misc]
name=CentOS.Karan.Org-EL$releasever - Stable
gpgkey=http://centos.karan.org/RPM-GPG-KEY-karan.org.txt
gpgcheck=1
enabled=1
baseurl=http://centos.karan.org/el$releasever/misc/stable/$basearch/RPMS/

 

2. 설치 과정

yum install fail2ban

설치경로는  /etc/fail2ban 이다.

 

3. 설정

설정파일은 /etc/fail2ban/jail.conf

[DEFAULT]

# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not
# ban a host which matches an address in this list. Several addresses can be
# defined using space separator.
ignoreip = 127.0.0.1/8

# "bantime" is the number of seconds that a host is banned.
bantime  = 600

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime  = 600

# "maxretry" is the number of failures before a host get banned.
maxretry = 3

 

ignoreip : 무시할 IP주소 (여기에 입력한 IP는 차단 하지 않음) CIDR Mask, DNS, IP를 입력가능

              공백을 이용하여 여러개 입력 가능

bantime : 해킹시도가 감지되었을 때 해당 IP를 차단할 차단기간 (기본값 : 600, 단위 : 초)

findtime : 로그에서 검색할 시간 (기본값 : 600, 단위 : 초)

maxretry : 일정시간동안 해킹시도 횟수

backend : 로그 파일 변경을 감지할 방법 (gamin, polling, auto)

               gamin : Gamin(file alteration monitor)설치된 경우 사용가능

               polling : 주기적으로 점검

               auto : Gamin이 설치된 경우 gamin사용, 아니면 polling 사용

설정파일에서 이메일 주소 변경

:%s/you@mail.com/내메일주소

:%s/fail2ban@mail.com/보내는사람 메일 주소

 

# This jail corresponds to the standard configuration in Fail2ban 0.6.
# The mail-whois action send a notification e-mail with a whois request
# in the body.

[ssh-iptables]

enabled  = false
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
           sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com]
logpath  = /var/log/sshd.log
maxretry = 5

[proftpd-iptables]

enabled  = false
filter   = proftpd
action   = iptables[name=ProFTPD, port=ftp, protocol=tcp]
           sendmail-whois[name=ProFTPD, dest=you@example.com]
logpath  = /var/log/proftpd/proftpd.log
maxretry = 6

# This jail forces the backend to "polling".

[sasl-iptables]

enabled  = false
filter   = sasl
backend  = polling
action   = iptables[name=sasl, port=smtp, protocol=tcp]
           sendmail-whois[name=sasl, dest=you@example.com]
logpath  = /var/log/mail.log

실제 적용해야 할 서비스 목록입니다. 가장 중요한 부분이겠죠?

해당 원하는 서비스를 확인후에 enabled = false를 true로 변경하시면 됩니다.

 

변경된 설정을 적용시켜주시고, 재부팅후에도 fail2ban이 실행되게 chkconfig에 등록합니다.

service fail2ban start

chkconfig –levels 235 fail2ban on

 

 

추가 설정

1. devecot-pop3설정

# sudo vi /etc/fail2ban/filter.d/dovecot-pop3imap.conf  <- 파일 추가
[Definition]
failregex = (?: pop3-login|imap-login): (?:Authentication failure|Aborted login \(auth failed|Disconnected \(auth failed).*rip=(?P<host>\S*),.*
ignoreregex =
# sudo vi /etc/fail2bain/jail.conf   <- 아래 내용 추가
[dovecot-pop3imap]
enabled = true
filter = dovecot-pop3imap
action = iptables-multiport[name=dovecot-pop3imap, port="pop3,imap", protocol=tcp]
logpath = /var/log/maillog
maxretry = 20
findtime = 1200
bantime = 1200
# sudo /etc/init.d/fail2ban restart   <- 서비스 재시작

 

Proftpd 필터 사용시 proftpd 설정에 따라 로그를 검사하지 못하는 경우가 생긴다. /etc/fail2ban/filter.d/proftpd.conf 파일을 아래와 같이 변경 (진한 부분을 추가)

failregex = (S+[<HOST>])[: -]+ USER S+: no such user found from S+ [S+] to S+:S+$
            (S+[<HOST>])[: -]+ USER S+ (Login failed): Incorrect password.$
            (S+[<HOST>])[: -]+ SECURITY VIOLATION: S+ login attempted.$
            (S+[<HOST>])[: -]+ Maximum login attempts (d+) exceeded$ 
USER S+: no such user found from S* [<HOST>] to S+s*$
          ^<HOST> .* nobody .*PASS .* 530