오드로이드
설치 분류

오드로이드U2 우분투 서버에 fail2ban 설치 후기

컨텐츠 정보

  • 18,226 조회
  • 2 댓글
  • 0 추천
  • 목록

본문

포고에서도 깔았던 fail2ban을 오드로이드에도 깔았습니다.

 

간단하게 설명드리면, SSH접속을 몇 번 실패하면 특정 시간만큼 ban 시키는 프로그램입니다.

 

우분투니깐 sudo 명령어를 넣어야 하지만 전 root 계정이기 때문에 그냥 바로 들어갑니다.

 

apt-get install fail2ban

위의 명령어를 넣으면 

 

 iptables libnfnetlink0 python-pyinotify whois

 

다른 패키지들도 따라옵니다.

 

그리고 

 

cd /etc/fail2ban

cp jail.conf jail.local

하면 jail.local 파일이 설정파일로 변신합니다.

 

nano jail.local

해서 살펴보면

 

[DEFAULT]

# "ignoreip" can be an IP address, a CIDR mask or a DNS host

ignoreip = 127.0.0.1

bantime  = 3600

maxretry = 3 

 위와 같이 찾을 수 있습니다.

 

ignoreip는 틀려도 제한 없는 ip를 지정하는 곳이구요.

bantime은 3600초 입니다. 원하시는 시간으로 넣으면 됩니다. 단위는 '초'입니다.

maxretry는 최대 시도 횟수입니다. 3번 틀리면 밴 당합니다.

 

destemail = your_email@domain.com

 위 부분은 어떤 상황이 발생 했을 때 이메일로 쏴주는 기능입니다.

 

이메일이 없다면 그냥 #으로 주석처리 해줍니다.

 

[ssh]

 

enabled = true

port    = ssh

filter  = sshd

logpath  = /var/log/auth.log

maxretry = 3 

 밑에 보시면 이런 구조들을 찾을 수 있습니다.

 

[ssh]가 제목이고 밑에는 옵션입니다.

 

enabled = true 를 해줘야 작동합니다.

 

제일 처음 설정했던 것보다 각 항목에 있는 maxretry가 우선순위가 있으니 꼭 조절해야 하겠지요.

 

전 [ssh-ddos], [vsftpd] 부분도 활성화 시켜줬습니다.

 

저장하시고 fail2ban을 재시작합니다.

 

service fail2ban restart

잘 들어갔나 보시려면 

 

iptables -nvL

를 치면

 

Chain INPUT (policy ACCEPT 11 packets, 752 bytes)

 pkts bytes target     prot opt in     out     source               destination

   11   752 fail2ban-vsftpd  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0

   12   792 fail2ban-ssh-ddos  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0          

   12   792 fail2ban-ssh  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0

 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination

 

Chain OUTPUT (policy ACCEPT 6 packets, 792 bytes)

 pkts bytes target     prot opt in     out     source               destination

 

Chain fail2ban-ssh (1 references)

 pkts bytes target     prot opt in     out     source               destination

   12   792 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0

 

Chain fail2ban-ssh-ddos (1 references)

 pkts bytes target     prot opt in     out     source               destination

   12   792 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0

 

Chain fail2ban-vsftpd (1 references)

 pkts bytes target     prot opt in     out     source               destination

   11   752 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0

여기서 중요한 것이 각 항목에 있는 1 references 입니다.

 

혹시 0 references가 뜬다면 활성화가 안된 것인데요. 제가 처음에 그랬습니다 -_-;;;

 

구글링을 하면서 별의 별 해결책을 다 찾아봤지만 안되더군요 -_-;;;

 

2013-07-29 23:55:16,224 fail2ban.actions.action: ERROR  iptables -N fail2ban-ssh

iptables -A fail2ban-ssh -j RETURN

iptables -I INPUT -p tcp -m multiport --dports ssh -j fail2ban-ssh returned 100

 이런 식으로 나올텐데요, 실제로 SSH비번을 6번 넘게 틀려도 밴 당하지 않는데요

 

로그를 보면 이렇게 나옵니다.

 

2013-07-29 23:57:18,347 fail2ban.actions: WARNING [ssh] Ban 밴당한IP

2013-07-29 23:57:18,355 fail2ban.actions.action: ERROR  iptables -n -L INPUT | grep -q 'fail2ban-ssh[ \t]' returned 100

2013-07-29 23:57:18,356 fail2ban.actions.action: ERROR  Invariant check failed. Trying to restore a sane environment

2013-07-29 23:57:18,393 fail2ban.actions.action: ERROR  iptables -N fail2ban-ssh

iptables -A fail2ban-ssh -j RETURN

iptables -I INPUT -p tcp -m multiport --dports ssh -j fail2ban-ssh returned 100

2013-07-29 23:57:18,400 fail2ban.actions.action: ERROR  iptables -n -L INPUT | grep -q 'fail2ban-ssh[ \t]' returned 100

2013-07-29 23:57:18,401 fail2ban.actions.action: CRITICAL Unable to restore environment

2013-07-29 23:57:41,424 fail2ban.actions: WARNING [ssh] 밴당한IP already banned

2013-07-29 23:58:04,448 fail2ban.actions: WARNING [ssh] 밴당한IP already banned

already banned라고는 하지만 실제로 밴되지 않습니다.

 

그래서 더 찾아보니 multiport를 allport로 바꾸면 된다는 말이 있었습니다.

 

앗 그게 어디있지? 찾아보니...

 

nano jail.local

여기에서

 

# 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

 찾으셨죠? ㅎ multiport 쪽에 문제가 있어서 allports로 바꿔주면 됩니다.

 

# 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-allports

그 다음 fail2ban을 재시작하면 1 references가 되어 있는 것을 볼 수 있습니다.

 

이것 때문에 하루종일 매달렸는데 겨우 성공했네요 ㅠㅠ

 

감격에 겨워 글을 올립니다.



관련자료

댓글 2
전체 21 / 3 페이지
RSS

최근글


새댓글


알림 0