반응형

PAM 설정을 통해 차단(아이디 잠금)


PAM이란

리눅스에서 플러그인 방식으로 적용할 수 있는 인증 모듈

인증 모듈을 별도로 개발하지 않고 공유 라이브러리 형태로 적용시킬 수 있다.

관리자가 인증을 중앙에서 통제할 수 있게 해 준다.


동작순서

사용자나 프로세스가 애플리케이션의 접근(Access)을 요청한다.

해당 애플리케이션의 PAM 설정 파일을 확인한다.

스택의 PAM모듈이 리스트상의 순서대로 호출된다.

PAM모듈은 성공 또는 실패 상태를 반환(Return)한다.

스택은 계속해서 순서대로 확인되며, 실패 상태를 반환한다해서 중단되지 않는다.

모든 PAM모듈의 상태 결과가 종합되어 전체 인증 과정의 성공 또는 실패 상태를 반환한다.



PAM 설정 파일 변경


CentOS


sudo nano /etc/pam.d/system-auth -c

2번째 줄 수정,8번째 추가

1
2
3
4
5
6
7
8
9
10
11
auth        required      pam_env.so
auth        required      pam_tally2.so deny=5 unlock_time=600
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 1000 quiet_success
auth        required      pam_deny.so
 
account     required      pam_unix.so
account     required      pam_tally2.so
account     sufficient    pam_localuser.so
account     sufficient    pam_succeed_if.so uid < 1000 quiet
account     required      pam_permit.so
cs



Ubuntu,Debian


sudo nano /etc/pam.d/common-auth -c

13번째줄 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# This file is included from other service-specific PAM config files,
# and should contain a list of the authentication modules that define
# the central authentication scheme for use on the system
# (e.g., /etc/shadow, LDAP, Kerberos, etc.).  The default is to use the
# traditional Unix authentication mechanisms.
#
# As of pam 1.0.1-6, this file is managed by pam-auth-update by default.
# To take advantage of this, it is recommended that you configure any
# local modules either before or after the default block, and use
# pam-auth-update to manage selection of other modules.  See
# pam-auth-update(8) for details.
 
auth    required                        pam_tally2.so onerr=fail even_deny_root deny=5 unlock_time=600
# here are the per-package modules (the "Primary" block)
auth    [success=1 default=ignore]      pam_unix.so nullok_secure
# here's the fallback if no module succeeds
auth    requisite                       pam_deny.so
# prime the stack with a positive return value if there isn't one already;
# this avoids us returning an error just because nothing sets a success code
# since the modules above will each just jump around
auth    required                        pam_permit.so
# and here are more per-package modules (the "Additional" block)
# end of pam-auth-update config

cs


옵션

pam_tally2.so:접근 시도 횟수를 관리하는 모듈

onerr:암호 실패시

evne_deny_root:root계정도 잠금

deny=5 : 5회 입력 실패 시 패스워드 잠금

unlock_time=600: 계정 잠김 후 마지막 계정 실패 시간부터 설정된 시간(600초)가 지나면 자동 계정잠김 해제


root_unlock_time=120: root계정을 120초 잠금

no_magic_root : root에게는 패스워드 잠금 설정을 적용하지 않음

magic_root:root 계정 틀린 횟수를 카운트 하되 잠금하지 않음

reset : 접속 시도 성공시 실패한 횟수 초기화


잠긴 계정 잠금해제하기

# pam_tally2 -u [User ID] -r



.d파일은 디렉터리 파일이다.


참고

https://www.enteroa.com/2017/06/05/centos-ubuntu-%EA%B3%84%EC%A0%95-%EC%9E%A0%EA%B8%88-%EC%9E%84%EA%B3%84%EA%B0%92-%EC%84%A4%EC%A0%95/




fail2ban(특정 IP를 차단)


fail2ban의 원리

fail2ban는 접속로그 기록을 분석해서 실패 기록을 근거로 차단하는 원리이다.

각 프로그램의 로그인 로그들을 연결해 주어야 이용가능하다.



설치

sudo apt-get update

sudo apt-get install fail2ban


시작

sudo /etc/init.d/fail2ban start


상태확인

sudo systemctl status fail2ban

sudo fail2ban-client status

sudo iptables -L




차단 현황을 확인

#fail2ban-client status

Status

- Number of jail: 1

- Jail list: sshd

*위 내용에서 현재 블럭되어있는 IP갯수가 1개임을 확인 할 수 있다.(IP정보는 따로 확인하여야한다)


차단된 IP정보 확인

#vi /var/log/fail2ban.log

위 로그에서 "[sshd] Ban xxx.xxx.xxx.xxx" 형태로 남아있는 로그를 찾아서 IP정보를 확인한다.


혹은 아래의 명령으로 한번에 확인할 수도 있다.


#cat /var/log/fail2ban.log* | grep "] Ban" | awk '{print $NF}' | sort | uniq -c | sort -n


fail2ban 차단ip 해제하기

# [root@victor jail.d]# fail2ban-client set sshd unbanip 221.xxx.xxx.xxx


fail2ban을 재시작하지않아도 바로 접속이 가능해집니다.




설정


업데이트시 기본 설정 jail.conf는 바뀔수 있다고합니다.

기본설정을 복사하여 jail.local 파일을 만들어서 관리해야합니다.

sudo cp -arp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

sudo nano /etc/fail2ban/jail.local


ctrl+w를 이용해 찾아가면서 설정을 해주면 편합니다.


기본설정

ignoreip     관리자 ip를 입력 차단되지 않음(주석처리되어있는데 주석을 제거후 수정해주자)

findtime      해당시간동안

maxretry     횟수만큼 실패하면

bantime      시간만큼 차단(초단위) 영구차단의 경우 -1



ssh설정


[sshd]

enabled=ture

port=ssh,<port1>,<port2>, ...


vsftpd 설정


[vsftpd]

enabled=true



vsftpd에 설정을 적용하기 위해선 로그파일에 로그인 기록이 남을수 있도록  /etc/vsftpd.conf를 수정해주어야합니다.


/etc/vsftpd.conf 설정

dual_log_enable=YES          #/var/log/xferlog파일과 /var/log/vsftpd.log 파일 모두 사용

xferlog_enable=YES            #xferlog를 사용할것인지

xferlog_file=/var/log/vsftpd.log        #파일 전송 로그를 기록할 파일명을 정의한다.

log_ftp_protocol=YES    #클라이언트와 서버 사이의 모든 FTP프로토콜 트랜잭션 로그를 기록함(없다면 추가)

xferlog_std_format=NO            # xferlog 형식으로 ftp log를 남기기 위한 설정이다. xferlog 표준 포맷은, 디렉토리 생성 등의 로그를 남기지 않으며, vsftpd 스타일의 로그는 디렉토리 생성을 포함한, 보다 상세한 로그를 남기기 때문에, NO로 설정할 것을 권장한다.


나머지 설정은 아래 사이트 참조

http://faq.hostway.co.kr/Linux_ETC/1405


vsftpd 설정후 재시작

sudo systemctl restart vsftpd


설정후 재시작

sudo /etc/init.d/fail2ban restart



반응형

'Linux' 카테고리의 다른 글

nano editor 단축키  (1) 2021.02.27
특정 IP 차단(iptables)  (0) 2020.06.25
ssh su권한 보안처리  (0) 2020.06.21
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기