도커 컨테이너의 보안 취약점을 체크

busybean3·2021년 9월 7일
1

도커

목록 보기
3/4

https://github.com/YouJaeBeom/Security_Vulnerability_Check_inDocker

소개 🚀

해당 프로젝트는 도커 컨테이너내에서 기본적인 보안 취약점을 해결할 수 있도록 해주는 Dockerfile입니다.

해당 Dockerfile로 이미지를 생성하고 컨테이너를 생성하게 되면 기본적인 보안 취약점을 해결 할 수 있습니다.

총 컨테이너 내부에는 Dockerfile, README.md, base.py, linux_vuln_check_script.sh, requirements.txt 가 들어가게 됩니다.

파일별 용도 🚀

각 파일별 용도는 아래와 같습니다.

  • Dockerfile : docker image를 만들기 위한 file로 보안취약점을 해결할 수 있도록 RUN 명령어를 통해서 스크립트화 했습니다.

  • base.py : 도커 컨테이너내에서 python file 을 돌리는 목적이였기 때문에 사용할 python file 입니다. 사용자에게 맞게 수정 혹은 다른 파일을 추가해도 문제 없습니다.

  • linux_vuln_check_script.sh : 기본적인 보안 취약점을 확인 할 수 있는 쉘 스크립트로 (https://github.com/newbieh4cker/centos_vuln_check_script) 가 원본입니다.

  • requirements.txt : 도커 컨테이너내에서 사용할 python 라이브러리를 적는 파일입니다.

사용법 🚀

해당 디렉토리내에서 ( ./Security_Vulnerability_Check_inDocker )

도커이미지 생성 명령어

도커이미지 생성 명령어 : 
docker build -t "docker_images_name:image_tag" .

를 사용하게 되면 docker_images_name:image_tag 로 도커 이미지가 생성되고, 이를 컨테이너로 바꿔주게 되면 컨테이너까지 생성됩니다.

도커이미지 컨테이너 생성 명령어

도커이미지 컨테이너 생성 명령어 : 
docker run -it -d "docker_images_name:image_tag"

도커컨테이너 접속 명령어

도커컨테이너 접속 명령어
docker exec -it "docker_images_name:image_tag" \bin\bash

이제 컨테이너 내부에서

source linux_vuln_check_script.sh

를 통해서 보안 취약점을 한번더 확인 할 수 있습니다.

Dockerfile 상세히보기 🚀

🔍 root 콘솔외에 외부접속 금지

## root 계정 원격 접속 제한

## [취약] 콘솔 로그인 이외의 로그인이 가능한것 제외

RUN apt-get install -y openssh-server

RUN mkdir /var/run/sshd



#set password

RUN echo 'root:root' |chpasswd



#replace sshd_config

RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin no/' /etc/ssh/sshd_config



RUN /etc/init.d/ssh restart

ssh service를 설치하고 /etc/ssh/sshd_config file 내에 PermitRootLogin option 을 no 로 바꿔줌으로써 콘솔외 외부접속을 금지합니다.

🔍 리눅스 계정 잠금 입계값 설정

## 리눅스 계정 잠금 임계값 설정

## [취약] 계정 잠금 정책이 설정되어 있지 않아 설정

RUN echo 'auth required /lib/security/pam_tally.so deny=5 unlock_time=120 no_magic_root \n account required /lib/security/pam_tally.so no_magic_root reset' >> /etc/pam.d/system-auth

Docker container 내 /etc/pam.d/system-auth 파일에 계정 잠금 임계값 설정합니다.

  • no_magic_root : root에게는 패스워드 잠금 설정을 적용하지 않음

  • deny=5 : 5회 입력 실패 시 패스워드 잠금

  • unlock_time=120 : 계정 잠김 후 마지막 계정 실패 시간부터 설정된 시간(120초)가 지나면 자동 계정잠김 해제

  • reset : 접속 시도 성공시 실패한 횟수 초기화

🔍 etc/passwd /etc/shadow 권한 수정

## shadow , passwd 파일 권한 변경

RUN chmod 400 /etc/shadow

RUN chown root:root /etc/shadow

RUN chmod 400 /etc/passwd

사용자의 패스워드를 해시로 암호화한 값이 저장된 파일들 root 권한만 read 할 수 있도록 수정합니다.

🔍 /etc/(x)inetd.conf 파일 권한 설정

## /etc/(x)inetd.conf 파일 권한 설정

RUN apt-get install xinetd -y

RUN apt-get install openbsd-inetd -y

RUN chown root /etc/inetd.conf

RUN chmod 600 /etc/inetd.conf

RUN chown root /etc/xinetd.conf

RUN chmod 600 /etc/xinetd.conf

인가자가 악의적인 프로그램을 등록하고 서비스를 실행시켜 기존 서비스에 영향을 줄 수 있어 root만 read write 하도록 수정합니다.

🔍 /etc/hosts

## host 권한 변경

RUN chmod 600 /etc/hosts

DNS서버 참조 전 /etc/hosts 파일에 적혀있는 domain과 IP주소를 참조해 해당 도메인을 찾아갈 때 사용하는 파일을 root 권한만 read write 로 설정합니다.

🔍 /etc/(r)syslog.conf

## /etc/(r)syslog.conf 파일 소유자 및 권한 설정

RUN apt-get install rsyslog -y

RUN chown root /etc/rsyslog.conf

RUN chmod 644 /etc/rsyslog.conf

로그 기록 설정 파일로 관리자외 변경금지하도록 설정합니다.

🔍 DoS 공격에 취약한 서비스 비활성화

## echo, discard, daytime, chargen 와 같은 Dos에 취약한 서비스 비활성화

RUN echo 'service echo \n { \n disable=yes \n id = echo-stream \n type = INTERNAL \n wait = no \n socket_type = stream \n }' >> /etc/xinetd.d/echo

RUN echo 'service echo \n { \n disable=yes \n id = echo-stream \n type = INTERNAL \n wait = no \n socket_type = stream \n }' >> /etc/xinetd.d/daytime

RUN echo 'service echo \n { \n disable=yes \n id = echo-stream \n type = INTERNAL \n wait = no \n socket_type = stream \n }' >> /etc/xinetd.d/discard

RUN echo 'service echo \n { \n disable=yes \n id = echo-stream \n type = INTERNAL \n wait = no \n socket_type = stream \n }' >> /etc/xinetd.d/chargen

RUN echo 'service echo \n { \n disable=yes \n wait = yes \n socket_type = stream \n protocol = udp \n user = root \n server = /usr/sbin/in.tftpd \n server_args = -s /tftpd \n }' >> /etc/xinetd.d/tftp

RUN echo 'service echo \n { \n disable=yes \n wait = yes \n socket_type = stream \n protocol = udp \n user = root \n server = /usr/sbin/in.tftpd \n server_args = -s /tftpd \n }' >> /etc/xinetd.d/talk

RUN service xinetd restart

RUN sed -ri 's/^#?echo/#echo/' /etc/services

RUN sed -ri 's/^#?discard/#discard/' /etc/services

RUN sed -ri 's/^#?daytime/#daytime/' /etc/services

RUN sed -ri 's/^#?chargen/#chargen/' /etc/services

RUN sed -ri 's/^#?tftp/#tftp/' /etc/services

RUN sed -ri 's/^#?talk/#talk/' /etc/services

/etc/services 내 주석처리

/etc/xinetd.d/ 내에 파일 생성 및 서비스 비활성화

  • echo 서비스 비활성화

  • discard 서비스 비활성화

  • daytime 서비스 비활성화

  • chargen 서비스 비활성화

  • Tftp 서비스 비활성화

  • Talk 서비스 비활성화

profile
엉덩이 무거운 개발자가 되기 위해서 몸무게를 찌웠다...

1개의 댓글

comment-user-thumbnail
2023년 1월 25일

잘 봤습니다 !! 감사합니다.

답글 달기