실습 Ansible Playbook 실습 - 취약점 분석 점검 항목 - root 계정 원격 접속 제한 - 설계 1
페이지 정보
작성자 꿈꾸는여행자 작성일 25-02-13 18:09 조회 3,004 댓글 0본문
안녕하세요.
꿈꾸는여행자입니다.
Ansible을 통한 Playbook을 활용 분야에는 Unix 취약점 분석 점검 항목이 있습니다.
다음 kisa에서 제공된 주요정보통신기반시설 기술적 취약점 분석 평가 상세 가이드 를
기준으로 Playbook을 작성해 보고자합니다.
https://www.kisa.or.kr/2060204/form?postSeq=12&lang_type=KO&page=1
해당 항목들을 설계 및 구현 해보는 과정을 진행하고자 합니다.
이번 항목에서는 다음 내용을 기준으로
설계해 보겠습니다.
1. 계정 관리
- root 계정 원격 접속 제한
- U-01
감사합니다.
> 아래
## ** OS별 점검 및 조치 Playbook **
OS와 버전이 다른 경우에도 **효율적으로 Playbook을 운영**할 수 있도록 **점검용 Playbook**과 **조치용 Playbook**을 분리하여 작성합니다.
---
# ** 1. 디렉토리 구조 설정**
OS별 변수를 효율적으로 관리하기 위해 `group_vars/` 및 `host_vars/`를 활용합니다.
```
ansible/
├── inventory
├── site.yml
├── check_root_access.yml # 점검용 Playbook
├── fix_root_access.yml # 조치용 Playbook
├── group_vars/
│ ├── linux.yml # Linux 공통 변수
│ ├── unix.yml # Unix 공통 변수
├── host_vars/
│ ├── rocky8.yml # Rocky Linux 8.10 개별 변수
│ ├── rhel7.yml # RHEL 7 개별 변수
│ ├── aix7.yml # AIX 개별 변수
```
---
# ** 2. 인벤토리 파일 (`inventory`)**
OS별 호스트를 분류하여 Playbook이 적용될 그룹을 지정합니다.
```ini
[linux]
rocky8 ansible_host=192.168.1.10 ansible_user=root
rhel7 ansible_host=192.168.1.11 ansible_user=root
ubuntu20 ansible_host=192.168.1.12 ansible_user=root
[unix]
aix7 ansible_host=192.168.1.20 ansible_user=root
solaris11 ansible_host=192.168.1.21 ansible_user=root
hpux ansible_host=192.168.1.22 ansible_user=root
```
---
# ** 3. OS별 변수 파일**
## ** `group_vars/linux.yml`**
```yaml
ssh_config_path: "/etc/ssh/sshd_config"
pam_login_file: "/etc/pam.d/login"
securetty_file: "/etc/securetty"
```
## ** `group_vars/unix.yml`**
```yaml
ssh_config_path: "/etc/sshd_config"
```
---
# ** 4. 점검 Playbook (`check_root_access.yml`)**
이 Playbook은 현재 설정을 점검하여 root 계정의 원격 접속이 올바르게 차단되었는지 확인합니다.
```yaml
---
- name: Check Root Remote Access Restrictions Across Multiple OS Versions
hosts: all
become: yes
tasks:
- name: Detect OS Version
debug:
msg: "OS: {{ ansible_distribution }} {{ ansible_distribution_version }} (Family: {{ ansible_os_family }})"
# SSH 설정 점검
- name: Check if SSH root login is disabled
command: grep -E '^#?PermitRootLogin' {{ ssh_config_path }}
register: sshd_config
changed_when: false
- name: Display SSH root login status
debug:
msg: "{{ sshd_config.stdout_lines }}"
# Telnet 설정 점검 (Linux 계열)
- name: Check if /etc/securetty contains pts entries
command: grep '^pts/' {{ securetty_file }}
register: securetty_check
failed_when: securetty_check.rc == 0
changed_when: false
ignore_errors: yes
when: ansible_os_family in ['RedHat', 'Debian', 'Suse']
- name: Display /etc/securetty pts entries status
debug:
msg: "pts entries exist in /etc/securetty, root remote access via Telnet may be allowed"
when: securetty_check.rc == 0
# PAM 설정 점검 (Linux 계열)
- name: Check if pam_securetty.so is enforced in /etc/pam.d/login
command: grep 'auth required /lib/security/pam_securetty.so' {{ pam_login_file }}
register: pam_securetty_check
failed_when: pam_securetty_check.rc != 0
changed_when: false
ignore_errors: yes
when: ansible_os_family in ['RedHat', 'Debian', 'Suse']
- name: Display pam_securetty.so status
debug:
msg: "pam_securetty.so is missing in /etc/pam.d/login"
when: pam_securetty_check.rc != 0
```
---
# ** 5. 조치 Playbook (`fix_root_access.yml`)**
이 Playbook은 점검에서 **취약한 설정을 수정**하여 root 원격 접속을 차단합니다.
```yaml
---
- name: Fix Root Remote Access Restrictions Across Multiple OS Versions
hosts: all
become: yes
tasks:
# SSH root 로그인 차단
- name: Disable SSH root login
lineinfile:
path: "{{ ssh_config_path }}"
regexp: '^#?PermitRootLogin'
line: 'PermitRootLogin no'
state: present
notify: Restart SSH Service
# Telnet 설정 변경 (Linux 계열)
- name: Remove pts entries from /etc/securetty
lineinfile:
path: "{{ securetty_file }}"
regexp: '^pts/\d+'
state: absent
when: ansible_os_family in ['RedHat', 'Debian', 'Suse']
# AIX Telnet 설정 수정
- name: Disable rlogin in AIX
lineinfile:
path: /etc/security/user
regexp: '^rlogin'
line: 'rlogin = false'
state: present
when: ansible_os_family == "AIX"
# HP-UX Telnet 설정 수정
- name: Ensure console is only allowed in /etc/securetty (HP-UX)
lineinfile:
path: /etc/securetty
line: 'console'
state: present
when: ansible_os_family == "HP-UX"
# PAM 설정 적용 (Linux 계열)
- name: Ensure pam_securetty.so is enforced in /etc/pam.d/login
lineinfile:
path: "{{ pam_login_file }}"
line: 'auth required /lib/security/pam_securetty.so'
state: present
when: ansible_os_family in ['RedHat', 'Debian', 'Suse']
handlers:
- name: Restart SSH Service
service:
name: sshd
state: restarted
when: ansible_os_family in ['RedHat', 'Debian', 'Suse', 'Solaris', 'AIX', 'HP-UX']
```
---
# ** 6. 실행 방법**
## ** 점검 Playbook 실행 (Check Mode)**
```bash
ansible-playbook -i inventory check_root_access.yml
```
**출력 예시 (점검 결과)**
```
TASK [Check if SSH root login is disabled] **************************
ok: [rocky8] => msg: PermitRootLogin no
ok: [aix7] => msg: PermitRootLogin yes (취약)
```
---
## ** 조치 Playbook 실행 (Fix Mode)**
```bash
ansible-playbook -i inventory fix_root_access.yml
```
**출력 예시 (조치 완료)**
```
TASK [Disable SSH root login] ***************************************
changed: [aix7]
TASK [Remove pts entries from /etc/securetty] ***********************
changed: [rocky8]
TASK [Restart SSH Service] ******************************************
changed: [aix7]
```
---
# ** 7. 효율적인 관리 요약**
**OS별 `group_vars`를 활용하여 설정 관리**
**점검 Playbook과 조치 Playbook을 분리하여 유지보수 용이**
**`when` 조건을 활용하여 OS 및 버전에 맞는 설정 적용**
**SSH 및 Telnet 설정을 통합하여 중앙 관리 가능**
- 이전글 Ansible Playbook 실습 - 취약점 분석 점검 항목 - root 계정 원격 접속 제한 - 설계 2
- 다음글 Ansible Playbook 실습 - 시스템 및 서버 관리 - 서버 프로비저닝 자동화 VM - 구현 2
댓글목록 0
등록된 댓글이 없습니다.
