한국어

네트워킹

온누리070 플레이스토어 다운로드
    acrobits softphone
     온누리 070 카카오 프러스 친구추가온누리 070 카카오 프러스 친구추가친추
     카카오톡 채팅 상담 카카오톡 채팅 상담카톡
    
     라인상담
     라인으로 공유

     페북공유

   ◎위챗 : speedseoul


  
     PAYPAL
     
     PRICE
     

pixel.gif

    before pay call 0088 from app


https://blog.asamaru.net/2016/04/07/how-to-protect-ssh-with-fail2ban-on-centos-6-7/


SSH 무작위 로그인 시도 막기(With Fail2Ban on CentOS 6/7 + selinux)



linux 서버를 공개망에서 사용하면서 방화벽을 사용하지 않는 경우 ssh 로그인을 지속적으로 시도하는 로그를 볼 수 있다. 대부분 외국 IP 대역에서 지속적으로 ssh 로그인을 시도하는데 무작위 비밀번호를 대입하는 것이다. 당연히 방화벽을 설정하는 것이 당연하나 특수한 목적으로 인해 접속 IP 대역을 제한할 수 없는 경우도 있을 수 있다. 이런 경우에 보안을 조금이나마 강화하기 위해 사용할 수 있는 것이 fail2ban 이다.

우선 실제로 해당 서버에 어떤 IP들이 ssh 로그인을 시도했는지 아래의 명령으로 확인 할 수 있다(이하는 CentOS 7 기준이다).

아래는 로그인 시도 IP를 확인하는 방법이다.

$ ls /var/log/secure | xargs grep -E "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" -o | sort | uniq

공개망에 연결된 서버라면 생각보다 많은 IP가 나와 놀랄수도 있다. IP들 중 자신이 아는 IP를 제외하면 대부분이 위에 언급한 공격일 확률이 높다.

다른 방법으로 아래와 같이 최근 로그인 실패 기록을 확인하는 것도 가능하다.

$ last -f /var/log/btmp | more

20 Linux Log Files that are Located under /var/log Directory에 보면 /var/log/btmp에 대한 설명을 확인 할 수 있다.

상황을 확인 했으니 이제 본격적으로 fail2ban을 사용해 보자.


fail2ban 설치

fail2ban은 지정된 시간 내에 지정된 횟수 이상으로 ssh 로그인을 실패하면 해당 IP에서의 접근을 지정된 시간만큼 차단하는 역할을 한다.

아래의 설치 과정은 CentOS 7을 기준으로 한다. 이는 iptables 대신 firewalld가 사용되며 systemd를 사용하는 것을 기준으로 한다는 뜻이다. CentOS 6이라면 아래의 참고자료를 살펴보기 바란다.

# epel-release 추가
$ rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

# fail2ban 설치
$ yum install -y fail2ban

# fail2ban 기본 설정
$ hostname=`/bin/hostname`
$ echo "[DEFAULT]" > /etc/fail2ban/jail.local
# 1시간 동안 접근 제한
$ echo "bantime = 3600" >> /etc/fail2ban/jail.local
# 결과 수신 메일 지정
$ echo "destemail = ~~~@~~~.com" >> /etc/fail2ban/jail.local
# 결과 발신 메일 지정
$ echo "sender = root@$hostname" >> /etc/fail2ban/jail.local
$ echo "" >> /etc/fail2ban/jail.local
# ssh 접근 검사 활성화
$ echo "[sshd]" >> /etc/fail2ban/jail.local
$ echo "enabled = true" >> /etc/fail2ban/jail.local
# nginx-http-auth 접근 검사 활성화 : 필요시에만 사용
$ echo "" >> /etc/fail2ban/jail.local
$ echo "[nginx-http-auth]" >> /etc/fail2ban/jail.local
$ echo "enabled = true" >> /etc/fail2ban/jail.local

fail2ban 서비스 활성

$ systemctl enable fail2ban.service

fail2ban 서비스 시작

$ systemctl start fail2ban.service

selinux 사용시 fail2ban 관련 설정

이하는 selinux 사용시에만 적용하면 된다.

$ checkmodule -M -m -o fail2ban-syslog.mod fail2ban-syslog.te
$ semodule_package -o fail2ban-syslog.pp -m fail2ban-syslog.mod
$ semodule -r fail2ban-syslog
$ semodule -i fail2ban-syslog.pp
$ rm -f fail2ban-syslog.mod
$ rm -f fail2ban-syslog.pp
$ semodule -l|grep fail2ban-syslog

$ checkmodule -M -m -o logrotate-fail2ban.mod logrotate-fail2ban.te
$ semodule_package -o logrotate-fail2ban.pp -m logrotate-fail2ban.mod
$ semodule -r logrotate-fail2ban
$ semodule -i logrotate-fail2ban.pp
$ rm -f logrotate-fail2ban.mod
$ rm -f logrotate-fail2ban.pp
$ semodule -l|grep logrotate-fail2ban

아래의 2개의 파일은 위 스크립트 실행에 필요한 파일이므로 해당 파일명으로 저장해서 사용하면 된다.

fail2ban-syslog.te

module fail2ban-syslog 1.0;

require {
type syslogd_var_run_t;
type fail2ban_t;
class dir read;
class file read;
class file open;
class file getattr;
}

