개발

U-10 파일 및 디렉토리 관리 > /etc/(x)inetd.conf 파일 소유자 및 권한 설정 자동화 파이썬코드

화이트해커 Luna 🌙 2023. 3. 16. 17:32
728x90
반응형

취약점


 

코드

 

#!/usr/bin/python3
# 파일명: U-10.py
import os
# 색깔변수
red   = '\033[91m'
blue  = '\033[94m'
reset = '\033[0m'
# /etc/xinetd.conf 파일과 /etc/xinetd.d 디렉토리의 권한과 소유자 확인
xinetd_file = '/etc/xinetd.conf'
xinetd_dir = '/etc/xinetd.d'
stat_info_file = os.stat(xinetd_file)
owner_file = stat_info_file.st_uid
mode_file = stat_info_file.st_mode

stat_info_dir = os.stat(xinetd_dir)
owner_dir = stat_info_dir.st_uid
mode_dir = stat_info_dir.st_mode

# 권한과 소유자가 취약하지 않은 경우
if owner_file == 0 and 0o100600 <= mode_file <= 0o100644 and owner_dir == 0 and 0o100600 <= mode_dir <= 0o100644:
    print(f'{blue}양호{reset} /etc/xinetd.conf 파일과 /etc/xinetd.d 디렉토리의 권한과 소유자가 올바릅니다.')

# 권한 또는 소유자가 취약한 경우
else:
    if owner_file != 0:
        print(f'{red}취약{reset} : /etc/xinetd.conf 파일의 소유자가 root가 아닙니다. {red}(현재 소유자: {owner_file}){reset}')
    if not (0o100600 <= mode_file <= 0o100644):
        print(f'{red}취약{reset} : /etc/xinetd.conf 파일의 권한이 rw------- 또는 rw-r--r--이 아닙니다. {red}(현재 권한: {oct(mode_file)}){reset}')
    if owner_dir != 0:
        print(f'{red}취약{reset} : /etc/xinetd.d 디렉토리의 소유자가 root가 아닙니다. {red}(현재 소유자: {owner_dir})')
    if not (0o100600 <= mode_dir <= 0o100644):
        print(f'{red}취약{reset} : /etc/xinetd.d 디렉토리의 권한이 rw------- 또는 rw-r--r--이 아닙니다. {red}(현재 권한: {oct(mode_dir)}){reset}')

    # 사용자로부터 y/n 입력을 받음
    answer = input(f'지금 소유자와 권한을 일괄 변경하시겠습니까? {red}(y/n){reset} ')

    if answer.lower() == 'y':
        # 파일 소유자와 권한 변경
        os.system('chown root /etc/xinetd.conf /etc/xinetd.d')
        os.system('chmod 600 /etc/xinetd.conf /etc/xinetd.d')
        print('/etc/xinetd.conf 파일과 /etc/xinetd.d 디렉토리의 소유자와 권한을 변경했습니다.')
    else:
        print('파일의 소유자와 권한을 변경하지 않았습니다.')
  • os 모듈 import 
  • 색깔을 지정하는 변수를 설정해서 예쁘게 출력한다.
  • xinetd_file 변수에 /etc/xinetd.conf 파일 경로를 할당
  • xinetd_dir 변수에 /etc/xinetd.d 디렉토리 경로를 할당
  • xinetd_file과 xinetd_dir의 권한과 소유자를 확인하고, owner_file, mode_file, owner_dir, mode_dir 변수에 할당
  • /etc/xinetd.conf 파일과 /etc/xinetd.d 디렉토리의 권한과 소유자가 취약하지 않은 경우, "양호" 메시지를 출력
  • 권한 또는 소유자가 취약한 경우, 취약 출력
  • 사용자로부터 y/n 입력을 받아 y인 경우, 파일 소유자와 권한을 일괄 변경
  • 파일 소유자와 권한을 변경한 경우, 변경 완료 메시지를 출력한다.

실제 테스트

728x90
반응형