한국어

네트워킹

온누리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

참고자료

조회 수 :
24424
등록일 :
2017.09.12
20:28:02 (*.160.88.18)
엮인글 :
http://www.webs.co.kr/index.php?document_srl=3311868&act=trackback&key=727
게시글 주소 :
http://www.webs.co.kr/index.php?document_srl=3311868
List of Articles
번호 제목 글쓴이 날짜 조회 수sort
63 ftp 파일전송 여러파일을 한번에 통째로 전송 admin 2011-12-16 37734
62 스트리밍서버 / 윈도우서버 admin 2011-12-16 37637
61 Deploying FreeRADIUS with the MySQL Cluster Database file admin 2013-03-22 37152
60 리눅스 tar 압축 백업복원시 소유자 문제해결 admin 2011-12-16 37125
59 Start / Stop and Restart Apache 2 Web Server Command 아파치 시작 스톱 명령어 admin 2019-04-08 37110
58 How to start GUI from command line? admin 2014-12-21 36921
57 리눅스 시작시 부팅 시, 프로그램 자동실행 등록하기 admin 2017-08-30 36645
56 WinXP 부팅안되는 현상 NTLDR is missing 메세지 admin 2012-07-12 36413
55 Cloned VMware CentOS6 Server "device eth0 does not seem to be present, admin 2017-08-29 36157
54 Using Windows RDP to Access your Ubuntu Instance admin 2014-12-21 35002
53 문서 편집기 vi vim command 명령어 정리 admin 2014-10-29 34227
52 검색 파일리스트 만든 다음 여러파일 한방에 압축하기 admin 2012-02-14 33098
51 Linux Memory Management – Virtual Memory and Demand Paging admin 2014-03-03 32953
50 [리눅스] 부팅 시 자동 실행 프로그램 등록|작성자 나눔HN admin 2018-06-01 32839
49 리눅스 시스템 유저 추가 명령어 useradd 사용하기 admin 2014-08-09 32492
48 윈도우 ssh 접속 프로그램 admin 2017-09-29 31617
47 리눅스의 막강한 네트워크 필터 iptables admin 2012-04-15 31581
46 Remote Desktop Connection from Windows 7/8 to Ubuntu 14.04 admin 2014-12-21 31495
45 Name Server 설정및 이해 admin 2011-12-13 31438
44 [리눅스] 소프트웨어 레이드의 리빌딩 및 리싱킹 속도를 높이는 5가지 방법 admin 2014-10-17 31062
43 debian 8.8 download cd1 cd2 cd3 etc admin 2017-09-03 30838
42 VMWARE VM Error boot loader install grub - install /dev/sda or /dev/hda MBR linux admin 2016-01-27 30757
41 Install GUI on Ubuntu Server admin 2014-12-21 30531
40 무료백신 여러가지 분류해놓은곳 admin 2011-12-16 30505
39 Event IP 20187 admin 2013-04-08 30488
38 zip 압축 파일 및 텍스트 파일의 한글 깨짐 해결 방법 admin 2018-03-28 30327
37 Wireshark admin 2011-12-16 30203
36 다양한 사운드 파일있는곳 admin 2011-12-19 30072
35 Linux and Unix touch command admin 2015-11-15 28904
34 /bin/false, /sbin/nologin 의 차이점 admin 2014-10-13 28569