2 분 소요

fail2ban 설치 및 간단 설정

리눅스 서버의 특정 서비스에 과도한 패스워드 공격이 가해지면 자동으로 해당 IP를

iptables 혹은 filrewalld 에서 차단하는 기능이다.

1. 설치

yum install -y fail2ban fail2ban-systemd whois 

iptables 로 차단 관리를 할 경우 firewalld 설정 제거

rm -f /etc/fail2ban/jail.d/00-firewalld.conf

기본 서비스 시작

systemctl enable fail2ban
systemctl start fail2ban

2. 설정

# cd /etc/fail2ban/

jail.conf 파일에 바로 설정도 가능하지만, 패키지 업데이트등이 이루어지면 설정이 초기화되는 상황이 발생한다. 기본 설정은 그대로 두고, 개별 설정을 부가적으로 적용하는 방법으로 설정한다.

# vi jail.local

[DEFAULT]
ignoreip = 127.0.0.1/8 192.168.201.0/24 192.168.123.0/24  ## 허가 IP 대역 
bantime = 10800 ## findtime 시간동안 maxretry 시도가 탐지된 경우 해당 IP 차단 시간 
findtime = 600  
maxretry = 5
backend = pooling
destemail = [email protected]   
sender = [email protected]
mta = sendmail
action = %(action_mwl)s

[sshd]
enabled = true
port = ssh,22

[dovecot]
enabled = true

[postfix]
enabled = true

[postfix-sasl]
enabled = true

dovecot 이나 postfix 처럼 기본 제공되는 filter 에서 해당되는 서비스를 활성화 시켜준다. sshd 는 필수적으로 적용해 주고.. filter 로 제공되는 기본 서비스는 아래와 같다.

# ls /etc/fail2ban/filter.d/
3proxy.conf                counter-strike.conf  haproxy-http-auth.conf  pam-generic.conf        sogo-auth.conf
apache-auth.conf           courier-auth.conf    horde.conf              perdition.conf          solid-pop3d.conf
apache-badbots.conf        courier-smtp.conf    ignorecommands          php-url-fopen.conf      squid.conf
apache-botsearch.conf      cyrus-imap.conf      kerio.conf              phpmyadmin-syslog.conf  squirrelmail.conf
apache-common.conf         directadmin.conf     lighttpd-auth.conf      portsentry.conf         sshd.conf
apache-fakegooglebot.conf  domino-smtp.conf     mongodb-auth.conf       postfix.conf            stunnel.conf
apache-modsecurity.conf    dovecot.conf         monit.conf              proftpd.conf            suhosin.conf
apache-nohome.conf         dropbear.conf        murmur.conf             pure-ftpd.conf          tine20.conf
apache-noscript.conf       drupal-auth.conf     mysqld-auth.conf        qmail.conf              traefik-auth.conf
apache-overflows.conf      ejabberd-auth.conf   nagios.conf             recidive.conf           uwimap-auth.conf
apache-pass.conf           exim-common.conf     named-refused.conf      roundcube-auth.conf     vsftpd.conf
apache-shellshock.conf     exim-spam.conf       nginx-botsearch.conf    screensharingd.conf     webmin-auth.conf
assp.conf                  exim.conf            nginx-http-auth.conf    selinux-common.conf     wuftpd.conf
asterisk.conf              freeswitch.conf      nginx-limit-req.conf    selinux-ssh.conf        xinetd-fail.conf
bitwarden.conf             froxlor-auth.conf    nsd.conf                sendmail-auth.conf      znc-adminlog.conf
botsearch-common.conf      groupoffice.conf     openhab.conf            sendmail-reject.conf    zoneminder.conf
centreon.conf              gssftpd.conf         openwebmail.conf        sieve.conf
common.conf                guacamole.conf       oracleims.conf          slapd.conf

설정이 완료되면 fail2ban 서비스를 재시작한다.

systemctl restart fail2ban

동작 상태를 확인한다.

# fail2ban-client status
Status
 |- Number of jail:	4
 `- Jail list:	dovecot, postfix, postfix-sasl, sshd

현재 등록된 서비스가 4개 있다는 것을 알수 있다. 이중 sshd 상태를 확인한다.

# fail2ban-client status sshd 
Status for the jail: sshd
|- Filter
|  |- Currently failed:	15
|  |- Total failed:	125
|  `- Journal matches:	_SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned:	8
   |- Total banned:	8
   `- Banned IP list:	111.61.241.100 165.22.69.147 37.187.12.126 139.59.40.240 139.59.68.15 27.191.237.67 217.182.79.176 119.29.16.190

여러 IP 들이 sshd 로 무작위 접근 했고, 차단된 것을 확인할 수 있다. 실제 iptables 에 차단된 rule 이 등록되었는지 확인한다.

# iptables -nvL

간혹 fail2ban 에 정상적인 IP 가 차단 된 경우 아래와 같은 방법으로 차단을 해제할 수 있다.

fail2ban-client set sshd unbanip x.x.x.x
사쿠라호스팅 호스팅 상품 보러가기 사쿠라호스팅 블로그