리그캣의 개발놀이터

[서버 보안 (CentOS7 / Ubuntu)] fail2ban 설치하기 본문

프로그래밍 기본/서버 구축 및 관리

[서버 보안 (CentOS7 / Ubuntu)] fail2ban 설치하기

리그캣 2019. 5. 22. 19:21

Linux 서버를 공개망에서 사용하게 되면

ssh 로그인을 지속적으로 시도하는 ip들이 있다

대부분 외국 ip대역이고 방화벽을 설정하긴 하지만 

특수한 목적으로 인해 IP 대역을 제한할 수 없는 경우가 있을 수 있다.

 

이런 경우 보안을 조금 더 강화하기 위한 방법이 fail2ban 설치이다.

 

CentOS에서 설치하기

 

Rpm 저장소를 설치

$rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

 

Yum 이용하여 설치

$yum install -y fail2ban fail2ban-systemd

 

이때 fail2ban-firewalld 패키지가 필수로 설치 된다.

firewalld 아닌 iptables 사용하기 위해 해당 파일은 삭제

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

 

fail2ban 자동 실행되도록 설정

$systemctl enable fail2ban.service

$systemctl start fail2ban.service

 

재부팅 후에도 자동 실행 가능하도록 설정

$chkconfig fail2ban on

 

Ubuntu에서 설치하기

 

$apt install fail2ban

 

 

fail2ban 설정 디렉터리 확인

 

설치 후 아래와 같이 파일이 나열되지 않고 3~4개만 나열된다면 yum remove로 epel-release 및 fail2ban을 지우고 재 설치 하여야한다.

 

# ll /etc/fail2ban/

 

drwxr-xr-x 2 root root  4096 May 22 13:36 action.d

-rw-r--r-- 1 root root  2328 May 11  2017 fail2ban.conf

drwxr-xr-x 2 root root  4096 Jul 14  2017 fail2ban.d

drwxr-xr-x 3 root root  4096 May 22 13:36 filter.d

-rw-r--r-- 1 root root 21502 Jul 14  2017 jail.conf

drwxr-xr-x 2 root root  4096 May 22 13:42 jail.d

-rw-r--r-- 1 root root  2375 May 11  2017 paths-common.conf

-rw-r--r-- 1 root root   642 May 11  2017 paths-debian.conf

-rw-r--r-- 1 root root  1070 May 11  2017 paths-fedora.conf

-rw-r--r-- 1 root root  1156 May 11  2017 paths-freebsd.conf

-rw-r--r-- 1 root root   975 May 11  2017 paths-opensuse.conf

-rw-r--r-- 1 root root   290 May 11  2017 paths-osx.conf

 

위와 같은 파일들이 보인다

  • /etc/fail2ban
    fail2ban
    설치 기본적으로 설정되는 디렉터리 입니다.
  • /etc/fail2ban/filter.d
    접속 로그파일 분석 filter.d 있는 서비스 필터를 사용. Sshd.conf 파일의 경우 sshd 서비스 로그필터 파일
  • /etc/fail2ban/action.d
    계정 접속 실패시 사용할 있는 action 파일들이 위치
  • /etc/fail2ban/jail.d
    fail2ban
    사용자 설정을 넣을 있는 디렉터리. Fail2ban 실행시 jail.d 디렉터리에 있는 모든파일들에 있는 설정파일 적용

    관련
    패키지 설치 세팅파일 생성
    fail2ban-firewalld 설치 -> 00-firewalld.conf 파일 생성
    fail2ban-systemd 설치 -> 00-systemd.conf 파일 생성
     
  • /etc/fail2ban/jail.conf
    기본설정 파일. 사용자 설정이 jail.d 디렉터리에 존재할 경우 사용자 설정이 기본설정보다 우선 적용
     
  • /etc/fail2ban/fail2ban.conf
    fail2ban
    로그 파일 설정파일.

 

fail2ban 파일 설정

 

 

#/etc/fail2ban/jail.conf

기본설정 파일.
해당 파일에서 직접 설정할 경우 업데이트 초기화된다. jail.conf 파일을 직접 수정하지 않고 새로 생성한 설정파일에서 생성하는 것을 추천.
그렇게 되면 yum update 진행하더라도 설정한 파일이 덮어씌워지지 않음.
 

  • 설정 내역은 아래와 같은 순서로 덮어 씌워짐
    /etc/fail2ban/jail.conf
    ->
    /etc/fail2ban/jail.d/*.conf
    ->
    /etc/fail2ban/jail.local

    ->

/etc/fail2ban/jail.d/*.local
 

fail2ban 기본 설정

 

$cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

 

$vi /etc/fail2ban/jail.local
 

설정 예제

 

[DEFAULT]

# Common

ignoreip  = 127.0.0.1/8

bantime   = 86400

findtime  = 86400

maxretry  = 3

banaction = iptables-multiport

 

# Mail

destemail = 알림 수신메일 주소

sender    = 알림 발신메일 주소

 

# Action

action_mw = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]

            %(mta)s-whois[name=%(__name__)s, sender="%(sender)s", dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"]

 

action    = %(action_mw)s

 

[sshd]

enabled = true

 

출처: <https://www.happyjung.com:6001/lecture/2471>

옵션 설명

 

#ignoreip
관리자 ip 입력하면 . 여기 입력한 ip 차단되지 않음

#bantime
인증 실패시 차단할 차단시간. -1 설정할 영구차단 (단위: )

영구차단을 하거나 차단시간을 많이 주면 재부팅시 fail2ban 다시 ip  차단 리스트를 읽어오기 때문에 느려짐

#findtime
입력한 시간 안에 허용횟수를 초과하여 실패 차단됨. (단위: )

 

#maxretry
차단 되기전까지 인증시도를 위한 허용횟수.
허용횟수를 초과하여 실패할 경우 차단

#banaction
ip
차단 방법
/etc/fail2ban/action.d 디렉터리에 있는 action 입력할 있음

firewalld 사용한다면 "firewallcmd-new" 입력
iptables 사용한다면 "iptables-multiport" 입력
 
#action
action
"%(action_mw)s" 값을 입력하면 ip  차단시 알림 메일이 전송된다.

알림 메일을 받지 않으려면 "%(action_)s" 값으로 변경하면 된다.
 

서비스 재시작

 

$/etc/init.d/fail2ban restart

 

 

fail2ban 설정 확인
 

$fail2ban-client status sshd

Comments