본문 바로가기

Engineering/보안취약점

[U-01] 계정관리 - root 계정 원격접속 제한

 

[U-01] 계정관리 - root 계정 원격접속 제한

 

 

  • 내용: 시스템 정책에 root 원격 접속 설정 적용 여부 확인
  • 목적: 외부 비인가자로부터 root 계정 접근 시도를 원천 차단
  • 확인 방법
1) 아래 명령어 입력
$ cat /etc/ssh/sshd_config | grep PermitRootLogin
2) 결과
    (수정전) PermitRootLogin  yes
    (수정후) PermitRootLogin  no

 

  • 쉘 스크립트
#!/bin/bash

sshd_config=/etc/ssh/sshd_config
sshd_config_bak=/etc/ssh/sshd_config.bak
hostname=$(hostname)
OS=$(cat /etc/system-release)

## sshd_config 파일 값 읽기
RootPermit=$(cat /etc/ssh/sshd_config | grep -i ^PermitRootLogin | awk '{print $2}')

echo ""
echo "============================================"
echo "Scan for Linux System Vulnerability"
echo "============================================"
echo "<U-01 root remote access restriction>"
echo  "Priority: H"                                                                                                                                                                             
echo "============================================"
echo "hostname: $hostname"
echo "OS: $OS"
echo "============================================"

if [[ $RootPermit == yes ]]; then
  echo "!!!WARNGING!!!"
  echo "Remote access as root user is enabled. Make sure to disable it(y/n)"
  read -r yesorno
  
  if [[ $yesorno == y ]]; then
## sshd_config 백업 파일 생성
    cp "$sshd_config" "$sshd_config_bak"

    sed -i -e "s/^PermitRootLogin yes/PermitRootLogin no/g" "$sshd_config"

    if [[ $? != 0 ]]; then
      echo "Some error has been occurred while modifying $sshd_config"
      exit 1
    fi  

    echo "Root remote access has been disalbed"

  else
    echo "Quit the task"
    exit 0
  fi  

## 이미 no라면 작업 종료
else
  echo "Root remote access is already restricted"
  exit 0
fi