서버해킹 시도대비 자동 차단하기 fail2ban
리눅스 시스템에서 ssh,http,ftp 서비스등 오픈된 공간에서 사용하는 서비스들은 언제나 해킹에 노출되어 있다.
fail2ban은 짧은시간에 무차별 접속시도 하는 ip를 차단 및 일정시간 접속하지 못하도록 하는 툴이다.
ssh보안강화
fail2ban을 적용하기 앞서 해킹시도가 빈번한 ssh서비스의 보안강화를 적용할 필요가 있다.
root접속을 막으면 일반계정으로 접속해야 하니 일반계정을 생성해두고 진행할 것.
sshd 포트변경 및 root접속 차단
아래에서는 ssh포트를 5881로 변경했다.
#vi /etc/ssh/sshd_config
Port 5881
PermitRootLogin no
SELinux 보안정책에서 ssh포트로 허용
#semanage port -a -p tcp -t ssh_port_t 5881
semanage를 찾을 수 없을 경우 설치해주고 적용 후 포트 허용할것.
#yum -y install policycoreutils-python
포트정의 확인
#semanage port -l | grep ssh
ssh_port_t tcp 5881, 22
이후 방화벽에 해당포트를 열어준다.
firewall-cmd
firewalld 데몬에서 사용되는 방화벽제어 명령
RHEL7/CENTOS7에서 방화벽데몬이 firewalld로 변경됨
유저프로그램은 좀더 사용하기 쉬운 firewall-cmd를 통해서 기존iptalbes을 제어한다.
#firewall-cmd --permanent --zone=public --add-port=5881/tcp
기본 ssh포트인 22번 포트를 삭제
#firewall-cmd --permanent --zone=public --remove-port=22/tcp
방화벽 설정 리로드
#firewall-cmd --reload
방화벽설정 확인
#firewall-cmd --list-all
방화벽 서비스 리스트확인
#firewall-cmd --list-services
sshd 재시작
#systemctl restart sshd
실제로 ssh서비스가 변경 포트로 동작하는지 확인
#ss -tnlp | grep ssh
LISTEN 0 128 *:5881 *:* users:(("sshd",pid=20756,fd=3))
LISTEN 0 128 :::5881 :::* users:(("sshd",pid=20756,fd=4))
fail2ban 설치
RHEL7/Centos7부터 /var/log/secure 로그를 더이상 사용하지 않고
systemd에서 관리하기 때문에 관련 로그를 읽을 수 있게 fail2ban-systemd도 설치해줘야 한다.
ip로 whois정보 확인용 jwhois도 같이 설치하자.
#yum install fail2ban fail2ban-systemd jwhois
fail2ban 서비스 시작
#systemctl start fail2ban
fail2ban가 재부팅 후에도 자동 시작하도록 서비스 등록
#systemctl enable fail2ban
fail2ban-client로 서비스별로 상태 확인
ssh에 banned된 ip가 보인다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | root@rhel74vm:/etc/fail2ban/jail.d # fail2ban-client status sshd Status for the jail: sshd |- Filter | |- Currently failed: 1 | |- Total failed: 2 | `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd `- Actions |- Currently banned: 4 |- Total banned: 4 `- Banned IP list: 112.85.42.198 218.92.1.131 27.3.150.15 94.177.244.205 root@rhel74vm:/etc/fail2ban/jail.d # fail2ban-client status vsftpd Status for the jail: vsftpd |- Filter | |- Currently failed: 0 | |- Total failed: 0 | `- Journal matches: `- Actions |- Currently banned: 0 |- Total banned: 0 `- Banned IP list: root@rhel74vm:/etc/fail2ban/jail.d # fail2ban-client status mysqld-auth Status for the jail: mysqld-auth |- Filter | |- Currently failed: 0 | |- Total failed: 0 | `- File list: /var/log/mysqld.log `- Actions |- Currently banned: 0 |- Total banned: 0 `- Banned IP list: | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #whois root@rhel74vm:/etc/fail2ban/jail.d # whois 112.85.42.198 [Querying whois.apnic.net] [whois.apnic.net] % [whois.apnic.net] % Whois data copyright terms http://www.apnic.net/db/dbcopyright.html % Information related to '112.80.0.0 - 112.87.255.255' % Abuse contact for '112.80.0.0 - 112.87.255.255' is 'hqs-ipabuse@chinaunicom.cn' inetnum: 112.80.0.0 - 112.87.255.255 netname: UNICOM-JS descr: China Unicom Jiangsu province network descr: China Unicom country: CN | cs |
*차단리스트 로그보기
#awk '($(NF-1) = /Ban/){print $NF}' /var/log/fail2ban.log | sort | uniq -c | sort -n
실시간으로 시스템 접근시도 확인
#journalctl -f