fail2ban 설치 및 세팅 > IT 기술백서

IT 기술백서

직접 알아내거나 검색하기 귀찮아서 모아 둔 것

Server | fail2ban 설치 및 세팅

본문

fail2ban 은 ftp, ssh, http 등으로 서버에 과도하게 로그인시도를 하거나 정상적이지 않은 접근을 시도할때 접속을 차단하는 기능을 제공한다.

 

centos 에 설치하는 과정이다.

 

설치

[code]

# yum -y install fail2ban-firewalld fail2ban-systemd

[/code]

 

설정

jail.conf 설정파일을 copy 하여 jail.local 파일을 만든다.

[code]

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

[/code]

 

[code]

[DEFAULT]

bantime = 30m # 30분 동안 차단한다

findtime = 10m # 10분 동안의 데이터를 가지고 판단한다

maxretry = 5 # 5번 이상 로그인 실패했을시 차단한다

destemail = [email protected] # 차단내역 알림을 받을 이메일

mta = sendmail

 

# ip 차단방법

# - firewalld 일 경우

banaction = firewallcmd-multiport

banaction_allport = firewallcmd-allports

# - iptables 일 경우

# banaction = iptables-multiport

# banaction_allports = iptables-allports

 

[sshd]

enabled = true

 

[postfix]

enabled = true

 

[dovecot]

enabled = true

 

[postfix-sasl]

enabled = true

 

[nginx-limit-req]

enabled = true

[/code]

 

fail2ban 을 재시작한다.

[code]

# systemctl restart fail2ban

# fail2ban-client status

[/code]

[code]

Status

|- Number of jail:      5

`- Jail list:   dovecot, nginx-limit-req, postfix, postfix-sasl, sshd

[/code]

 

smtp 인증시도 방어

# vim /etc/fail2ban/filter.d/postfix-sasl.conf

[code]

[INCLUDES]

before = common.conf

[Definition] _daemon = postfix/smtpd

failregex = ^%(__prefix_line)swarning: [-._\w]+\[<HOST>\]: SASL (?:LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5) authentication failed(: [ A-Za-z0-9+/]*={0,2})?\s*$

[/code]

 

nginx 설정

# vim /etc/nginx/conf.d/[mysite].conf

[code]

# 1초에 2회까지만 응답하기 3회 이상이면 403 출력

limit_req_zone $binary_remote_addr zone=protect:10m rate=2r/s;

 

server {

    ....

    # php, html inc 확장자에만 적용한다 (루트에 적용하면 css, js 등을 못불러오는 사태가 발생한다)

    location ~ \.(php|inc|html|htm) {

        limit_req zone=protect;

        limit_req_status 403;

    }

}

[/code]

 

nginx 재시작

[code]

# systemctl restart nginx

[/code] 

댓글 0개

등록된 댓글이 없습니다.

Menu