작은숲:위키노트/Fail2ban

큰숲백과, 나무를 보지 말고 큰 숲을 보라.

리눅스에서 무차별 대입 공격을 막기 위한 방법 중 하나는 fail2ban을 설치하는 것이다.

fail2ban 설치

CentOS에서는 간단히 [[작은숲:위키노트/Yum|Yum]으로 설치해준다. 다른 위키노트:리눅스 배포판에서도 대부분 패키지로 지원할 것이다. 만약 지원하는 패키지가 없다면, 번거롭겠지만 Fail2ban 사이트에 가서 소스를 받아 설치해야 한다.

# yum install fail2ban
Loaded plugins: security
Setting up Install Process
...
Installed:
  fail2ban.noarch 0:0.9.3-1.el6.1
Dependency Installed:
  gamin-python.x86_64 0:0.1.10-9.el6     ipset.x86_64 0:6.11-4.el6    libmnl.x86_64 0:1.0.2-3.el6
  python-inotify.noarch 0:0.9.1-1.el6
Complete!

끝났다!

fail2ban 설정

fail2ban이 0.9.3으로 업데이트 되면서 fail2ban의 설정 파일인 /etc/fail2ban/jail.conf을 건드릴 일이 없어졌다. 이제 사용자 설정은 /etc/fail2ban/jail.d 디렉토리 아래에 저장하면 된다. fail2ban 데몬은 시작하면서 /etc/fail2ban/jail.d 디렉토리에 있는 모든 파일을 읽어들여 이 설정들을 /etc/fail2ban/jail.conf에 있는 기본 설정에 우선하여 적용한다. 보통 사용자 설정은 /etc/fail2ban/jail.d/local.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 = 5
# Default banning action (e.g. iptables, iptables-new,
# iptables-multiport, shorewall, etc) It is used to define
# action_* variables. Can be overridden globally or per
# section within jail.local file
banaction = iptables-multiport
# Choose default action.  To change, just override value of 'action' with the
# interpolation to the chosen action shortcut (e.g.  action_mw, action_mwl, etc) in jail.local
# globally (section [DEFAULT]) or per specific section
action = %(action_)s
[sshd]
enabled = true

위 내용은 /etc/fail2ban/jail.conf 내용 중 수정할 여지가 있는 설정들이다.

ignoreip
무슨 짓을 하더라도 fail2ban에서 차단하지 않을 IP 주소를 설정한다. 주로 관리자가 사용하는 IP 주소가 될 것이다. 여러 개를 같이 쓸 수 있고 공백문자로 띄어 쓰면 된다.
bantime
fail2ban에게 걸린 경우 얼마 동안 차단할 것인지 설정한다. 초 단위이고 기본값은 600초, 10분이다.
findtime
검사할 대상을 몇 초 전의 로그부터 찾을 것인지 설정한다. 기본값은 600초이다. fail2ban/var/log/secure 로그에 나온 접속 기록으로 차단할지 여부를 결정한다.
maxretry
몇 번의 시도가 실패하면 차단할 것인지 설정한다. 기본값은 5이고, 다섯 번의 접속 시도가 실패하면 차단하게 된다. fail2ban은 앞에서 설정한 findtime 초 동안 maxretry 번의 접속 시도가 실패하면 차단하게 된다.
banaction
CentOS 7의 경우 기본 방화벽이 [[작은숲:위키노트/Iptables|Iptables]가 아니고 firewalld이다. 따라서 만약 iptables를 쓴다면 이 값을 iptables-multiport으로 변경해야 한다. 또한 fail2ban을 설치할 때 fail2ban-firewalld 패키지도 같이 설치된다. 이 패키지가 설치되면 /etc/fail2ban/jail.d/00-firewalld.conf 파일이 만들어지는데 iptables를 쓴다면 이 파일도 지워야 한다.
action
기본 설정은 %(action_)s이다. 만약 fail2ban의 처리 결과 등을 메일로 받아보고 싶다면 이 값을 %(action_mw)s로 변경한다. 이 경우 destemail 지시자에 메일을 받을 관리자의 메일 주소를 제대로 설정해야 한다.

그리고 마지막에 sshd에 대해 검사하고 차단하도록 추가한다.

데몬 실행

설정이 끝나면 데몬을 실행하고, 나중에 부팅 후에도 자동으로 올라오도록 설정해준다.

# service fail2ban start
fail2ban 시작 중:                                          [  OK  ]
# chkconfig --levels 235 fail2ban on

참고

이 작은숲 문서의 출처는 위키노트의 위키노트/Fail2ban 문서입니다.