CentOS 7 SSH 보안적용

CentOS 7 SSH 보안적용

공인 ip 서버의 ssh에 대한 무작위 대입 공격이 포착되었습니다.

# journalctl -f
Failed password for invalid user sammy from 188.166.122.206 port 52448 ssh2
error: maximum authentication attempts exceeded for root from 61.177.172.160 port 24090 ssh2 [preauth]
Disconnecting authenticating user xxxx 61.177.172.160 port 24090: Too many authentication failures [preauth]

1. 외부(집 등)에서도 ssh 접속을 해야될 경우

sshd_config의 최대 시도 횟수 및 failban 적용.

MaxAuthTries에 설정을 해도 다시 재접속 할 경우 접속이 가능하다고 합니다.

Fail2ban은 반복해서 틀릴 경우 아이피를 차단합니다.

# vim /etc/ssh/sshd_config
MaxAuthTries 3

# yum install epel-release
# yum install fail2ban
# yum install fail2ban-systemd

# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

# vim /etc/fail2ban/jail.local
ignoreip = 127.0.0.1/8 xxx.xxx.xxx.0/24

bantime  = 60m

findtime  = 60m

maxretry = 3

[sshd]
enabled = true
port    = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s

# systemctl enable fail2ban
# systemctl restart fail2ban
# systemctl status fail2ban
# sudo fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 9
|  |- Total failed:     226
|  `- Journal matches:  _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned: 14
   |- Total banned:     14
   `- Banned IP list:   188.166.122.206 61.177.172.160 112.85.42.73 112.85.42.128 92.255.85.237 112.85.42.119 218.92.0.221 222.186.30.112 122.194.229.59 122.194.229.40 218.92.0.211 165.232.112.163 212.192.241.124 112.85.42.28

기타

hostname

# yum -y install vim-enhanced
# vim /etc/hosts
ip hostname
# hostnamectl set-hostname utility-server

# shutdown -r now

# hostname

timezone

# date
2022. 02. 14. () 04:57:13 EST
# timedatectl
 Local time: 월 2022-02-14 04:57:41 EST
           Universal time: 월 2022-02-14 09:57:41 UTC
                 RTC time: 월 2022-02-14 09:57:41
                Time zone: America/New_York (EST, -0500)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
# timedatectl list-timezones | grep -i seoul
Asia/Seoul
# timedatectl set-timezone Asia/Seoul
# timedatectl

2. 회사 내에서만 접속해야될 경우

firewall-cmd --permanent --zone=public --remove-service=ssh

firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="xxx.xxx.xxx.0/24" port port="22" protocol="tcp" accept'

firewall-cmd --reload
firewall-cmd --list-all

*참고: RHEL 7버전 이하에서는 hosts.allow, hosts.deny 파일에 접근 허용할 호스트 IP를 등록하면 되었으나 8버전부터는 모듈이 지원되지 않습니다.

기타로 계정 별 ip 제한하기

# vim /etc/ssh/sshd_config 
  #아래 추가
  AllowUsers 사용자계정@xxx.xxx.xxx.xxx
# systemctl restart sshd

댓글남기기