본문 바로가기
개인 공부/리눅스

Fail2Ban

by 몽섭 2025. 1. 21.
728x90
반응형
SMALL

 

 

FAIL2BAN 실습

  • FAIL2BAN을 이용한 포트포워딩 및 웹사이트 외부 접속 차단 테스트

 

 

환경 설정

1. 클라우드 환경

  • Naver Cloud (무료 크레딧 활용) 
    • Fail2Ban 서버 :
      • 공인 IP 
        •  Port 
          • 22(SSH)

2. 운영체제

  • Rocky Linux 9.4

 

3. 소프트웨어 구성

  • Fail2Ban 1.0.2

FAIL2BAN

1. Fail2Ban 서버 설정

# epel 저장소 설치
sudo dnf install -y epel-release

# fail2ban 설치
sudo dnf install -y fail2ban
  • Fail2Ban은 표준 패키지 저장소에는 포함되어 있지 않기 때문에 epel 저장소를 먼저 설치해야 합니다.

 

 

# 재부팅시 fail2ban 자동시작
systemctl enable fail2ban

# fail2ban 시작
systemctl start fail2ban

# fail2ban 상태확인
systemctl status fail2ban

 

 

 

# fail2ban / jail.conf 설정파일 생성
cp /etc//fail2ban/jail.conf /etc/fail2ban/jail.local
  • jail.local 로 변환하여 복사할 경우 .conf보다 우선시 하여 읽기에 기본 설정을 유지하면서 필요한 부분만 변경할 수 있습니다.
  • Fail2Ban 업데이트에도 영향을 받지 않아 안전합니다.

 

 

# fail2ban 기본설정 파일을 수정합니다.
vim /etc/fail2ban/jail.local

 89 # "ignoreip" can be a list of IP addresses, CIDR masks or DNS hosts. Fail2ban
 90 # will not ban a host which matches an address in this list. Several addresses
 91 # can be defined using space (and/or comma) separator.
 92 #ignoreip = 127.0.0.1/8 ::1   <== 주석처리를 해제합니다.
 
 100 # "bantime" is the number of seconds that a host is banned. 10분동안 밴됩니다.
 101 bantime  = 10m
 102 
 103 # A host is banned if it has generated "maxretry" during the last "findtime"
 104 # seconds. 로그인 실패 수가 카운트 되는 시간을 뜻합니다.
 105 findtime  = 10m
 106 
 107 # "maxretry" is the number of failures before a host get banned. 비밀번호를 5회틀리면 차단됩니다.
 108 maxretry = 5
 
 274 [sshd]
 275 
 276 # To use more aggressive sshd modes set filter parameter "mode" in jail.local:
 277 # normal (default), ddos, extra or aggressive (combines all).
 278 # See "tests/files/logs/sshd" or "filter.d/sshd.conf" for usage example and deta    ils.
 279 #mode   = normal
 280 enabled = true <== 내용을 추가해야합니다. jail이 활성화 됩니다.
 281 port    = ssh
 282 logpath = %(sshd_log)s
 283 backend = %(sshd_backend)s
 
 # 저장 후 fail2ban을 재시작 합니다.
 systemctl restart fail2ban

 

 

  • 접속 및 차단 테스트를 위해 putty를 통해 접속해 봅니다.

 

  • 정상적으로 접근이 되는 것을 확인하였습니다.
  • 차단 테스트를 해보겠습니다.

 

  • 이러한 알림이 뜨면 설정이 잘못된 것 입니다.

 

  • 5회 이상 틀리게 되면 이러한 알림과 함께 서버에서 나가지게 됩니다(로컬호스트여서 입니다).

 

# fail2ban 작동 확인
fail2ban-client status sshd
  • SSH를 이용하거나 차단된 10분 이후 서버에 접속하여 Ban 됐던 상황을 확인합니다.

이상으로 Fail2Ban을 이용한 차단테스트를 마치겠습니다.

728x90
반응형
LIST