본문 바로가기
하드웨어/NAS, Linux, Ubuntu

fail2ban, NAS 로그인 여러번 실패 시 ip 차단하기

by 컴덕지니 2022. 10. 17.
728x90
반응형

얼마전 해커들이 무작위로 NAS에 로그인 시도를 한다는 이야기를 보았다.

그래서 나도 로그인 실패 기록을 확인해보았는데....

이렇게 실패한 기록이 20배 이상 더 있다.

보자마자 헛웃음이 나왔는데 내 NAS에서 꺼내갈게 뭐있다고 이렇게 로그인을 해댄건지..

다행히 로그인 성공한 기록을 살펴보니 해커가 성공한 흔적은 없다.

 

 

그래도 기분 나쁘니 앞으로는 로그인 실패시 차단을 하도록 만들기로했다.

찾아보니 리눅스에서는 fail2ban을 많이 사용하는듯 하다. 그래서 나도 설치했다.

 

그런데 이렇게 오랜만에 글을 쓰는 이유는 내가 설정해놓고도 하는 법을 까먹기 때문이다.

근데 이거도 원래는 문서로 링크를 저장해두는데 이번엔 헛스윙을 많이 해서 고생을 좀 했다. 그래서 구체적으로 글을 써놓기로한다.

 

 

fail2ban 설치

$ sudo apt-get install fail2ban

 

fail2ban 설정

$ sudo nano /etc/fail2ban/jail.conf

 

여기서 처음 주석처리되어있는 #[DEFAULT]말고 더 아래에 주석 없는 [DEFAULT] 밑을 확인한다

[DEFAULT]

.

# "ignoreip" can be a list of IP addresses, CIDR masks or DNS hosts. Fail2ban (여기 있는 ip는 밴을 안한다는 뜻)
# will not ban a host which matches an address in this list. Several addresses
# can be defined using space (and/or comma) separator.
ignoreip = 127.0.0.1/8 192.168.0.0/24

.

# "bantime" is the number of seconds that a host is banned. (차단 시간, 길수록 부팅 오래걸린다고함)
bantime  = 10m

# A host is banned if it has generated "maxretry" during the last "findtime" (아래 실패 횟수동안 걸리는 시간)
# seconds.
findtime  = 60

# "maxretry" is the number of failures before a host get banned. (몇 번 실패시?)
maxretry = 3

.

# Destination email address used solely for the interpolations in (차단시 여기로 메일 보냄)
# jail.{conf,local,d/*} configuration files.
destemail = 자신의 메일 주소

# Sender email address used solely for some actions
sender = 보낸이 메일 주소 (난 fail2ban@@nas.com라고함)

# E-mail action. Since 0.8.1 Fail2Ban uses sendmail MTA for the (메일 보내는 방식이라는데 잘 모르겠다 그냥 이렇게 두면 되는것같다)
# mailing. Change mta configuration parameter to mail if you want to
# revert to conventional 'mail'.
mta = sendmail

 

설정 적용하기 위해 재시작

$ sudo systemctl restart fail2ban
혹은
$ sudo service fail2ban restart

 

작동상태확인

$ sudo systemctl status fail2ban.service
  ● fail2ban.service - Fail2Ban Service
     Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-10-17 02:49:10 KST; 18min ago
     Docs: man:fail2ban(1)
     Process: 663901 ExecStartPre=/bin/mkdir -p /run/fail2ban (code=exited, status=0/SUCCESS)
     Main PID: 663902 (f2b/server)
     Tasks: 5 (limit: 9310)
     Memory: 11.0M
     CGroup: /system.slice/fail2ban.service
                  └─663902 /usr/bin/python3 /usr/bin/fail2ban-server -xf start

 

초록색 굵은 글씨로 되어있는 active (running)가 있어야 정상.

이게 failed (result: exit-code) 라 떠서 때문에 고생했는데 /etc/fail2ban/jail.conf 을 잘못 건들여서 그런거였음.

728x90
반응형

댓글