개발

U-03 계정관리 > 계정 잠금 임계값 설정 자동화 파이썬 코드

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

취약점

 

 


 

코드

 

#!/usr/bin/python3
#-*- coding: utf-8 -*-
# 파일명: U-03.py
# 사용자 계정 로그인 실패 시 계정잠금 임계값이 설정되어 있는지 점검

# 색깔변수
red = '\033[91m'
blue = '\033[94m'
reset = '\033[0m'

with open('/etc/pam.d/system-auth') as f:
    content = f.read()
    if 'auth required /lib/security/pam_tally.so deny=5' in content and 'unlock_time=120' in content:
        print(f'{blue}양호{reset}')
    else:
        print(f'{red}취약{reset} - 계정 잠금 임계값이 설정되어 있지 않거나 10회 이하의 값으로 설정되어 있습니다')
        # 파일 수정 여부 확인
        while True:
            ans = input("파일을 수정하시겠습니까?(y/n) ")
            if ans == 'y':
                with open('/etc/pam.d/system-auth', 'a') as f:
                    f.write('\nauth required /lib/security/pam_tally.so deny=5 unlock_time=120\nno_magic_root\naccount required /lib/security/pam_tally.so no_magic_root reset\n')
                print(f'{blue}/etc/pam.d/system-auth 파일이 수정되었습니다{reset}')
                break
            elif ans == 'n':
                print("파일을 수정하지 않았습니다.")
                break
            else:
                print("잘못된 입력입니다. 다시 입력해주세요.")
  • /etc/pam.d/system-auth 파일을 읽어들인 후, 파일 내용에 "auth required /lib/security/pam_tally.so deny=5"와 "unlock_time=120"이 포함되어 있는지 확인
  • 두가지 설정이 모두 포함되어 있으면 "양호" 메시지를 출력하고, 그렇지 않으면 "취약" 메시지를 출력
  • 파일 수정 여부를 확인하기 위해 while 루프를 사용
  • y일 경우 /etc/pam.d/system-auth 파일의 내용을 수정하고 수정 후에는 "파일이 수정되었습니다" 메시지를 출력한다. n일 경우 파일을 수정하지 않는다
  • 색깔변수를 이용해 예쁘게 출력.

 


실제 테스트

양호

 

 

취약(수정)


환경은 CentOS7 입니다

728x90
반응형