리눅스 서버 sshd 무작위 로그인 시도 차단하기 – fail2ban
fail2ban 설치 및 간단 설정
리눅스 서버의 특정 서비스에 과도한 패스워드 공격이 가해지면 자동으로 해당 IP를
iptables 혹은 filrewalld 에서 차단하는 기능이다.
1. 설치
# yum install -y fail2ban fail2ban-systemd whois
iptables 로 차단 관리를 할 경우 firewalld 설정 제거
# rm -f /etc/fail2ban/jail.d/00-firewalld.conf
기본 서비스 시작
# systemctl enable fail2ban
# systemctl start fail2ban
2. 설정
# cd /etc/fail2ban/
jail.conf 파일에 바로 설정도 가능하지만, 패키지 업데이트등이 이루어지면 설정이 초기화되는 상황이
발생한다. 기본 설정은 그대로 두고, 개별 설정을 부가적으로 적용하는 방법으로 설정한다.
# vi jail.local
[DEFAULT]
ignoreip = 127.0.0.1/8 192.168.201.0/24 192.168.123.0/24 ## 허가 IP 대역
bantime = 10800 ## findtime 시간동안 maxretry 시도가 탐지된 경우 해당 IP 차단 시간
findtime = 600
maxretry = 5
backend = pooling
destemail = alang@clunx.com
sender = admin@clunix.com
mta = sendmail
action = %(action_mwl)s
[sshd]
enabled = true
port = ssh,22
[dovecot]
enabled = true
[postfix]
enabled = true
[postfix-sasl]
enabled = true
dovecot 이나 postfix 처럼 기본 제공되는 filter 에서 해당되는 서비스를 활성화 시켜준다.
sshd 는 필수적으로 적용해 주고..
filter 로 제공되는 기본 서비스는 아래와 같다.
# ls /etc/fail2ban/filter.d/
3proxy.conf counter-strike.conf haproxy-http-auth.conf pam-generic.conf sogo-auth.conf
apache-auth.conf courier-auth.conf horde.conf perdition.conf solid-pop3d.conf
apache-badbots.conf courier-smtp.conf ignorecommands php-url-fopen.conf squid.conf
apache-botsearch.conf cyrus-imap.conf kerio.conf phpmyadmin-syslog.conf squirrelmail.conf
apache-common.conf directadmin.conf lighttpd-auth.conf portsentry.conf sshd.conf
apache-fakegooglebot.conf domino-smtp.conf mongodb-auth.conf postfix.conf stunnel.conf
apache-modsecurity.conf dovecot.conf monit.conf proftpd.conf suhosin.conf
apache-nohome.conf dropbear.conf murmur.conf pure-ftpd.conf tine20.conf
apache-noscript.conf drupal-auth.conf mysqld-auth.conf qmail.conf traefik-auth.conf
apache-overflows.conf ejabberd-auth.conf nagios.conf recidive.conf uwimap-auth.conf
apache-pass.conf exim-common.conf named-refused.conf roundcube-auth.conf vsftpd.conf
apache-shellshock.conf exim-spam.conf nginx-botsearch.conf screensharingd.conf webmin-auth.conf
assp.conf exim.conf nginx-http-auth.conf selinux-common.conf wuftpd.conf
asterisk.conf freeswitch.conf nginx-limit-req.conf selinux-ssh.conf xinetd-fail.conf
bitwarden.conf froxlor-auth.conf nsd.conf sendmail-auth.conf znc-adminlog.conf
botsearch-common.conf groupoffice.conf openhab.conf sendmail-reject.conf zoneminder.conf
centreon.conf gssftpd.conf openwebmail.conf sieve.conf
common.conf guacamole.conf oracleims.conf slapd.conf
설정이 완료되면 fail2ban 서비스를 재시작한다.
# systemctl restart fail2ban
동작 상태를 확인한다.
# fail2ban-client status
Status
|- Number of jail: 4
`- Jail list: dovecot, postfix, postfix-sasl, sshd
현재 등록된 서비스가 4개 있다는 것을 알수 있다. 이중 sshd 상태를 확인한다.
# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 15
| |- Total failed: 125
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 8
|- Total banned: 8
`- Banned IP list: 111.61.241.100 165.22.69.147 37.187.12.126 139.59.40.240 139.59.68.15 27.191.237.67 217.182.79.176 119.29.16.190
여러 IP 들이 sshd 로 무작위 접근 했고, 차단된 것을 확인할 수 있다.
실제 iptables 에 차단된 rule 이 등록되었는지 확인한다.
# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
f2b-sshd tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 22,22
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain f2b-sshd (1 references)
target prot opt source destination
REJECT all -- 182.151.41.208 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 45.131.108.196 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 140.143.136.41 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 1.194.238.226 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 54.38.139.210 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 104.248.205.24 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 46.101.245.176 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 217.182.79.176 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 139.59.40.240 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 37.187.12.126 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 27.191.237.67 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 165.22.69.147 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 139.59.68.15 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 119.29.16.190 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 111.61.241.100 0.0.0.0/0 reject-with icmp-port-unreachable
RETURN all -- 0.0.0.0/0 0.0.0.0/0
혹 fail2ban 에 정상적인 IP 가 차단 된 경우 아래와 같은 방법으로 차단을 해제할 수 있다.
# fail2ban-client set sshd unbanip x.x.x.x
정말 감사합니다 ^^
최근 서버로그들이 너무 금방 차서 봤더니 무작위 공격들이 여기저기서 들어오고 있더군요
fail2ban 을 이용하니 수월하게 해결됬습니다.
특히 postfix sasl 로그에서 찍히는 아이피는 iptables 에서 차단해도 계속 쌓였는데
그것도 없어졌네요
좋은 정보 감사드립니다.
Volvo Brakes
I like the efforts you have put in this, regards for all the great content.
Good post! We will be linking to this particularly great post on our site. Keep up the great writing
I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.
I do not even understand how I ended up here, but I assumed this publish used to be great
Hmm nice post bro.
Best nice post bro.
V3ry nice post.
Amazing post bro, thank you.
Besting.. Great post.
Ty for post
Thank u, best p0st..
Thank u, best p0st..
Thank you sir..
This article opened my eyes, I can feel your mood, your thoughts, it seems very wonderful. I hope to see more articles like this. thanks for sharing.
Awesome! Its genuinely remarkable post, I have got much clear idea regarding from this post
I like the efforts you have put in this, regards for all the great content.
Awesome! Its genuinely remarkable post, I have got much clear idea regarding from this post
Pretty! This has been a really wonderful post. Many thanks for providing these details.