#============= fail2ban_t ==============
allow fail2ban_t syslogd_var_run_t:dir read;
allow fail2ban_t syslogd_var_run_t:file read;
allow fail2ban_t syslogd_var_run_t:file open;
allow fail2ban_t syslogd_var_run_t:file getattr;

logrotate-fail2ban.te

module logrotate-fail2ban 1.7;

require {
type fail2ban_client_exec_t;
type logrotate_t;
type init_var_lib_t;
class file { open read execute getattr write create execute_no_trans setattr unlink ioctl rename};
}

#============= logrotate_t ==============
allow logrotate_t fail2ban_client_exec_t:file execute_no_trans;
allow logrotate_t fail2ban_client_exec_t:file { open read execute ioctl };
allow logrotate_t init_var_lib_t:file { open read getattr write create unlink setattr rename };

설치가 다 되었다면 아래의 명령 실행시 다음과 비슷한 결과를 볼 수 있다.

$ firewall-cmd --direct --get-all-rules

ipv4 filter INPUT 0 -p tcp -m multiport --dports ssh -m set --match-set fail2ban-sshd src -j REJECT --reject-with icmp-port-unreachable

그리고 아래의 명령으로 적용 상황을 확인할 수 있다.

# fail2ban 상태 확인
$ fail2ban-client status sshd

Status for the jail: sshd
|- Filter
|  |- Currently failed: 1
|  |- Total failed: 153
|  `- File list:    /var/log/secure
`- Actions
   |- Currently banned: 0
   |- Total banned: 2
   `- Banned IP list:

# ipset에 설정(차단)된 내용 확인
$ ipset --list

Name: fail2ban-sshd
Type: hash:ip
Revision: 1
Header: family inet hashsize 1024 maxelem 65536 timeout 3600
Size in memory: 16656
References: 1
Members:

# fail2ban 로그 확인
$ cat /var/log/fail2ban.log

# 차단 IP 목록
$ awk '($(NF-1) = /Ban/){print $NF}' /var/log/fail2ban.log | sort | uniq -c | sort -n

참고자료

조회 수 :
24211
등록일 :
2017.09.12
20:28:02 (*.160.88.18)
엮인글 :
http://www.webs.co.kr/index.php?document_srl=3311868&act=trackback&key=1db
게시글 주소 :
http://www.webs.co.kr/index.php?document_srl=3311868
List of Articles
번호 제목 글쓴이 날짜 조회 수sort
93 iptables rules tcp drop all port except tcp 22 admin 2014-02-26 81448
92 Centos net install web http admin 2012-03-02 81402
91 Linux 시스템 백업과 복원 admin 2012-02-28 76959
90 한국인/국내기업은 얼마나 Linux Kernel에 기여할까? admin 2012-04-29 76594
89 Linux 시스템 백업과 복원 admin 2013-04-04 74013
88 리눅스 부팅시 시작시 프로그램 명령어 실행하기 (Linux Init script) admin 2017-08-30 72784
87 OpenVPN - Getting started How-To admin 2017-09-20 67048
86 How to install a Debian 9 (Stretch) Minimal Server 데비안 9 설치 admin 2018-06-13 63392
85 useradd Command 리눅스 admin 2017-12-15 63314
84 Which is better, GCC or Clang? admin 2018-06-13 62805
83 How To Install Java with Apt-Get on Ubuntu 16.04 oracle java admin 2017-10-13 47129
82 윈도우 2003 작업 스케줄러 설정 admin 2011-12-16 47112
81 FREE RADIUS 활용및 응용 admin 2013-02-23 46915
80 apt-get install linux-image-2.6.26-2-686-bigmem admin 2015-06-27 46860
79 How to disable IPv6 in Debian Lenny and Squeeze admin 2011-12-29 45025
78 How to disable IPv6 in Debian , 리눅스 아이피설정 admin 2011-12-16 44800
77 [Linux/SSL] 리눅스 서버에 Apache 2 설치 및 SSL 설정하기(mod_ssl, openssl) admin 2012-04-15 42189
76 How to install Java on linux with no Internet connectivity (using local repository) admin 2017-10-01 41986
75 리눅스 네트워크 설정 LINUX admin 2011-12-19 41443
74 kali linux 해킹 hacking attack DDOS etc tools admin 2015-06-24 41429
73 scp, ssh, rsync등을 사용할때에 SSH 비밀번호 묻는것 피하기 admin 2012-04-15 40930
72 모든 CPU 벤치마크 수치 admin 2011-12-16 40668
71 linux 파티션 디렉토리 용량 확인방법 전체 폴더 크기 사이즈 admin 2012-01-05 40454
70 Debian CD DVD 다운로드 링크 헤메지말고 바로 다운로드하지요 admin 2012-08-18 38810
69 리눅스에서 자바(JDK) 설치하기 admin 2013-04-08 38297
68 SSH Without Authentication Using Key Files (CentOS 5.6) admin 2013-11-22 38278
67 리눅스 네트워크 설정 멸령어 Linux admin 2013-04-08 37996
66 seagate HD 시게이트 하드 디스크 A/S 에이에스 기간 조회 및 받는 3가지 방법 admin 2013-12-09 37932
65 scp 명령어를 이용한 파일 복사 및 전송 admin 2014-10-29 37837
64 암호 없이 SSH 접속하기 admin 2013-09-05 37782